GraphRAG With Python And Neo4j
Written by Nikos Vaggalis   
Tuesday, 20 May 2025

Use this Neo4j GraphRAG library to build your own knowledge graph-based applications.

But why GraphRAG, the concept, in the first place? GraphRAG is an advanced RAG technique which consumes structured and unstructured documents to create knowledge graphs which extract context on top of the vectors. It's that property that renders graph based RAG to provide highly relevant answers to the users' questions.

As examined in our previous article,Ingres vs Postgres MVCC Explained With Neo4j's LLM Knowledge Graph Builder, the superiority of GraphRAG stems from that it also manages to capture the context/relationships between the vectors. That article went through a GraphRAG solution that was utilized with Neo4j as the underlying data store, while on the front end used the "LLM Knowledge Graph Builder" application which consumes, parses and provides a UI to the user to interrogate his documents.

But that was the full package of a pre-made application. To utilize and customize a GraphRAG solution to your own needs, like building knowledge assistants, search APIs, chatbots, or report generators, you can leverage the underlying GraphRAG for Python library living under package neo4j-graphrag.

So to perform a GraphRAG query using the neo4j-graphrag package, a few components are needed first:

  • A Neo4j driver: used to query your Neo4j database. Again AuraDB under free tier can be used.
  • A Retriever: the neo4j-graphrag package provides some implementations and lets you write your own if none of the provided implementations matches your needs.
  • An LLM: to generate the answer, we need to call an LLM model. The neo4j-graphrag package’s LLM interface is compatible with LangChain. Developers can also write their own interface if needed.

The LLMs provided as dependencies when installing the package are :

  • ollama: LLMs from Ollama
  • openai: LLMs from OpenAI (including AzureOpenAI)
  • google: LLMs from Vertex AI
  • cohere: LLMs from Cohere
  • anthropic: LLMs from Anthropic
  • mistralai: LLMs from MistralAI

For instance to install the packge with Mistral you use:

pip install "neo4j-graphrag[mistralai]"

Then instantiate the MistralAILLM class:

from neo4j_graphrag.llm import MistralAILLM

llm = MistralAILLM(
model_name="mistral-small-latest",
api_key=api_key, # can also set `MISTRAL_API_KEY` in env vars
)
llm.invoke("say something")

The other thing you need is the Neo4J Python driver :

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
driver = GraphDatabase.driver(URI, auth=AUTH)

Then a LLM for entity extraction, and an embedding model to create vectors on text chunks for similarity search,
This is a sample code that brings this all together:

neoj4pythonWhile this was a real world scenario, in most cases you are going to utilize SimpleKGPipeline, which is a class that simplifies the process of building a knowledge graph from text documents. It abstracts away the complexity of setting up the pipeline and its components. Consult the API for the details.

And it's open source too and although it requires a Neo4j Graph Database backend, you can opt for the cloud offering of the fully managed AuraDB which comes with a free tier too or just use a dockerized Neo4J instance.

 

More Information

GraphRAG for Python-Docs

GraphRAG for Python-Github

Related Articles

Ingres vs Postgres MVCC Explained With Neo4j's LLM Knowledge Graph Builder

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Scylla Launches ScyllaDB X Cloud
19/06/2025

The developers of ScyllaDB have announced an updated version of the managed version of its database that is aimed at meeting workloads based on demand.



Turing Papers At Auction
08/06/2025

Alan Turing's personal copy of his PhD dissertation and an original offprint of "On Computable Numbers" together with a  loose-leaf copy of his portrait photograph that bears his signature are th [ ... ]


More News

pico book

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Monday, 02 June 2025 )