You can query Neo4j from a LangChain application using the Neo4jGraph
class.
The Neo4jGraph
class acts as the connection to the database when using other LangChain components, such as retrievers and agents.
In this lesson, you will modify the simple LangChain agent to be able to answer questions about a graph database schema.
The database contains information about movies, actors, and user ratings.
Query Neo4j
To query Neo4j you need to:
-
Create a
Neo4jGraph
instance and connect to a database -
Run a Cypher statement to get data from the database
Open the genai-integration-langchain/neo4j_query.py
file:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/neo4j_query.py[tag=**]
Update the code to:
-
Connect to the Neo4j database using your connection details:
pythonUnresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/neo4j_query.py[tag=import_neo4j] Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/neo4j_query.py[tag=neo4jgraph]
-
Run a Cypher query to retrieve data from the database:
pythonUnresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/neo4j_query.py[tag=query]
The results are returned as a list of dictionaries, where each dictionary represents a row in the results.
[ {'actor': 'Henry Czerny', 'role': 'Eugene Kittridge'}, {'actor': 'Emmanuelle Béart', 'role': 'Claire Phelps'}, {'actor': 'Tom Cruise', 'role': 'Ethan Hunt'}, {'actor': 'Jon Voight', 'role': 'Jim Phelps'} ]
You can use data returned from queries as context for an agent.
Schema
You are going to modify the agent to retrieve the database schema and add it to the context
.
You can view the database schema using the Cypher query:
CALL db.schema.visualization()
Open the genai-integration-langchain/scheme_agent.py
file.
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/schema_agent.py[tag=**]
Add the code to create a connection to the Neo4j database using the Neo4jGraph
class:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/schema_agent.py[tag=import_neo4jgraph]
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/schema_agent.py[tag=neo4jgraph]
Modify the retrieve
function to use the Neo4jGraph
instance to query the database and retrieve the schema information:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/schema_agent.py[tag=retrieve]
The agent will add the database schema to the context
and use it to answer questions about the database.
Update the question and run the application:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/schema_agent.py[tag=question]
Click to see the complete code
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/schema_agent.py[tags="**;!examples"]
When asked "How is the graph structured?", the agent will response with a summary of the graph, typically including details about nodes, relationships, and their properties.
Click to see an example response
The graph is structured using nodes and relationships.
-
Nodes:
-
Movie: Has indexes on
year
,imdbRating
,released
,imdbId
,title
, andtagline
. It has unique constraints onmovieId
andtmdbId
. -
User: Has an index on
name
and a unique constraint onuserId
. -
Actor, Director, and Genre: Do not have indexes listed. The Genre node has a unique constraint on
name
. -
Person: Has an index on
name
and a unique constraint ontmdbId
.
-
-
Relationships:
-
ACTED_IN: Connects
Actor
,Director
, orPerson
nodes withMovie
nodes. -
RATED: Connects
User
nodes toMovie
nodes. -
IN_GENRE: Connects
Movie
nodes toGenre
nodes. -
DIRECTED: Connects
Person
,Actor
, orDirector
nodes toMovie
nodes.
-
In summary, the graph represents a network where movies are connected to various entities like users, genres, actors, directors, and general persons through specific relationships indicating roles like acting, directing, rating, and categorization into genres.
Experiment by asking the agent other questions about the database schema. Here are some examples:
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/schema_agent.py[tag=examples]
When you are ready, continue to the next module.
Lesson Summary
In this lesson, you learned how to query Neo4j from LangChain and updated the agent to retrieve the database schema.
In the next module, you learn how to use vectors to create RAG and GraphRAG retrievers.