GraphRAG is an AI approach that combines retrieval augmented generation with knowledge graphs, enabling agents to traverse interconnected entity relationships for complex multi-hop reasoning queries. Unlike traditional vector RAG for simple similarity searches, GraphRAG excels when dealing with relationship-heavy data requiring deep reasoning across multiple hops. The implementation uses Neo4j graph database with the Neo4j GraphRAG Python package, where an LLM converts natural language questions into Cypher queries to retrieve information from the knowledge graph. This approach is particularly valuable for enterprise Q&A systems, compliance auditing, research assistants, and customer support automation where entities and their connections need to be traced through multiple relationships.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
GraphRAG in Python: Agentic AI with Knowledge Graphs
Added:Today, we're going to learn how to do graph rack in Python, which means retrieval augmented generation rack with knowledge graphs. Agents that have access to knowledge graphs and that can retrieve information from knowledge graphs. We're going to talk about the pros and cons, when to use knowledge graphs versus traditional rack, and we're going to see how to do this in Python with Neo4j, which is a graph database. Now, if you like this video and get value out of it, let me know by hitting the like button and subscribing, and now let us get right into it.
>> [music] >> All right, so we're going to talk about graph rack in this video today. So, retrieval augmented generation with knowledge graphs in Python. For this, let me maybe briefly boot up my paint to give you a visual understanding of what we're doing here. Rack in general, the idea is you have an agent, let's say this is now our agent, looks a little bit like an alien, and this is our user, and the user has certain questions, asks the agent something, agent responds, and sometimes the agent might need information that it doesn't have automatically in its model weights essentially. So, what we have is maybe a database with some information, the agent can go to that database, retrieve information from the database, and then answer the user question in a grounded way. So, in a in a way that's grounded with an answer that's grounded in context essentially. Now, the only difference between normal rack like this and graph rack is that you have a knowledge graph. So, you have entities and connections and a very detailed graph structure, very extensive graph structure with connections and patterns and everything.
And the idea is that the agent can now traverse this graph or also use embedding methods to find certain nodes.
For example, this can be a person, a person has a certain location that they live in, and other people also live in that location, and one of these people created an operating system called Linux or a kernel called Linux, and Linux is released in in certain year or something like this. So, all these concepts can be connected in a graph, and this can make sense in a number of different scenarios. For this, I prepared here very fancy uh slides.
This is just a couple of keywords that I want to go through. If you're not interested in the theory, by the way, if you just want to see how to code it, you can skip to that section. But, basically, you want to be using knowledge graphs in GraphRAG whenever you have a lot of entities, a lot of connections, when you have relationship-heavy data, but also relationship-heavy queries. So, if you're asking questions that require multi-hop reasoning, not just who lives in a particular place, but where does the guy live that created an operating system that someone I know uses, and so on and so forth. So, you have multi-hop reasoning. That is when you want to use GraphRAG and knowledge graphs in general. Whenever you have deep reasoning over interconnected concepts, and not just lookups. And this can also be nice for explainability and audits.
We're going to talk about this when we get here to the typical examples. But, basically, it's very easy to trace exact paths using the graph structure.
And you don't want to be using knowledge graphs in GraphRAG if you just want to do simple retrieval, if you're just looking for simple chunks from documents, if you just want to look up some facts, if you just want to find the most similar text or something like this. This is where you want to use classic RAG. Whenever something is simple, when you don't have a lot of data, when you don't have a lot of relationships, when data changes a lot as well, because maintaining a graph can be costly. You want to use classic RAG.
GraphRAG and knowledge graphs are useful whenever you have relationship-heavy queries, relationship-heavy data, that stays the same, or stays similar for the most part. So, in a nutshell, use classic vector RAG for similarity search and semantic search. Use GraphRAG for relationship-heavy use cases. Now, typical use cases for GraphRAG, these are just a couple. Uh enterprise Q&A systems. Let's say you have a company, you have a lot of complex, interconnected uh a complex, interconnected knowledge base. You want to ask a question, and it has to consider multiple things that are uh part of this huge ecosystem in a company. That would be something that you can model as a knowledge graph and then the agent can access this knowledge graph. Compliance and auditing as I mentioned, you can trace exactly what happens and what has to be done. You can link entities and policies and evidence.
A research assistant as well, you can link papers to patents to code bases and so on. Process and workflow automation, you need to retrieve the constraints, you need to retrieve the context, maybe there are some policies to consider.
That can also be useful. And customer support automation because you have a customer or the customer can be linked to an issue, the issue can be linked to a subscription. This also can be linked to a known bug in the past, then maybe to a past ticket, to troubleshooting resources and so on. Whenever you have stuff that can be connected across multiple hops and you need that in your system, you need that for the agent that you're working with, then GraphRAG makes a lot of sense. So, that's the in a nutshell summary. What we're going to do in particular in this video today is we're going to use Neo4j GraphRAG. So, Neo4j in general is just a graph database and Neo4j GraphRAG is an official Python package that they have that we can use to just provide a prompt. We still need to connect an intelligence, so an AI to be able to turn this prompt into a Cypher query.
Cypher is the query language of Neo4j.
We're going to talk about this here in a second when we look into the code. But basically, we're passing a prompt and this library turns it into a Cypher query and then returns the results and then our agent can present the answer to our question using the knowledge graph as the basis for the answer. But enough of the talking now, let us go into the environment and set up so we can actually play around with this in Python. And the very first thing that we need is a Neo4j database, a graph database that we can put our knowledge graph into. And for this, you can either install Neo4j locally on your system.
You can do that, for example, by installing Neo4j for desktop. It's available for Linux, Mac, and Windows.
You can use a package manager to install Neo4j locally. You can use a Docker container, so Docker compose with a service for Neo4j. Or you can connect to an online managed instance if you have one. I'm personally going to go with a Docker route. If you know how to use Docker, you can do that as well. I'm not going to cover Docker itself in this video today, so I'm not going to show you how to install Docker. I'm going to show you how to set up everything so you can run Neo4j using Docker Compose, but I'm not going to cover Docker in and of itself. So, if you're not comfortable with Docker, you can either just follow the instructions, make sure you have it installed on your system, and just accept that it's working, or you can go with something like Neo4j for Desktop.
That's up to you, but you need to have a Neo4j database that you can connect to.
We're going to work on localhost with Docker today. In addition to that, you also need an API key from any provider.
I'm going to use OpenAI, so you can go to platform.openai.com/api-keys.
You can create a new secret key, and then what you want to do is you want to create a.env file with your key in it. So, we're going to open up a terminal here, navigate to any directory that you want to be working in. You don't have to use a terminal, you can also use VS Code, whatever. In my case, I'm going to go into the tutorial directory, and here I'm going to create a file called.env, so.env.
And in here, I'm going to place OpenAI_API_Key equals, and then here I'm going to put now my key, which I'm not going to show on screen. And later on, we're going to load this key into the environment by using a Python package, so we can actually use it with LangChain, which is what we're going to use here for the agent. All right, so right now I have the.env file here, nothing else. And the next thing that we need to do is we need to install some packages. Now, how you do this is up to you. You can go and just say pip or pip3 install, and then the packages we're going to need today.
I personally like to use the uv package manager. This is just optional. If you don't want to use it, go with pip or pip3 install. What I'm going to do is I'm going to say uv init to create a uv project, and then I'm going to say uv add instead of pip install, which is basically the same thing. What are we going to need? We're going to need LangChain with OpenAI, so you want to say LangChain, and then in square brackets OpenAI, because we're going to use this for the agent, with OpenAI support. We're also going to need Neo4j.
We're also going to need Neo4j_graph rack, which is an official package from Neo4j. And we're going to need python-dotenv to load the environment variables for the.env key value pairs into the environment. So, UV add all this, we'll pip install all this. There you go, it's done. And then we can go into a script called main.py, which we're going to start from scratch here. And here we can code a basic example for graph rack. And the first thing that we want to do now after the installation is we want to create a file called docker-compose.yaml.
This is going to be for the instance of Neo4j. We're going to say services, and the service that we want to create here is going to be the Neo4j database. So, just Neo4j. The image that we're going to use is Neo4j version 5. So, make sure this is indented properly. And we're going to map two ports. One is going to be for the web UI. This is the port uh 7474.
So, make sure you map 7474 to 7474.
And then we also have a port for a protocol called Bolt. Bolt is basically a protocol, a TCP protocol, for Neo4j specifically. So, this is what they use internally. The port for that is 7687.
So, 7687, like this. These are the two ports that we need to map. And then we also need to provide an environment variable, which is going to set our uh authentication.
So, username and password. For this, we're going to say Neo4j _auth, and this is going to be set to Neo4j/ whatever the password is. Let's go with password123.
We're going to keep it super minimal and simple. That is our entire setup here.
Just have Docker installed, have Docker Compose installed, and then a file like this. All you have to do then is you need to say docker-compose up. Now, the first time I would run it without the dash D just to see the output, but then you can detach to keep it running in the background. So here now you can see changed password for user. This is booting up for the first time. And then we have a Neo4j database running on localhost with the two ports for the web interface and also for Bolt. As you can see here, HTTP and Bolt are running. We can go to the web interface here, localhost 7474 browser. If we want to, we can connect here with Bolt or Neo4j.
We're not going to use the web interface though. We're going to just go into Python directly. If you want to detach this, you can just press D. This is going to put it into the background. And now we can start coding at a file called main.py. Let's start it from scratch here. I'm going to move this away and we're going to focus on the code. And the code is just going to be having a LangChain agent that has a tool that can query the knowledge graph and then it can answer based on the information that's in the knowledge graph. And for the sake of simplicity, the knowledge graph will be created every time we run the script. So we're going to insert the data into the Neo4j database just for the sake of simplicity. So let's go into full screen and start by saying from.env import load_dotenv. This is what we need to load the OpenAI API key into the environment. So we're just going to run this function here.
Then what we want to do is want to say from LangChain agents import create_agent.
That is going to allow us to obviously create an agent. From LangChain tools, we want to import the tool decorator.
This is going to allow us to um define a function as a tool.
Then from Neo4j itself, we want to import the graph database class. This is going to be just for the driver for connecting to the database. From Neo4j graph rack.llm, we want to import the And then also from Neo4j graph rack.
retrievers import. And here we have different retrievers. If you use autocomplete, you can see we have different retrievers. Some of them work with embeddings as well. Some of them are hybrid. Some of them are more complex, but the simplest one that you can use is a text to Cypher retriever. Cypher with a Y is the query language of Neo4j. So, basically it takes in a prompt and produces a Cypher query. That's the simplest one that we can use. It does that using AI. So, we're going to go with the text to Cypher retriever.
And these are basically the imports. We load the environment variable, and then we can create a driver instance. So, graph database, the connection will be bolt. So, here we're using now the bolt protocol. Bolt and then colon {slash} {slash} localhost, and then the port was I think 7687.
That is our connection string, and then we also need the authentication. So, off is going to be equal to a tuple, and the user is Neo4j, and the password was password 1 2 3. So, that's the driver now. And with the driver now, we can say driver execute query, and we can run a command in our graph database. We're going to use a command in Neo4j called merge. Merge is basically match if it exists, otherwise create it. So, what we can provide here is we can provide entities or relationships, and they're going to be selected or matched, so found if they exist in the database, otherwise they're going to be created.
And I'm going to start with a very simple example. I'm just going to say there is an object called F. So, that's going to be the identifier. I'm going to say colon to say that this is a person.
So, F is of type person. F is an entity, a node of type person. And then I can provide a dictionary here, and say that this person has a name for example. And in my case now, this person will be me, Florian Dedoff is the name.
And then I can say the country that this person lives in is Austria. So, that would be inserting or selecting one entity in the graph database.
Now, I can do the same thing now with different entities. I can say merge C for YouTube channel and the name of a YouTube channel can be NeuralNine, for example.
Now, there's no connection here yet. We just have two entities, a person and a YouTube channel.
In addition to that, I could say merge and we could say that there is an operating system and the name of this operating system is Linux. Now, we know Linux is a kernel, but basically this is also kind of an operating system. I'm not going to go more granular here.
Let's just say Linux is the operating system and of course you can provide any attributes you want. You can say release year or something like this if you want to. We're just going to keep name for now.
And then we can create another person.
So, I can copy this, for example. I can paste it down here. I can say this person here, the identifier is going to be L and this person is going to be Linus Torvalds and his country is Finland.
So, that is also now an entity and we can do the same thing with relationships. So, I can say merge F in parentheses and I can say that F owns something.
So, I can say F and this syntax here, square brackets, colon and the name of the relationship, then an arrow to the other entity would be F owns something.
So, F owns C. The person Florian owns the channel NeuralNine. This would be this relationship here.
And I can now do this for all the obvious things here. I can say, for example, that F also uses Linux.
So, uses the operating system Linux.
And then, of course, I can say that um the person L here created the operating system Linux. So, that now would be a knowledge graph that knows that there's a person called Florin Dediu living in Austria that owns a YouTube channel that uses an operating system called Linux. And this operating system called Linux was created by a person Linus Torvalds who lives in Finland. This is information that is structured in a graph database.
So, you can literally think of this as a graph with nodes and edges. And what we can do now is first of all, we can provide the description of the schema as a string because we need to provide to the model that we're going to use to the agent we're going to use uh or in this case to the retriever of the Neo4j graph rack library, we need to provide the schema of this database. So, we don't have to again specify all the information. This is a very minimal example because we only have a very few entities, but we want to explain okay, what kind of data is in our database so that our agent or in this case the retriever knows what to look for. So, we can say here for example, node labels and I can say that we have a person that contains name and country.
Then we have a YT channel which contains a name. And then we have an OS which also contains a name. And then we can say here uh relationships.
And the syntax is basically the same. We just have here person in parentheses person and then owns a YouTube channel. And of course, in this example, it looks like I'm just repeating the whole thing, but in reality of course, you would have many more people, more YouTube channels, more operating systems. So, this would be a very condensed version of the uh structure of the of the knowledge graph. So, a person can own a YT channel.
Then a person can also uh create stuff or have created stuff uh which is the operating system in this case. And then we can also say that a person uses operating systems. Both is correct. Then maybe you want to stop the string or end the string right here. So, that would be the schema.
And with all this now, we can create an instance of a retriever. So, I can say that a retriever is equal to What was the class again? Text to Cypher retriever.
The driver for this retriever is going to be the driver we defined above.
The LLM, so the intelligence is going to be an OpenAI LLM. The model name is going to be equal to GPT-4-mini.
Feel free to choose whatever you want here.
Uh we're not going to provide any additional parameters, and then we also want to have the schema, which is actually called the Neo4j schema, and this Neo4j schema is our schema text.
So, this is like a like a prompt, a context prompt to provide information about what is in the knowledge graph, how the knowledge graph is structured, and that is the actual data here. Very minimalistic, but you get the idea.
So, this is now the retriever. In this retriever, we can pass a question to it, it will turn it into Cypher and give us an answer. And we want to provide this retriever now as a tool to our agent.
So, I'm going to say here at tool, and we're going to define a function called query KG, query knowledge graph, which is going to take in a question, which is a string, and it will also return a string back um as a result. And of course, since we're working here with agents, we need to provide a docstring to explain what this function can do.
Uh query the knowledge graph for information. You can pass entire user questions, queries, results, or maybe not result, let's say returns graph rows.
That would be our docstring. Then the result would just be retriever.search given the question. That is the parameter for this function and then we can just return everything joined together on backslash n, which is a line break. So, I'm going to say here join item.content for item in results.items.
Or if we have nothing, we're just going to say no content.
Like this. This is a tool that our agent can use now and this agent will be created as usual here with a create agent function. The model uh is going to be also GPT-4o mini.
The tools are going to be a list of tools containing just query kg and the system prompt here will be you are a helpful assistant with access to a knowledge graph.
Query it whenever you need to. And then finally, we can go into querying. I can say if name is equal to main, what we're going to do is we're going to ask a question. The question is going to be the following. Let me copy paste this because this is just easier. Don't have to type it out manually, but it's a question that uses all the uh notes and connections. What country is the creator of the operating system used by the person who runs Neural Nine from? That has a very uh complex or complicated question here. It's not well formulated, but you get the idea. There is a guy who runs Neural Nine. He uses an operating system. Someone created this operating system. What country is this guy from? This is the question that we're asking here and now we can say response uh response is equal to agent.invoke and we can just pass here a dictionary.
Messages and messages will point to a list, and here we're going to have user and the query, and then we can just print response messages uh -1, and I see right now that I had a typo here, messages.
And then we can just say print response then messages -1 and then {dot} content.
So, if I didn't make any mistakes, this should now run and give me an answer.
And of course, I have a mistake because we don't just create a graph database.
We need to say actually graph database {dot} driver. And we also need to make sure that we close the parentheses here.
So, after Neural Nine.
All right, let's go and run main.py again. And then after a while, you can see I get the creator of the operating system used by Florian Detof, who runs Neural Nine, is Linus Torvalds, and he is from Finland. So, it answered my query. We can also take a look at what's happening exactly under the hood if we're interested in that. In this case, we would have to be streaming, so I'm going to say here uh for step in agent, and then stream.
And instead of invoking, we're going to stream now. So, I'm still going to pass the same object here with messages, then again the tuple of user and Q. Then also stream mode is going to be equal to values.
So, we're going to iterate over that, and here we're just going to say step and then messages and then -1. We're going to do a pretty print to see what's happening here. And of course, user also needs to be in quotation marks. And now we can say UV run main.py and see what's happening step by step behind the scenes. You can see who is the creator of the operating system used by Neural Nine. It didn't get the answer here. Who runs Neural Nine? Florian Detof. What operating system does Florian Detof use? Linux.
Who created Linux? Linus Torvalds. What country is Linus Torvalds from? Finland.
And then at the end you can see here the final answer which tells me again, you don't see this right now. Maybe I have to press enter a couple of times, that Finland is the answer basically. Now, I also want to show you that this can be even more impressive if we use a more intelligent model and add more data. So, I have here an example py file with much more data. This is now AI generated because I didn't want to type this out myself, but the code remains the same.
We just changed the model here for the retriever to GPT 55 for the agent itself. And basically now the question is on Neural Nine a lot of automation is taught. What is the capital of the country where the inventor of the programming language covered there was born in? So, multiple steps we have Neural Nine automation content. There I'm teaching Python. Okay, what is the inventor of Python? Who is the inventor of Python? What country is he from?
What's the capital of that country? And this is all here in the data. So, I own Neural Nine. There's Python automation.
And then you have to connect Python to Guido van Rossum or whatever the name is pronounced or however the name is pronounced. And if if I do that now, if I say UV run example.py, we're going to see that this is going to be a one-shot query. We don't even need to split this up. You can see here the question is passed immediately and GPT 55 will convert this into a proper Cypher query and we get the record programming language Python inventor Guido van Rossum inventor birth country Netherlands capital Amsterdam. So, the answer is Amsterdam. This is how you can do graph rack in Python. So, that's it for this video today. I hope you enjoyed it and hope you learned something. If so, let me know by hitting the like button and leaving a comment in the comment section down below. Also, in case you're interested on my website you will find a services tab and a tutoring tab. There you can contact me if you need help with a project, if you need freelancer, or if you want me to teach you one-on-one. You can contact me at the bottom of both pages via LinkedIn or email. Besides that, don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free. Other than that, thank you very much for watching. See you in the next video and bye.
Related Videos

Expanding Stikbot thumbnails
leopoldshorts
2K views•2023-09-24

Digital Discrimination: Cognitive Bias in Machine Learning
redmonktechevents2974
4K views•2019-12-18

Evolutionary Approach to Clustering by Ujjwal Maulik
ICTStalks
279 views•2019-06-26

Rose Yu "Learning from Large-Scale Spatiotemporal Data"
networkscienceinstitute
2K views•2019-03-04

Stanford Seminar - Generalization through Task Representations with Foundation Models
stanfordonline
4K views•2025-07-14

Satellite-Based Wheat Yield Forecasting using GEE & Transformer Neural Network
gisrsinstitute
634 views•2025-06-15

Paradigm Shifts in Data Processing for the Generative AI Era: Robert Nishihara of Anyscale & Ray.io
GradientFlow
2K views•2025-01-02

How to Build Your Own GenAI-Based Knowledge Management System
2150GmbH
360 views•2025-06-03
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23