Overview
You will be updating a LangChain agent, adding a set of tools, to interact with Neo4j.
In this lesson you will:
-
Review the agent code
-
Investigate how the agent works
-
Experiment with different queries
Continue with the lesson to create the text to Cypher retriever.
Agent
Open workshop-genai/agent.py.
import os
from dotenv import load_dotenv
load_dotenv()
from neo4j import GraphDatabase
from langchain.chat_models import init_chat_model
from langchain.agents import create_agent
from langchain_core.tools import tool
# Initialize the chat model
model = init_chat_model("gpt-4o", model_provider="openai")
# Connect to Neo4j database
driver = GraphDatabase.driver(
os.getenv("NEO4J_URI"),
auth=(
os.getenv("NEO4J_USERNAME"),
os.getenv("NEO4J_PASSWORD")
)
)
# Define functions for each tool in the agent
@tool("Get-graph-database-schema")
def get_schema():
"""Get the schema of the graph database."""
results, summary, keys = driver.execute_query(
"CALL db.schema.visualization()",
database_=os.getenv("NEO4J_DATABASE")
)
return results
# Define a list of tools for the agent
tools = [get_schema]
# Create the agent with the model and tools
agent = create_agent(
model,
tools
)
# Run the application
query = "Summarise the schema of the graph database."
for step in agent.stream(
{
"messages": [{"role": "user", "content": query}]
},
stream_mode="values",
):
step["messages"][-1].pretty_print()Review the code and try to answer the following questions:
-
What is the agent’s function?
-
What do you think the response to the
querywill be? -
How could you extend the agent?
Run the agent to see what it does.
Review
This program is a LangChain agent that uses a Neo4j database. The agent has access to a single tool which retrieves the database schema. The agent uses this tool to answer questions about the database structure.
The code:
-
Creates an
OpenAILLMmodel.python# Initialize the chat model model = init_chat_model("gpt-4o", model_provider="openai") -
Connects to your Neo4j database.
python# Connect to Neo4j database driver = GraphDatabase.driver( os.getenv("NEO4J_URI"), auth=( os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD") ) ) -
Defines a
Get-graph-database-schematool.python# Define functions for each tool in the agent @tool("Get-graph-database-schema") def get_schema(): """Get the schema of the graph database.""" results, summary, keys = driver.execute_query( "CALL db.schema.visualization()", database_=os.getenv("NEO4J_DATABASE") ) return results # Define a list of tools for the agent tools = [get_schema]The tool uses the Neo4j driver to get the database schema and return it as a string.
Determine what tool to use
The agent will use the tool’s name (
Get-graph-database-schema), and docstring (Get the schema of the graph database.) to determine whether it should execute the tool to resolve a user’s query. -
Creates a react (Reasoning and Acting) agent using the
modelandtools.python# Create the agent with the model and tools agent = create_agent( model, tools ) -
Runs the agent, passing the
queryand streams the response.python# Run the application query = "Summarise the schema of the graph database." for step in agent.stream( { "messages": [{"role": "user", "content": query}] }, stream_mode="values", ): step["messages"][-1].pretty_print()When you run the agent, you will see:
-
The messages between
Human,AI, andTool -
The context of the database schema
-
The agent’s final response
-
Experiment
Experiment with agent, modify the query to ask different questions, for example:
-
"What questions can I answer using this graph database?" -
"How are concepts related to other entities?" -
"How does the graph model relate technologies to benefits?"
Lesson Summary
In this lesson, you reviewed an agent which can answer questions about a Neo4j database schema.
In the next lesson, you will add a vector + cypher tool to the agent to enable semantic search capabilities.