Files
txtai/examples/77_GraphRAG_with_Wikipedia_and_GPT_OSS.ipynb

145 KiB

GraphRAG with Wikipedia and GPT OSS

Retrieval Augmented Generation (RAG) is one of the most popular techniques in the AI space today. RAG takes a user request, retrieves the best matching content and then plugs that context into an LLM prompt to generate an answer. When otherwise not mentioned, most assume the context is generated using a vector database query. But there is no rule that says context can't be generated with other methods. It could be a simple web query, SQL query, text index search or other traditional search.

We also often hear the term GraphRAG. GraphRAG means different things to different people. Here we're going to build an example that uses txtai, wikipedia and gpt-oss to research a specific topic with graphs. txtai has a built-in graph component that automatically generates a graph network over the data loaded into an embeddings database. We'll use a pre-built embeddings database hosted on the Hugging Face Hub, txtai-wikipedia-slim.

Install dependencies

Install txtai and all dependencies.

In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[graph,pipeline-llm]

Load txtai-wikipedia-slim

Next, we'll load the embeddings database. This database is the top 100K most viewed Wikipedia articles with both a dense vector index and graph network enabled.

In [ ]:
from txtai import Embeddings

embeddings = Embeddings().load(provider="huggingface-hub", container="neuml/txtai-wikipedia-slim")

Build context with a graph query

The txtai graph component supports the openCypher query language via the GrandCypher library.

openCypher is a language for expressive and efficient data querying of a property graph. In this example, we'll traverse the embeddings database graph looking for paths between nodes similar to chatgpt and anthropic.

In [67]:
g = embeddings.search("""
MATCH P=(A)-[]->(B)
WHERE SIMILAR(A, 'chatgpt') AND SIMILAR(B, 'anthropic')
RETURN P
LIMIT 10
""", graph=True)

The query above is an extremely powerful combination of an vector similarity node search and a graph traversal query that walks the paths between nodes. It's much more expressive than simply saying find nodes similar to each of the concepts independently. It can be considered a deep graph search.

Plot the context network

Let's show the context as a graph plot!

In [ ]:
import matplotlib.pyplot as plt
import networkx as nx

def plot(graph):
    labels = {x: f"{graph.attribute(x, 'id')}" for x in graph.scan()}
    colors = ["#D32F2F", "#0277bd", "#7e57c2", "#757575"]

    results = embeddings.batchsimilarity(labels.values(), ["Anthropic Claude", "Google Gemini", "OpenAI GPT"])
    colors = [colors[x[0][0]] for x in results]

    options = {
        "node_size": 1000,
        "node_color": colors,
        "edge_color": "#454545",
        "font_color": "#efefef",
        "font_size": 10,
        "alpha": 1.0,
    }

    fig, ax = plt.subplots(figsize=(20, 9))
    pos = nx.spring_layout(graph.backend, seed=0, k=0.9, iterations=50)
    nx.draw_networkx(graph.backend, pos=pos, labels=labels, **options)
    ax.set_facecolor("#303030")
    ax.axis("off")
    fig.set_facecolor("#303030")

    plt.show()

plot(g)

Print the context as text

Let's further inspect the graph nodes.

In [92]:
context = ""
for x in g.scan():
    uid = g.attribute(x, "id")

    context += f"- id: {uid}\n"
    context += f"  url: https://en.wikipedia.org/wiki/{uid.replace(' ', '_')}\n"
    context += f"  text: {g.attribute(x, 'text')}\n"
    context += f"  links: {[g.attribute(n, 'id') for n in g.edges(x)]}\n"
In [93]:
print(context)
- id: ChatGPT
  url: https://en.wikipedia.org/wiki/ChatGPT
  text: ChatGPT is a generative artificial intelligence chatbot developed by OpenAI and released on November 30, 2022. It uses large language models (LLMs) such as GPT-4o as well as other multimodal models to create human-like responses in text, speech, and images. It has access to features such as searching the web, using apps, and running programs. It is credited with accelerating the AI boom, an ongoing period of rapid investment in and public attention to the field of artificial intelligence (AI). Some observers have raised concern about the potential of ChatGPT and similar programs to displace human intelligence, enable plagiarism, or fuel misinformation.
  links: ['GPT-4', 'GPT-4.5', 'OpenAI', 'Gemini (chatbot)', 'GPT-3', 'GPT-4.1', 'Gemini (language model)', 'Anthropic', 'Claude (language model)']
- id: GPT-4
  url: https://en.wikipedia.org/wiki/GPT-4
  text: Generative Pre-trained Transformer 4 (GPT-4) is a multimodal large language model trained and created by OpenAI and the fourth in its series of GPT foundation models. It was launched on March 14, 2023, and made publicly available via the paid chatbot product ChatGPT Plus until being replaced in 2025, via OpenAI's API, and via the free chatbot Microsoft Copilot.
  links: ['ChatGPT', 'GPT-3', 'GPT-4.5', 'GPT-4.1', 'OpenAI', 'Gemini (chatbot)', 'Gemini (language model)', 'Claude (language model)']
- id: GPT-4.5
  url: https://en.wikipedia.org/wiki/GPT-4.5
  text: GPT-4.5 (codenamed "Orion") is a large language model developed by OpenAI as part of the GPT series. Officially released on February 27, 2025, GPT-4.5 is available to users subscribed to the ChatGPT Plus and Pro plans across web, mobile, and desktop platforms. Access is also provided through the OpenAI API and the OpenAI Developer Playground, but the company plans to phase out API access to the model in July.
  links: ['GPT-4.1', 'GPT-4', 'ChatGPT', 'GPT-3', 'OpenAI', 'Claude (language model)', 'Gemini (language model)', 'Anthropic', 'Gemini (chatbot)']
- id: OpenAI
  url: https://en.wikipedia.org/wiki/OpenAI
  text: OpenAI, Inc. is an American artificial intelligence (AI) organization founded in December 2015 and headquartered in San Francisco, California. It aims to develop "safe and beneficial" artificial general intelligence (AGI), which it defines as "highly autonomous systems that outperform humans at most economically valuable work". As a leading organization in the ongoing AI boom, OpenAI is known for the GPT family of large language models, the DALL-E series of text-to-image models, and a text-to-video model named Sora. Its release of ChatGPT in November 2022 has been credited with catalyzing widespread interest in generative AI.
  links: ['ChatGPT', 'GPT-4', 'GPT-3', 'GPT-4.5', 'Anthropic', 'GPT-4.1', 'Gemini (chatbot)', 'Gemini (language model)']
- id: Gemini (chatbot)
  url: https://en.wikipedia.org/wiki/Gemini_(chatbot)
  text: Gemini, formerly known as Bard, is a generative artificial intelligence chatbot developed by Google. Based on the large language model (LLM) of the same name, it was launched in 2023 in response to the rise of OpenAI's ChatGPT. It was previously based on the LaMDA and PaLM LLMs.
  links: ['Gemini (language model)', 'ChatGPT', 'GPT-4', 'Anthropic', 'OpenAI', 'GPT-4.5']
- id: GPT-3
  url: https://en.wikipedia.org/wiki/GPT-3
  text: Generative Pre-trained Transformer 3 (GPT-3) is a large language model released by OpenAI in 2020.
  links: ['GPT-4', 'GPT-4.1', 'ChatGPT', 'OpenAI', 'GPT-4.5', 'Claude (language model)', 'Gemini (language model)']
- id: GPT-4.1
  url: https://en.wikipedia.org/wiki/GPT-4.1
  text: GPT-4.1 is a large language model within OpenAI's GPT series. It was released on April 14, 2025. GPT-4.1 can be accessed through the OpenAI API or the OpenAI Developer Playground. Three different models were simultaneously released: GPT-4.1, GPT-4.1 mini, and GPT-4.1 nano.
  links: ['GPT-4.5', 'GPT-4', 'GPT-3', 'ChatGPT', 'OpenAI', 'Gemini (language model)', 'Claude (language model)']
- id: Gemini (language model)
  url: https://en.wikipedia.org/wiki/Gemini_(language_model)
  text: Gemini is a family of multimodal large language models (LLMs) developed by Google DeepMind, and the successor to LaMDA and PaLM 2. Comprising Gemini Ultra, Gemini Pro, Gemini Flash, and Gemini Nano, it was announced on December 6, 2023, positioned as a competitor to OpenAI's GPT-4. It powers the chatbot of the same name. In March 2025, Gemini 2.5 Pro Experimental was rated as highly competitive.
  links: ['Gemini (chatbot)', 'GPT-4', 'ChatGPT', 'GPT-4.5', 'GPT-4.1', 'GPT-3', 'OpenAI', 'Anthropic']
- id: Anthropic
  url: https://en.wikipedia.org/wiki/Anthropic
  text: Anthropic PBC is an American artificial intelligence (AI) startup company founded in 2021. Anthropic has developed a family of large language models (LLMs) named Claude as a competitor to OpenAI's ChatGPT and Google's Gemini. According to the company, it researches and develops AI to "study their safety properties at the technological frontier" and use this research to deploy safe models for the public.
  links: ['Claude (language model)', 'OpenAI', 'ChatGPT', 'Gemini (chatbot)', 'GPT-4.5', 'Gemini (language model)']
- id: Claude (language model)
  url: https://en.wikipedia.org/wiki/Claude_(language_model)
  text: Claude is a family of large language models developed by Anthropic. The first model was released in March 2023.
  links: ['Anthropic', 'GPT-3', 'GPT-4.5', 'GPT-4', 'ChatGPT', 'GPT-4.1']

GraphRAG

Now that we have our graph context, we'll plug that into an LLM prompt.

In [ ]:
from txtai import LLM

llm = LLM("unsloth/gpt-oss-20b-GGUF/gpt-oss-20b-Q4_K_M.gguf", n_ctx=20000)
In [82]:
from IPython.display import display, Markdown

out = llm(f"""
Analyze the following context and write an article about it
{context}
""", defaultrole="user", maxlength=20000, stripthink=True)

display(Markdown(out))
<IPython.core.display.Markdown object>

Wrapping up

There we have it, GraphRAG in a very straightforward and easy-to-understand manner. The best ideas often are the simple ones!