Neo4j

The Neo4jTools toolkit enables agents to interact with Neo4j graph databases for querying and managing graph data.

Neo4jTools enables agents to interact with Neo4j graph databases for querying and managing graph data.

Prerequisites

The following example requires the neo4j library.

1uv pip install -U neo4j

You will also need a Neo4j database. The following example uses a Neo4j database running in a Docker container.

1docker run -d -p 7474:7474 -p 7687:7687 --name neo4j -e NEO4J_AUTH=neo4j/password neo4j

Make sure to set the NEO4J_URI environment variable to the URI of the Neo4j database.

1export NEO4J_URI=bolt://localhost:7687
2 export NEO4J_USERNAME=neo4j
3 export NEO4J_PASSWORD=your-password
4 export OPENAI_API_KEY=xxx

Install dependencies

1uv pip install -U neo4j openai kern-ai

Run the agent

1python cookbook/14_tools/neo4j_tools.py

Example

The following agent can interact with Neo4j graph databases:

1from kern.agent import Agent
2from kern.tools.neo4j import Neo4jTools
3
4agent = Agent(
5 instructions=[
6 "You are a graph database assistant that helps with Neo4j operations",
7 "Execute Cypher queries to analyze graph data and relationships",
8 "Provide insights about graph structure and patterns",
9 "Help with graph data modeling and optimization",
10 ],
11 tools=[Neo4jTools()],
12)
13
14agent.print_response("Show me the schema of the graph database", stream=True)

Toolkit Params

ParameterTypeDefaultDescription
uriOptional[str]NoneNeo4j connection URI. Uses NEO4J_URI if not set.
userOptional[str]NoneNeo4j username. Uses NEO4J_USERNAME if not set.
passwordOptional[str]NoneNeo4j password. Uses NEO4J_PASSWORD if not set.
databaseOptional[str]NoneSpecific database name to connect to.
enable_list_labelsboolTrueEnable listing node labels.
enable_list_relationshipsboolTrueEnable listing relationship types.
enable_get_schemaboolTrueEnable schema information retrieval.
enable_run_cypherboolTrueEnable Cypher query execution.

Toolkit Functions

FunctionDescription
list_labelsList all node labels in the graph database.
list_relationshipsList all relationship types in the graph database.
get_schemaGet comprehensive schema information about the graph.
run_cypherExecute Cypher queries on the graph database.

Developer Resources