Examples to help you understand the GraphRAG mechanism
![Towards Data Science](https://miro.medium.com/v2/resize:fill:48:48/1*CJe3891yB1A1mzMdqemkdg.jpeg)
Developing a chatbot that can tackle real questions and give appropriate, precise answers is really a hard job. While there has been remarkable progress in large language models, an open challenge is to couple these models with knowledge bases in order to deliver reliable and context-rich responses.
The key issues almost always come down to hallucination (the model is creating wrong or non-existing information) and contextual understanding, where the model is unable to understand the nuanced relationships between different pieces of information. Others have tried to build robust Q&A systems without much success, since the models often return shabby answers, though they are connected to comprehensive knowledge bases.
While RAG can reduce hallucination by connecting the generated response to real-world data, answering complex questions accurately is a different cup of tea. Users are often greeted with answers such as, “The xx topic is not explicitly covered in the retrieved text” even when the knowledge base clearly contains the information, albeit in a less obvious manner. This is where GraphRAG (Graph Retrieval-Augmented Generation) comes in handy, improving the model’s model’s ability to provide precise and contextually rich answers by leveraging structured knowledge graphs.
RAG: Bridging Retrieval and Generation
RAG represented a major step in combining the best of both retrieval-based and generation-based methods. Given a query, RAG retrieves relevant documents or passages from a large corpus and then generates the answer with this information. One can, therefore, be sure that the generated text can be informative and context-relevant as it is grounded on fact data.
For example, in a question like ”What is the capital of France?” the RAG system will look in its corpus for documents related to the country of France and the mention of its capital, Paris. It will retrieve relevant passages and respond by generating an answer such as ”The capital of France is Paris.” This style fits very well with a simple query and clearly documented answers.
However, RAG falters on more complex queries, specifically those where one needs to understand relationships between entities, when these relationships are not explicit in retrieved documents. The system is coming to its failure and the downfall with questions like “How did the scientific contributions of the 17th century influence early 20th-century physics?” (more on this example later).
GraphRAG: Harnessing the Power of Knowledge Graphs
GraphRAG, as first outlined in the Microsoft Research Blog here, aims to get around these limitations by infusing graph-based retrieval mechanisms into the model. Basically, it reorganizes the unstructured text of the knowledge base into a structured knowledge graph, in which nodes represent entities (e.g., people, places, concepts), and edges represent relationships between entities.
This structured format enables the model to better comprehend and employ the interrelations between different pieces of information.
Let us now go into a little bit of detail to understand the concept of GraphRAG, in a comparison with RAG, using the easy way.
As starter, let’s take a hypothetical knowledge base comprising sentences from various scientific and historical texts as follows:
1. “Albert Einstein developed the theory of relativity, which revolutionized theoretical physics and astronomy.”
2. “The theory of relativity was formulated in the early 20th century and has had a profound impact on our understanding of space and time.”
3. “Isaac Newton, known for his laws of motion and universal gravitation, laid the groundwork for classical mechanics.”
4. “In 1915, Einstein presented the general theory of relativity, expanding on his earlier work on special relativity.”
5. “Newton’s work in the 17th century provided the foundation for much of modern physics.”
In a RAG system, these sentences would be stored as unstructured text. And asking “How did the scientific contributions of the 17th century influence early 20th-century physics?”, for instance, could have put the system in a difficult position if the exact phrasing and retrieval quality of the documents did not link the 17th-century influence directly with early 20th-century physics. RAG might give answers like “Isaac Newton’s work in the 17th century provided the foundation for much of modern physics. Albert Einstein developed the theory of relativity in the early 20th century”, since the mechanism was able to retrieve relevant information but cannot clearly explain the influence of 17th-century physics on early 20th-century developments.
In contrast, GraphRAG turns this text into a structured knowledge graph. A knowledge graph represents how different things are related to each other. It uses a set of ontologies, which are a set of rules to help organize the information. This way, it can find hidden connections, not only the obvious ones.
Using GraphRAG system, the previous knowledge base will be transformed into nodes and edges like the following.
Nodes: Albert Einstein, theory of relativity, theoretical physics, astronomy, early 20th century, space, time, Isaac Newton, laws of motion, universal gravitation, classical mechanics, 1915, general theory of relativity, special relativity, 17th century, modern physics.
Edges:- (Albert Einstein) – [developed] → (theory of relativity)- (theory of relativity) – [revolutionized] → (theoretical physics)- (theory of relativity) – [revolutionized] → (astronomy)- (theory of relativity) – [formulated in] → (early 20th century)- (theory of relativity) – [impacted] → (understanding of space and time)- (Isaac Newton) – [known for] → (laws of motion)- (Isaac Newton) – [known for] → (universal gravitation)- (Isaac Newton) – [laid the groundwork for] → (classical mechanics)- (general theory of relativity) – [presented by] → (Albert Einstein)- (general theory of relativity) – [expanded on] → (special relativity)- (Newton’s work) – [provided foundation for] → (modern physics)
When prompted with the question “How did the scientific contributions of the 17th century influence early 20th-century physics?” GraphRAG’s -based retriever can recognize the progression from Newton’s work to Einstein’s advancements, highlighting the influence of 17th-century physics on the early 20th-century development. This structured retrieval enables the answer to be contextually rich and accurate: “Isaac Newton’s laws of motion and universal gravitation, formulated in the 17th century, provided the foundation for classical mechanics. These principles influenced Albert Einstein’s development of the theory of relativity in the early 20th century, which expanded our understanding of space and time.”