GraphRAG

GraphRAG retrieves the context an LLM needs by traversing a knowledge graph, not only by matching embeddings. Vector search finds the entities a question is about, and graph traversal follows their relationships to the facts connected to them.

The retrieved context is added to the user’s question before it goes to the LLM. In this example, a question about a patient’s symptoms is enriched with the related medical records and treatments from the knowledge graph:

A question about patients with similar symptoms is enriched with relevant medical records, treatments and symptom descriptions retrieved from the knowledge graph, and the enriched prompt is sent to the LLM

In Memgraph, a whole retrieval runs as one Cypher query: search, expansion, ranking and prompt assembly happen inside the database. This is Atomic GraphRAG.

Retrieval pipelines

Which pipeline to use depends on the type of question:

Question typeWhere the answer isPipelineExample question
AnalyticalA value in the graphText2Cypher“How many organizations of each type are covered?”
LocalAround one entityLocal graph search“What is connected to NVIDIA?”
GlobalAcross the whole datasetQuery-focused summarization“What are the main themes overall?”

The diagram shows the steps of each pipeline, from the question type to the context passed to the LLM:

A user question is routed by question type to Text2Cypher, local graph search or query-focused summarization, and each pipeline returns relevant context for the answer

To let an agent choose the pipeline for each question, see Agentic GraphRAG.

Run the example

The command starts Memgraph and the Memgraph MCP server in Docker, loads a knowledge graph of finance news and runs all three pipelines on it. You need Docker and git, and no API key.

curl -sSL https://install.memgraph.com/agentic-graphrag | bash

Agentic GraphRAG explains each step and how to connect an agent to the graph.

Build your own

Before you start, check the prerequisites: a running Memgraph with MAGE, your data and an LLM API key. Then:

  1. Model your domain as a graph. See Data modeling and Model a knowledge graph.
  2. Load your data into Memgraph. See Knowledge graph creation.
  3. Create a vector index, a text index or both, so a query can find the entities a question is about.
  4. Write the retrieval as a Cypher query, starting from the pipeline that fits your questions. See Atomic GraphRAG.
  5. Connect your LLM or agent through the Memgraph MCP server or a framework such as LangChain or LlamaIndex.
  6. Before you go to production, size and configure Memgraph for the workload. See Memgraph in GraphRAG use cases.

To use a framework that builds the knowledge graph and runs retrieval for you, see LightRAG, which stores its graph in Memgraph. For every package you can build with, see AI Toolkit and integrations.

Examples

  • GraphRAG notebook: Builds a knowledge graph, computes embeddings and implements several retrieval strategies, step by step.
  • Agentic GraphRAG app: An agent that chooses a retrieval strategy for each question and runs vector search, deep path traversals, community detection and PageRank.
  • GenAI stack: An application template with Memgraph, LangChain and GPT or Llama.

Case studies

These Memgraph users describe how they use GraphRAG and knowledge graphs:

  • Precina Health uses GraphRAG on patient data for type 2 diabetes care.
  • Cedars-Sinai uses a knowledge graph and retrieval-augmented generation in Alzheimer’s research.