0PricingLogin
AI Agents · Lesson

Neo4j Queries from Agent Tools

Cypher query generation, graph traversal, and result parsing tools.

Why Neo4j for Agent Knowledge?

Neo4j is a graph database optimized for traversing relationships. For agents working with knowledge graphs, it lets you ask questions like Who works with whom? or What companies are connected to this person? efficiently.

Connecting to Neo4j

The neo4j Python driver connects to a Neo4j instance. Use environment variables for the connection URI and credentials. Always close the driver when done.

from neo4j import GraphDatabase
import os

URI = os.environ.get('NEO4J_URI', 'bolt://localhost:7687')
USER = os.environ.get('NEO4J_USER', 'neo4j')
PASSWORD = os.environ.get('NEO4J_PASSWORD', 'password')

driver = GraphDatabase.driver(URI, auth=(USER, PASSWORD))

def test_connection():
    with driver.session() as session:
        result = session.run('RETURN "Connected to Neo4j" AS message')
        record = result.single()
        print(record['message'])

test_connection()

# Always close driver when application exits
# driver.close()

All lessons in this course

  1. Entity Extraction for Knowledge Graphs
  2. Neo4j Queries from Agent Tools
  3. Combining Vector and Graph Retrieval
  4. Building a Knowledge-Augmented Agent
← Back to AI Agents