Video: GenAI and Graph Foundations: Building GraphRAG with Neo4j | Duration: 7224s | Summary: GenAI and Graph Foundations: Building GraphRAG with Neo4j | Chapters: Workshop Introduction (8.88s), Workshop Introduction (200.75s), Generative AI Fundamentals (543.63s), GraphRAG Fundamentals (884.8s), Knowledge Graphs (1223.875s), Environment Setup (1440.085s), Environment Setup (1673.105s), Constructing Knowledge Graphs (1873.59s), Extracting Graph Schema (2020.35s), Q&A Session (2207.485s), Building Knowledge Graphs (2304.36s), Knowledge Graph Exploration (2686.685s), Chunk Size Configuration (2808.35s), Knowledge Graph Analysis (3002.935s), Defining Schema (3180.39s), Defining Graph Schema (3425.425s), Q&A Schema Validation (3810.635s), Adding Structured Data (3928.015s), Graph Progress Review (4233.68s), Entity-Based Lesson Discovery (4331.305s), Entity Graph Visualization (4500.075s), Retrieval Introduction (4643.255s), Vector RAG Retrieval (4694.645s), Vector Index Retriever (4871.05s), Vector Retriever Exercise (5040.84s), Q&A Session (5200.575s), Vector RAG Pipeline (5333.865s), Query Results Analysis (5601.06s), Enhanced Graph Retrieval (5650.59s), Graph Retrieval Queries (5840.45s), Graph Exploration Demo (6001.665s), Text to Cipher (6176.4s), Text to Cypher Retriever (6261.62s), Text to Cypher (6492.155s), Q&A Session (6692.355s), Wrap-Up and Resources (6917.905s), Free Tier Access (7128.94s), Closing Remarks (7155.65s)
Transcript for "GenAI and Graph Foundations: Building GraphRAG with Neo4j": Hello, everybody. Good morning, good evening, maybe good afternoon, depends where you watch us from. Let us know in chat, if you are, and where you are, and if you're excited about this. I'm very excited because, I'm also happy to have Martin. Martin O'Hanlon is developer advocate at Neo4j join me today. Or actually, he will be delivering, so maybe I'll join him. It's probably better. For, for the road to notes, kickoff for 2026 edition. So this will be our series of workshops, with a couple of interesting topics. We'll talk today about Neo four j fundamentals and Gen AI, how to start getting... Get the ball rolling, basically. We'll do something about hallucinations to prevent those next week. We'll do something about graph engineering. We do something about edge and memory. Obviously, graph rack is is part of the story as well. So it's gonna be interesting sessions. As I said, today is the first day. We'll do this another time later today as well. And then it will go every Thursday until the November 5, which is the week before notes, which is happening on the November 12. So, I have posted in chat as well all the road to road to notes workshops. So if you have just registered for this one today, check it out and see if there are others that are interesting to you. Everything will be recorded. Everything will be on demand more or less immediately after we're done. So if the timing, of one of these sessions... Because I know this is a morning session here in Europe, at least. So the others are evening sessions or, or later in the day. So They might not work for everybody, but we will definitely make everything available. You can always jump in and watch them on demand at any time as you like it. If you have questions during the sessions, there is a little, Q and A button. You should see top right of your screen where it says q and a. Please use that for your questions. We will have some time, during the session today to answer those. I'm I'm here as well, and my, our dear colleague, Vasundra, is also joining. She is, helping out with questions, so don't be shy. Don't dump anything in there. As I said, we will take some time during the session to answer some live, but we'll try to cover some... Most of those, I hope, in chat. And, yeah, if anything you like, anything is interesting, let us know where you're watching this from. Comments, whatever. Put them in chat. Like to see it lively. It's always nice to engage with other graph friends, during this session. And, yeah, I think that's the the housekeeping item. I'm very interested in, so we'll we'll do a little bit of fundamentals. This is basically, yeah, groundworks to everything that's coming, how to do something with graph reg, how how to get, you know, a model working, get your data in, use your data, and make that available for AI systems and do some retrieval with it as well. So I think that's that's a super interesting session today, and I'm very much looking forward to it. And with that, yes, I think, handing it over to Martin. Thank you, Alex. Thank you very much. Yeah. So, welcome everybody to, Generative AI and Graph Foundations. So just before I kick off, just some things to let you know. If I look this way, I'm looking at questions. I'm not ignoring you. I just have two screens. So this, I'm presenting. This, I'm looking at questions. So, yeah, if I look to the side, that's what I'm doing. So let's just have a quick, sort of recap. Hi. I'm Martin. Thank you very much, Alex, for the introduction. You can find me virtually everywhere online, with at Martin Ohanlon. There are very few Martin Ohanlon's. Fortunately for me, it makes my usernames easy to remember. So what are we gonna do today? So we've got sort of, four steps that we're gonna do that we're gonna do to do to do to do today. Live, we will certainly do the first three. So we're gonna look at what is generative AI, so how Neo can... Neo four j can help you improve improve your generative AI applications. We're gonna look at knowledge graph construction, so how we can use generative AI to turn unstructured data into graphs. Then we will look at retrievers. So we will look to connect the knowledge graph that you're gonna create up to some generative AI retrievers. And then there's a final set... Module number four, which, you can take away and integrate the retrievers that you have created into an agent as well. So that's the kind of a rough recap of what we are gonna do today. In terms of how, so, obviously, I'm gonna present. I'm gonna go through all of the activities. However, you can follow along and I would encourage you to do so. We... To do that, we are gonna use Neo four j's GraphAcademy platform. You can find it at graphacademy.neo4j.com. Completely free. We will provide all of the resources for you to... In order to be able to follow along and, or, you know, continue with this workshop after the session as well. So GraphAcademy, what what we'll see is lessons. We'll see slides. We'll see interactive activities where you can run code and run cypher... Cypher Stamps and see the results as well. So I am gonna go straight into this. So this is what you are gonna need in order for... To be able to, complete this session. So, first of all, you are gonna need, the workshop materials. In order to get that, you're gonna need a GraphAcademy account. So I have created a document, which is this one here. And what I'm gonna do is I'm gonna put the link in the chat. And if you follow that link, you'll end up at this document. So the first thing you'll need to do is if you haven't, already used GraphAcademy before is go to GraphAcademy and sign up. I'm I'm already signed in, but, yeah, there will be an option to sign in sign in to GraphAcademy, and, you know, and you can use whatever, SSO that you like. But... Yeah. So you'll need to sign into GraphAcademy. The other thing that we're gonna need need is we're gonna use this link here. So this is where all the workshop shop content is. So this this workshop is not publicly listed on GraphAcademy, but if you if you have the link, obviously, you can go straight to it. So, yeah, Neo4j and Generative AI workshop. I'll give you a moment, but what I want you to do is once you have a GraphAcademy account and once you once you have, got yourself to the... To this link, just enroll. So we're gonna enroll, and then we'll talk a little bit about actually how how the GraphAcademy platform works. So I can see a lot of people are in the notes document, which is normally a pretty good sign that people are following are following the links as well. So what I will do throughout through... Throughout this session is there will be a period where, I talk, and there will be a period where, you do. Yeah. I talk. You do. I will take those opportunities when you are doing things to look at any questions. So if you do have questions, kinda put them in the q in the q and a. That would be super helpful. Alex has been helpful and has put the link in, into the, chat as well. Perfect. Okay. So this is, the kind of layout of GraphAcademy. So we have this little menu here. These will... These are the modules that we're gonna be going through today, which sort of is in the structure that I introduced earlier. And you will have access to all of the content that I present and all of the activities that I present. And they will look like this to you, like lesson 1.1, what is generative AI. I am going to use a slightly different view. I'm gonna use the slide view. I'm gonna use the slide view of this content because it's a little bit a little bit more comfortable for me to present in. But, again, it's exactly the same the the same content. Okay. So just to recap, plea... If you wanna follow along, please take a moment, enroll into GraphAcademy, follow the link, to get to the workshop content. But I'm just gonna spend a few minutes just talking I'm gonna spend, a few minutes just talking, about what is generative AI. I... And we we have had a... Somebody in the chat says the link doesn't work. I'm just making sure that it does. If anybody needs the workshop content, they will find it here. Okay. Right. So let's just spend a little bit of time talking about what is generative AI. I wanna spend, maybe only just ten minutes just making sure that we all know the same terms, and that we are talking about the same thing. So when I talk about generative AI, I talk about artificial intelligence systems that are designed to create new content that resembles human made data, whether that's text, images, audio, code, video, doesn't matter. But they essentially work in the same way. We take a lot of training data, we build a generative AI model, and that generates new content that is, statistically similar to, to the training data. Let's just make sure we understand. Gen AI models are not intelligent in the way that humans are. They don't understand or comprehend the the content that they generate. They are they rely on statistical patterns and correlations learned from their training data. Right? While they can produce coherent, contextually relevant output, they don't understand. Right? And that's important. So we are gonna be, focusing on, text generating Gen AI models, so large language models. Right? And they... Large language models, so when I say LLM, I'm talking about large language models, text generating gen a gen a gen a... Gen AI models, your GPTs, your Geminis, so on and so forth. And if we think about the way an LLM works, it is a probabilistic continuation of an... Of of the instruction it receives. So if we send it continue the sequence a b c, it could respond with d e f. And typically, to get an LLM to perform a task, we give it a prompt. We say, a prompt being what is an LLM? Give a response using simple language and we get a description back. So the description is the continuation of the prompt that we give it. And while they have huge amounts of potential, we should always be cautious. If you have used any AI model, you know this to be true. Right? And they're also entirely black boxes. They do not know or understand the information that they output. They simply predict the next word in the sequence. And one of the significant challenges to to that is access to data. The sources for this training data, often from the Internet, books, publicly available text, could be of questionable quality or even incorrect. And also note that that training happens at that point in time. It may not reflect the current state of the world or include any private information. So if we ask it things like, tell me about the latest features, the latest Neo four j features, If that is not incorporated into the L and M's training bay... Tech data, and no additional context is provided, here are some old features with incomplete information. It's based on the data that that it that it that it receives. We should always be entirely cognizant of hallucination as well. If we had trained an LLM sufficiently with enough information that said the moon is made of cheese, an LLM would confidently respond with that, with that description. So we can improve the accuracy of our LLMs by providing context, and that context could be, information that's relevant to the prompt, and it helps the model generate more accurate and relevant responses. So if we want to, summarize a company's results, if we provide a recent stock market report as a as a point of fact, the LLM will do a better job. We have provided context... Additional context that allows the LLM to do what it's good at, which is generating human like text. But we should also be aware of that cutoff. Right? And they don't have access to, real time and pro... And proprietary data. So if we provide not only a stock market report but also annual results, again, we can expect to get a better response. The additional context allows our LLM to create a better response for us. Supplying context to your prompts helps LLMs generate, more accurate, relevant, trustworthy responses by reducing hallucinations and compensating for access... For the lack of access access to data. That is what we are here to talk about today. It's how graphs and how Neo four j can help us provide context to an LLM that helps us produce a more accurate accurate response. So that's, sorry. I'll just go back. That is my five minute introduction to generative generative AI. Just wanna make sure that everybody, has the same terms in mind. So when we talk about LLMs, we're all talking about the same thing. When we're talking about hallucination, we're talking about the same thing. And when we're talking about context, we're all talking about the same thing. So that's generative AI. Let's spend a moment talking about GraphRag. So before we talk about Graph RAG, let's talk about RAG. So RAG or Retrieval Augmented Generation is an approach, which gives LLMs additional context that allows them to give better responses. And typically, the way a retrieval or better generation process works is this, is, a user prompts a question, a query, whatever, to an application. Now that application is going to use that question, use the prompts that the user gave it to find additional context by... Typically, by searching some sort... Some some some sort of data store, but it could be API calls as well, to retrieve some context. And that context enhances the prompts that we give to the LLM and we get a better output. That is the the top level of how the kind of... How the RAG process works. And, you know, RAG systems provide responses context contextually aware. You know, if you're building a, the example I've used here is a chatbot about the Olympics. What's the latest news on the on the Olympics? We can use real real time data through an API to provide better context to our LLM to get an up to date position on on, you know, on what's been happening in the Olympics in this in this use use case. So the key component or one of the kinda key components within this process is that retriever. Right? So the retriever's job sits in between the user and the LLM, and its job is to find the right context, the right amount of context, right, that is relevant to the question that gives the LLM sufficient information to be able to fill its gaps in training and also give it better information about what the user is looking to do. And, we are gonna explore various types of retrievers during this session. We're gonna look at full text full text search, vector search, and text decipher as well. So those data sources that we need, can vary widely, and it depends on the application, the type of information that that that they need. But it could be documents, APIs, knowledge graphs, so graph representations of information, that can provide context and relationships between entities. And if we extend, our news agency chatbot, we could have a knowledge graph that supplied that supplied additional context to our news chatbot, whether that was from articles, you know, existing, whether that's proprietary articles or public domain articles through news APIs, but we could use that additional information to provide a better set of context. So GraphRack retrieve... Graph Retrieval Augmented Generation, you know, is is an approach that uses the strengths of graph databases to provide relevant context to LLMs. The term Graph RAG was actually coined by Microsoft in a research paper they did in 2023. I may have got the year wrong. It might have been 2022. And, in sort of couple of years ago. And, essentially, what they... What their research paper said was if you supplement the information you get back from a vector based rag retriever with information you get from a knowledge graph, you get significantly better results. And what that what that research and further research passed and, implementation has shown since then is you get richer context, improved accuracy, better explainability, flexible queries, and enhanced reasoning. That is why GraphRack, is typically seen as a, as a good retrieval mechanism for generative AR. And the way it typically works like this is... So graph enhanced vector search. So this is a really common approach in graph... In GraphRag. And essentially what we do is we say, I have a a user who submits a prompt. We use that prompt, that question. Yeah. We create a vector and graph retrieve retriever. And the first thing we do is we go off, use that question to find similar nodes to that question, and then use the graph to find the related nodes about those similar nodes. And that allows us to find additional relevant context, and we then use that information to, create an enhanced prompt to send to to the LLM. And we are gonna build some of, of those retrievers today. We also can use things like tech... Full full text search. Very, very useful if you're looking for specific things. So for example, find all of the information about this company name or this person name or this very specific location. And we can also, use text text to cipher, cipher being the query language, of, of Neo four j. And this allows us to express questions and use the schema plus the question to convert that into a Cypher statement that we can run directly against our graph. Okay. So, I'm just taking a moment. I'm looking to my left, seeing, what... What's been going on. Okay. I'm gonna do the last bit of me talking. So let's just have a moment to to talk about what knowledge graphs are. So we've spoken, about knowledge graphs, introduce them as a store for generative AI applications, but what are they? So to give it a term, a definition, a knowledge graph is an organized representation of real world entities and their relationships. So knowledge graphs provide a structured way to represent entities, their attributes, and their relationships. Very useful for generative AI applications because structured but interconnected data that focuses on how things are related. Okay. So search engines have used knowledge graphs for a long time, typically to provide additional context and information about people, places, things, so on and so forth. So if we were to imagine a a knowledge graph about Neo four j, it would look a little bit like this. So the entities being Neo four j, a blog post, some documentation, a website, some locations, a technology. So Neo four j, the company, was founded in and is located in Malmo, which is a location... Is also located in London. London happens to be the capital city of, of, The United Kingdom. Now we can take this very flexible structure and use it to break down sources of information. So if we have multiple sources of information that are useful for our, our generative AI AI application by storming... Storing them in a graph, we can understand how they are related together, not just, how... Not just consider them to be individual sources. Knowledge graphs can store data and relationships along with the framework of of the organizing principles or the ontologies as well. So, if we imagine a GraphAcademy course, courses have lessons, so we have content that have types. So we have types of quizzes, challenges, videos, practical activities. Those types are all type... Are a type of content. But by using having that organizing... Those organizing principles alongside the data that represents it, we can say things like a course has a module, a module has a lesson. A lesson is a type of, is a type of content. So again, it allows us to store not only our domain information, our data, but also the principles that organize it. So in Gen AI, knowledge graphs play a crucial role because they capture and organize that domain specific information and that proprietary information. And they aren't they aren't limited to to sit to strictly structured or unstructured data. We can use them to represent both models. Okay. Right. I think that is, so, the last of me talking for a little bit and what we, and what we're gonna do is we're gonna kind of do do some practical act... Some practical activity. So at... So throughout this workshop, I'm gonna be using, the Neo four j's graph reg for Python package. And I have created a and I have created a repository for us to use which has got starter code in it, data that we need, so on and so forth. So the first thing I'm gonna do is I'm actually gonna set up set up that repository. And again, if you're following along, you can do the same. So just to kinda recap where we are. So if you're following following along, we are in this first module generative AI, and we're just moving into this last lesson here, which is the, GraphRag for Python package. Okay. So, GraphRag for Python package allows you to access Neo4j's Gen AI functions, including retrievers, Graphite pipelines, and knowledge graph construction, and we are gonna use it to to do all of those things. So to do that, we need to set ourselves up a dev environment. So I'm gonna go through that process as well. Oh, and I didn't mean to press escape. What I wanted to do was, I was just gonna go back to the lesson view. This is this is the lesson view, and let's just have a quick look at how we set that up. So, again, if you wanna continue, there is, this... There is the workshop gen AI repository that we have created. It's Neo4j Graph Academy. It's here. Now you can use a GitHub, Codespaces as an online IDE and workspace for this for this course. It'll automatically clone that repository and set up the environment. I I I would... If you have access to GitHub, I would encourage you to do that. If you don't, you can develop on your local machine as well. I'm gonna use the Codespaces, because it makes things a little bit easier. If you've not used Codespaces before, essentially, a a development container and an IDE in the cloud, hosted by GitHub. So I've... And we'll wait a little bit bigger. So I have just opened up that Codespace, and it will take... What it's gonna do is it's gonna create that development container for me. It's gonna clone the repository, and it's gonna install the requirements that we that we need as well. So I'm just gonna let that takes maybe a minute or so to run, just to kind of set up set set up the container and get started. But while we are doing that, I will have a a quick look and see see if we have any questions. But just to kinda note, if you want to, once you've created your dev environment, you can go through to set it to set up your your environment, and there are database credentials for you here. These credentials are specific to you. Please don't copy mine, right, because they're specific to me. You... When you, log in and use GraphAcademy, you will see database database credentials for you here. You are also going to need an OpenAI API key, which you will find in the notes document that I just shared. Alright. So let's just have a quick look, see if we have any, sort of messages and questions. There is one, from. Miguel. Miguel says, is a knowledge graph static like a camera snapshot in time? Oh, I mean, Miguel, that is a... That is an excellent question. I mean, the the simple answer is no. So, knowledge graphs, most of the benefit comes from from the fact that they are not static and and they are adaptable and updated typically in real time. But, yes, no, no. Absolutely not. Don't represent a snapshot in time. So, that was that was good that that was good timing. So, I've just gone back to... So I'm just back in my GitHub code space. So it's cloned the repository, and has, hopefully set everything up for me. So there are some instructions here, but, again, there's some instructions on the lesson as well. What we need to do is we need to create a new file called dot e m v. This has got all our environment variables in it, essentially, our connection to our Neo four j database, and our, OpenAI API key. So I have just created a dot m v file. I'm gonna go... I can copy the contents, from GraphAcademy. Again, this is my instance. It's one that was created for me, when, I enrolled on the course in Graph Academy. I'm also gonna need this OpenAI API key. And just to kinda note, I have put an OpenAI API key for you to use during this session into that notes document. So I can pop that in there. So, there we go. I have, that is my environment, my my environment variable setup. Again, if I go back to the testing your setup. So there is a program called test environment dot p y, and what that's gonna do is is essentially test them to make sure that your environment file is set up correctly and you can connect to a database. So I have a... So so if I run my, test environment, hopefully, there we go. I get back, run five tests in three point one five seconds. Everything was fine, which is great, which which was what we were looking for. And that's, essentially the hard bit done. Right? We have cloned our repository. We have set up our environment to be able to talk to our Neo four j database and, copied in our OpenAI API key. So we've had a question. Where can I find the find the instruction documents? So it's... It is... It's in the chat, but I'll pop it... I'll pop the link in there again. So... Oh, I'll pop the link in there again. Ah, Alex has already done it. Thank you very much. Yeah. So that's where you'll find, the OpenAI API key. And you'll also find a link to this workshop that we are currently going through at the moment, and we are in this lesson here. Okay. So while you're doing that, I'm gonna move on to, to the next lesson. So this is just... What we're gonna do is we're gonna talk about knowledge graph construction and then we then we are gonna spend probably the next sort of half an hour, twenty minutes actually building a knowledge graph from unstructured data using our Python, our Neo four j Python. Neo four... Graph rag for Python package. Alright. So if you're following along, we're just on this... We've just moved into the second module, broad constructing knowledge graphs. Again, I'm gonna use the slide view, so I get... But you you can follow along in the lesson content. Okay. So we are going to, throughout this lesson, look at how we can construct a knowledge graph, how we can create that collection of entities and relationships from unstructured data. So when we construct a Knowledge Graph from unstructured data, we are gonna do five steps. We're gonna gather our data. We're gonna, build it all together. We're gonna chunk it up. So we're gonna split it into bits. We're then gonna vectorize it. So we're gonna use an embedding model to create a vector of, of those chunks. We will then pass that data to an LLM to extract the nodes and relationships, from that text, and we will then use that output to generate a graph. So that's our kind of five step five step process. So if we look at an example. If I wanted to construct a Knowledge Graph based on the Neo4j Wikipedia page. First of all, I would collect that data. I would gather the text from that data. So Neo four j's graph data... Database management system. I would split that text into smaller chunks. Right? And the size of the chunks is something we will discuss we will discuss later, but I'll split it into bits. I will then create embeddings and vectors for each for each one of those chunks. I will then use that text. Sorry. I will then pass that text to an LLM. I will give it a task. You will task to identify the entities and relationships. The real prompt is significantly more complicated than this, but this is the essence. We will pass it to an LLM with a prompt to say extract the entities and relationships. It will return some JSON. From that JSON, we will create, a Cypher a Cypher query to create create our graph. So if we think about all that what that text said, you know, Neo four j is a graph database that is developed by Neo four j Incorporated, the company, and is implemented in Java, which is a programming language. So that's how we can generate a graph, from from our text. Perfect. So that's the process that we are gonna go through. So, first, the first thing we are we are gonna do is we're gonna use that, we're gonna use the neo4j graph reg package, to look and say, how can we extract data, or sorry, extract a graph schema from some text. Alright. So, again, if you're if you're following along, we are in this lesson here. Second lesson, extracting a schema from text. Again, all of the information is is here for you to follow along. You don't have to follow follow along, with me. But what we're gonna do is we're gonna open up this code here, this extract schema py file, and it's gonna use this class to extract some schema from a a given text input. Alright. So let's, let's find that program. So so if we look in here, workshop gen AI, here is our extract schema program. So we can see, if we look through the code, we are creating a schema from Text Extractor class. We're passing in an OpenAI LLM model, which happens to be GPT 5.2. We are defining some text, so Neo4j is graph databases management system, and then we are look... Using that schema extractor to extract a schema. And we can just run this. Right? Let's run it. What it's gonna do is it's gonna go through that process of using a prompt, using an LLM to extract, a a graph schema from a piece of text. And if so, let's just have a quick look at what it's returned. So the first... So we can see it's returned a Graph Schema. First thing it's returned is a node with a type of, graph database management system. It's given it a name. It's come up with another, node type, which is a company. And it's also created some relationship types, developed by... And a pattern that says the graph database management system node is developed by a company. And it's also created some constraints, around that. But we can see how we... What we have done is we have taken a piece of text and we have generated a graph schema from it. And, if you're running this, there are some there are some other examples that you can that you can have a look through. So I'll I'll just run another one. The awful tower is a wrought iron lattice tower from Paris. So I can just swap that text for any text, that I want. And if I run it, what I should get back is a graph schema that I could use to store the information within this text. Yeah. So tower located in location, location, located in location. So Eiffel Tower located, on the Champs De Mars in, in Paris in France. Yeah. So, again, take a moment. I'll just have a quick look and see if there are if if there are any questions. But please, you know, put your own own text in here. Have a go. So there's a a few questions that come through. Can the graph be built on a user defined JSON schema containing the constraints? Yes, it can. We will look at how we define a schema later, in this workshop. Since the end, won't they be random as in two extractions because it's resulting in two sets of relationships? There is a... There is an element of nondeterminism about this. Absolutely. You're right, Deepak. Sorry. Deepak. We will we will discuss some of that. But, yes, but what... We will look at ways that we can, create a more consistent, a a more consistent model as well. Is there a way to extract entities and relationship from data structure without doing, embeddings and LLMs? I mean, certainly, you do not have to do embeddings and create vectors, and you don't have to use LLMs either. You could use other entity and relationship extraction tools. The one that instantly comes to mind is is spaCy, but, obviously, you know, the tool or the model, whichever one you use, will, impact your your your your performance as well. Okay. Perfect. And the rest of the ones... Look. They've all been answered. Thank you very much. Perfect. Okay. So hopefully you've had some time to experiment with that. What we are gonna do now is we are gonna go through that process and actually create a graph, and we're gonna do this iteratively. So what we're gonna do is we're gonna learn how to create a knowledge graph from unstructured from some unstructured data, and we're gonna use, component... A class called the simple KG pipeline. Simple knowledge graph pipeline. Simple suggests it can only do simple things. That's not true. It can do very complex things. Simple represents, the fact that, it has a simple API that you that that that you can use rather than it being, an an... A not advanced tool. But, essentially, what it does is it... Is the is the the KG pipeline, runs through those five steps that we talked about earlier. Loading text, splitting it, creating embeddings, extracting entities from chunks, chunks, and then finally writing the data to a Neo four j database as well. And the pipeline looks like this. Interestingly, you can use any of these components within your own pipe pipeline. The simple KG pipeline simply links them all together. So this is what we are gonna do is we are gonna take some unstructured data. Now the unstructured data that we are gonna take is actually a course from GraphAcademy, and it's actually... And and it's the Neo four j and generative AI fundamentals course. So we're gonna take that unstructured data, that raw text. We're gonna use a simple KG pipeline, and we're gonna create a graph from that information. So your task is... There is a starter program called KG Builder, that's in that repo. I'd like to have a look at it, review the code and how to create that KG pipeline, run the pipeline, and and look at actually how does it create that graph from that PDF document, and then have a look at the graph itself. So just to kind of recap, we are here. We're in, module two, knowledge graph construction. We're on this lesson here, create a graph. So what I am gonna do is I'm gonna do the same process. I... I'm gonna run through, run through the same the same process. But if I have a quick look at the lesson view, this is the lesson view that you guys are seeing. It's exact... I'm I'm gonna run through the same the same process and then take a moment to kind of, answer any questions as well. But you have all all of the information in front of you. Please, you know, take a moment to have a go. That's that's really why, why we are here. So... But before I look at any questions, let's just... I'm gonna go through the same process. So here is my program, and the KG builder needs a few things. The first thing it needs is a connection to our Neo4j database, and we're using the graph database driver to do that with the information that you put in your environment file. We're gonna need an LLM. In this case, we're gonna use a GPT five Nano, a relatively small model. And we're also gonna we're also gonna need an embedding model, and we're gonna use OpenAI's Text Embedding three Small model. And when we create our knowledge graph our knowledge graph pipeline, we are gonna give it an LLM. We're gonna give it a connection to our database. Sorry, a connection to our Neo4j instance. We're gonna tell it which database we want to use. We're gonna give it an embedder, our our embedded model. And then finally, we're gonna set a from file. So we're gonna create our Knowledge Graph from files. And then finally, what we do is we're gonna run our KG builder based on this file path. So this is a PDF file that represents the course content that we are... That is... That's our unstructured data. And we can have a look at it. If we go into this data folder here, we have a quick look at this. Oh, I don't have the PDF extension installed for some reason. But, yeah, you should be able to be able to have a look at that at that PDF document. What you'll see is a, the text that represents the course. And in fact, I have a copy on my local machine. So data... If I open up that PDF document, this is what it looks like. Yeah. So you can see it's the... It's all of the course content. I'm just gonna run it. So when I run it, it's gonna go through that process that go through those five steps for those five steps, but... And split up that data. Okay. Let's have a quick look at, questions while I'm let... While I'm letting that that run. So we've had a few questions. It looks like some of them have been, have been answered. There was a great one. You know, what about large doc... Documents? Would it work for large documents? As we've responded, yeah. We'll we'll chunk them up. And we are gonna do that actually in the in the very next next lesson. We're gonna look to chunk up chunk chunk up the documents. But, sort of slightly wider question, what would be the right approach to create a date... Domain specific graph, like ontologies for customer data data sources. I think you've used the right words. I would look to create my data model first, understand what it is that I'm... That I I am looking to model. And when I do my data extraction, it could be for very large numbers of documents. We'd use my on top... My ontology to constrain my, entity and relationship extraction. And, again, we will look at how we do that, a little bit further on. But as we can see, the process has run. It's a relatively small sub... Subset at the moment, and, but we should be able to see the data that has been created. So there is some... So we can explore the knowledge graphs. We can see that if I run this, we can see that, I have indeed extracted data from one document, and I've created one chunk. I'm hoping that there are... Yeah. There is at least, there is at least some entities. So, taking the chunk text, with no other input in terms of, like, what what it should be looking for, it has come out and said, okay. Well, there is a... There is an entity that represents generative AI, and there's also a representative... An entity that represents large language models. And it has created a relationship that says generative AI uses model large language models. What we will see is as we as as we start to refine this process, we will get more information out and more sophisticated graph as well. But, yeah, hopefully, hopefully you've seen similar results. So I see I see questions flick through and then immediately answers appear, which which is great. But there's an interesting question. Why is it... Why is embedding required, if it's stored in plain text with a relationship? We can use embeddings or not. The advantage of using embeddings, as we will see later when we build some retrievers, is that we can use a vector search and use a vector index over those embeddings to find semantically similar information. Okay. Alright. So, that's great. I'm in a good state. I have managed to, take a document and I've managed to create a graph from it. What we are gonna move on to now, so the next lesson is all about chunking. So... Oh, Chunking. Right. So the graph that I've just created, you you may have noticed, but there was one document and there was one chunk. So the default chunk size is actually quite large. And the larger the chunk sizes... The chunk size, the more context it has, but you can often end up with less granular data. So what we are gonna do is we're gonna modify, that simple k g pipeline to use a different chunk size. So your task, delete the existing graph. Well, we're gonna recreate it anyway, and we're gonna create a fixed size splitter. So what we're gonna do is we're gonna create, a splitter that splits, each of the documents into fixed size fixed size chunks. We're gonna then modify the KG pipeline to, to use that text splitter. We're gonna run it, and then we're gonna see see the result that it has. Again, I am going to go through the same process. So, I I will go through the same process as well, and model what we do. But again, all of the information is in front of you. We are in this lesson here, chunk size. Okay. So first off, let's delete our graph. We don't need we don't need need what's in it. It's relatively small. We're gonna recreate it in it anyway. So what we're gonna do is we're gonna modify that program, that kg builder program to create this, this text splitter. Let's say, I'm just gonna copy that text. I'm gonna put it... I'm gonna put it here. So if we look at the text splitter, basically what I'm saying is I want every chunk to be 500 characters, roughly. There is some spacing and thing... Things like that it takes into account. But the other thing it says is I want the chunk overlap to be a 100 characters. So each of my chunks, will have an overlap with the previous one, and that helps when we're doing things like semantic search. And then finally, what I need to do is I need to modify my k g... My, sorry, my simple k g pipeline to include this text splitter. So I've created my text splitter, I've configured it, and I need to modify my pipeline to use it. And once I've done that, I can run it. So it's gonna go through exactly the same process with the same data. All I've done is changed, how I have, chunks up that document. So we'll go through the same the same process. And one of the interesting things that we find is just by chunking the document, I get significantly more entities out. So if we look at the resolver, basically what it said is the number of, nodes that it found was 18. The number of nodes it created was 16, suggesting there were some there was there was some duplicates in there. So just by changing the the chunk size can have a significant impact into what our code looks like. So if we run it... Run some Cypher... So let's just have a look at that... Look at those, those doc... Those documents and those chunks. So we can see that there is... This is the document that we're importing, and we can now see that there are, well, nine chunks. And we can see that each chunk is about 500 characters. And we can use those chunks to, to say, okay. Well, what entities were ex... Were extracted from from those chunks? Let's have a look at our knowledge graph. And, actually, we can immediately see that we have a more sophisticated graph. So, again, this is based on, you know, the only... We haven't given the, pipeline any information. We basically just said find what you can find. So from chunk chunk one and chunk two, both relate to, gen... Generative AI, which describes one of... Which is a which large language models is is is one of. We can see that chunk, chunk one and chunk seven relate to this entity, and chunk eight relates to LLMs. And LLMs hallucination describes one of LLMs. So we can start to see that just by changing our chunk size means that we can create a more sophisticated graph. Okay. Let's just have a quick look at some some of the questions I've had. Could you please explain, the Cypher text group? I mean, sure. If you're not familiar familiar with Cypher, we can spend a moment. So this, is, is a, what's called a... Is a match clause, match finds patterns in data. And this is the pattern that we are with... That we want to find. We essentially want to find find me any document that has a relationship to a chunk to to a chunk. And then we are returning properties from the nodes that we are returning. So in this case we are returning the document path, the chunk index, the chunk text, and the size of the chunk sets, I. E. Chunk text, I. E. How much, how many characters does it does it does it contain, and then we are ordering it. This pattern, slight... Slightly more complicated. Essentially, what we're doing is we're finding a path. We're finding a path through data. And it starts with chunk nodes, and it says find all of the entities, and then find one or more connections between, the entities that are chunked... That that that the chunks are related to to any other entity. Yeah. And then return that whole path. If you are interested and want to know more about kind of, like, how to write SOFA, there are some great courses on GraphAcademy, particularly, Cypher Fundamentals as well. Okay. So I'm gonna move on. Before we do, there's a there's a there's a great, I think it's less of a question, more of a more of a statement, which is we may lose context if we extract, chunk based on size. Absolutely. Yeah. Absolutely. This is... Chunking by size is probably the most simplistic, way way of chunking. There are huge numbers of chunking strategies by section, by relevance even. And interestingly, that's one of the reasons, to say we may lose context if we extract chunks based on size. That's one of the reasons we do overlap, is so is so that we maintain some of the context between the chunks as well. Okay. Right. So, our next section is we're gonna move on to defining a schema. So... Oh, define a schema. So the knowledge graph that we have created is unconstrained, and it means that any entity or relationship, right, that can be created based on... That can be created has been sort of extracted from the text. Now that's not strictly true. But essentially, what we have done is we we have, we have given we have given the LLM, the the decision making in terms of what it thinks is important. And almost always, that is not the right thing to do. Right? Because the LLM doesn't have sufficient information to understand, well, what is important to us in that data. So what we are gonna do is we're gonna modify that simple KG pipeline to use a custom schema. Now providing this schema allows us to create a much more structured and meaningful knowledge graph, one that makes sense for our use case. So the pipeline instructs the LLM to only identify and specify those types of nodes and relationships. And we can define, a schema in in three ways. We can define nodes that it should look for, person, organization, location, for for example. Account, transaction, product. Relationships, so works at, located in, part of, and we can also express pattern. Person, works at, organization. Organization located in, location. And we don't have to express all of those things either. We could just express, the node entity types we were looking we were looking for and let the LLM try try and find whatever relationships it could. And, you know, this is typically a, a a design choice that we make to start with and then iterate on. So, again, your task, is I want you to, there is a a program called kg builder schema. I want you to have a look at it. I want you to review, the code, and I want you to define a set of nodes and relationships, that are relevant to the data. Right? Then I'd like you to modify that pipeline to use that defined schema, run it, iterate it, and then, you know, review the graph and observe how the schema has... How the schema has influenced the structure of the graph. Again, the instructions are in the lesson. I am gonna run through them as well, but I'll give you all the opportunity, to have a go as well. So, again, we're on this lesson, lesson 2.5, define a schema. We're just here. Yeah. And here is the start. So this is the program we're gonna open, k g build a schema. Yeah. I'll do that. So let's just have a quick a quick look at this code. So, again, we're using a simple k g... KG builder. So we've got the things that we've we've seen before, driver connection, LLM, and embedder, a text splitter. What we can also do is express some node types. So we can express some node types, so technology, concepts, example, processes. And this will instruct, well, my code space just stopped for some reason. Please excuse me. Give me one second. Like I say, all the information is, sort of here in front of you, and there are instructions underneath. I'm just gonna, just reopen my code space. I don't know why GitHub would take that moment to close it for me, but it felt that was important. I am going to revert to my local backup. So, yeah, I'm... Apologies. I'm not sure what's happened to my code space, but, Seems. that couple of other people at the same Oh, really? Maybe it's a GitHub issue then. RISE just says he's back on. Oh, okay. He's, been try. stopped. Deepgram says they added a limit to the free tier, but I think we are Oh, way. under. So the... Yeah. So the the the free the free tier is, like, a hundred and twenty hours. Yeah. Oh, Oh, it's. back. Okay. It seems. to be back. Yeah. Deep Twist is back too. So it seemed like, an issue. Okay. If yours is still down, give it a give it a second and, keep keep refreshing, and then it should be back up. Yeah. Very interesting. I mean, apologies. I mean, if if multiple people experience the same issue, Phil, it feels like a GitHub problem, doesn't it? But... Okay. Alright. Looks like we are back online. Yeah. So I've defined my schema, which is this collect... Collection of node types. And if I was to run it, the the, builder would look to... The KG pipeline would look specifically only for those node node types. I haven't expressed anything else. I haven't expressed any relationships or any patterns. So, the, the quote... The prompt would instruct the LLM to find relationships between between those those those different node types. Then more often than not, actually, what you are looking to do is to express, more than just simple node types. For example, we might want to also give a description. Yeah. So a benefit. Sorry, a label and and actually provide a label. So a label is a related learn... Oh, sorry. A resource is a a learning resource such as a book article. Right? And we can also express properties. So we can start to be more sophisticated with actually the information that we want to get out of, our our LLM. What I am gonna do is, is I'm gonna set, set up my builder to look specifically for these node types. So I want... So I... I want these node types. I want these relationships. So what I'm looking for is things that are related to... Technologies that are related to each other, so on and so forth, and then these patterns. Now this is a learning... This, you know, the... Our source data is a learning resource. It's part of a lesson. So I'm interested in things like how technology is related to each other, how concepts are related to technology, what examples exist and how they're used as part of those technologies, so so on and so forth. So once I've set my node types, my relationship types, and my patterns, I can pass those to my KG builder as part of that schema. And I can run it. Right? I should absolutely delete my existing graph just to kinda make sure that, I get a clean run. And once I run my builder with our enhanced or our sort of defined schema, we can take... Start to have a look at, the entities and relationships that it has extracted. Okay. Perfect. Right. So, we can see that it's extracted. That it's found 17 and, created 16. So based on the scheme the scheme that I have defined, again, if I start to look at how entities are related to... Entities are related to chunks, what I can now see is a much more, is a much more defined graph. So I have concepts concepts of generative AI models is related to the technology of general. Absolutely. Yes. I have a chunk. I have a resource of an image. Yeah. Sites, generative AI. I have, process of model training, is part of generative... Part of the techno... Technology of generative AI. I have a technology of LLM limitations is... Has the challenge of hallucination. Right. So I can start to see... So I I can see how how even with a single document, how my graph has been has been influenced or been constrained by the nodes, the relationships, and, and the patterns that I gave it. And this is how we can create a graph that is, relevant to our use case based on, you know, unstructured data which is, which is difficult to which is difficult to analyze. So I will just take, again, just take a moment here just to kinda go over over any questions. So so do these notes and relationships terms depend on the input text? That is to say how LLM interpret... Interprets technology as I wish. You are right to, to to question that. The the really important point to kinda note is if you have a specific term, yeah, is as I have done with, the, no type of resource is to provide a description about what it is that you want... To provide a description of your definition of that of that, that entity, and to provide properties also stays the LLM, to understanding actually what is the node type, the entity that you are looking for. Okay. Just got a few more other questions. Is the schema used to filter only relevant data? A great answer. Partly, a schema does more than filtering. It defines what kinds of entities and relationships the graph should contain and gives, extraction a consistent structure. I can't answer that answer. That's brilliant. Yeah. Okay. Right. So, we are here. We have defined a schema. So... And and and we've looked at the path. If I look at the, the counts, I can see that actually one one document, nine nine chunks, 16 entities. Right? So I'm starting to kind of, like, build build up build up my graph. Okay. So we are gonna move on to, adding some structured data to our knowledge graph and actually building the whole graph from, from the con... From the complete dataset. So far that we have we have just been using, that single, that sync that single document. So add structured data to the knowledge graph. Right. So the knowledge graph that we created is solely based on unstructured data. In the real world, you will have structured data sources as well. And by combining our structured and unstructured data, we can enhance our Knowledge Graph. Right? So we are gonna look at a relatively simple implementation of this. So the repository, there's a c... There's a CSV file in there which contains metadata about the lessons the documents were created from. And if we look at that doc, you'll see that there's a file name, what course it's from, what module it's from, what lesson it's from, and what URL it has as well. And we can use this CSV file as input and a structured data source. So what we're gonna do is we're gonna create a knowledge graph that combines our structured data and our unstructured data. And it's gonna look a little bit like this. So we will have a document. A document is a PDF of this lesson. This lesson is where... This lesson node is where we are gonna store our structured data. And then we have, obviously, entities related to chunks that are from documents. I'm gonna look at how we can use that structured data. Okay. So, again, task is, there is some some starter code called k... KG StructureBuilder. I'd like to review that code, and run the program, then we're gonna create that whole graph. And we're gonna look at that and we're gonna look at that graph and and see how the structured data gives gives us new new ways of querying it as well. So again, you can continue with the lesson to load the structured data. We are on this listen... This lesson here, add structured data to the knowledge graph. I'm gonna go through the same process, to demo but we are here, load structured data. So let's start with this KG, KG Structured Builder program. So if we're here, KG structured builder. And again, you know, hopefully this code is start... Is starting to look familiar. We have our connection to our database. We have our LLM. We have our embeddings. From, from the last, lesson, we have our schema. I haven't changed this. It's the same schema that we had, that we were using before. We have our KG builder. But the significant difference of what we're gonna do here now is we are gonna load some data from the CSV file. And if we have a look at the CSV file, we can see that it has a document path, the course name, the module name, the lesson name, and also a URL to where that less... To where that lesson exists. This is our Cypher statement. So this is the Cypher statement that's gonna create our structured data and also link it to, our documents to our unstructured data. So it's gonna match the document. It's gonna create a lesson node, and it's gonna set set set some properties on that lesson node, and then finally create a relationship between the document and the lesson that it's that it's that it's just created. Now this is gonna run for every single document. Right? So for every document in that CSV file, it's gonna run through that c... That's that CSV file. It is gonna use the k g... KG Builder that we've used before, to create the unstructured data. So it's gonna create the unstructured data first from that PDF document, and then it's gonna create the the structured graph. So it's gonna use that Cypher statement. It's gonna run that Cypher statement, to, link up our unstructured data to our structured data. So this this is what the program does. I'm gonna give it I'm gonna give it a run, and then we should, and and then we'll take some time to observe the results as well. Okay. So it's gonna go through that process. Process each document one at one at one at a time. It's gonna give us give us some results back. And then again, it's gonna... Every time it process... Processes that document, it's gonna add that that structured data as well. Okay. I'm gonna leave that run... That that to run, and then we will spend a little bit of time just having a look to see if there are any questions. Alex, I've not been keeping up. Is there any questions that we think we need to... Let me... Did you? No worries. And here's a hat. What would you like to know? Well, different, Alec. I, you've you've been keeping up very well. Oh, There. are no questions at the moment that we haven't answered yet or Vasu, that's brilliant. answered. Yeah. So... Okay. Amazing. Great. I mean, if the... If you have questions, thoughts, you know, yeah, please share. them. Exactly. I would I would, yeah. I'd be great to spend to spend some time talking about, you know, specifics... Specific use cases, things that you've ex... You've explored potentially, so so on and so forth. Anyway, we can see that, this has been sort of tricking along in the background. We are... I think we are nearly at the point where it is... We may have finished. I've seen the odd error which suggests, LLM may have returned either no information or, malformed information. But it looks like the majority has been successful and, yeah. Well, certainly, we have created 235 notes within within our graph. So, yeah, I've gone through this whole the whole process. I've, added structured data to, to my graph, and generated the whole graph from all of the documents that were, that represent that course. So let's just have a quick look at that graph. Alright. So the first thing we can do is is, let's try and find all of the lessons that cover a specific technology or concept. So what we're gonna do is we are gonna try and find, any technology node, so any technology entity that contains the name knowledge graph. Right? So find any in... Find any technology that contains the name the the name knowledge graph. And then what we do then is we use those knowledge graph nodes to find the chunks, to find the documents, to find the lessons. Right? So what we should get back is lessons that relate to knowledge graphs. Okay. So, we use the entity to find entities that related to knowledge graph. So we've got knowledge graph extraction, knowledge graph extraction tool, knowledge graph, and knowledge and knowledge and knowledge graph. There are some thirteen thirteen records. And because we've added our unstructured data, we can also see that actually there are the lessons that, this relates to. So let's have a look at, for example, this lesson here. So if I go to this URL, this is a lesson on graph rag, and I'm guessing somewhere in here, the term knowledge graph is defined. Yeah. It's just not showing. It's... Well, it is. There's there's three results in there somewhere. But... Yes. So, so what we can do is we can see, by using our unstructured data to find particular terms, we can find the lessons that relate to that. We can also use our knowledge graph to summarize, the content of each lesson. So for example, if I find every lesson, find every document, find every chunk, right, and let's say I'm interested in what technologies and what concepts are covered by each lesson. So I can run a query. I haven't deduplicated it so we've got multiple rows. But essentially what we can see is, this lesson here covers, generative AI and generative AI AI models. Let's find one. Maybe, this one here, vector index, covers knowledge graph extraction tools and entities and relationships. So we can start to understand actually what do... What does our unstructured doc, our unstructured data contain? I am just going to just to... What I would quite like to see is what our entity graph looks like now. So let's just... I thought this would... What what I'm doing is I'm just go... I'm just going going off to find the the cipher statement. It would have been... I I thought that would be quicker than rewriting it, but we'll have a look. So actually, now we can see that our graph is significantly more complicated. Right? So our our blue nodes represent represent our chunks. Our sort of pale orange y color represent our entities. And we can start to see that there is a huge amount of crossover between our chunks and our entities, meaning that we have found, so for example, this is a resource that that is the Neo four j documentation, and we can see that it relates to many different chunks within different documents. And also, the resource Neo four j documentation cites the technology Neo four j. Now this sort of super node right right in the middle is Neo four j. It is, of course, about Neo four j. We would expect, we we we would expect our graph to show significant correlation or connections to the technology Neo four j. Let's try and find another node that looks sort of interesting. Oh, what's this? What's here? What's here? I'm guessing graph rag. Right. There is a technology graph rag, and, again, we can see how that tech... How that tech... How that technology is related to other, other benefits, resources, processes, another resource, concepts, technology, so on and so forth. And we can also see how the, the the schema that we defined has heavily influenced, in fact has constrained, the graph that has been created making it more useful for us. Okay. I want just a quick, nod just to have a quick look to see if there are any questions. Right. Have... Great question. How can we graph a query using vector search not text not text filter? You are in for a treat. The very next lesson that we are gonna go to is on vector rag, and we are gonna do exactly that. Okay. Let me find where we are. So what we're gonna do is we're gonna move on to module three three, which is, looking at looking at retrieval. There is a, an optional lesson here which has got some instructions on creating your own knowledge graph from your own data so data data sources. I would encourage you to do that. It's a really great exercise, you know, even if you do it for only maybe only one or two documents that you have. Look at them, define a schema, extract the data, and review the graph and see how you can use that graph. But we're gonna move on to retrieval. Okay. So module that we're gonna look at now, we're gonna look at how we can use retrieval techno... Techniques to find relevant information from our knowledge graph. We're gonna look at vector, vector and cipher, and text decipher retrievers retrievers. Alright. So first one is let's have a look at vector rag. Again, I'm gonna use a slide view. So we current... We previously looked at RAG and the role of retrievers in finding relevant information. One of the challenges is trying to understand what the user is asking for and then finding that correct information. So what we're gonna do is we're gonna learn about semantic search and how we can use vector indexes to help us do that. So semantic search looks to find, results that are related to the intent of the question or the intent of the search term rather than, like, individual keywords. Right? So the example that I use quite a lot is if you input apple, you might predominantly get results about fruit. However, in a semantic search, semantic search, we are trying to gauge the intent, you know. Actually, are they searching about the fruit or are they searching about the tech company or is it something completely completely different? And the way, semantic search works is that it uses vectors. Right? So we used an embedding model to convert, our text into vectors. And we we turn our embedded model into that into that list of into that list of numbers. And we can use those numbers to understand how, how close a term is, sorry, how close two terms are to each to each other in terms of meaning. And the way that we do that is by looking at those vectors and measuring the distance between them. So you can use the distance or the angle between the vectors to gauge how semantically similar they are. So we went through that process of turning those chunks into vectors by using that embedding model. If we compare the vectors between two separate things, we can determine how similar they how similar they are. And, you know, we've seen this process. Typically, when you're using that vector based search, we take our source data, we push it through an embedding model, we create that embedding, we put it in a vector index, and we use a vector index to search it. And that's the kind of RAG process that we saw earlier and we are gonna use that, to to to to... You're gonna use that vector search to find semantically similar information in our knowledge graph that we created. Alright. So we're gonna create a vector plus cipher retriever. So the chunks in the knowledge graph include vector embeddings. Oops. I pressed the wrong button. And, you know, if we have a look at one of them... Chunk, return c. So if I have a look at one of these chunks, we can look at the properties in it and we can see it's got some text. And we can also see it's got an embedding, which is a big list of numbers. And the simple kg builder created those embeddings for us. Right? And we're gonna create a retriever that can use both the both the unstructured and that structured data to provide additional context. So to use these embeddings, we need to create a vector index. So that's what we're gonna do is we're gonna create a vector index, on the chunk node, on the embedding property within that chunk node. We need to tell it how many dimensions it's got. It's got 1,536. That happens to be what is returned by the text embedding three small model that we used, and we're gonna use cosine vector similarity. But we need to create a vector index and we can just run... We can run that in order to be able to search to to search that vector index. So what we're gonna do is we are gonna use that vector index to search all of our chunks for a term. Right? Now in order to search for a term, we need to turn out... We we need to convert our term into an embedding. So the term that we're gonna search for is retrieval augmented generation, and we are gonna use this function here, AI text embed, and we're gonna turn that into an embedding using the same text embedding three model. And then we are gonna then search our chunks for our user embedding and get the the the top five back. Okay. Now to run this, we need to give it an OpenAI API key. We need to turn this into a text embedding and we need to give it an OpenAI key. We can use the same OpenAI key that we have been using for our Python program. So slot that in there. And if I run this code, what I'm expected to see is chunks being returned. So the text of the chunk and its score, how how similar it is to retrieval augmented generation. So, the... Okay. Right. So, we have... We've got five five records back. We've got five chunks, chunks, chunks returned. The first one returned is a section on the retrieval augmented generation process. Right? Makes sense. Right? You know, we've asked to talk about RAG. The second one we got back is a lesson on what is RAG? RAG being retrieval augmented generation. The third one we get back is one on graph RAG. Yeah. Again, related to retrieval augmented generation but not the same as... What else do we get back? The fourth row is, some information that relates to the RAG process. Again, interestingly, this this fourth chunk doesn't include the term retrieval augmented generation, but it is about the RAG process, so we so so we get that chunk back. And we can, you know, change this term to be, you know, other things that are related to a Neo four j Cypher. We can get other other, chunk chunk chunks back. The score that we get back is a score between one and zero, one being exactly the same, zero being entirely unrelated, although you would never get a zero back. Okay. So, that's how we could use a vector index to search for semantically similar information. Alright. Now the exercise that I've got for you is, so the graph reg package includes, retrievers retriever classes that we can use. So your task, is to look and create a vector cipher retriever. So what this retriever's gonna do is it's gonna use your... Use the graph that you have created and find semantically similar information to the data that you pass to it. Right? So let's spend a little bit of time going through that. Again, I'm gonna do the same thing just to kinda point out where we are. We are in this lesson here. We're in retrieval and we're gonna create this vector plus cipher retrieval. Okay. So if you're following along, we are here. And what we're gonna do is we're gonna spend some time and actually create a vector plus cipher retriever. Before I do that, I'll just have a quick look at looking at questions. It's quite... It's turned into quite a sunny day here. So, so I've got a question. Thanks for the workshop. Thank you. Embedding cycle is available in Neo four j desktop desk desk desktop version. Yes. So the, you will need to install the GenAI plug in. But if you're using a Neo Neo four j desktop, there's an option on your... I think it's it's on the instance menu where you where you can pick the plugins that you want to use. But, yes, you can, you can use that in Neo four j desktop. Case of making a knowledge graph, is it logical if using NER plus decision model, Jeff, for relationship? Response, yes. NER with a ML decision model for relationship extraction could be a good alternative. Yes. I agree. Yep. So the knowledge graph is stored in Neo four j database and Cypher is the query language interact with the database. Yes. Is there a limit to the number of nodes or entities in the d b... DB? Yes. Yeah. I... So the response is, yes. Knowledge Graph is stored in Neo four j. There isn't there there isn't a limit. You have billions of nodes, you know, and depending on your implementation of of Neo of of Neo four j, you know, hundreds of terabytes data data. If you're worried about scale, like massive scale, we have a product called InfiniGraph where you can go huge, do property sharding, and how to create enormous graphs. Another question, can the pipeline be used for images? Yes. But you need a different extraction method. There is a there is a course in GraphAcademy. I'm trying to remember the name. I'm pretty sure it's called, Introduction to Vector Indexes in Unstructured Data or something like that. And that that does that does include using vectors to search for images as well. Okay. I'm gonna go through the same process of, of creating my vector cipher rank. So oh, let's have a bit of a tidy up. I feel like we've got too many windows open. So we're gonna start with this program here. So if we have a quick look at this program, vector cipher rag. Oh, has anybody else had the same problem with the code space stopping again? I'm gonna revert to my backup. Vector cipher rag. So let's just have a quick look at look at this program and see what it does. So there are things that are familiar. We need a, connection to our database. We still need an embedder. Yeah. So we still need a way of turning users' queries into Embeddings even though we've embedded all of the data that's in our graph. We're gonna have a users' query. We create query. We need to turn that into an Embedding. We have a retrieval query. So our retrieval query is the data that we're gonna return back. Now at the moment, that's relatively simple. Basically, return the node and the score. So the chunk text and the score. And then we recreate our retriever. So our retriever needs to know where an... Where our database is, what's the database name is, what index we're using, what vector index we're using, and that was the chunk embedding index that we just created, what embedder we're using, and then what our retrieval query is. We are gonna use an LLM. So we're gonna create a RAG pipeline. So, essentially, we're gonna match up, a user's query, to a retriever to get... Getting a response back, and we're gonna use, an... The LLM to, take the user's response, the context we give it from the retriever, to generate, an answer. So when we create our rag pipeline, we give it our retriever. This is our retriever that's gonna use our vector index. And we give it, an, give it an LLM. And we give it a quest... A question. What is a knowledge graph and where can I learn more more about them? And then we're gonna run that process. So we're gonna run our, GraphRag pipeline. We're gonna pass it our query text. We're gonna tell it from our retriever information, use the top five return... Responses returned, and then we're gonna print that answer. So let's give that a run. So what we should see is is an answer to what is a knowledge graph. A knowledge graph is an is an organized representation of real world entities, things like people, products, places, and the relationships between them. It provides a structured interconnected data, interconnected data. To learn more, see see this see this blog blog post. I'm getting deprecated warning back. I think my... The local environment I'm just using while I'm waiting for my code space to start up again, I think it's probably on a on a later version. Sorry. An older version. That's why I'm getting a deprecated warning. I will see if my code space will start up again. But we can see how, the question relates to, how the answer relates to to to the question. Now it is useful to be able to see the context that the, that the retriever is using. So what I'm gonna do is I'm not only gonna print the answer, so so I'm gonna print response to answer, I'm also gonna print the context. So based on the response that I get back, what retriever... What did the retriever return? So we can start to see the correlation between question, retriever's context, and answer. So again, we'll give this a run. So start to get a little bit more difficult to to read, but what we can see is this is our... This is obviously our response, and this is the context that we got back from our knowledge graph. So, first record being what is a knowledge graph, some information that we can see came directly from, from our our our graph. And we can also see the score. So in this case, point eight nine. So pretty relevant. Right? And then then we can also see that we got another row back and another one and another one. Right? So we can start to see how the context that we get, relates to the response that we... That that that we get back as well. Let me just see. Okay. I I apologize on GitHub's behalf, about Codespaces today. Let's just... So I'm just running the same thing on the codes... On the code space as well. Okay. So where the benefit of using a graph comes from is not only using it to do, to do vector search, but we created our knowledge graph. So, from the text within within our documents. And we can enhance our retrieval query. So this is, when the, when when the retriever runs, it does a vector search, and it then retrieves some data from the graph. Now at the moment, our retrieval query is is pretty simple. And it basically says just return the chunk text and the score. But what we can do is we can expand on that to say, okay, well, go from the chunk, the note that was returned by the vector search, find the documents, find the lessons. Yep. And then we can say, okay, well, give me the text and the score, that's fine. But also give me the lesson URL. And also use the knowledge graph to collect, well, actually, well, what what technologies, are also... Are covered in that lesson and also what concepts are covered in that lesson as well. So we can use our graph to expand the information the information that we get. So I'm I'm I'm just gonna do that. And what I'm gonna do is I'm gonna modify my retrieval query, to be more sophisticated, and to use the graph to retrieve additional context. Let's clear this. Again, when I run it, I'm asking it exactly the same question, which is, you know, what is a knowledge graph and where can I learn more more about them? And what we get back is, a knowledge graph is organized structured rep... Structured representation of real world world entities. And you can learn more about them here in this Neo4j Graph Academy lesson and also this blog post. And if we look at the context, we can see that our context is now richer. It includes additional information from our graph, but also, data about what the lesson what what the lesson URL is. Now interestingly, if we compare this to the response that we got before... So this is, before we enhance that query. Oh, did I just run this multiple times? I think so. Yeah. So we get a similar response back, but says to learn more, go to this blog post. The response that we received previously, previously said, to learn more, go to this GraphAcademy lesson. So we can see how it has used the additional context that we've provided it from our graph to provide a more sophisticated response. So I'll pop that back to Hamlet what we've want. Okay. Now we can make our retrieval query as complicated as we need to. Now this retrieval query essentially uses the structure of the graph, yeah, to return information, about the facts that are stored. So if we look at it... So what it does is it says, from the chunks, use the entities in the relation... Use the entities to, find how chunks are related to it... To each to each other. And says, okay. Well, you know, based on the knowledge graph, I can see that the process of model training is part of the technology generative AI. I can see that the resource the the resource... This image resource cites the technology generative AI. I can see that this concept of generative AI is related to the technology of gen... Of gen... And so on and so forth. Let's move there. The technology of vector index is related to the technology of Neo4j. So we can use our, our knowledge graph to, to output facts and information that we were able to extract from our unstructured data. And there's a a more sophisticated retrieval query here that uses that concept to create, more kinda... Again, more text... Textually relevant, context. This is good. Let's just clear that so we can see what's going on. So, again, we get a similar we get a similar response back to learn more. But what we're also getting getting back is more summarized knowledge as well. So knowledge graphs are useful for tasks like complex queries, analytics, insights. Yeah. Interconnected understanding of of of information. And if we look at our context, we can see how our... Oh, so difficult to read. Here we go. We have used the graph to output and define information that we've gained from that... From from our graph. Alright. So have a moment. Experiment. Right? Have a go. Try some different questions. Try and match up the context that you receive from the graph to, to the to the response that you have got. And I will spend a moment just kind of looking at some questions before we move on to our last part where we are going to have a look at text decipher. Will it be possible to show how the knowledge graph, how the embeddings and nodes, nodes are related? So I think probably the easiest thing that I can do is maybe just, if we just explore the chunks within our within our graph... So these are... Actually, let's let's limit limit 10. So these are just some random chunks that I've picked out out from from within the graph. And we can see that, actually, this graph... This chunk here, which is from this document, here's the, is the is the next chunk to this one and the next chunk to this one, relates to knowledge graph extraction, the technology of knowledge graph extraction. And we can also see that this chunk, yeah, relates to, that entity as well. And we can start to expand the graph and say, okay. Well, how are nodes and relationships, related to each other? So vector search. So the technology vector search, is actually, prevalent in three different chunks, but we can also see how the concept of context is related to the technology of vector search. We can also see how the technology GraphRag is related to the technology of vector search. So, you know, this this graph has been built from, information within, within within those those unstructured documents. So, Sonny, what the difference is, graph rag vector from regular vector DBs? Yeah. There's a great response, response already. Difference is what gets retrieved after semantic sense... Similarity finds a relevant starting point. A regular vector DB answers which chunks are semantically similar to my question, whereas GraphRack can answer which information is relevant and what other facts are connected. Yeah. I'm I'm I'm not sure. I could add a great deal great deal to that response. That is... That that that is a great is a great response. Another question for Mark. So we're gonna, Mark asked, is text text to cipher better than it was a year ago when I used it? Is it a reliable tool for an agent now? We're gonna look at a text to cipher retrieve in there. To answer your question, is it is it better than it was a year ago? Like, abs... Absolutely. Is it is it a reliable tool for an agent there? I think there is, there is a huge, like like, what is your use case? Yeah. I think text to text to cipher is absolutely a reliable tool for an agent, but I would think about what's your use case? How are you how are you how are you using it? If you're using a single text to cipher to cover an enormous, very, very, very complicated graph and schema, you may wanna think about actually what... Might wanna create multiple text decipher tools that concentrate on parts of the schema. But, yeah, like, it depends. But, yes, it it is inherently better than it was a a year ago. Technology always always moves forward, and and, yeah, you can use it as a reliable tool. Okay. So I am gonna move on to, do the last, section in what we are gonna do today, which is this Text to Cypher retriever. So, again, I'll use the slides. Text to Cypher Retriever. Right. So there is a there is a Text to Cypher Retriever in the Neo4j GraphQL Python package that allows you to create pipelines that answer natural language questions. And it does that by generating a Cypher Cypher statement. Text to Cypher is very helpful if you want to get precise information from, the knowledge graph. So how how many lessons are in a course? What is the total of? What's the most, what what what order was for the greatest quantity? Some... So on and so forth. And what we... What we're gonna do is we're gonna use that text to cipher retriever to answer questions about data in the knowledge graph. Again, you've got a task to do. So similar process, there is a starter program, which is this Text to Cypher reg program. We'd like to review the code, run it, explore it, explore how we generate Cypher queries, and then sort of experiment with different questions and observe and observe, the results as well. Again, same process as before. I will run through the same the same activity and then I'll take a moment to answer questions. So take a moment to answer questions as well. Alright. But yes. So, again if you're following on in the lesson, we are here. Text decipher retriever, and we just did create a text to recipher text to recipher retriever graph rag pipeline. So, this is the program that we're that we're gonna be looking at. Again, I'm sure it's looking familiar. Connection to our Neo four j database. We need an LLM. We're gonna be using our LLM, not only as part of our pipeline, but also to generate, our Cypher queries. One of the things that we can do when we're looking to generate text to Cypher is essentially what we're doing is we're turning questions, sort of natural language, into Cypher queries. And we can actually provide examples as well. And that helps the, the LLM, particularly if there is nuance within our data model, to be able to kind of, like, construct better queries. So one of the examples that I have said is, like, if you learn... If if you are finding a note by its name, use essentially case case insensitive search. Because what I want it to do is for the kind of use case that I've got, I want it to be a little bit looser in terms of its matching. But, again, if there was a use case here where actually full text search was more, use... Useful to you, you could provide an example. I don't know. Maybe there is a specific, meaning or or description within your on... Your ontology, again, you could provide an example. And it just helps steer the LLM to kinda understand your use case. But we're gonna build a retriever. So it's a text to cipher retriever, which date... Database we're using, which NLM we're using, and any examples that we wanna give it. We're gonna build a retriever, and we're gonna generate a question. How many technologies are mentioned in the knowledge graph? We're gonna use that reg retriever. We're gonna print the answer. What we're also gonna do is we're gonna look at actually, in order to generate that answer, what cipher statement did it create, yeah, and what information came back from that site... From that site... That that cipher statement as well. So, let's run that. Athena, I keep missing it. Okay. I don't think it's... Okay. So ask that the question, how many technologies are mentioned in the knowledge graph? 76 technologies are mentioned in the knowledge graph. That's the the response that I get back. And we can see the cipher that it generates says match technology, return, count, distinct. So, only return unique technologies and then count them. And then we got a record technology count of 78 back. And it... From from this context, it constructed the response 78 technologies I mentioned in the knowledge graph. And we can start to see how, we can use that natural language to generate Cypher, to generate Cypher statements. And I would take some time, to experiment. Right? There's... There are some queries here. So for example, how does Neo four j relate to other other tech... Other technologies? So, again, it's gonna use that question. It's gonna use that schema to generate a response. So Neo four j relates to other technologies in a few ways. That's a more sophisticated question I've asked it. Directly related, conceptually related, and variants and terms. Right? The context also includes closely named associated entities like Neo4j graph. But we can see that it's... That Cypher statement, it created a match technology. We can see how the example that I gave it in, helps the LLM, helps the LLM, do a case insensitive search. And we can also see where it's used optional matches to say, okay. Well, how is this related to to, to other tech... Technologies and concepts? And then it's returned a technology and we can see that the response that we get back is OpenAI, textual information, graphs, etcetera, etcetera. So spend some time... Keep going the wrong way. Spend some time experiment. But just to kinda recap, Text to Cypher is very useful if you're looking to get specific information out. I would also say as part of an agentic system, is to use Text to Cypher to provide sort of like catch all, catch all tools and scenarios. So for example, if you're building an agent that has a collection that has a collection of tools that are gonna use your knowledge your knowledge graph. You may have a tool that basically says, I need context, I need additional information, but there isn't another tool that fulfills, that fulfills the role. Text to cipher can often be a really good way of filling that gap. Okay. I'm just checking on time as well. So I've got a few minutes to ask any any questions. So, I'll I'll just start from the top. In the example, why, why is an LLM required? The Graph DB can answer that query that query. So in the example I've just used, the LLM is used for two different things. So I'm I'm I'm using the same LLM for two different purposes. The first one I'm using it for is I'm using it as part of the retriever, and it's actually the LLM that turns the natural language question, this one, how does Neo4j relate to other technologies into that Cypher query. And it does that by using, the schema, so the graph schema. So the text to Cypher retriever takes the schema from the database, passes it to the LLM along with the natural language question and any and any examples, and it generates the cipher statement from that. The second thing I'm using the LLM for is as part of the GraphRack pipeline, and it's and it's the LLM that turns the user's question, the response that comes back from the execute cipher statement, into the final, you know, 72 technologies what relate to Neo4j or whatever. Yep. Yeah. So, you can use different LLMs as well. So you don't have to use the same LLM for your retriever, so the one that generates Cypher, as you do for your GraphRack pipeline. Either you can use different ones. So just another couple of questions. Is there any way to make L and M already know the schema ontology before text to Cypher? I don't know. Such as cache in context. So I... In the models that I'm familiar with, I don't think so. The the... So I think the question you're asking is, is there any way to, for the LLM to have state so I don't have to pass the schema or the ontology every time I wanna generate a piece of say... A p... A piece of cipher? Certainly, the vast majority of models that exist today are stateless. The the things that I would do is, at the moment, we have created a text to Cypher retriever that essentially can deal with the whole of the database. So this... The whole schema gets passed to, passed to generate the cipher every... The cipher every single single time. You can constrain and have multiple TextDecipher retrievers. So you could have one TextDecipher retriever that just looks after, maybe, I don't know, customer accounts and products. And then you could have another text recite cipher, retriever that just looks after, Documents, users, and permissions or something like that. Right? So you can you can create multiple retrievers and use part of the retriever. You can say, okay. Well, your job is to look after this part of the graph, these nodes, these relationships, these patterns. Yep. Also, any tips for making TextCypher, answer deterministic without without hallucination? So I think what you're saying there is how how do I make it more reliable? How do I make it create, valid Cypher statements? There are, there are multiple things you can do. I think my first my first one would be would be is, create, multiple retrievers and have them look after sections of the graph. Okay. So we have five minutes left. There are a couple of things I just wanted to say before I finish, and then I will pass back to back to Alex. So if you've been following along, we've just finished this retrieval section. There is a section here on agents where you can build a simple agent that uses the retrievers that you have created, as part of this workshop. It's it's a nice module to continue, and to kind of, like, bring every... Everything together. Unfortunately, not something we can fit into the kind of two hour window that we that that we have here. Other than that, I would, we have been using GraphAcademy for for this for this workshop. There are many, many, many courses on GraphAcademy. It is all completely free. They're all hands on in this sort of vein. So, learn something, do some... Something. Learn something, do something. There are lots of create... Created learning paths. So if you're looking for more information about how can I use Neo four j, as part of my generative AI applications, you will find a GenAI pathway that will take you through, from, you know, right to the kind of start, right up to, you know, creating your own GraphRack agents and using graphs as part of your generative AI AI applications? The workshop we've done today is kind of an abridged version of a few courses. So again, if you're looking for more detail or you feel the pace has been a little bit too, a little bit too fast, the courses on GraphAcademy are a great way of filling that gap. And then finally, lots and lots of free certifications. This is, our first road to node session, our first, like, two hour workshop. Nodes will be on November 12, as, as, Alex introduced right right at at the start. Please save please save the date. Please come along. There is a fantastic agenda. And I think that is it from me and unless Alex wanted to say anything before we go. I've lost it. There you are. Yeah. Amazing. Thank you very much, Martin. It was a a super session. Fantastic session. Yeah. Like, thank you, AJ. Really, well done. I think it was a good, overview of of all the all the basics, all the topics. One thing that I was, just thinking about when you were answering this question about, can the LLM already know the schema? I just shared the link to NAMS, Neo4j Agent Members Service, Oh, yes. in chat. That could be a way of making a couple of these things available. It's a bit more complicated. This is a bit more fundamental here, but if you build upon it, I think NAMS could help you go in the way of providing this information as a sort of repository for your agents so that they can look into things and you don't have to put them in, you know, in the front every time. Yeah. Yeah. Yeah. That's a great, pointer. Yeah. So, Neo four j agent memory. Cool. And then there was another question about the OpenAI key that we obviously provide for this workshop. How long will that be available, and if people can, can try? Not not Yes. long. Exactly. Yeah. So... Yeah. Yeah. Not long after after we close after we close this session, there is a free tier and it is pretty generous on on OpenAI. So if you... If if you've never created a sort of developer account, you can do, and you can create a free API key that you can use, and you'll certainly get through this workshop using it. Yep. Super. Alright. I think that's that's it. Thank you very much. At this point, also to to Wazundra. She was relentlessly answering questions. and. and, Thank. you so much. Yeah. Thank you so much. And in a in a in a speed, very, very speedy, speedy way. So, really, really, great that we had you, join us today. So that's... That was... It was super, super helpful. Thank you all for watching. Thank you for participating. Thank you for your question. Thank you for joining us. If you want to do this again, we are gonna do this in a couple of hours. So you can you can join us this this this evening later, as well for for a second round. And, obviously, I shared the link to Rotonotes. Other workshops are coming until the November 5. So please register for those if you are interested in joining any of the other ones. And, yeah. I think that's it for today, and for this this one. Thank you, Martin. Thank you, Wazu. Thank you, everybody, and, see you next time. Thank you so much.