GraphRAG explained with an imaginary quiz game and not a single line of source code
Before I describe the game, I would like to thank Petar Aleksandrov for doing the graphical design of the game elements.
Have you heard about GraphRAG and wondered what it is and what the benefits are over traditional RAG? With this made-up card game, I will try to demonstrate that without going into code and technical details. You probably need to know what RAG is to understand the metaphors (but if you don’t, you can skip to the end of this blog, where I give a brief overview of what it is, as well as graph theory).
The Game
The game is a simple quiz game where the players assume the role of chatbots that get to answer the quiz questions. In our example here, the trivia is about music, but it could be about anything.
Besides a notepad and a pen to keep score, the game consists of the following parts:
- Question cards — They all have the same back side, and the front side contains one question and one answer (along with a reference to a clue card).
- Clue cards — There are 12 clue cards with the clue’s topic on the back. The front has some further information on the topic.
- A game board — The game board has 12 fields for the 12 clue cards and some indication of the relationship between them.
- A spinner — A spinner with three fields labeled STANDARD, RAG, and GRAPH RAG.
To begin, you shuffle all the question cards and place them in a pile, back side up, next to the game board. The spinner is placed next to the game board.
Now you place all the clue cards on their respective fields on the game board, back side up.
Gameplay
A player draws the top question card and reads the question.
Now the player spins the spinner.
If it lands on STANDARD, you have to act as a regular chatbot and answer the question (“Who in The Beatles played in another band together with Jeff Lynne”) to the best of your knowledge. How well do you know The Beatles? I guess you know John Lennon and Paul McCartney at least. Given that Paul is still alive (unless you believe the conspiracy theories that he died in 1966 and was replaced by a look-alike), while John spent his few years after the Beatles with Yoko Ono, Paul might be your answer. If so, it would be wrong.
If it lands on RAG, you pick up the clue card referenced at the bottom of the question card (in this case, The Beatles).
Here you get a list of the band members, and you may think, “Ah, yes! Those are the names of the other two guys,” and you may think that Ringo Starr sounds like a fellow who would play in a band with someone named Jeff Lynne. That would also be wrong.
Finally, if it lands on GRAPH RAG, you also get to select one relationship type and draw all clue cards connected with that relationship in one step from the referenced clue card (The Beatles in this case). We would select the relationship type SAME MEMBER, which lets us pick three more clue cards (in addition to The Beatles).
That reveals the clues shown below, besides those we already have, of The Beatles.
Here, we see that George Harrison played with Jeff Lynne in Traveling Wilburys, so the correct answer is George Harrison, which is not hard to get right if we are lucky enough to land on GRAPH RAG on the spinner.
If you got it right, you get a point. Either way, you put the clues back, back side up, put the question at the bottom of the pile, and the turn moves on to the next player.
Wrapping Up
That is essentially what GraphRAG is about and how GraphRAG and regular RAG improves standard LLM chatbots. We showed how it improved the answers and reduced hallucinations, but it is even more powerful when the “clues” are about a business domain that the LLM has no clue about (e.g., your HR records).
So how does the metaphor of this game translate to real GraphRAG, using a graph database like Neo4j? The game board would represent the database, and the clue cards are all the documents stored in the database (with the lines on the game board being the graph). But rather than having 12 documents, you would likely have millions. When we get a question, we wouldn’t have a direct link to the relevant document as we had on the question cards. Instead, we’d have vector embeddings and vector search to find the most relevant document based on its content and the content of the question. When the application has both the question and the relevant documents, it passes it all to the chatbot and asks it to respond to the question based on those documents and include a source reference.
You can read more about vector embeddings and how GraphRAG can be implemented for real in these two other blog posts I wrote: A GenAI-Powered Song Finder in Four Lines of Code and GraphRAG in (Almost) Pure Cypher.
Disclaimer
Please understand that this is just a hypothetical game used to make a point. It is not a fully developed game. I have not written more than the one question card, nor given any thought to how many questions could be devised on those clue cards. Frankly, I’m not sure how playable it would be with just 12 clue cards, which you would probably have memorized after a five-minute game.
Explaining RAG and Graph
For those who have never heard of RAG, or who thinks a graph is a linear plot on a chart, here is a quick summary of the two concepts.
RAG
Retrieval-Augmented Generation (RAG) is a way to improve the usability of generative AI (GenAI) chatbots, like ChatGPT. It’s used to reduce hallucinations, improve the explainability of the answers (by providing source references), and make it possible for the chatbot to answer questions about things it wasn’t trained on, like your business domain. This is done by fetching relevant documents that may contain the answers, and providing this to the chatbot together with the questions.
Graphs
With graphs, we do not refer to charts or diagrams, but graph theory as used in discrete mathematics. A graph consists of vertices (sometimes referred to as “nodes”) connected by edges (sometimes referred to as “relationships”). In this blog, the game board represents a graph, the clue cards vertices, and the relationships between are the edges.
Further Reading
For a more thorough explanation of graphs, RAG, and GraphRAG, you can check out this blog post from Neo4j CTO, Philip Rathle: The GraphRAG Manifesto: Adding Knowledge to GenAI.
GraphRAG — The Card Game was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.