GraphRAG

Machine Learning
Introduced by
Yunfan Gao
Year
2023
Type
System architecture
Family
Deep Learning, Language Models, Knowledge Graphs

What does it do?

GraphRAG is a retrieval-augmented generation approach in which a language model receives context drawn from a knowledge graph built from source documents, rather than from flat text passages alone.

Documents are processed to extract entities and the relationships between them, and a query is answered using the part of the graph that is retrieved. The approach is aimed at questions whose answers depend on connections between facts spread across several documents.

The intuition behind the method

Large language models (LLMs) can produce incorrect or outdated statements when the relevant material is not available to them. Retrieval-augmented generation (RAG) addresses this by retrieving documents before generation. A retrieved set of text passages, however, keeps the content of each passage without recording how the entities in different passages relate. GraphRAG stores those relations explicitly, with entities as nodes and relationships as edges, so that a query can reach an entity and follow its connections to others. Following edges allows an answer to combine facts from different documents: for example, a statement in document A linking entity X to entity Y, together with a statement in document B about Y. Whether this improves answers in practice depends on how accurately the graph was extracted, so the extracted entities and relationships should be checked against the source text before the graph is relied on.

History

GraphRAG extends retrieval-augmented generation, an approach that supplies a language model with material retrieved from an external source before it generates an answer. General surveys of retrieval-augmented generation for large language models, such as Gao et al. (2023), describe this family of methods in broad terms.

When to use it

Choose GraphRAG when the source material is a collection of documents whose answers depend on relationships between entities, such as connections between research papers, parties in legal documents, or clinical concepts, and when questions often need facts from several documents at once. Standard RAG is usually sufficient when the document set is small and simple, or when a question can be answered from a single passage; the extraction stage then adds cost without a clear benefit. Building the graph requires running entity and relationship extraction over every document, storing the resulting graph, and traversing it at query time. These costs are justified only when relational structure is actually needed.

Limitations and considerations

  • Answer quality is bounded by entity and relationship extraction: extraction errors enter the graph and are then retrieved as if they were correct.
  • Graph construction and traversal add computation and an extraction stage beyond a simple vector similarity search.
  • A triple-style representation (subject, relationship, object) records only the core of a statement, so qualifications and surrounding context in the source text may be lost.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Keeps explicit relationships between entities, so the context given to the model can show how entities are connected, not only which passages match the query.
  • Allows multi-hop questions to be answered by traversing edges that may link facts from different documents.
  • Makes the relationships supplied to the model visible, which can make it easier to check where the connections in an answer come from.

Applications

  • Literature synthesis across research papers, where relationships between papers and concepts matter
  • Legal document retrieval for contract analysis and due diligence, where relationships must be extracted from the documents; extraction errors can mislead conclusions, so outputs need review by a qualified legal professional before they inform a decision
  • Medical knowledge base queries for diagnosis support that require multi-hop clinical reasoning; extraction errors can mislead clinical reasoning, so outputs of this kind need review by qualified specialists and must not replace clinical judgement
  • Customer support drawing on product documentation, where an answer must combine information from several sections or documents

How it works

How it works

1

Source documents are processed to extract named entities, such as people, places and organizations, and the relationships between them. The quality of this step limits what the graph can later contain.

2

The extracted entities and relationships are assembled into a graph in which entities are nodes and relationships are edges. Entity types and relationship types are attached as annotations, so that retrieval can distinguish, for example, a partnership relation from a location relation.

3

The query is analysed to identify entities and relationships that are likely to be relevant; these serve as starting points in the graph. Graph algorithms then rank entities by relevance. Personalized PageRank is one choice: it favours entities that are well connected to the starting points. How the query is mapped to starting entities (for example by matching query terms or embeddings against entity names) and how many ranked entities and relationships are passed on as context varies between implementations.

4

The retrieved entities and their relationships are arranged as structured context and passed to a language model, which generates an answer based on that graph content.

Common pitfalls

  • Incomplete entity extraction leaves relevant entities out of the graph, and no later traversal can recover them.
  • Spurious or missing relationship edges change which paths are reachable, so extracted relations should be validated before multi-hop answers are trusted.
  • Poorly specified queries may not map to the right starting entities, so ranking begins from the wrong part of the graph; query interpretation has to be adequate for the method to work.

Frequently asked questions

How does GraphRAG differ from standard retrieval-augmented generation?

Standard RAG retrieves text passages whose embeddings are similar to the query. GraphRAG first builds a knowledge graph from the documents and then queries it for relevant entities and their relationships. Because the graph stores edges, a query can follow connections, such as entities linked through two or three steps to a given entity, which a ranking by passage similarity does not represent explicitly.

How is the knowledge graph constructed from unstructured text?

Named entity recognition (NER) identifies entities, and relationship extraction (RE) identifies relationships between them. Both tasks are commonly carried out with sequence labelling or transformer-based classifiers. The extracted triples (subject, relationship, object) become graph edges. Errors in either NER or RE propagate into the graph.

How does graph traversal help answer questions?

Traversal can answer questions that need facts from several documents, such as which organizations company X partnered with, by following partnership edges. Centrality measures such as PageRank rank entities by importance, which helps prioritize entities likely to matter for a query. Whether this improves answers for a given task depends on the quality of the extracted graph.

How should I check whether a GraphRAG answer can be trusted?

Sample extracted entities and relationships and compare them with the source text, because extraction errors enter the graph and are retrieved as if they were correct. For each answer, confirm that the edges it relies on are supported by the cited passages. Then run the same questions through a standard RAG baseline and compare the answers, so that any gain attributable to the graph can be seen.

Related methods

Sources

  1. Gao, Y., Xiong, Y., Gao, X., Jia, K., Pan, J., Bi, Y., Dai, Y., Sun, J., & Wang, M. (2023). Retrieval-augmented generation for large language models: A survey. arXiv preprint arXiv:2312.10997.

    Open publication reference