# Knowledge Graphs & GraphRAG: Techniques for Building Effective GenAI Applications

Zach Blumenthal & Andreas, Neo4j | AI Engineer World's Fair 2024 | 1:39:52

Source: https://www.youtube.com/watch?v=2DyHW23L6Cs
Channel: AI Engineer (https://www.youtube.com/@aiDotEngineer). Summarised by AIE Talks.
Page: https://aietalks.com/talks/knowledge-graphs-graphrag-techniques-for-building-effective-genai-applications
Published: 2024-11-01
Tags: embeddings, rag

## TL;DR
- GraphRAG combines vector search, knowledge graph traversal, and graph data science to retrieve more useful context for an LLM.
- Purchase relationships can rerank vector search results so recommendations reflect both product meaning and a customer's behavior.
- Graph embeddings and nearest-neighbor methods can infer new article relationships that support recommendations, entity resolution, fraud detection, and customer segmentation.

## Summary
Zach Blumenthal leads a hands-on workshop that builds a fashion recommendation application with H&M data, Neo4j, LangChain, OpenAI embeddings, and a small Gradio interface. The workflow begins with a graph containing customers, products, clothing articles, departments, and purchase transactions. Vector search finds products that match a text query such as "denim jeans." Graph traversal then uses co-purchase behavior to personalize and rerank those results. The workshop also creates graph embeddings for articles with FastRP and uses KNN to infer "customer also likes" relationships. A LangChain pipeline combines personalized search, graph-based recommendations, customer details, and season information in an LLM prompt that generates an email. Zach is clear that the score combination is a starting point and should be tested against labeled recommendation outcomes. He also discusses alternatives such as pre-filtering, post-filtering, external vector databases, and function calls for graph queries.

## Key ideas
### GraphRAG combines vector search with graph structure and graph data science
[05:26](https://www.youtube.com/watch?v=2DyHW23L6Cs&t=326s)
Zach defines retrieval augmented generation as an intermediate step where an application retrieves relevant data before sending a question to an LLM. This can reduce hallucinations, add domain context, and improve traceability. In this workshop, GraphRAG combines vector search, knowledge graph traversals over structured data, and graph data science. The example uses an H&M clothing dataset to build a fashion assistant. The application retrieves products related to a customer's interests, adds personalization from purchase behavior, generates recommendations, and asks an LLM to write the final email. Zach says the same workflow could support support agents, internal search, and customer experience applications.

### The workshop graph models customers, products, articles, and purchases
[11:50](https://www.youtube.com/watch?v=2DyHW23L6Cs&t=710s)
The data model contains customers and clothing articles connected by purchase transactions. Articles are variants of products, with differences such as color and size. Products carry fields such as descriptions, garment groups, product types, and names. Articles also have properties including color and design patterns, while departments describe where articles are sold. The workshop loads department, product, article, customer, and transaction CSV files into Neo4j. It creates uniqueness constraints for later queries and adds a combined text property to products. That property joins product name, type, group, description, and related fields so it can be indexed for vector search.

### Text embeddings and vector indexes are separate steps
[26:48](https://www.youtube.com/watch?v=2DyHW23L6Cs&t=1608s)
For the first search system, the workshop embeds product descriptions with OpenAI's text embedding model. The embedding step sends product text to the model, receives a numeric vector, and stores it as a node property called text embedding. The vector index then makes those stored vectors searchable. Neo4j's vector index uses HNSW for approximate nearest-neighbor search and can use cosine similarity. A query such as "denim jeans" is embedded into a query vector, then compared with product vectors to return relevant descriptions and metadata. Zach also shows that LangChain can create a Neo4j vector store around the same index.

### Co-purchase paths can personalize ordinary semantic search
[45:38](https://www.youtube.com/watch?v=2DyHW23L6Cs&t=2738s)
The workshop adds graph patterns after the initial vector retrieval. Starting from a specific customer, the query follows purchases to articles, finds other customers who bought the same articles, and examines what those customers bought next. The resulting peer group provides a co-purchase score based on how often candidate products appear in those shared purchase paths. The system combines that purchase score with the vector search score and reranks the results. This changes the order for a query such as "denim jeans" depending on the customer. Zach explains that optional matching allows the system to retain vector results when a customer has no useful matching purchase relationships.

### Graph embeddings compress multi-hop structure into searchable vectors
[1:19:07](https://www.youtube.com/watch?v=2DyHW23L6Cs&t=4747s)
A graph embedding represents nodes according to their position and structure in a graph. The workshop projects article and customer purchase relationships into an in-memory graph, then uses FastRP to create article embeddings. The model uses the topology of co-purchase relationships, with settings such as embedding dimensions and iteration weights controlling the representation. KNN compares the resulting vectors and writes "customer also likes" relationships back into the graph. This makes it possible to recommend articles through inferred relationships rather than running a long traversal for every request. Zach says similar methods can support entity resolution, fraud detection, classification, and customer segmentation.

### Graph embeddings need recalculation as the graph changes
[1:20:19](https://www.youtube.com/watch?v=2DyHW23L6Cs&t=4819s)
Zach says the current workflow requires recalculating graph embeddings when the graph changes. FastRP can make that practical for large graphs, but the embedding is still a computed view of the current graph rather than an automatically updated property. He suggests averaging a node's neighborhood as an intermediate embedding when an immediate full recalculation is not desirable. The useful behavior comes from representing graph topology as features, which lets the system infer relationships that are difficult to express with a fixed query. The embedding does not discard relationships. Existing edges determine where nodes sit in the embedding space, and the graph remains available for later traversals.

### The final LangChain chain gives the LLM two retrieval sources
[1:36:45](https://www.youtube.com/watch?v=2DyHW23L6Cs&t=5805s)
The final application combines a personalized vector retriever with a graph-based recommendation retriever. The first uses product text embeddings, then applies a graph query that considers the customer's peer group and purchase score. The second follows the inferred "customer also liked" relationships created with graph embeddings. LangChain passes both product lists, the customer name, the customer's interests, and the time of year into a prompt for GPT-4o. The prompt tells the model to use only the supplied products and to write an engaging email. Changing the customer ID produces different recommendations, while changing the month changes seasonal wording and product selection.

## Notable quotes
- "GraphRAG is the combination of three things that we're going to look at today: Vector search, knowledge graph traversals, and graph data science." (05:46)
- "All this cell is doing is basically making these Vector array properties inside of the node and then the vector index actually sets the index to make that searchable." (33:43)
- "The more purchases there are, the more likely it is that that individual customer is likely to like that product just based on the common purchase behaviors in the graph." (55:19)
- "The idea behind embeddings is that you're able to infer relationships between things inside of your graph that you know would be very hard to do without some sort of machine learning or just traditional methods." (1:28:18)
- "In a production system you would have recommendations and whether or not those recommendations were successful, and then you can try different methodologies and see which ones get the best historic score." (1:05:09)

## Tools & references mentioned
- Neo4j
- Neo4j Graph Data Science
- GraphRAG
- LangChain
- OpenAI
- GPT-4o
- Google Colab
- Gradio
- Vertex AI
- Gemini
- AWS Bedrock
- H&M
- FastRP
- K-nearest neighbors
- HNSW
- t-SNE
- Lucene
- Graph Academy
- Michael Hunger
- Andreas
- Toage

## Who should watch
- You are building a RAG application and need structured customer, product, or transactional data to affect retrieval.
- You want a concrete Neo4j and LangChain example rather than a chatbot-only RAG demo.
- You need to decide between graph traversal, vector search, graph embeddings, or a combination of them for recommendations or entity resolution.

## Related talks

- [Intro to GraphRAG](https://aietalks.com/talks/intro-to-graphrag) (Zach Blumenfeld, Neo4j, 1:18:35)
- [GraphRAG: The Marriage of Knowledge Graphs and RAG](https://aietalks.com/talks/graphrag-the-marriage-of-knowledge-graphs-and-rag) (Emil Eifrem, Neo4j, 19:15)
- [Practical GraphRAG: Making LLMs Smarter with Knowledge Graphs](https://aietalks.com/talks/practical-graphrag-making-llms-smarter-with-knowledge-graphs) (Michael Hunger, Jesús Barrasa & Stephen Chin, Neo4j, 19:46)
- [Graph Intelligence: Enhance Reasoning and Retrieval Using Graph Analytics](https://aietalks.com/talks/graph-intelligence-enhance-reasoning-and-retrieval-using-graph-analytics) (Alison Cossette & Andreas Kollegger, Neo4j, 1:41:16)
- [Agentic GraphRAG: Simplifying Retrieval Across Structured and Unstructured Data](https://aietalks.com/talks/agentic-graphrag-simplifying-retrieval-across-structured-and-unstructured-data) (Zach Blumenfeld, Neo4j, 15:25)
