GraphRAG + GPT-4o-Mini is the RAG Heaven
Source: tai_blog

Disclaimer: This implementation of GraphRAG is inspired by the paper From Local to Global: A Graph RAG Approach to Query-Focused Summarization by Darren Edge et. al. The code is not entirely similar to the papers codebase  though the prompts for certain tasks are taken from the papers codebase.   This is the first blog in a multi-part blog series series about GraphRAG. In this blog series  our goal is to achieve the following    Understand the fundamentals of GraphRAGThe need for GraphRAG: GraphRAG vs. Semantic/Keyword-based RAGImplement GraphRAG components from scratch in PythonApply GraphRAG for Content-Based Movie Recommendation: GraphRAG4ReccomendationUse GPT-4o-Mini for creating the graph and providing recommendationsWe will achieve the following output by the end of this multi-part blog series.   The following is the GitHub repository for the GraphRAG4Rec codebase.   GitHub - vatsalsaglani/GraphRAG4Rec: A naive implementation of GraphRAG for Movie Recommendation onA naive implementation of GraphRAG for Movie Recommendation on IMDB Top 1000 movies dataset. github.com   Other PartsPart 2: GraphRAG vs Semantic/keyword-based RAGPart 3: Extract  entities  relations  and claims to build the graph (coming soon)Part 4: Batch communities and prepare summarization reports (coming soon)Part 5: Query processing and recommendation generation via map-reduce prompting (coming soon)In this blog Well understand the fundamentals of GraphRAG with an example.   What is GraphRAG?GraphRAG is an advanced Graph-based Retrieval Augmented Generation (GraphRAG) approach introduced in the paper From Local to Global: A Graph RAG Approach to Query-Focused Summarization by Darren Edge et. al. This approach combines graph theory  information retrieval  and LLMs.   The core concept is that the entities in our text are represented as nodes in the graphs and the relations between these entities represent the edges between the nodes. The graph is then hierarchically divided into communities and summarized into community reports.   At query time  weve to decide how deep should we explore the communities to find relevant communities. The more the depth the more the computations/LLM calls.   Once the relevant communities are retrieved we can answer the user query using the summary reports of those communities.   The following diagram depicts the entire process.   Key components of GraphRAGAs shown in the above image  weve divided the GraphRAG process into the following three key components.   ExtractEmbedQueryLets understand these components individually.   ExtractIn the extract component  we do the following things    Extract EntitiesExtract Relations among EntitiesExtract Claims on EntitiesExtract Claims on RelationsWell understand this with an example.   Suppose we have the following text.   Movie: The Shawshank Redemption Two imprisoned men bond over a number of years  finding solace and eventual redemption through acts of common decency.\nYear: 1994\nDirector: Frank Darabont\nCast: [Tim Robbins  Morgan Freeman  Bob Gunton  William Sadler]\nCertificate: A   Step 1: Extract Entities   The Shawshank Redemption (Movie)Frank Darabont (Person  Director)Tim Robbins (Person  Actor)Morgan Freeman (Person  Actor)Bob Gunton (Person  Actor)William Sadler (Person  Actor)1994 (Year)A (Certificate)Step 2: Extract Relations   The Shawshank Redemption  directed by  Frank DarabontThe Shawshank Redemption  stars  Tim RobbinsThe Shawshank Redemption  stars  Morgan FreemanThe Shawshank Redemption  stars  Bob GuntonThe Shawshank Redemption  stars  William SadlerThe Shawshank Redemption  released in  1994The Shawshank Redemption  has certificate  ASteps 34: Extract Claims for Entities and Relations   The Shawshank Redemption: Two imprisoned men bond over a number of years  finding solace and eventual redemption through acts of common decency.The Shawshank Redemption: Released in 1994The Shawshank Redemption: Has certificate AThe central node will be the name of the movie  The Shawshank Redemption.   If we were to plot the entities  relations  and claims it would look something like the image below.   EmbedAfter processing the required steps for the first component  Extract  for all the documents  the extracted information will be embedded into a graph.   As we need to embed movies into a graph lets take two more movie texts.   Movie: Inception\nGenre: [Sci-Fi  Action  Thriller]\nYear: 2010\nDirector: Christopher Nolan\nCast: [Leonardo DiCaprio  Joseph Gordon-Levitt]\nCertificate: PG-13   Movie: The Matrix\nGenre: [Sci-Fi  Action]\nYear: 1999\nDirector: The Wachowskis\nCast: [Keanu Reeves  Laurence Fishburne]\nCertificate: R   Extract output for Inception   Step 1: Extract Entities   Inception (Movie)Christopher Nolan (Person  Director)Leonardo DiCaprio (Person  Actor)Joseph Gordon-Levitt (Person  Actor)2010 (Year)PG-13 (Certificate)Sci-Fi (Genre)Action (Genre)Thriller (Genre)Step 2: Extract Relations   Inception  directed by  Christopher NolanInception  stars  Leonardo DiCaprioInception  stars  Joseph Gordon-LevittInception  released in  2010Inception  has certificate  PG-13Inception  has genre  Sci-FiInception  has genre  ActionInception  has genre  ThrillerSteps 34: Extract claims on Entities and Relations   Inception: A skilled thief with the rare ability to extract information from peoples minds is offered a chance to regain his old life as payment for a task considered to be impossible: inception  the implantation of another persons idea into a targets subconscious.Inception: Released in 2010Inception: Has certificate PG-13Inception: Is a Sci-Fi filmInception: Is an Action filmInception: Is a Thriller filmExtract output for The Matrix   Step 1: Extract Entities   The Matrix (Movie)The Wachowskis (Person  Directors)Keanu Reeves (Person  Actor)Laurence Fishburne (Person  Actor)1999 (Year)R (Certificate)Sci-Fi (Genre)Action (Genre)Step 2: Extract Relations   The Matrix  directed by  The WachowskisThe Matrix  stars  Keanu ReevesThe Matrix  stars  Laurence FishburneThe Matrix  released in  1999The Matrix  has certificate  RThe Matrix  has genre  Sci-FiThe Matrix  has genre  ActionSteps 34: Extract claims on Entities and Relations   The Matrix: A computer programmer discovers that reality as he knows it is a simulation created by machines to subjugate humanity  and joins a rebellion to overthrow them.The Matrix: Released in 1999The Matrix: Has certificate RThe Matrix: Is a Sci-Fi filmThe Matrix: Is an Action filmEmbed Step 1: Create a Graph   Now that we have the entities  relations  and claims from all three movies we can embed these into a graph like the following.   Embed Step 23: Detect Communities and Establish Hierarchy   We can divide the graph into the following two communities based on the genres.   Drama and Crime CommunitySci-Fi and Action CommunityWe can use a hierarchical community detection algorithm  the Leiden Algorithm  to cluster the nodes into separate communities.   First  lets look at how the hierarchical communities will come out.   We have the following hierarchies.   C0  Movies: This community contains all the movies in our dataset. It represents a diverse range of movies spanning across different genres  periods  and themes. The movies share common elements such as directors  actors  genres  and release year  but differ in their specific content and style.C1  Drama and Crime: This community focuses on dramatic storytelling with elements of crime.C1  Sci-Fi and Action: This community combines elements of science fiction and action.C2  Pure Sci-Fiction: This sub-community  represented by The Matrix  focuses on science fiction concepts with a heavy emphasis on action.C2  Sci-Fi Thriller: This sub-community  represented by Inception  combines science fiction elements with psychological thriller aspects.With this hierarchy  we have both global and local categorization. The C0 and C1 clusters/groups/communities are very broad  global  and the C2 cluster/group/community are very specific  local.   Embed Step 4: Summarize Communities   C1  Drama and CrimeIntense character-driven narrativesExploration of human relationships and emotionsThemes of justice  redemption  and perseveranceRealistic portrayals of criminal justice systems2. C1  Sci-Fi and Action   Futuristic or alternative reality settingsMind-bending concepts and technologiesBlend of intellectual stimulation and visual spectacleExploration of the nature of reality and consciousness3. C2  Pure Sci-Fi Action   Dystopian future settingsAdvanced technology as a central plot elementHigh-octane action sequencesThemes of human vs. machine4. C2  Sci-Fi Thriller   Complex  layered narrativesPsychological manipulation and explorationBlurring lines between reality and imaginationIntellectual puzzles and mind-bending conceptsSummary reports can also contain awards  performance by actor directory  box office results  etc. as well.   Well take a short detour and understand the Leiden algorithm with the movie example.   About Leiden AlgorithmThe Leiden algorithm is an improved version of the Louvain method for community detection. It works by optimizing modularity  a measure of the density of links inside communities compared to links between communities.   First  lets understand modularity.   Modularity is a measure of how well a network is divided into communities. We can think of it as    High modularity means there are many connections within communities and few connections between different communities.Low modularity means connections are more evenly spread  with no clear community structure.For our movie example  high modularity would mean movies within a community have many shared characteristics like the Sci-Fi and Action community. Low modularity means fewer characteristics in common like the Drama and Crime community.   Hierarchical Community Detection StepsLets look at the community detection steps for the movie example.   Step 1: Start with individual nodes   Begin with each movie as its own community.   Community 1: The Shawshank RedemptionCommunity 2: InceptionCommunity 3: The MatrixStep 2: Merge nodes into communities   Look at the connections between movies like shared genres or themes and merge them if it improves modularity.   Merge Inception and The Matrix into a Sci-Fi and Action community.The Shawshank Redemption remains in its own Drama and Crime community.Step 3: Create the first level of hierarchy (C1):   C1 Community 1: Drama & Crime (The Shawshank Redemption)C1 Community 2: Sci-Fi & Action (Inception  The Matrix)Step 4: Communities as Nodes   Now consider communities as nodes.   Step 5: Repeat Steps 1  2  and 3 at a higher level   Look for connections between these community nodes. In this case  there arent enough communities to merge further  so we shop here for the C0 level.   Step 6: Refine lower levels   Go back to the Sci-Fi and Action community and look for subcommunities.   Split Inception and The Matrix based on their more specific characteristics.Step 7: Create the second level of hierarchy (C2)   C2 Community 1: Pure Sci-Fi Action (The Matrix)C2 Community 2: Sci-Fi Thriller (Inception)Finally  we have the following hierarchy.   QueryIn the query part  we use a map-reduce approach to find relevant communities using a map operation. The map outputs are then provided to the reduce (reducer) to answer the user query.   Lets look at the query process with an example query  I want to watch a crime drama.   The following is how the entire process will look like.   Map Phase   We first have the map phase. Here  every community report is passed to the mapper which will output how relevant the community is for the given query along with the movies.   The output of the map phase through every community will look like the following.   Drama and Crime C1:{ community: Drama & Crime C1  relevance_score: 95  movies: [The Shawshank Redemption]  reason: Directly matches the crime drama genre request }Sci-Fi and Action C1{ community: Sci-Fi & Action C1  relevance_score: 10  movies: [Inception  The Matrix]  reason: Does not match the crime drama genre request }Pure Sci-Fi Action C2{ community: Pure Sci-Fi Action C2  relevance_score: 5  movies: [The Matrix]  reason: Does not match the crime drama genre request }Sci-Fi Thriller C2{ community: Sci-Fi Thriller C2  relevance_score: 15  movies: [Inception]  reason: Has some thriller elements but not crime drama }Reduce Phase   The outputs from the map phase are passed to the reducer along with the user query to get a list of relevant suggestions along with other recommendations.   The following is how the output of the reduce phase will look like.   { relevant_communities: [ { community: Drama & Crime C1  relevance_score: 95  movies: [The Shawshank Redemption] } ]  other_suggestions: [ { community: Sci-Fi Thriller C2  relevance_score: 15  movies: [Inception] } ] }Moreover  we can communicate this output via an LLM by providing the user query and the relevant communities with movie details along with suggestions. We can prompt the LLM to personalize the output based on the user query and the relevant movies and extra suggestions.   ConclusionIn this blog  we got an introduction to GrapRAG and the key components  extract  embed  and query. Along with that we also learnt about hierarchical community detection using the Leiden algorithm. In the upcoming blogs  well build upon this knowledge to develop a GraphRAG module for a content-based movie recommendation system  GraphRAG4Recommendation.   See you in Part 2: GraphRAG vs semantic/keyword-based RAG.