How to Use the Neo4j GraphRAG Package for Python

Category :

In this article, we will walk you through the installation and usage of the Neo4j GraphRAG package for Python. This powerful tool allows developers to leverage the capabilities of Neo4j for building applications that can execute complex searches, making use of vector indexes to find similarities among data. Let’s get started!

Installation

Before diving into usage, you’ll need to install the Neo4j GraphRAG package. This package supports Python versions from 3.8 to 3.12.

To install the latest stable version, simply run the following command in your terminal:

pip install neo4j-graphrag

Optional Dependencies

For visualizing your pipelines in Neo4j, you might consider installing pygraphviz.

Examples

Let’s walk through creating a vector index using the Neo4j GraphRAG package. Think of this process like setting up a library where you first need to create a catalog index before you can add and retrieve books.

Creating a Vector Index

Before you can store embeddings, ensure the number of dimensions in the vector index matches those in your embeddings. Here’s how to create the vector index:

from neo4j import GraphDatabase
from neo4j_graphrag.indexes import create_vector_index

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
INDEX_NAME = "vector-index-name"

# Connect to Neo4j database
driver = GraphDatabase.driver(URI, auth=AUTH)

# Creating the index
create_vector_index(driver, INDEX_NAME, label="Document", embedding_property="vectorProperty", dimensions=1536, similarity_fn="euclidean")

Populating the Vector Index

Once your catalog is ready, it’s time to fill it with information:

from neo4j import GraphDatabase
from neo4j_graphrag.indexes import upsert_vector

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")

# Connect to Neo4j database
driver = GraphDatabase.driver(URI, auth=AUTH)

# Upsert the vector
vector = ...
upsert_vector(driver, node_id=1, embedding_property="vectorProperty", vector=vector)

Performing a Similarity Search

To find related information, you can perform a similarity search. This is like asking the librarian to find similar books based on a specific title you are interested in:

from neo4j import GraphDatabase
from neo4j_graphrag.retrievers import VectorRetriever
from neo4j_graphrag.llm import OpenAILLM
from neo4j_graphrag.generation import GraphRAG
from neo4j_graphrag.embeddings.openai import OpenAIEmbeddings

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
INDEX_NAME = "vector-index-name"

# Connect to Neo4j database
driver = GraphDatabase.driver(URI, auth=AUTH)

# Create Embedder object
embedder = OpenAIEmbeddings(model="text-embedding-3-large")

# Initialize the retriever
retriever = VectorRetriever(driver, INDEX_NAME, embedder)

# Initialize the LLM
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})

# Initialize the RAG pipeline
rag = GraphRAG(retriever=retriever, llm=llm)

# Query the graph
query_text = "How do I do similarity search in Neo4j?"
response = rag.search(query_text=query_text, retriever_config={"top_k": 5})
print(response.answer)

Troubleshooting

If you run into issues while using the Neo4j GraphRAG package, here are some troubleshooting tips:

  • Ensure your Neo4j database is properly set up and running.
  • Check that you are using a supported Python version (3.8 to 3.12).
  • Look into any error messages for clues; they often guide you to the specific problem.
  • If you are experiencing bugs or need support, consider reaching out via Neo4j Online Community or Discord.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

The Neo4j GraphRAG package is a robust tool for developers looking to implement advanced querying capabilities using vector indexes. By following the steps outlined above, you should be well on your way to harnessing the power of Neo4j in your applications.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox

Latest Insights

© 2024 All Rights Reserved

×