Recipe / Retrieval

Graph RAG primer

Traditional vector RAG retrieves passages by semantic similarity, but it loses the relationships between entities. Graph RAG augments embeddings with a typed knowledge graph so the model can traverse multi-hop facts, scope answers to communities, and cite provenance at the edge level. This primer walks through the three stages every Graph RAG pipeline shares.

1.Extract entities and relations

Stream your corpus through a structured-output LLM call that emits triples of the form(subject, predicate, object)along with a confidence score and the source span. Normalize entities by lowercasing, stripping punctuation, and resolving aliases against a canonical name index. Persist each triple with the originating chunk id so you can backtrack from any answer to the exact sentence that produced it.

2.Build the graph and detect communities

Load triples into a property graph (Neo4j, KuzuDB, or even a SQLite edge table). Run Leiden or Louvain community detection so dense neighborhoods become summarizable units. For each community, ask the LLM to write a one-paragraph summary that names the cluster, lists its key entities, and highlights the dominant relations. These summaries become the medium-grain index that bridges raw passages and full-corpus overview queries.

3.Query across local and global modes

At query time, classify the question as local (a specific entity), global (a corpus-wide theme), or hybrid. Local queries seed on matched entities and walk one or two hops. Global queries fan out across community summaries and map-reduce the partial answers. The Meridian SDK exposes a single retrieve call that dispatches to the right mode automatically.

from meridian import GraphRAG

rag = GraphRAG(model="azure/model-router")
rag.ingest("./corpus/")

answer = rag.query(
    "How did the supply chain shift impact Q3 margins?",
    mode="auto",
    max_hops=3,
)
print(answer.text)
print(answer.citations)