LlamaIndex Neo4j Graph Stores Integration

0.7.0 · active · verified Thu Apr 16

The `llama-index-graph-stores-neo4j` library provides an integration for LlamaIndex to use Neo4j as a backend for graph stores. It enables persisting, visualizing, and querying knowledge graphs directly within a Neo4j database, supporting both triplet-based and property graph models. The library is actively maintained as part of the broader LlamaIndex ecosystem, with frequent updates to ensure compatibility and introduce new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Neo4jPropertyGraphStore` and integrate it with LlamaIndex to create a knowledge graph. It assumes a running Neo4j instance and requires an OpenAI API key for LlamaIndex's internal LLM processing during graph extraction. It uses environment variables for Neo4j credentials and the OpenAI API key for secure configuration.

import os
from llama_index.graph_stores.neo4j import Neo4jPropertyGraphStore
from llama_index.core import StorageContext, KnowledgeGraphIndex, SimpleDirectoryReader
from llama_index.core.schema import Document

# Ensure Neo4j is running (e.g., via Docker) and credentials are set
# For local Docker setup: docker run -p 7474:7474 -p 7687:7687 -e NEO4J_AUTH=neo4j/password --name neo4j-apoc neo4j:latest

NEO4J_URI = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
NEO4J_USERNAME = os.environ.get("NEO4J_USERNAME", "neo4j")
NEO4J_PASSWORD = os.environ.get("NEO4J_PASSWORD", "password") # Default password for fresh install
NEO4J_DATABASE = os.environ.get("NEO4J_DATABASE", "neo4j")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "") # Required for LlamaIndex processing

if not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY environment variable not set.")

# Initialize Neo4j Property Graph Store
graph_store = Neo4jPropertyGraphStore(
    username=NEO4J_USERNAME,
    password=NEO4J_PASSWORD,
    url=NEO4J_URI,
    database=NEO4J_DATABASE,
)

# Create storage context
storage_context = StorageContext.from_defaults(graph_store=graph_store)

# Example document (in a real scenario, use SimpleDirectoryReader or other loaders)
documents = [
    Document(text="LlamaIndex is a data framework for LLM applications. It helps connect custom data sources to LLMs.")
]

# Create a Knowledge Graph Index
# Note: This will extract entities and relationships and store them in Neo4j.
index = KnowledgeGraphIndex.from_documents(
    documents,
    storage_context=storage_context,
    # Other parameters like `llm`, `kg_extractors` can be configured
    # For this quickstart, default LLM settings will use OpenAI, hence API key is needed.
    max_triplets_per_chunk=2, # For simpler quickstart
)

print("Knowledge Graph Index created and stored in Neo4j.")

# Example query (requires an LLM configured in LlamaIndex Settings or passed to the query engine)
# query_engine = index.as_query_engine()
# response = query_engine.query("What is LlamaIndex?")
# print(response)

view raw JSON →