top of page

Graph Retrieval-Augmented Generation ( Graph RAG ) Key Concepts

Writer's picture: DebasishDebasish


Introduction

In the field of Large Language Models or LLM and Natural Language Processing (NLP), Graph Retrieval-Augmented Generation (Graph RAG) is an emerging technique that leverages the strengths of knowledge graphs and advanced text generation models to create more contextually accurate and informative content. This approach integrates structured data from knowledge graphs into the generation process, enhancing the model's ability to produce factually correct and contextually rich text.

 

What is Graph RAG?

Graph Retrieval-Augmented Generation (Graph RAG) is a hybrid approach that leverages the strengths of graph databases and retrieval-augmented generation models. In essence, it integrates structured information from knowledge graphs into the text generation process, allowing the model to generate content that is enriched with factual and relational data.


Retrieval-Augmented Generation (RAG)


Retrieval Augmented Generation (or RAG for short ) was introduced as a technique to improve Large Language Models’ (LLMs) by incorporating information from external, reliable knowledge bases.

The principle behind RAG is straightforward; when an LLM is asked a question, it does not just rely on what it already knows. Instead, it first looks up relevant information from a specified knowledge source. This approach assures that the generated outputs references from a vast amount of contextually enriched data, augmented by the most current and relevant information available.


RAG primarily functions through a two-phase process: retrieval and content generation.

Retrieval-Augmented Generation ( RAG ) Phases:

  • Retrieval Phase :                                                                       

During the retrieval phase, the algorithm locates and gathers relevant snippets of information pertinent to the user’s prompt or inquiry.

The system identifies documents with content semantically related to the query and calculates relevance using a similarity measure, typically the cosine similarity between their vectors. After collating external knowledge, it appends this to the user’s prompt and sends it as an enriched input to the language model.


  • Content Generation Phase :

In the subsequent generative phase, the LLM combines this augmented prompt with its own training data representation to produce a response that is customised to the user’s query. This response provides a mix of personalised and verifiable information, suitable for use through applications such as chatbots.

 

Why is Retrieval-Augmented Generation important?


  • LLMs are a key artificial intelligence (AI) technology powering intelligent chatbots and other natural language processing (NLP) applications.

  • The goal is to create bots that can answer user questions in various contexts by cross-referencing authoritative knowledge sources.

  • Unfortunately, the nature of LLM technology introduces unpredictability in LLM responses. Additionally, LLM training data is static and introduces a cut-off date on the knowledge it has.


Known challenges of LLMs include:

Presenting false information when it does not have the answer.

Presenting out-of-date or generic information when the user expects a specific, current response.

Creating a response from non-authoritative sources.

Creating inaccurate responses due to terminology confusion, wherein different training sources use the same terminology to talk about different things.


RAG is one approach to solving some of above challenges. It redirects the LLM to retrieve relevant information from authoritative, pre-determined knowledge sources. Organizations have greater control over the generated text output, and users gain insights into how the LLM generates the response.



Above Images shows the complete RAG Pipeline  


Key Components of Graph RAG

  1. Knowledge Graphs:

  2. Definition: Knowledge graphs are structured representations of knowledge where entities (nodes) are interconnected by relationships (edges).

  3. Examples: DBpedia, Wikidata, and proprietary organizational knowledge bases.

  4. Function: They store information in a way that is both human-readable and machine-processable, allowing for efficient retrieval of structured data.

2.    Retrieval-Augmented Generation:

  • Traditional RAG: Uses external text sources to retrieve relevant information during text generation.

  • Graph RAG: Retrieves relevant subgraphs or specific facts from knowledge graphs, providing a structured set of information for the generation model.


  • Graph-Based Retrieval:

o   Process: Initiates with a query or prompt that is used to search the knowledge graph for relevant nodes and edges.

o   Techniques: Utilizes algorithms like shortest path, subgraph matching, and node ranking to identify the most relevant information.

4.    Integration with Generation Models:

o   Methods: Embedding graph information into the input sequence or using it to guide the generation process.

o   Popular Models: Transformers like GPT-3, BERT, and their variants.

 

Use of Graph RAG

Graph RAG (Graph Retrieval-Augmented Generation) is an advanced approach combining graph-based data structures with retrieval-augmented generation techniques to enhance the capabilities of language models. Graph RAG is designed to question answering over private text corpora that scale with both the generality of user questions and the quantity of source text to be indexed. Graph RAG uses LLM to build a graph-based text index and then uses it to answer global queries. The two stages of the process are:

·     To derive an entity knowledge graph from the source documents.

·     To pre-generate community summaries for all groups of closely related entities.

Given a question, each community summary is used to generate a partial response, before all partial responses are again summarized in a final response to the user.

Graph RAGs achieve two things particularly:

·     Enhanced search relevancy.

·     Enabling new scenarios that might require a very large context. For example, finding trends in data, summarization, etc.

How does it do it?

Source Documents → Text Chunks

·     Granularity: Input texts from source documents are split into chunks.

·     Trade-off: Longer chunks need fewer LLM calls but may degrade recall due to longer context windows.

·     Example: On HotPotQA dataset, a chunk size of 600 tokens extracted nearly twice as many entity references as a chunk size of 2400 tokens.

Text Chunks → Element Instances

·     Goal: Identify and extract graph nodes and edges from text chunks.

·     Process: Use LLM prompts to identify entities and relationships, outputting delimited tuples.

·     Customization: Tailor prompts with few-shot examples relevant to specific domains.

·     Efficiency: Multiple rounds of “gleanings” ensure additional entities are detected without compromising chunk size.

Element Instances → Element Summaries

·     Summarization: LLM abstracts and summarizes entities, relationships, and claims from text.

·     Duplication Handling: Despite potential inconsistencies in entity references, the approach is resilient due to the detection of closely-related entities and their summarization.

Element Summaries → Graph Communities

·     Graph Modeling: Create an undirected weighted graph where nodes are entities and edges are relationships.

·     Community Detection: Use the Leiden algorithm to partition the graph into hierarchical communities, enabling efficient global summarization.

Graph Communities → Community Summaries

·     Summary Creation: Generate report-like summaries for each community.

·     Utility: Summaries help understand the global structure and semantics of the dataset, aiding in answering global queries.

Community Summaries → Community Answers → Global Answer

·     Query Processing: Use community summaries to generate answers.

·     Intermediate Answers: Summaries are divided into chunks, and the LLM generates answers with helpfulness scores.

·     Final Answer: Combine top-scoring intermediate answers into the final global answer.

 

 

Benefits of Graph RAG

1.    Enhanced Contextual Understanding:

o   Coherence: Better understanding of the context and relationships between entities leads to more coherent text generation.

2.    Improved Factual Accuracy:

o   Reliability: Utilizes verified and structured information from knowledge graphs, reducing the likelihood of generating incorrect facts.

3.    Richer Information Integration:

o   Complexity: Ability to reflect complex interdependencies by integrating multifaceted relationships and attributes of entities.

4.    Scalability and Flexibility:

o   Adaptability: Knowledge graphs can be continuously updated and expanded, providing a scalable knowledge base.

Applications of Graph RAG

1.    Question Answering Systems:

o   Precision: Retrieves precise answers from knowledge graphs, ensuring accurate and comprehensive responses.

2.    Content Generation:

o   Domains: Useful in healthcare, finance, and legal sectors where accuracy and context are crucial.

3.    Educational Tools:

o   Tutoring: Development of intelligent tutoring systems that offer accurate and context-rich explanations.

4.    Enterprise Knowledge Management:

o   Reports and Summaries: Assists in creating detailed reports, summaries, and insights based on organizational knowledge graphs.

Challenges and Future Directions

1.    Complexity of Integration:

o   Seamlessness: Requires sophisticated methods to ensure smooth interaction between graph-based information and text generation models.

2.    Scalability of Graph Retrieval:

o   Efficiency: Efficiently retrieving relevant subgraphs from large-scale knowledge graphs is challenging, necessitating advancements in retrieval algorithms.

3.    Balancing Structure and Flexibility:

o   Creativity: Ensuring that structured information from graphs does not constrain the generative capabilities of models.

How to do RAG?

To achieve Graph RAG for question answering, you need to select what part of the information that is available to you to send to the LLM. This is usually done by querying a database based on the intent in the user question. The most appropriate databases for this purpose are vector databases, which via embeddings capture the latent semantic meanings, syntactic structures, and relationships between items in a continuous vector space. The enriched prompt contains the user question together with the pre-selected additional information, so the generated answer takes it into account.


As simple as the basic implementation is, you need to take into account a list of challenges and considerations to ensure good quality of the results:

  • Data quality and relevance is crucial for the effectiveness of Graph RAG, so questions such as how to fetch the most relevant content to send the LLM and how much content to send it should be considered.

  • Handling dynamic knowledge is usually difficult as one needs to constantly update the vector index with new data. Depending on the size of the data this can impose further challenges such as efficiency and scalability of the system.

  • Transparency of the generated results is important to make the system trustworthy and usable. There are techniques for prompt engineering that can be used to stimulate the LLM to explain the source of the information included in the answer.

 

The Different Varieties of Graph RAG

Graph RAG is an enhancement over the popular RAG approach. Graph RAG includes a graph database as a source of the contextual information sent to the LLM. Providing the LLM with textual chunks extracted from larger sized documents can lack the necessary context, factual correctness and language accuracy for the LLM to understand the received chunks in depth. Unlike sending plain text chunks of documents to the LLM, Graph RAG can also provide structured entity information to the LLM combining the entity textual description with its many properties and relationships, thus encouraging deeper insights facilitated by the LLM. With Graph RAG each record in the vector database can have contextually rich representation increasing the understandability of specific terminology, so the LLM can make better sense of specific subject domains. Graph RAG can be combined with the standard RAG approach to get the best of both worlds – the structure and accuracy of the graph representation combined with the vastness of textual content.

We can summarize several varieties of Graph RAG, depending on the nature of the questions, the domain and information in the knowledge graph at hand:


Graph as a Content Store:

Extract relevant chunks of documents and ask the LLM to answer using them. This variety requires a KG containing relevant textual content and metadata about it as well as integration with a vector database.


Graph as а Subject Matter Expert: 

Extract descriptions of concepts and entities relevant to the natural language (NL)  question and pass those to the LLM as additional “semantic context”. The description should ideally include relationships between the concepts. This variety requires a KG with a comprehensive conceptual model, including relevant ontologies, taxonomies or other entity descriptions. The implementation requires entity linking or another mechanism for the identification of concepts relevant to the question.


Graph as a Database:

Map (part of) the NL question to a graph query, execute the query and ask the LLM to summarize the results. This variety requires a graph that holds relevant factual information. The implementation of such a pattern requires some sort of NL-to-Graph-query tool and entity linking.

 

Conclusion

Graph Retrieval-Augmented Generation (Graph RAG) represents a significant advancement in NLP by combining the robustness of knowledge graphs with the sophistication of modern text generation models. Addressing current challenges and leveraging the benefits of this approach can lead to more intelligent and reliable AI systems, transforming how machines generate and understand human language.

Comments


bottom of page