Context Engineering for AI Agents
Key Takeaways
The video discusses Context Engineering for AI Agents using Neo4j graph database and RAG (Retrieval Augmented Generation), focusing on building dynamic systems to assemble complete and structured context for each LLM implication. It covers various tools and techniques such as Graph Academy, OpenAI API, and Cypher query language to construct knowledge graphs and integrate with LLM models.
Full Transcript
Hello everyone and thank you for joining today's session. My name is Ree and I'll be your moderator and host for today's session. We are going to get started in a few minutes. We're just waiting so everyone has a chance to join. In the meanwhile though, there is a little bit of setup if you'd like to code along with us live today. There is a link to the session resources in the video description on YouTube. I'll also be posting the link for everyone watching on LinkedIn very shortly. I'd recommend watching on YouTube though. The LinkedIn player experience is not so good. Uh so yeah, if you'd like to code along with us today, uh there was some setup info uh shared with you via email, but [music] yeah, it's in the video description and I'll be posting posting a link to it very shortly. However, we are going to be running through a couple slides at the beginning. So, if you haven't done the setup uh instructions just yet, if you haven't completed them, there will [music] be uh some time for you to do that at the beginning. So, I'd recommend having a look at the resources document now uh which is in the video description and in the chat. I'll post it again for everyone uh very shortly. But yeah, have a look at that now if you'd like to uh code along with us today. However, if you want to just watch along, we are going to be uh recording this session and we'll be uh sending out the recording to everyone that's registered uh in the next couple of days. So, make sure you register for the session. Uh you can scan the QR code on screen now and you can also head over to datacamp.com/webinars where you'll find uh this session as well as all of our future sessions as well. If you have any questions at any point throughout the session, let us know in the chat. We're going to be answering your questions for the final 10 minutes of the session. So yeah, make sure you stick around for that. And for anyone that's just joined, welcome. Uh my name is Reason. I'll be your moderator and host for today's session. We're going to get started very shortly. Uh we're just waiting for the last few people to join and then we will get started. If you haven't done so already, check out the resources document that is in the video description on YouTube. For anyone watching on LinkedIn, I'll be posting the link to it very shortly. And then yeah, that's you all set up. We are going to be sharing the slides that we're covering at the beginning of the session. We're going to be sharing those with the recording. So make sure you register for the session and then we can send you the recording [music] and all of the uh complimentary documents. Brilliant. I think that's everything. So now I will uh kick us off. Let's go. So hello everyone. Let me hide the music and then we will get started. As you may have noticed, it's just me today. There's no Richie. So we will have to struggle through this uh and not have his uh beautiful voice getting us going. But without further ado, welcome to the session. Hello everyone. Uh so I'm Ree. I'm usually the back end moderator for these sessions. You may know me. You may you may not know me, but I'm not Richie. But you're all data scamps and data champs regardless. Uh, one of the biggest biggest keys to success with AI is making sure that it has the right information to solve the problem. When you're a human interacting with a chatbot, it's fairly straightforward. You type what the AI needs in a prompt. But for AI applications and AI agents, some automation is needed and then you need you end up with more sophic sophisticated solutions like rag or retrieval of augmented generation and context graphs. The latter of which are the focus for today. Our guest today is Nia Mlin. I will just bring you on now. Nia, welcome. >> Hey everyone, thank you so much. >> Pleasure to have you. So uh Nia is a senior developer advocate for AI at graph database company Neo4j. Nia is a software engineer and developer relations expert. Uh Nia helps developers use Neoforj for uh AI engineering and Nia is also the CTO and founder of Afro in the AI technical community. Previously Nia was lead developer advocate at Couchbase and the chief of staff for the Commonwealth of Massachusetts. So yeah, without further ado, over to you Nia. >> Awesome. Well, thanks so much everyone for coming. Excited to join you all today. Um, and we are going to talk about effective context engineering techniques for artificial intelligence. So, we're going to dive a little bit into um the entire Genaii landscape. Um, how to build out context, what are some different techniques that we can use to build out context as well. and then we're going to get into a hands-on demo. So, just a a key point here for our hands-on demo. We were going to be using GitHub code um code spaces, but it seems like GitHub code spaces is downed this morning. Uh an outage. We we we love an outage. But, uh so instead, we're actually going to be developing locally today. Um, so if you haven't already, um, because you were depending on just using our enclosed environment, um, I'd love for you to, um, install Python. Um, if you have not already, um, and, um, you'll also need to install, um, a couple different dependencies as well. Um, I'll give you all the information here, but Python's the main thing, as well as an IDE that we'll be working in. Now, if it's we're we're going to be talking a little bit um, before we get to the hands-on demo. So, if you're not able to install those things prior to us actually live coding today, then that's perfectly fine. What I want you to do is just sit back um enjoy the ride. Um and then after I have the link where that you can do all of this work yourself, right? You can do all this all within the enclosed environment. Um we're going to be using what's called Graph Academy today. Um, and Graph Academy is Neo4j's online learning platform where you can learn about genai, graph, context, graph, all the newest concepts, what's happening within artificial intelligence. Um, and hopefully GitHub code spaces will actually be um, uh, be up for you. I just got a ping from my colleague that said it's been a little bit unstable for them, too. So, um, let's just get started. Again, if you don't um if you don't have time or you can't coordinate getting Python installed plus an IDE, then just sit back, relax, and I'll provide all of the resources for you to do this completely hands-on um right after this call. So, let's get started with what we're going to be talking about today. So, okay, just a quick introduction to myself. Um um my name is Nia Mlin. I'm a senior software engineer and a senior developer advocate here at Neo4j. And my research focuses a lot on graph theory, graph algorithms, and um trying to help people understand context engineering as this blackbox phenomenon. Um and we're trying to break open that black box in order to decipher, trace, and explain how artificial intelligence systems work. So, um, we'll we don't have to go all the way into my, um, into my background, but if you want to find me on LinkedIn to chat a little bit later, there is a QR code there for my LinkedIn, and I'll have it at the end of this talk as well. So, let's start off with a question. What happens when dozens of AI agents actually have to work together? Right? So, we would hope that it's an actually harmonious experience, right? um where it's like a sheet of music of agents just dancing together, right? But in reality, if simply unleash unleash this swarm of different agents, right? You then start to find out that they're trying to work together to solve a particular problem, but maybe some are cording on a task and then others just really need to stay out of each other's way. So, there's a large risk of something that would happen and to look like this. Okay, chaos. [laughter] It's like planes that are on their own flight paths and without air traffic control, right? Each plane can fly um it can but it's flying without coordination and without guidance. And so really you have chaos in the skies. So the same actually goes for agents. So when they work in swarms without structure and orchestration, it actually becomes madness. And so we have we often find that this statistic rings true for a lot of um uh agent development. So when teams are trying to put agents into production, 95% of AI projects or AI pilot projects are failing according to this 2025 study that was done by MIT. And I have the QR code here so that you can read the study yourself. Right? Don't take my word for it. Actually read the um read the white paper. Um, but when that project fails, that's a lot of time and a lot of money that has already been invested into that work. So, let's freeze time. Why are our AI agents failing? Right? AI agents are failing often because they don't have the right context. Okay, this is the key part. They're flying around in swarms without air traffic control, which then, you know, says, "Okay, one plane should leave should land right now. This other plane is having an emergency. So we need this gate open with medical staff ready right all these things we need that air traffic control we need the overall context in order to understand what's happening that's the missing ingredient so we get into context engineering so context engineering for those who are not experts and can't just define it themselves um and I wish we were live in person right now because I would call on you to say okay how do you define context engineering but how I define it is that it's a discipline that systematically provides models with the right information, with the right tools, and with the right um with the right instructions that an AI agent will need in the correct format at the correct time. Okay? Um to specifically accomplish a task. So, it's unlike prompt engineering where you're just trying to say the most cleverly worded thing to the LLM to get it to do what you want it to do. Context engineering centers around building dynamic systems, right, that assemble complete and structured context for each LLM implication. So, this is a shift in focus from um using your cleverly worded prompts to actually doing cohesive context design, right? So, that's why context engineering has become like a critical skill for AI AI engineers these days. And there's lots of ways to do context as well to do context engineering. I have a few here. I'm not going to dive deep into them today because I want us to actually get into the hands-on tutorial. Um, but context engineering has all of these different facets. So, we have rag uh plus hybrid search which you know as a um in a previous life I was a teacher. So, I always like to have little uh images to help those who um learn more visually plus those who learn with more auditory um uh uh help. So um I have um rag plus hybrid f search which I represent as this this really hilarious meme. How do you do fellow kids? Right? It's the OG way of doing context engineering. Right? It's um it's some I would call naive rag for example. It's it's um rag vector search and then you can include hybrid search um in order to make it more complex um and in order to make it hallucinate less as well. So I'm again I'm not going to get into each of these. We can do a whole talk on each of these, but I just wanted to share a little bit of the the various ways that you can do context engineering. Memory management is a second way. For example, and I have that represented as Dory. But all in all, what you have to remember about memory management is that you are trying to uh get your agent to um decide what it's going to forget. So Dory is a very forgetful fish in the movie Finding Nemo. Anything you tell her, she'll forget instantly, right? Um, but with your AI agents, with memory management, you were deciding, okay, like what is okay to forget? Should I forget the the older stuff, right? Should I forget the newer stuff? And you'll see that usually um with your models, they'll um they'll opt for forgetting older stuff to uh to promote the newer um context that you're providing. But that's another uh another aspect of context engineering. And then we have tools and function calling, right? Um I have this represented as MacGyver for those of us who are older. We remember MacGyver would always have any tool. He was the the guy with the plan, right? He would always have a tool to get a job done. Um, he couldn't do it himself, but he would always have these tools. So, I have that represented as tools and function calling because you can use your agent in instead of having your agent do a lot of that heavy lifting. Um, you can actually have your agent just call a tool so that your agent can focus on the things that it's actually good at, which is reasoning, right? And then, um, up above that I have structuring and ordering context. We all know um and Enthropic has lots of guidelines on this, but we all know that when we put when we have a context um a context window, you want to make sure that you are putting the most important context at the top so that your agent can remember that context, right? Don't put it at the bottom of that of that prompt, right? It's just going to forget it. You need to put it at the top. So that structuring and ordering of the context is actually really important. >> [snorts] >> And then lastly, what we're going to get into today and building out knowledge graphs as well is knowledge graph augmented context which actually uses knowledge graphs as the key point as the key center for um for um uh augmenting your context or or making your context much more uh making your context and your agents much more trustworthy. So [snorts] we're going to dive into that quickly. So for those who are not familiar with knowledge graphs, I just wanted to give you a representation of what they look like. A knowledge graph um represents uh data in ways that's both human readable and machine readable. So here we have a human readable view of an apple or a human view of an apple, right? Red apple. Pretty pretty simple. But usually with naive rag or vector-based rag, you'll have this vectorbased um uh uh representation of an apple, right? whole bunch of numbers, whole bunch of um like really mess right because you're like I have not memorized what a what the view of an apple is or the vector view of an apple is. I don't know if you have. So here on the other side of that we have a knowledge graph view of an apple. So we have vectorbased view and then a knowledge graph view. We have for the nomsgraph view you actually can see these um what we call nodes which are the circles and then edges which is the relationships um how things are related to each other in a human readable and machine readable way. So we see that an apple has a body which is round. We're not talking about the company apple. We're talking about the fruit apple. It's a fruit. It's sweet. It has a white inner most of the time. It has a stem. All this information. So you can see when you represent data in a knowledge graph, you can see that it's much more understandable for humans and also for machines. And just quickly, this is another representation of how people mostly use data themselves now. Um, most people are using data or storing their data in tables and rows. And so it's pretty hard to see how things are related to each other or how things um have relationships to each other. And I've drawn these lines here to just try and show you what those relationships could look like. However, um that's not normal within a SQL table. You'd have to do complex joins in order to show and showcase the relationships. But then when you're doing those complex joins, a trade-off is that it slows down your application, right? It it pulls it builds a lot of bulk into your application. [snorts] And so if you take that same data and showcase it as a knowledge graph within your uh data structure, you can actually as a human see how things are very well connected, right? You don't have to do all these complex joins and and trying to figure out okay like how does that thing actually relate to each other, but actually everything is more intuitive. Everything is much more clear. [snorts] So in other words, um if you're using a knowledge graph with your when building out agents, a knowledge graph can answer not just what is relevant to this query text, but what else, right, is connected to this topic that's necessary for me to include in this query, right? You're able to traverse a knowledge graph to see what else is related to this thing. Very, very, very helpful for building out context. All right, so let's get into it. Let's get into it. So, I have a hands-on demo and um again, uh this was super fast, but I wanted folks to be able to see. And normally this demo is u or this demo, this workshop is usually around three hours long. So, we're going to try and pack as much. We're not going to get very far, but we're going to try and pack as much as we can within this demo. And and what's great about Graph Academy is that um it provides literally all the information that you need. You don't need me to walk you through this. Um you can go on to Graph Academy um and to the workshop that we're about to do and you can do this all on your own. Right? So as soon as this workshop ends, then you'll be able to finish out the workshop and finish out building out your agent. But first of all, what you'll need in order to get started is all of these links. So we have and I think that my colleague had push had um pushed this information to you all. Um but you'll need this dev. So, you'll need to go to dev.ne4j.comworkshopai-gi-notes and that is going to bring up this uh document right here. I see a few of you are already in here. So, this document here will have our um that this a link to this document as well. Um you'll need to sign up for Graph Academy. Hopefully, you were able to do that ahead of time because it takes a little bit of time um to get set up with Graph Academy. Um, and in Graph Academy, that's where we're going to be doing all of this hands-on work. Um, so you'll also see that this is the workshop that we're going to get into. I have that here as well. Um, I was already getting started with you all just because of the the GitHub uh code spaces thing. So, I wanted to make sure that we can run this all locally. Um, but you'll see that and then yes, everyone you see in AP uh OpenAI API key, right? Yes, that is for you to use during this workshop. Okay, super exciting. Don't worry, we're gonna turn this off as soon as this workshop's done, unfortunately. Um, but we wanted to make sure that this, you know, paying and having a open AAI um API key would not be a barrier to you um to you actually enjoying and working with this workshop. So, we have that API key for you. Um, and then if you just want to access a Neo Forj database directly, we have instructions on how to do that as well. But this is the document that you'll want to get started with. We have the OpenAI um API key there. And what we're going to do since hopefully everyone has already signed up for Graph Academy, we're going to go into the Neo Forj and Generative AI workshop here. So, I'm just going to click this link and we're going to open up this course. So, like I had mentioned, this course is normally a three-hour course. Um actually, let me uh let me reset myself. It says that I've already done um uh I've already done 16% of the progress, and I actually did the whole thing many times. Um, but I just wanted to make sure again um that we were all good for this one. So, okay. So, let's get started. Let's get started. All right. A couple of things I'm just going to skip over just for time sake because you know we don't have a lot of time at all. But let's dive in. So, we have continue. Of course, we're starting at the top. Generative AI. So, I'm going to pretty much skip a lot of this what is generative AI conversation because I think many of us have already been in the space. Um, and I don't want to, um, let's, you know, I want to get into what we're going to be doing today. So, we're going to skip over a little bit of this work. Press ready, set, let's go. It's going to give you an overview of how AI works, how generative AI works, what langu large language models are for those who are new to the concepts, right? what prompts are if you haven't built out an agent before. Um, how you determine your answers, hallucinations within artificial intelligence, accuracy piece, evaluations as well, how you evaluate whether your agent had actually did what it was supposed to do. Um, and then what we had mentioned about hallucinations. Um, so we're just going to skip all over this for time sake and get to what I like to call the meat and potatoes of things. All right. So a quick quick overview for those who don't know about graph rag. Graph rag the only difference if you've already heard about rag. Graph rag the only difference is that um uh for graph rag um the um the retrieval path in rag. So retrieval augmented generation the retrieval path involves a knowledge graph. So I'm actually going to present it. Yeah do this presenter view. Um but the retrieval path in includes a knowledge graph. So that's the only difference that you need to know about how graph rag works. We have an architecture as well um where you can understand how to do information retrieval, how to get the response um but again I want to get us building. So if you um after this talk if you want to go back and actually do a lot of the background information then please do and it's all accessible here. So I talk about the rag process grounding retrievers which we're hopefully going to have time to do today. um and then different data sources as well. So graph rag again is where the um where the retrieval path in rag involves a knowledge graph. Perfect. Lots of different benefits to graph rag as well. Um we can learn about full text search text to cipher. Cipher is the Neo4j query language. And again a lot of it's it's it's a lot of context right now and I'm skipping over a lot. Um but I want to make sure that we can build. So, let's close that out. We did most of this. So, perfect. Great. Now, I already went over this a little bit, but what is a knowledge graph? A knowledge graph is a as a representation of data and how it's related. So, we see here that um uh we have Neo Forj as a company. Um and then we have for example, this is a node and then we have these relationships um connected to another node. So Emo was our is our founder still is our founder um founder and CEO. He's the founder of Neoforj. We can see this directionality happening and Emmo works at Neo4j. So we see all this directionality. This is just a simple representation of how data is uh shown within a knowledge graph and we already went through that a little bit. Cool, cool, cool. Let's dive in. Great. All right. So now we are going to be working today with the graph rag for Python package. Um so this is a um this is where we're going to get started. So um we have a repository here. It's a GitHub repository. It it's already been created for this course. Um it contains all the starter code that we're looking for. So um we can either use GitHub code spaces, but I think it's actually down right now. So normally we would do this all within an isolated environment GitHub code spaces, but it's actually not working. So what we're going to do is we're going to run this all locally on our machine. So you will already have or already need to have Python installed. If you don't already, I'm happy to provide resources on how to get that done. Um but we'll do that after this entire talk. But if you do have uh Python already installed, then let's continue. So, [snorts] we'll want to of course set up a a virtual environment as well that we'll be working in um because you don't want to just install um Python or install pip everywhere because it'll mess up your other Python uh projects if you've already developed in Python. Um but initially what I want you all to do is to clone this repository here. So, [snorts] github.com/newj-graphacademy.ji geni and you can see um the actual workshop and um all the information. You can either press um uh copy the URL here or just straight from um straight from here. And I already have a um a VS Code IDE up where I have um I'm logged into a virtual environment. Um and I have already run um the clone um so I already have the project up. So you're going to just run that clone that get clone within a terminal and it will install the entire project. Um, you're going to need to then run pip install-r requirements.ext. You can just copy this um so that you can um port that into your terminal. But don't worry, you do not need a Neo Forj database um for this um this is all going to be working within a sandboxed instance. And again, I think my colleague had mentioned there's going to be questions. I'm going to save those questions toward to after um uh just to make sure but I'll take a quick quick peek. Hello and welcome everyone. Welcome from Texas. Welcome from South Africa. Okay, great. So now let's let's actually get started. So what I want you all to do is to um copy this. So this is our environment variables. Um we are going you already have when you have um when you've cloned down this repository, you're already going to have a m.ample. So, this is just an example. M. What you're going to do is create another I think my I'm not sure if my um screen just stopped sharing. So, let me make sure it still is. Awesome. We're back. Great. So, um perfect. So, I've created a M file. We've replaced the OpenAI API key with the one that was in that. Um Yeah. Yeah, absolutely. Let me bump this text size for everyone. Thank you for the for the ping. Um, bump this text size for everyone. Um, we've included the open API key and we're going to keep everything else that's the same from Graph Academy. Okay. Awesome. Awesome. So, let's keep those all the same and then we're going to add the open a open AI API key and then we're going to test our environment. So, I had already done this. Um what I need to do is you run Python 3 um within the within the um the uh folder Python 3 test environment.pi py and then you should get an okay if your environment is actually correctly set up. And once we've done that then we can continue. Perfect. So let's go. So let's I love that my It's okay. We're flexible everyone. My screen share stop keeps um stop keeps ending, but that's okay. We're flexible. All right, so now let's get into actually building out this. So again, once we're constructing knowledge graphs, we typically are following these steps, right? We're gathering data, chunking data, and then vectorzing that data, and then passing that data to an LLM. Let's try and figure out why this keeps airing out. But it's okay. Even if my screen doesn't share, um, keep listening to me. Um, and um, I'm I'm following exactly what's in the Graph Academy course. Okay. So, we can know how to vectorize our data, chunk our data, um, extracting nodes and relationships, and then generating a graph from that. So today the source information that we're going to be taking it will be from the Neo Forj Wikipedia site Wikipedia page and it's going to gather the text from that page split that text all into different chunks then generate embeddings based on those chunks and then extracting entities from that um entities and relationships from the uh using the LLM um and then we're going to parse all of that information as well. So eventually we will generate what the graph looks like. So, let's do all of that. Okay. So, now we're going to actually open up workshop.jai extract schema. So, we're going to go over here. We're going to go to um workshop.ji. Then we're going to go to the extract schema um file. And here, I'll move this out the way. Perfect. And I'll move this out the way as well, just so that we can stay on the on the code. Awesome. So we'll see a full um schema for schema from text extractor here. So this code is going to use this let's see the schema extractor right and schema from text extractor class um to extract the schema from a given text input. So this extractor here of course we can see we have open llm we're going to use chat gbt4 which also is going to be deprecated I think within the next couple weeks. So, um, we're going to, um, uh, this entire demo is going to be changed over to the newer, um, models. Just keep that in mind. But the whole point of this is that given the text here on line 17, um, Neo Forj is a graph database management system developed by Neo Forj Incorporated. It's a simplified version of the a simplified version of the extracted schema would look like having some node types, relationship types, and then patterns in general. So um let's continue. So yes, we'll have this extracted schema. We're going to continue. [snorts] And then there's a pipeline for how you would then create your knowledge graph. So next after we've analyzed this, we're going to go to knowledgerraph builder. py. And we can see here that we're starting off, we're doing all of our imports, but then we have this graph database driver here. So let's review what's happening. So, it's going to load a single PDF file, right? Um, and we'll go over here. It's going to load a single PDF file. We have this PDF file at the bottom. Um, and um, but initially we're going to create a connection to the graph database. Um, and we're going to instantiate our LLM model as well. Um, and then after that, we're going to create our embedding model, which we see on line 26. And then we're going to set up this simple KG pipeline here. Um so KG is just knowledge graph pipeline um where we have the LM the driver the um Neoraj database and then the embedder and then from PDF is true and that is this PDF that we see here on line 38. So we're going to run uh the pipeline to create um the graph from a single PDF uh from a single PDF. So, let's try and run this. And we're looking for Python 3. And this file is called kg kg_builder. py. Oh. Uh, did I spell that wrong? No. Search file. Oh. Uh, workshop.jai. Okay. Um, let's do Python 3. Then we need workshop.ai/ geni slash awesome. Okay, so we're going to run the program. It'll it'll happen eventually. It's going to take its sweet time. workshop.ji. Did I spell everything right? kg_builder. py. Yep. All right. But once we run the program, the pipeline is going to process that PDF document and then create a graph within Neo forj. Cool. Nice. Okay. So, we see the resolver number of nodes to resolve six. And then number of created nodes, zero. Okay. So, um we have I wonder why it only created zero. Oh. Aha. I think I might have needed to run the extract schema first. So, let's do this one more time. Python 3. We're actually going to extract the schema first. Schema. py of course of course you have to get your your files uh file names correct workshop genai and this always happens with local development everyone um if we would have been in our in our nice um system everything would have worked out okay all right perfect so yeah so apologies you have to run the extract schema first which is going to get our schema we can see all these different types um uh the different property names And then after that then we would run um the knowledge graph builder py and we'll see how many actual nodes and relationships came out out of running that. So let's see. So this is going to take a little bit but um but at the end of the day we should see the resolver the number of nodes to resolve and the number of created nodes. And then uh after that point again this is going to run for a little while. Um we should be able to then explore our knowledge graph and we could have done that much more easily. Sweet. Okay, so the resolver still zero for number of created nodes. That's per the fine. We're going to debug this later because we don't have a lot of time. Um but that's okay. Number of nodes to resolve. But let's keep going within the um within the space. So if we jump back to graph academy actually then we can see jumping back to graph academy and everyone I'm in um section knowledger graph construction but we can run this uh cipher query here. So you can run this cipher query and it is going to um we have a match. We're going to match documents from specific documents based on the chunks and then return the path and the text. So once we run this cipher query, we can actually see a lot of that information that we had from um from the Wikipedia article, right? What is generative um what are generative AI specifically? Um and then this all of this information that we can see from the chunks. So we have a default chunk size and then we can continue right the extracted entities and relationships between them can be found using a variable path query. We can run that. Um and now you can see this knowledge graph start to start to um start to be generated. Right? So this is all about chunks from the knowledge graph builder that we had just run. Um and we can see for example within this model I think it's this one right here. Um what is generative AI tasks from LLM? Um AI models AI models detailed information as well. So let's continue. And what I want to do okay so let's continue. So now we're just going to delete this existing graph so that we have nothing here. Um and then we can um let's keep going. So text footer chunk size. So to modify the chunk size, you'll need to create a fixed size splitter object and then pass it to the knowledge graph pipeline. Um so um we would then go and and mon and modify the workshop.ji knowledger builder.py. So we have our knowledgrapher.py here. Lowgraph builder. py. Perfect. We're going to modify this. Perfect. So we have um so we're going to modify the um the file to import fixed size splitter class. So the fixed size splitter class so you can copy it from directly from the graph academy and we can put it if we're going to import it. I want to um I can put it here actually. Should I put it here? We got it from there. Fix size splitter. Great. Okay, that should be okay. Text splitter. Yeah, that should be all right. Beautiful. And then we'll also and make sure to save if you don't have autosave on here. And we'll also edit this simple KG pipeline class. Um, perfect. Here. Um, and we'll add um the text splitter at the end. So we'll add this textlitter at the end of from PDF. So textlitter textlitter equals textslitter. Okay. Then after that we will then run this locally. Where is what happened to my terminal? I don't want a new terminal. I want my terminal. Okay, there we go. Great. All right, I don't need the new one. Cool. So, now we can run this again. And this is the knowledge graph builder. Yep, we'll run it again. And as that going, as that is going, we could jump back over to Graph Academy and we can actually view the documents and the associated chunks using the following cipher query. So let's see if that is finished. Yes. Okay, it is finished. And we could run this. Great. So, we can view the documents and the chunks using this cipher query here. There's a lot of syntax here that you might be like, what is cipher? How does it work? It's hard [laughter] to um go from 0 to 100 within pretty much a 40 to 45 minute lesson that we have here today. Um I'm not going to dive into the syntax of cipher for you all. However, um if you did want to learn more about cipher and how to build out these queries, we can go to graphacademy um.com and learn more about how to build out how to work with cipher. And cipher again is our query language that we use to um uh to talk to graphs really. So um so we'll view the entities then after um using the following cypher query. We're going to run this cipher query here. All right, perfect. Now we see all of these different entities, chunks, etc. So we can experiment with different chunk sizes as we can see uh here within cipher you can edit all of this information right to um to experiment. So we have chunk which is going to be to the third degree with entities and then match all of those chunks. Um I'm not going to dive more into those but this is just I'm going to give you all a primer into all of the things that you can do um within graph and then building out your graph. So again, this is about a three-hour workshop that we would normally do. Um, but we're trying to do all within this tiny amount of time. Um, but if we jump to the next section, then we can add the schema to the pipeline. Um, we would uh open up the knowledge graph builder schema if you're following along. Perfect. So we have the knowledge graph builder schema here and we're going to review what's happening within this space. So I'll move this all the way over here. Um, you can define specific nodes and edges, nodes and relationship types. We'll go down. We'll scroll down. See, so we have these different node types here. Um, and then we see that we're going to add that node type to our schema and then run it, right? Terminal. I don't want a completely new terminal, but that's fine. Um, Yeah, let's go back to the old one. Beautiful. All right. So, this is knowledge graph builder builder schema.py. Want to run that. And once that runs, there's lots of different ways to um we're just editing the schema, right? We're we're recreating a knowledge graph based on all of this information. Love it. Love it. Love it. So eventually that will work. Great. Yeah. Oh yeah. There we go. Okay. So now if we were to go back into Graph Academy, we can view what's happening here, right? So we saw that we just generated a number of different relationship types, a number of different nodes, um nodes and descriptions. And now we're going to recreate this entire um this entire thing. So we're just going to delete what we had previously. We're going to run this now. Um view the entities and the chunks and had no data of course, right? So run the program. Oh yeah, because we just deleted it. So run the program. And then once you run it, then it will show all the different chunks. We can do all the relationships as well. [snorts] Um, and then see what the patterns actually look like within the space. So, I'm going to skip ahead a little bit as well. Again, this is just a primer for you all. Um, but I want to skip ahead so that we can get into some more work. So, we have structured data happening, lots of different structured data, but and we're going to skip down um and I want to get into retrieval and agents, right? Retrieval and agent work. So, let's jump down to um to let's start off with what is an agent? Most people know an agent is a system that combines your LLM with the ability to take to take actions in the real world. Um and then we have different components, agents, tools, decision-m all the things that I had mentioned with the context engineering, but how they work is that they're retrieving different tasks and planning and then selecting tools, observing, and then iterating. It's this loop, right? Um the um this loop of understanding and learning. So let's create the agent. Perfect. So we'll be updating a link chain agent. So we're going to work open up agent. py. I'm so glad we got to this part because this is where the fun is. So, I'm just going to move this here. All right. So, within agent.py here, um I want you all to like to try and figure out, okay, so what's this agents function, right? What is it actually trying to do, right? Um what do we think this response to the specific query is going to be? So we have a query um that we're going to run within the application summarize the schema of a graph database right and then how would we extend this agent even after right so um in general what this code is going to do is we're going to instantiate a um a neo forj instance specifically but first we're going to instantiate a um a chat model for openai and then we're going to connect to the neo forj database and then define uh the get graph database schema right so we have this get graph database schema and then after that um we will then um uh create the reasoning and acting agent using models and different tools. So perfect models and tools and then once we run the agent we're going to pass that query in and then stream out a response. So let's try to run this agent right now. Okay, so we need three and we're looking for workshop dash genai slash agent.py. Okay, what's workshop jai? Oh, of course. Okay, agent. py. Okay, cool. All right, so we can see lots of different things happening, right? We have our knowledge craft builder happening. We're seeing some constraints. Let's go and scroll all the way up to the top. And so this is showing us different properties um uh different relationships that are happening within. And of course, like it's hard to visualize because it's all of this text within a um uh within a terminal type of try time to represent what um graphs and nodes and edges look like. Um but this is the response from our agent. So um we're going to see within here um a message between the human and the AI and tool all together. Um and then let's make sure first we're going to copy run the application. So we see in for step in agent stream we have a message and then that message is going to have ro user and content and then the query that we had just put and then it's going to stream out those values and then um step across those images those messages sorry and then it says pretty print but it didn't pretty print here because this is not pretty. Um but it would it should have pretty printed. Um and then we can experiment with this query as well. So let's move on as well. So um before we finish everything because this is not necessarily finished but we're just trying to build out our agents. Um so we'll go back to the uh Neo forj um graph academy course and so we'll see that we can modify our agent to then create a vector cipher retriever um that uses a chunk for the vector index and then defining new tools and then adding new tools as well. So, I don't think we're going to do that right now, but if we were to have time to do it, we would update our agent with a specific embeder here using um text embedding ADA- D-002 and then we would create a retrieval dash query, right? Um that the retrieval will use to add the additional context. So we see that this is a this is a retrieval query where it's going to retrieve all of the different chunks all the different nodes and all the different entities from our um from our results um and then return them as items in values and that we see here and then we're going to format it um as associated entities. So these are the when I when I said entities we have um entities and relationships entities are the nodes right um just representation of all of that data and after that we'd create a vector cipher retriever using a chunk embedding index with a neoj driver this just a way to interact with your neoj graph database and then the embed itself so you can define the tool function to search out the lesson content for that specific retriever um and then update the tools and modify the query and we have the completed code there as well. Okay, so lastly we would be able to query the database and then officially create the entire agent as well. So maybe we have a little bit of time just to go through these what these steps would do. Um so querying the entire database we'd use a tool called text to cipher um retriever to retrie to retrieve and to convert those user queries into cipher statements. So it's like text to cipher takes a natural language um query and turns that or or transitions that into something that a graph can understand. So oftentimes you'll see for example we have a um a knowledge graph um LLM knowledge graph builder tool which takes in um uh which takes in natural language and puts out the answers to your questions um based on retrieving based on traversing a knowledge graph. So um with this tool you can it's it's pretty much the same thing right. So it's actually taking that text, converting it to cipher, and then retrieving um the information that's necessary for your agent to know. So you use that database query tool um in order to do that work. And then we'd modify our agent based on that that um actual retriever. So we see where we would import the retriever have an example um find a node with the name whatever name and then query and then a sample query as well. So we provide the sample query so that you don't have to know how to write cipher. Um but this is this would be the translation um between u what h a human readable uh query would be like and then the actual query cipher query that would be necessary in order to return the answer for the information that the user had asked and we would build out the retriever from that place. So then we define a tool function to query the database using that specific retriever. [snorts] Um we'd have the tool which is query database and then we've defined what that tool actually is. It's a catchall specifically here. It's a catchall tool to get answers for specific questions about lesson content. Um and then um we would return the result from there. And you can update the whatever kind of tools that you want to use. Get schema search lessons or query the database. You can include whatever tools that you're looking for. um and then modify the query as well. And then we'd run the agent and see the output. And then the last piece of the puzzle here because we have a little bit of time and this is a optional challenge, right? So for this challenge, you're going to apply what you've learned with all of this. So when you go through the entire course yourself, um you'd apply what you learned from this and try to create an agent that creates a custom set of tools. So you can do the the um uh lesson knowledge graph which is this entire like lesson your use your own knowledge graph as well. You can augment to use your own knowledge graph still within Neo Forj sandbox database. So you don't have to worry about um any hosting problems or anything like that with your data. um but from your from either the previous challenge or your own information and then create creating a agent using the example code from this workshop um to um uh to gather that information and gather that important context. Defining that set of tools, whatever set of tools that you want to use and then testing the agent with different user queries to see how it actually performs, right? to see if it actually is using these tools that you provided in order to get the answers that you're looking for. Okay. And Okay, perfect. So, again, this is a very short very, very short primer on how to build out agents. This is normally a three-hour course, [laughter] so we had to skip a lot of like foundational information. If you're brand new to knowledge graphs, you might be like, "Oh, this is a lot. This is super confusing right now. But what I want you to do is to um uh actually go through this course because it's actually very very detailed on taking you from zero to 100, right? Um on building what is graph, what is knowledge graphs, context graphs as well. I I'm um I didn't get into the the crux of context graphs, but I do have a demo that I can show you. But really what context graphs does it that is it it takes all the information from your agents, takes all the context, and it actually shows you the the why behind the decisions that your agent has you has um has made. So instead of just building out an agent and it you know making whatever decisions that it makes a using a context graph um would allow your agent to understand why did I decide that right so for example if you're in a financial industries's um uh area then um and you're building a agent that um is asking oh you know should we provide a credit line increase to Jessica right for example um your agent's going to then either say yes or no we should provide that credit line increase but it's actually very important to understand why we should do so. So context graphs um elucidates what we call decision traces which is the history of how an agent got to its decision that it makes um today. Right? So it takes in all the all the information all the added context to allow your agents to be able to answer why. right? Why did you decide this decision? Um and um what are some next steps that we would take based on that? So that's a whole another a whole different demo. You have to kind of like know a lot of this background information in order to be able to get to that point. So um uh it's a it's it it can be a little bit complex but I think that it's necessary um for those who for those of you who are on the call in order to understand how you can build out agents agents that use context as well. Um and what I want to do is I'll even show you all um context graph you know forj we have a blog post on how to build out context graphs um done by my colleague William Lion and I'll post this in the resources uh chat as well for you all um so that you can have it. Yeah, of course. No problem. Um, I'll post it to my colleague so that he can Beautiful. And so this both is a blog post, but it also includes the live demo of this context graph piece. Um, so I do want to save some time for questions. Um, but you can see for this um, if we were to yeah, here like if we were to approve a credit limit increase for this person named Jessica um, uh, why should we or why shouldn't we? And we see all of the decisions of why we shouldn't in this case actually approve that um for Jessica. And that's in this case it's because she actually has a lot of fraud that has happened on her account where someone has was trying to get access to her data not her data her money really um and they were trying to um trying t
Original Description
Session Resources Doc: https://media.datacamp.com/cms/resources---context-engineering-for-ai-agents.pdf
Set-Up Info: https://bit.ly/4lm7gVg
Register for this session to get the recording and resources sent to you! https://www.datacamp.com/webinars/context-engineering-for-ai-agents
Nyah Macklin, a Senior Developer Advocate for AI at Neo4j, will show you how to engineer context effectively for AI applications. You’ll explore multiple context management approaches—including prompt management, retrieval-augmented generation (RAG), and knowledge graphs—and learn how to use Neo4j as a memory layer for AI chatbots and agents. By the end, you’ll have practical patterns you can apply directly to your own AI systems.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Agent Foundations
View skill →Related Reads
📰
📰
📰
📰
I Built an AI Chatbot for Fun. It Broke in a Way That Taught Me Database Design.
Medium · Data Science
The first paid AI API request should be a receipt, not a leap of faith
Dev.to AI
AI, Vibe Coding, and the Rise of SynthClaw Coagent: The Future of Developer Productivity
Dev.to AI
We let an AI reviewer approve and merge its own PRs — here's the open-source gate that makes it safe
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI