LlamaIndex Neo4j Vector Store

0.6.0 · active · verified Thu Apr 16

The `llama-index-vector-stores-neo4jvector` library provides an integration for LlamaIndex to use Neo4j as a vector store. It enables storing, indexing, and querying of document embeddings within a Neo4j graph database, supporting operations like vector search, hybrid search, and metadata filtering. The current version is 0.6.0, and it maintains an active development and release cadence within the LlamaIndex ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `Neo4jVectorStore` with LlamaIndex. It initializes the vector store with Neo4j connection details, loads sample documents, creates a `VectorStoreIndex`, and then performs a query. It assumes Neo4j credentials and OpenAI API key are set as environment variables and that a 'data' directory exists for document loading.

import os
from llama_index.vector_stores.neo4jvector import Neo4jVectorStore
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.core.embeddings import resolve_embed_model

# Set environment variables for Neo4j and OpenAI
# Ensure you have a running Neo4j instance (e.g., Docker, AuraDB)
# NEO4J_URI = "bolt://localhost:7687"
# NEO4J_USERNAME = "neo4j"
# NEO4J_PASSWORD = "password"
# OPENAI_API_KEY = "sk-..."

# Configure LlamaIndex settings (LLM and Embedding Model)
Settings.embed_model = resolve_embed_model("openai") # Or any other embedding model

# Neo4j connection details
neo4j_url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
neo4j_username = os.environ.get("NEO4J_USERNAME", "neo4j")
neo4j_password = os.environ.get("NEO4J_PASSWORD", "password")
embedding_dimension = 1536 # OpenAI's default embedding dimension

# Initialize Neo4jVectorStore
try:
    neo4j_vector_store = Neo4jVectorStore(
        username=neo4j_username,
        password=neo4j_password,
        url=neo4j_url,
        embedding_dimension=embedding_dimension,
        index_name="vector",
        node_label="Chunk",
        embedding_node_property="embedding",
        text_node_property="text",
    )
except ValueError as e:
    print(f"Error connecting to Neo4j: {e}. Please ensure Neo4j is running and credentials are correct.")
    exit()

# Create a dummy document for demonstration (in a 'data' directory)
# You might need to create a 'data' directory and a 'test.txt' file
# e.g., echo "This is a test document about LlamaIndex and Neo4j integration." > data/test.txt

# Load documents
# Ensure 'data' directory exists and contains documents
try:
    documents = SimpleDirectoryReader("data").load_data()
except Exception as e:
    print(f"Error loading documents: {e}. Make sure a 'data' directory exists and contains files.")
    documents = []

if not documents:
    print("No documents loaded. Creating a dummy document.")
    from llama_index.core.schema import Document
    documents = [Document(text="LlamaIndex integrates with Neo4j to provide a powerful vector store for RAG applications.")]

# Create a VectorStoreIndex
index = VectorStoreIndex.from_documents(documents, vector_store=neo4j_vector_store)

# Query the index
query_engine = index.as_query_engine()
response = query_engine.query("What is LlamaIndex?")
print(response)

# Example of retrieving documents directly (without LLM synthesis)
retriever = index.as_retriever(similarity_top_k=2)
nodes = retriever.retrieve("How does LlamaIndex work with Neo4j?")
for node in nodes:
    print(f"Retrieved Node: {node.text[:100]}...")

# Clean up (optional, depends on your use case)
# neo4j_vector_store._driver.close()

view raw JSON →