GraphR2R - Production Ready GraphRAG with R2R + Neo4J

Easily build and deploy Graph RAG using R2R

Owen C., Nolan T., Shreyas P. / 08/09/2024
7 min read
Knowledge Graph Architecture
Figure 1: Graph constructed using R2R

Introduction

A crucial challenge for Large Language Models (LLMs) is their ability to handle queries about private datasets, or data that postdates their training cutoff. Retrieval Augmented Generation (RAG) presents a solution to this issue, but it has some limitations. In this blog post, we will discuss some of its limitations and how GraphRAG solves them. We will also go over the process of deploying a GraphRAG system using R2R and Neo4J.

In basic RAG systems, data is separated into chunks, embedded and stored in a vector database. To answer queries, the query is embedded using the same embedding model and chunks are retrieved that are most similar to the query. Through this approach, relevant knowledge that was not part of the original training data is added to the context and the LLM is able to answer questions about it. However, basic RAG faces many challenges, out of which two are particularly important:

  1. Answering questions that require an understanding of complex relationships between different chunks of information
  2. Answering questions that require a global context, where the answer to the question requires knowledge of the entire dataset

GraphRAG addresses both of these challenges!

Method#

With current RAG systems, it is safe to say that retrieval is the hardest part. Sometimes relevant chunks are missed, and other times irrelevant chunks are retrieved. Both scenarios reduce the quality of the answer. This is where GraphRAG shines! GraphRAG addresses these challenges by incorporating a knowledge graph structure into the RAG pipeline. So, the complex relationships between different chunks of information are preserved and can be easily retrieved and used to answer questions.

Here's how it works:

  1. Knowledge Graph Construction: Instead of just chunking and embedding text, GraphRAG extracts entities and relationships from the text to build a knowledge graph. This graph captures the complex relationships between different pieces of information.

  2. Graph-based Retrieval: When a query is received, GraphRAG can traverse the knowledge graph to find relevant information. This allows it to follow chains of relationships that might not be apparent in a simple vector search.

  3. Community Detection: The knowledge graph is analyzed to identify communities of closely related entities. These communities can represent broader themes or topics within the dataset.

  4. Community Summarization: Each community is summarized, providing a higher-level view of the information contained within it. This helps capture global context that might be lost when looking at individual chunks.

  5. Hybrid Retrieval: GraphRAG combines traditional vector similarity search with graph-based retrieval. It can use vector search to find relevant communities and then use graph traversal to explore related information within and across communities.

By combining these techniques, GraphRAG can provide more comprehensive and contextually relevant information to the language model, enabling it to generate more accurate and insightful responses. Figure 2 shows a high-level architecture of a GraphRAG system. It is inspired by the architecture of the GraphRAG paper.

Knowledge Graph Architecture

Figure 2: High-level architecture of a GraphRAG system

Our first model for GraphRAG used Triplex to extract triples from the dataset and then used Neo4J to store the triples in a graph database. To answer questions, we used a text-to-graphql agent (LLM with a prompt) to convert the question into a graphql query and then used Neo4J to execute the query and return the results. Through this method, we enhance the retrieval process and are able to answer even more complex questions.

Example Outputs:#

We ingested 50 of Paul Graham's Essays and asked the following questions:

  1. What does paul graham discuss in his essays?
  2. What are the key themes and insights in his essays?
  3. What are the technological themes in his essays?
  4. What is the conclusion of his essays?

We then compared the outputs of basic RAG and GraphRAG for the following question:

  • Basic RAG output: The algorithm was able to retrieve 10 chunks, out of which 3 were relevant to the question. The LLM was then able to answer the question using the retrieved chunks. Clearly the answer is not comprehensive and is only able to answer the question to a certain extent.
Paul Graham discusses various topics in his Essays,
including the importance of startups, the nature of
wealth, and the dynamics of programming languages
[1], [2], [3].
  • Graph RAG output: The algorithm retrieves many essays and chunks from different reports from the dataset. It then clusters the essays into communities and summarizes them. The LLM is then able to answer the question using the summarized communities. The answer is comprehensive and is able to answer the question to a greater extent than the basic RAG system.
### Overview of Paul Graham's Essays
Paul Graham, a prominent essayist and co-founder of Y
Combinator, has written extensively on various topics,
primarily focusing on startups, technology, and
societal trends. His essays are known for their
insightful analysis and practical advice, particularly
for entrepreneurs and programmers.
### Key Themes in Paul Graham's Essays
#### Startups and Entrepreneurship
Graham frequently discusses the dynamics of startups,
offering advice on how to build successful companies.
He emphasizes the importance of finding a good idea,
assembling a strong team, and maintaining a relentless
focus on growth. In his essay "How to Start a Startup,"
he outlines the critical steps and mindset required to
launch a successful venture [Data: Reports (1, 3, 5, 7,
9, +more)].
#### Technology and Innovation
Another recurring theme in Graham's essays is the role
of technology in driving innovation. He often explores
how technological advancements can disrupt existing
industries and create new opportunities. In "The
Acceleration of Addictiveness," he discusses how
technology can be designed to be addictive, and the
ethical implications of such designs [Data: Reports (2,
4, 6, 8, 10, +more)].
#### Societal Trends and Observations
Graham also delves into broader societal trends,
analyzing how changes in technology and culture impact
society. In "The Refragmentation," he examines how the
internet has led to the fragmentation of traditional
media and the rise of niche communities [Data: Reports
(11, 13, 15, 17, 19, +more)].
### Practical Advice and Insights
#### Building and Scaling Startups
Graham provides practical advice on various aspects of
building and scaling startups. He stresses the
importance of iteration and learning from failures, as
well as the need for founders to be adaptable and
resilient. His essay "Do Things that Don't Scale"
advises startups to focus on manual efforts initially
to understand their customers deeply before automating
processes [Data: Reports (12, 14, 16, 18, 20, +more)].
#### The Hacker Ethos
A significant portion of Graham's writing is dedicated
to the hacker ethos and the culture of programming. He
advocates for the importance of creativity and problem-
solving skills in programming, often highlighting how
these skills are crucial for innovation. In "Hackers
and Painters," he draws parallels between hackers and
artists, emphasizing the creative aspect of coding
[Data: Reports (21, 23, 25, 27, 29, +more)].
### Conclusion
Paul Graham's essays offer a wealth of knowledge and
insights, particularly valuable for entrepreneurs,
technologists, and anyone interested in understanding
the intersection of technology and society. His
writings provide both theoretical perspectives and
practical advice, making them a significant resource
for those looking to navigate the complexities of the
modern technological landscape.

Setting up using R2R + Neo4J#

It is very easy to setup a GraphR2R using R2R and Neo4J. You can find the instructions to setup your own GraphR2R in the cookbook here.

Feel free to reach out to us at [email protected] if you have any questions!

Was this helpful?