Skip to main content

Introduction

GraphRAG is a powerful feature of R2R that allows you to perform graph-based search and retrieval. This guide will walk you through the process of setting it up and running your first queries.

An example knowledge graph constructed from companies in the YC directory.

Note that graph construction may take long for local LLMs, we recommend using cloud LLMs for faster results.

Start server

Ingesting files

We begin the cookbook by ingesting the default sample file aristotle.txt used across R2R tutorials and cookbooks:
Example Response
You can also ingest your own files:
The initial ingestion step adds parses the given documents and inserts them into R2R’s relational and vector databases, enabling document management and semantic search over them. The aristotle.txt example file is typically ingested in under 10s. You can confirm ingestion is complete by querying the documents overview table:
Example Response
When ingestion completes successfully for a given file we will find that ingestion_status reads success in the corresponding output.

Create Knowledge Graph

Knowledge graph creation is done in two steps:
  1. create-graph: Extracts nodes and relationships from your input document collection.
  2. enrich-graph: Enhances the graph structure through clustering and explaining entities (commonly referred to as GraphRAG).
This will run a cost estimation step to give you an estimate of the cost of the graph creation process.
Example Response
Then, you can run the graph creation process with:
Example Response
This step will create a knowledge graph with nodes and relationships. Below is a visualization of the graph which we produced with Neo4j (deprecated as of now. We are working on a new visualization tool):
Aristotle Graph
  1. Using the r2r inspect-knowledge-graph command.
The output should be roughly as follows:

Graph Enrichment

Now we have a graph, but this graph is not searchable yet. We need to perform the graph enrichment step. The graph enrichment step adds node and relationship descriptions, performs hierarchical leiden clustering to create communities, and embeds the descriptions. These embeddings will be used later in the local search stage of the pipeline. If you are more interested in the algorithm, please refer to the blog post here.
Similar to the graph creation step, this will run a cost estimation step to give you an estimate of the cost of the graph enrichment process.
Example Response
Now, you can run the graph enrichment process with:
Now you can see that the graph is enriched with the following information. We have added descriptions and embeddings to the nodes and relationships. Also, each node is mapped to a community. Following is a visualization of the enriched graph (deprecated as of now. We are working on a new visualization tool): Enriched Graph GraphRAG currently supports two types of searches: local and global. Local searches are faster and cheaper than global. A local knowledge graph search performs similarity search on the entity, relationship and community description embeddings.
The answer will be returned in JSON format and contains results from entities, relationships and communities. Following is a snippet of the output:
Global searches can be used for queries that require reasoning over the whole dataset. They provide more accurate results, however they use a large amount of queries and are expensive. We recommend checking the number of clusters created in the graph and setting the max-llm-queries to be a fraction (> 0.1) of that number.
Extracting the global_result from the payload that is returned above we find the following:

RAG

You can directly use these search results as part of your RAG pipeline.
The output is as follows:

Conclusion

In conclusion, integrating R2R with GraphRAG significantly enhances the capabilities of your RAG applications. By leveraging the power of graph-based knowledge representations, GraphRAG allows for more nuanced and context-aware information retrieval. This is evident in the example query we ran using R2R, which not only retrieved relevant information but also provided a structured analysis of the key contributions of Aristotle to modern society. In essence, combining R2R with GraphRAG empowers your RAG applications to deliver more intelligent, context-aware, and insightful responses, making it a powerful tool for advanced information retrieval and analysis tasks. Feel free to reach out to us at founders@sciphi.ai if you have any questions or need further assistance.