Graphs and RAG

Graph RAG with LangChain and Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Meet your instructor

 

  • Manager, Developer Education at Neo4j
  • Developer Education through Neo4j GraphAcademy
  • 20+ years of software development experience
  • 10+ years Neo4j experience

 

Adam's picture

1 https://graphacademy.neo4j.com
Graph RAG with LangChain and Neo4j

The R in RAG

An application diagram with a User input being added to a prompt that is sent to an LLM

Graph RAG with LangChain and Neo4j

The R in RAG

The prompt requires context that comes from documents and data sources

Graph RAG with LangChain and Neo4j

The R in RAG

This data is often converted into vector embeddings

Graph RAG with LangChain and Neo4j

Where does semantic search fail?

Vectors work well for

  • Fuzzy or Open-Ended questions

Vectors are ineffective for

  • Highly Specific or Fact-Based Questions
  • Numerical or Exact-Match Queries
  • Logical Queries

What does Paul Graham think about Generative AI?

How many Generative AI Startups has Paul Graham invested in?

Graph RAG with LangChain and Neo4j

Vectors vs. knowledge graphs

Text Vector Embedding
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

A graph diagram showing the entities mentioned in the lyrics for Don't stop believing.  A small town boy, with BORN and RAISED relationships to South Detroit

Graph RAG with LangChain and Neo4j

Vectors vs. knowledge graphs

Text Vector Embedding
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

The nodes are a person: Small town boy, a location: South Detroit, a train: Midnight Train and a location: Anywhere

Graph RAG with LangChain and Neo4j

Vectors vs. knowledge graphs

Text Vector Embedding
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

The relationships BORN and RAISED go from the person to the location.  The GOING train goes from the Midnight Train node to the Anywhere node

Graph RAG with LangChain and Neo4j

Knowledge graphs and Neo4j

 

  • Neo4j is the world's leading graph database
  • Flexible, schema-optional
  • langchain-neo4j LangChain integration
  • LCEL: LangChain Expression Language

The neo4j and langchain logos

1 https://db-engines.com/en/ranking/graph%20dbms
Graph RAG with LangChain and Neo4j

Nodes represent things

from langchain_neo4j.graphs.graph_document \
  import Node

book = Node(
type="Book",
id=f"9781098127107",
properties={ "title": "Building Knowledge Graphs" }
)

A book node in a graph diagram with properties for ID and Title

Graph RAG with LangChain and Neo4j

Relationships connect nodes

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(





)

Two WRITTEN_BY relationships going from a Book node to two Person nodes that represent the authors

Graph RAG with LangChain and Neo4j

Relationships connect nodes

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,



)

The source node of the WRITTEN_BY relationship is the Book node

Graph RAG with LangChain and Neo4j

Relationships connect nodes

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,
target=author,


)

The destination node for a WRITTEN_IN relationship is a Person

Graph RAG with LangChain and Neo4j

Relationships connect nodes

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,
target=author,
type="WRITTEN_BY"
properties=dict(...)
)

The node diagram including the Book and two WRITTEN_BY relationships to authors to demonstrate that nodes can have many relationships.

Graph RAG with LangChain and Neo4j

Connecting to Neo4j

from langchain_neo4j import Neo4jGraph


graph = Neo4jGraph( url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD )
1 https://neo4j.com/docs/getting-started/
Graph RAG with LangChain and Neo4j

Saving graph documents

from langchain_neo4j.graphs.graph_document import GraphDocument


# Wrapper for Nodes and Relationships doc = GraphDocument(
nodes=[book, jesus, jim],
relationships=[jesus_wrote_book, jim_wrote_book]
)
# Save nodes and relationships to the database graph.add_graph_documents([graph_document])
Graph RAG with LangChain and Neo4j

So, what is GraphRAG?

The previous diagram with context from documents and their embeddings

Graph RAG with LangChain and Neo4j

So, what is GraphRAG?

The previous diagram with context from documents with an added knowledge graph showing nodes and relationships derived from the documents

Graph RAG with LangChain and Neo4j

Let's practice!

Graph RAG with LangChain and Neo4j

Preparing Video For Download...