Querying a knowledge graph

Graph RAG with LangChain and Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Cypher is pattern matching with ASCII-art

 

A graph pattern of Person acted in Movie

 

MATCH


Graph RAG with LangChain and Neo4j

Cypher is pattern matching with ASCII-art

 

A graph pattern of Person acted in Movie

 

MATCH (        )               (       )


Graph RAG with LangChain and Neo4j

Cypher is pattern matching with ASCII-art

 

A graph pattern of Person acted in Movie

 

MATCH ( :Person)               ( :Movie)


Graph RAG with LangChain and Neo4j

Cypher is pattern matching with ASCII-art

 

A graph pattern of Person acted in Movie

 

MATCH ( :Person)-            ->( :Movie)


Graph RAG with LangChain and Neo4j

Cypher is pattern matching with ASCII-art

 

A graph pattern of Person acted in Movie

 

MATCH ( :Person)-[ :ACTED_IN]->( :Movie)


Graph RAG with LangChain and Neo4j

Cypher is pattern matching with ASCII-art

 

A graph pattern of Person acted in Movie

 

MATCH (a:Person)-[r:ACTED_IN]->(m:Movie)


Graph RAG with LangChain and Neo4j

Filtering results

 

A graph pattern of Person acted in Movie

 

MATCH (a:Person)-[r:ACTED_IN]->(m:Movie)

WHERE a.name = 'Tom Hanks'
Graph RAG with LangChain and Neo4j

Controlling outputs

 

A graph pattern of Person acted in Movie

 

MATCH (a:Person)-[r:ACTED_IN]->(m:Movie)

WHERE a.name = 'Tom Hanks'
RETURN a.name, m.title AS movieTitle, r.roles AS roles
Graph RAG with LangChain and Neo4j

How Cypher works

a diagram demonstrating a node in the center of a network and its relationships being expanded to highlight the path that is taken through the network

Graph RAG with LangChain and Neo4j

The CREATE clause

The CREATE clause creates a pattern in the graph

CREATE (p:Person {name: "Adam"})

CREATE (c:Company {name: "Neo4j"})
CREATE (p)-[:WORKS_FOR]->(c)
Graph RAG with LangChain and Neo4j

The MERGE Clause

The MERGE clause will find or create a pattern in the graph

MERGE (p:Person {name: "Adam"})

MERGE (c:Company {name: "Neo4j"})
Graph RAG with LangChain and Neo4j

Cypher statements in LangChain

from langchain_neo4j import Neo4jGraph

graph = Neo4jGraph(url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD)
# Execute a Cypher statement
graph.query(

"MATCH (p:Person {name: $name}) RETURN p",
{"name": "Your name"}
)
1 Cypher injection: https://neo4j.com/developer/kb/protecting-against-cypher-injection/
Graph RAG with LangChain and Neo4j

Let's practice!

Graph RAG with LangChain and Neo4j

Preparing Video For Download...