← все видео

Context Engineering for AI Agents

DataCamp · 2026-03-17 · 1ч 6м · 1 415 просмотров · YouTube ↗

Топики: ai-loop-engineering

🎧 Аудио

📝 Summary

model=deepseek-v4-flash · prompt=summary-v7 · 14 309→4 267 tokens · 2026-07-20 11:58:31

🎯 Главная суть

AI-агенты проваливаются в 95% проектов (исследование MIT 2025) из-за отсутствия правильно структурированного контекста. Контекстная инженерия — это систематическое предоставление моделям нужной информации, инструментов и инструкций в нужном формате и в нужное время, в отличие от простого prompt engineering. Knowledge graphs (графы знаний) — ключевой инструмент для построения динамического контекста, который позволяет агенту не только находить релевантные данные, но и понимать их связи, а Graph RAG (где в пайплайн RAG включён граф знаний) улучшает точность и уменьшает галлюцинации. В демо-части на Python (с использованием Neo4j, LangChain и OpenAI) был построен граф знаний из Wikipedia-статьи о Neo4j, извлечены сущности и отношения, а затем создан AI-агент, способный отвечать на запросы, используя как схему графа, так и текстово-циферный ретривер.

Проблема: агенты работают как самолёты без диспетчерской

Когда десятки AI-агентов пытаются совместно решать задачу, без структуры и оркестрации возникает хаос — каждый действует сам по себе, конкурирует за задачу или дублирует работу. Это приводит к тому, что, по данным MIT (2025), 95% AI-проектов не доходят до продакшена. Причина — отсутствие «диспетчерской службы», то есть единого контекста, который бы объяснял каждому агенту, что происходит, какие приоритеты, какая информация нужна прямо сейчас.

Определение контекстной инженерии

Контекстная инженерия — дисциплина, которая систематически предоставляет моделям правильную информацию, правильные инструменты и правильные инструкции в правильном формате и в правильное время для выполнения конкретной задачи. В отличие от prompt engineering (где акцент на остроумных формулировках для LLM), контекстная инженерия сосредоточена на построении динамических систем, которые собирают полный и структурированный контекст для каждого вызова LLM. Это сдвиг фокуса с «как сказать» на «что и когда предоставить».

Пять методов контекстной инженерии

  1. RAG + гибридный поиск — классический RAG с векторным поиском, иногда дополненный гибридным (vector + keyword), чтобы уменьшить галлюцинации.
  2. Управление памятью — агент должен решать, что забыть: обычно забывает более старую информацию в пользу новой.
  3. Инструменты и вызов функций — делегирование выполнения специфических задач внешним инструментам, оставляя агенту фокус на рассуждении.
  4. Структурирование и упорядочивание контекста — наиболее важный контекст должен быть в начале промпта (правило «сверху вниз», рекомендованное Anthropic).
  5. Контекст на основе графов знаний — использование knowledge graph как центрального элемента для построения контекста, что делает агента более надёжным (тема демо).

Что такое knowledge graph

Knowledge graph представляет данные в виде, понятном и человеку, и машине. В отличие от таблиц SQL, где связи между данными требуют сложных JOIN и замедляют приложение, граф состоит из узлов (nodes) и отношений (relationships) — они видны сразу. Например, для фрукта «яблоко» граф покажет: яблоко → имеет тело → круглое; яблоко → является фруктом; яблоко → сладкое и т.д. Такая структура позволяет при ответе на запрос не только найти релевантный узел, но и «пройти» по связям к дополнительной необходимой информации.

Graph RAG — единственное отличие

Graph RAG — это RAG, в котором путь извлечения (retrieval path) включает knowledge graph. Разница с обычным RAG только в том, что дополнительно к векторному поиску или вместо него используется traversal по графу, что даёт возможность ответить не только на «что здесь релевантно», но и «что ещё связано с этим и должно быть включено в контекст».

Демо: настройка среды

GitHub Codespaces был недоступен из-за сбоя, поэтому всё запускалось локально. Требовались: Python, IDE, зависимости из requirements.txt. Neo4j не нужна — используется sandbox-экземпляр Graph Academy. OpenAI API-ключ был предоставлен организаторами (чтобы не было барьера). Всё остальное (переменные окружения) бралось из файла .env с предустановленными значениями для подключения к Neo4j sandbox. Для самопроверки запускался скрипт test_environment.py.

Шаг 1: извлечение схемы из текста

Сначала запускался workshop.genai.extract_schema.py. Он использует класс SchemaFromTextExtractor, который принимает LLM (ChatGPT-4) и образец текста («Neo4j is a graph database management system developed by Neo4j Inc.»). На выходе получается упрощённая схема: типы узлов, типы отношений и шаблоны (patterns). Эта схема затем используется при построении графа.

Шаг 2: построение knowledge graph

Файл knowledgegraphbuilder.py загружает PDF-файл (Wikipedia-статью о Neo4j), создаёт эмбеддинги, извлекает сущности и отношения через LLM, формирует пайплайн KnowledgeGraphBuilder. Пайплайн принимает LLM, драйвер Neo4j, embedder и путь к PDF. Запуск: python workshop.genai.kg_builder.py. Сначала выводится «resolver: number of nodes to resolve: 6, number of created nodes: 0» (нужно сначала выполнить extract_schema). После корректного порядка появляются созданные узлы и отношения.

Шаг 3: просмотр результата в Neo4j

С помощью Cypher-запроса MATCH (doc:Document)<-[:PART_OF]-(chunk:Chunk) RETURN doc, chunk можно увидеть документы и разбитые на чанки части. Затем запрос с переменной длиной пути для извлечения сущностей и связей между ними — визуализируется граф с узлами: «What is Generative AI», «Tasks from LLM», «AI models» и т.д.

Шаг 4: изменение размера чанка

По умолчанию используется дефолтный размер чанка. Чтобы модифицировать, нужно импортировать FixedSizeSplitter и передать его в пайплайн как text_splitter. После повторного запуска builder создаются новые чанки с другим размером, и их можно просмотреть теми же Cypher-запросами.

Шаг 5: схема в пайплайне

В workshop.genai.knowledgegraphbuilder_schema.py можно задать конкретные типы узлов и отношений (например, node_types = [...]). При добавлении их в пайплайн новая версия графа строится строго по этой схеме — это даёт контроль над тем, какие сущности и связи будут извлечены.

Создание AI-агента

Файл agent.py:

Расширение агента: vector cypher retriever

Чтобы добавить контекст из чанков, в агента включается VectorCypherRetriever с эмбеддингом (TextEmbedding-ADA-002). Определяется retrieval-запрос, который ищёт чанки, связанные сущности и возвращает их. Создаётся инструмент search_lesson_content, который вызывает ретривер. Затем список инструментов агента расширяется (теперь их два: get_schema и search_lessons), модифицируется системный промпт для агента.

Text-to-Cypher для естественных запросов

Инструмент TextCypherRetriever преобразует запрос пользователя на естественном языке в Cypher-запрос и выполняет его. Добавляется метод query_database, который использует этот ретривер с образцовым запросом (sample query), чтобы пользователю не нужно было знать синтаксис Cypher. Это позволяет задавать вопросы вроде «найди узел с именем ...» и получать ответ без написания Cypher.

Контекстные графы для отслеживания решений

Context Graph — это расширение, которое сохраняет всю цепочку решений агента (decision trace). В демо-примере с кредитным лимитом агент сначала рекомендовал одобрить повышение лимита для «Джессики», но после проверки history (контекстный граф) выяснилось, что три месяца назад на её счёт была попытка мошенничества — запрос на увеличение лимита шёл от злоумышленника. Граф сохраняет каждый шаг: какие данные были получены, какие выводы сделаны, почему принято решение «отклонить». Это делает агента аудируемым и объяснимым.

Вопросы и ответы (из вебинара)

📜 Transcript

en · 10 545 слов · 156 сегментов · clean

Показать текст транскрипта
hello everyone and thank you for joining today's session my name is reese 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 go along with us today, there was some setup info shared with you via email, but yeah, it's in the video description and I'll be posting a link to it very shortly. However, we are going to be running through a couple of slides at the beginning. So if you haven't done the setup instructions just yet, if you haven't completed them, there will be some time for you to do that at the beginning. So I'd recommend having a look at the resources document now, which is in the... video description and in the chat. I'll post it again for everyone very shortly. But yeah, have a look at that now if you'd like to come along with us today. However, if you want to just watch along, we are going to be recording this session and we'll be sending out the recording to everyone that's registered in the next couple of days. So make sure you register for the session. You can scan the QR code on screen now and you can also head over to datacamp.com forward slash webinars where you'll find 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. My name is Reason. I'll be your moderator and host for today's session. We're going to get started very shortly. 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 and all of the uh complementary 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'll have to struggle through this and not have his beautiful voice getting us going. But without further ado, welcome to the session. Hello, everyone. So I'm Rhys. I'm usually the back end moderator for these sessions. You may know me. You may not know me, but I'm not Richie. But you're all data scams and data champs regardless. One of the 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 end up with more 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 Naya Macklin. I will just bring you on now, Naya. Welcome. Hey, everyone. Thank you so much. Pleasure to have you. So Naya is a senior developer advocate for AI at graph database company Neo4j. Naya is a software engineer and developer relations expert. Naya helps developers use Neo4j for AI engineering. And Naya is also the CTO and founder of Afros in the AI technical community. Previously, Naya was lead developer advocate at Couchbase and the chief of staff for the Commonwealth of Massachusetts. So, yeah, without further ado, over to you, Naya. Awesome. Well, thanks so much, everyone, for coming. Excited to join you all today, and we are going to talk about effective context engineering techniques for artificial intelligence. So we're going to dive a little bit into the entire Gen AI landscape, 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. Just a key point here for our hands-on demo, we were going to be using GitHub Codespaces, but it seems like GitHub Codespaces is downed this morning. An outage. We love an outage. Instead, we're actually going to be developing locally today. If you haven't already, because you were depending on just using our enclosed environment, I'd love for you to install Python if you have not already. You'll also need to install a couple of different dependencies as well. I'll give you all the information here. But Python is the main thing as well as an IDE that we'll be working in. We're going to be talking a little bit before we get to the hands-on demo. 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, enjoy the ride. And then after I have the link where that you can do all of this work yourself, right? You can do all of this all within the enclosed environment. We're gonna be using what's called Graph Academy today. And Graph Academy is Neo4j's online learning platform where you can learn about Gen AI, Graph, Context Graph. all the newest concepts, what's happening within artificial intelligence. Hopefully GitHub code spaces will actually be up for you. I just got a ping from my colleague that said it's been a little bit unstable for them too. Let's just get started. Again, 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 right after this call. Let's get started with what we're going to be talking about today. Just a quick introduction to myself. My name is Naya Macklin. I'm a senior software engineer and a senior developer advocate here at Neo4j. My research focuses a lot on graph theory, graph algorithms, and trying to help people understand context engineering as this black box phenomenon. We're trying to break open that black box in order to decipher, trace, and explain how artificial intelligence systems work. So we don't have to go all the way 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 the 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 courting 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 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 disguise So the same actually goes for agents. So when they work in swarms without structure and orchestration, it actually becomes madness. And so we often find that this statistic rings true for a lot of 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 white paper. 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. This is the key part. They're flying around in swarms without air traffic control, which then says, okay, one plane should land right now. This other plane is having an emergency, so we need this gate open with medical staff ready. All of 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. And I wish we were live in person right now because I would call on you to say, OK, 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 with the right instructions that an agent will need in the correct format at the correct time. OK, 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 that assemble complete and structured context for each LLM invocation. So this is a shift in focus from 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. But context engineering has all of these different facets. So we have RAG plus hybrid search, which, you know, as in a previous life, I was a teacher. So I always like to have little images to help those who learn more visually, plus those who learn with more auditory uh help so um i have um rag plus hybrid 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 the 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 get your agent to 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? But with your AI agents, with memory management, you are deciding, okay, like what is okay to forget? Should I forget the older stuff, right? Should I forget the newer stuff? And you'll see that usually with your models, they'll opt for forgetting older stuff. to to promote the newer context that you're providing. But that's another another aspect of context engineering. And then we have tools and function calling. Right. I have this represented as MacGyver. For those of us who are older, we remember MacGyver would always have any tool. He was the guy with the plan. Right. He would always have a tool to get a job done. 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. instead of having your agent do a lot of that heavy lifting, you can actually have your agent just call a tool so that your agent can focus on the things that is actually good at, which is reasoning. Right. And then up above that, I have structuring and ordering context. We all know, and Anthropic has lots of guidelines on this, but we all know that when we have 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 prompt, right? It's just gonna forget it. You need to put it at the top. So that structuring and ordering of the context is actually really important. And then lastly, what we're gonna 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 augmenting your context or making your context much more, making your context and your agents much more trustworthy. So we're gonna 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 represents 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 simple. But usually with naive reg or vector-based reg, you'll have this vector-based representation of an apple, right? whole bunch of numbers, whole bunch of like really mess, right? Because you're like, I have not memorized 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 vector based view and then a knowledge graph. We have for the knowledge graph view, you actually can see these what we call nodes, which are the circles and then edges, which is the relationships of 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 understanding. for humans and also for machines. And just quickly, this is another representation of how people mostly use data themselves now. 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 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, that's not normal within a SQL table. You'd have to do complex joins in order to show and showcase the relationships. when you're doing those complex joins a trade-off is that it slows down your application right it pulls it builds a lot of bulk into your application and so if you take that same data and showcase it as a knowledge graph within your 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 So in other words, if you're using a knowledge graph when building out agents, a knowledge graph can answer not just what is relevant to this query text, but what else is connected to this topic that's necessary for me to include in this query. 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. Again, This was super fast, but I wanted folks to be able to see. And normally this demo or this demo of 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 what's great about Graph Academy is that it provides literally all the information that you need. You don't need me to walk you through this. You can go on to Graph Academy 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 pushed had um pushed this information to you all um but you'll need this dev so you'll need to go to dev.neo4j.com workshop Gen AI dash Gen AI dash notes. And that is going to bring up this. document right here. I see a few of you are already in here. So this document here will have a link to this document as well. 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 to get set up with Graph Academy. And in Graph Academy, that's where we're going to be doing all of this hands-on work. So you'll also see that this is the workshop that we're going to get into. I have that here as well. I was already getting started with with you all just because of the GitHub code spaces thing. So I wanted to make sure that we can run this all locally, but you'll see that. And then yes, everyone you see in open AI API key, right? Yes. That is for you to use during this workshop. Okay. Super exciting. Don't worry. We're going to turn this off as soon as this workshop's done, unfortunately. But we wanted to make sure that this, you know, paying and having an open AI API key would not be a barrier to you, to you actually. enjoying and working with this workshop. We have that API key for you. Then if you just want to access a Neo4j database directly, we have instructions on how to do that as well. But this is a document that you'll want to get started with. We have the OpenAI 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 Neo4j 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've mentioned, this course is normally a three-hour course. Actually, let me reset myself. It says that I've already done... uh i've already done 16 of the progress and i actually did the whole thing many times um but i just want 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's sake because you know we don't have a lot of time at all but let's dive in so we have continued course we're starting at the top generative ai so i'm gonna much skip a lot of this what is gender day by I conversation because I think many of us have already been in the space and I don't want to 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 language 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 do you evaluate whether your agent had actually did what it was supposed to do? And then what we had mentioned about hallucinations. So we're just going to skip all over this for time's 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 for graph RAG, 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 present review, but the retrieval path includes a knowledge graph. So that's the only difference that you need to know about how GraphRag works. We have an architecture as well, where you can understand how to do information retrieval, how to get the response. But again, I want to get us building. So if you, 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 talk about the RAG process, grounding, retrievers, which we're hopefully going to have time to do today, and then different data sources as well. So GraphRAG, again, is where the retrieval path in RAG involves a knowledge graph. Perfect. Lots of different benefits to GraphRAG as well. we can learn about full text search text to cypher cypher 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 representation of data and how it's related. We see here that we have Neo4j as a company and then we have, for example, this is a node and then we have these relationships. um connected to another node so emmo was our is our founder still is our founder um founder and ceo he's the founder of neo4j 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 shown within a knowledge graph and we already went through that a little bit cool cool cool let's dive in Great. Now, we are going to be working today with the GraphRag for Python package. This is where we're going to get started. We have a repository here. It's a GitHub repository. It's already been created for this course, and it contains all the starter code that we're looking for. We can either use GitHub Codespaces, but I think it's actually down right now. Normally, we would do this all within an isolated environment, GitHub Codespaces, but it's actually not working. do is we're going to run this all locally on our machine. 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, but we'll do that after this entire talk. But if you do have Python already installed, then let's continue. We'll want to, of course, set up a virtual environment as well that we'll be working in because you don't want to just install Python or install pip. everywhere because it'll mess up your other Python projects if you've already developed in Python. But initially, what I want you all to do is to clone this repository here. So github.com slash Neo4j dash Graph Academy slash workshop that Jenny and you can see the actual workshop and all the information you can either press copy the URL here or just straight from straight from here. And I already have a a VS code IDE up where I have logged into a virtual environment and I have already run the clone. I already have the project up. You're going to just run that clone, that git clone within a terminal and it will install the entire project. You're going to need to then run pip. install dash r requirements.txt 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 neo4j database um for this um where this is all going to be working within a sandbox instance and again i think my colleague had mentioned there's going to be questions i'm going to save those questions for 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 dot m dot example so this is just an example dot m what you're going to do is create another oh 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 dot m file we've replaced the open ai 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 ai api key and we're going to keep everything else that's the same from graph academy okay Awesome. Let's keep those all the same and then we're going to add the OpenAI API key and then we're going to test our environment. I had already done this. What I need to do is run Python 3 within the folder, Python 3 test underscore environment.py and then you should get an okay if your environment is actually correctly set up. Once we've done that, Then we can continue. Perfect. Let's go. I love that my... It's okay. We're flexible, everyone. My screen share keeps ending, but that's okay. We're flexible. Now let's get into actually building out this. Again, once we're constructing knowledge graphs, we typically are following these steps. We're gathering data, chunking data, and then vectorizing 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, keep listening to me, and I'm following exactly what's in the Graph Academy course. Okay, so we can know how to vectorize our data, chunk our data, 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 Neo4j 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 entities and relationships from using the LLM. 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.gen.ai extract schema. So we're going to go over here. We're going to go to workshop.gen.ai. Then we're going to go to the extract schema 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 code. Awesome. So we'll see a full 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 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 ChatTBT4, which also is going to be deprecated, I think, within the next couple weeks. So we're going to... uh this entire demo is going to be changed over to the newer models just keep that in mind but the whole point of this is that given the text here on line 17 um neo4j is a graph database management system developed by neo4j 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. 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 knowledgegraphbuilder.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? And we'll go over here. It's going to load a single PDF file. We have this PDF file at the bottom. But initially, we're going to create a connection to the graph database. And we're going to instantiate our LLM model as well. 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. So KG is just knowledge graph pipeline where we have the LLM, the driver, the Neo4j 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 the pipeline to create the graph from a single PDF. from a single PDF. Let's try and run this. We're looking for Python 3 and this file is called kg underscore builder dot py. Did I spell that wrong? No such file. Oh, workshop.genai. Let's do Python 3 and then we need workshop.genai slash Awesome. We're going to run the program. It'll happen eventually. It's going to take its sweet time. KG underscore builder dot PY. Once we run the program, the pipeline is going to process that PDF document and then create a graph within Neo4j. Cool. Nice. We see the resolver, a number of nodes to resolve, six, and then number of created nodes, zero. So we have, I wonder why I did zero. I think I might have needed to run the extract schema first. So let's do this one more time. We're actually going to extract the schema first. Schema.py. Of course, of course. You have to get your files. The file name's correct. Workshop.genai. This always happens with local development, everyone. If we would have been in our nice system, everything would have worked out. Perfect. Apologies, you have to run the extract schema first, which is going to get our schema. We can see all of these different types, the different property names, and then after that, then we would run the Knowledge Graph Builder, PY, and we'll see how many actual nodes and relationships came out of running that. So this is going to take a little bit, 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 after that point, again, this is going to run for a little while, 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 a number of created nodes. That's perfectly fine. We're going to debug this later because we don't have a lot of time, but that's okay. Number of nodes to resolve. But let's keep going within the space. So if we jump back to Graph Academy, actually, then we can see a jumping back to Graph Academy and everyone I'm in section knowledge graph construction. But we can run this cipher query here. So you can run this cipher query and it is going to, 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 have from the Wikipedia article, right? What is generative AI specifically? 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 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 what is generative AI, tasks from LLM, 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. And then we can, let's keep going. So text splitter 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. So we would then go in and modify the workshop.gen.ai knowledgegraphbuilder.py. So we have our knowledgegraphbuilder.py here. knowledgegraphbuilder.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 fixed 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 auto save 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 text splitter at the end of from pbs So tech splitter underscore tech splitter equals tech splitter. Okay. Then after that, we will then run this locally. Where is my terminal? I don't want a new terminal. I want my terminal. Okay. There we go. Great. All right. I don't need to do 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 is going, we could jump back over to Graph Academy and we can actually view the documents and the associated chunks using the following Cypher 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 Cypher query here. there's a lot of syntax here that you might be like what is cipher how does it work it's hard to um go from zero 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 Cypher for you all. However, if you did want to learn more about Cypher and how to build out these queries, we can go to graphacademy.com and learn more about how to build out, how to work with Cypher. Cypher, again, is our query language that we use to talk to graphs, really. We'll view the entities then after using the following Cypher query. We're going to run this Cypher query here. All right, perfect. Now we see all of these different entities, chunks, etc. We can experiment with different chunk sizes. As we can see here within Cypher, you can edit all of this information to experiment. We have chunk, which is going to be to third degree with entities and then match all of those chunks. I'm not going to dive more into those, but I'm going to give you all a primer into all of the things that you can do within graph and then building out your graph. So again, this is about a three-hour workshop that we would normally do, but we're trying to do all within this tiny amount of time. But if we jump to the next section, then we can add the schema to the pipeline. We would 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. You can define specific nodes and edges, nodes and relationship types. We'll go down, we'll scroll down. So we have these different node types here. And then we see that we're going to add that node type to our schema and then run it, right? I don't want a completely new terminal, but that's fine. Yeah, let's go back to the old one. Beautiful. All right. So this is knowledge graph builder underscore, builder underscore schema dot py. I'm going to run that. And once that runs, there's lots of different ways to, we're just editing the schema, right? We're recreating a knowledge graph based on all of this information. Love it, love it, love it. So eventually that will work. great 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 is incredible 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 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 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, and we're going to skip down. And I want to get into retrieval and agents, retrieval and agent work. So let's jump down 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 actions in the real world. and then we have different components agents tools decision making 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. i want you all to like to try and figure out okay so what's this agent's 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 Neo4j instance specifically. But first, we're going to instantiate a chat model for OpenAI. Then we're going to connect to the Neo4j database and then define the GetGraphDatabase schema. We have this GetGraphDatabase schema. Then after that, we will then create the reasoning and acting agent using models and different tools. Perfect models and tools. Then once we run the agent, we're going to pass that query in and then stream out a response. Let's try to run this agent right now. We need three and we're looking for workshop-genai slash agent.py. Let's workshop.genai. Of course. Agent.py. Cool. All right. So we can see lots of different things happening, right? We have our knowledge craft builder happening. We've seen some constraints. Let's go and scroll all the way up to the top. And so this is showing us different properties, different relationships that are happening within. And of course, like it's hard to visualize because it's all of this text within a. uh within a terminal type of 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 gonna 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 gonna copy Run the application. We see for step in agent.stream, we have a message, and then that message is going to have role, user, and content, and then the query that we had just put, and then it's going to stream out those values and then step across those messages. Then it says pretty print, but it didn't pretty print here because this is not pretty. But it should have pretty printed. Then we can experiment with this query as well. Let's move on as well. Before we finish everything, because this is not necessarily finished, but we're just trying to build out our agents. We'll go back to the Neo4j Graph Academy course. We'll see that we can modify our agent to then create a vector cipher retriever that uses a chunk. for the vector index, and then defining new tools, and then adding new tools as well. 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 embedder here using text embedding ADA-002. And then we would create a retrieval dash query, right, that the retrieval will use to add the additional context. So we see that this is a retrieval query, which is going to retrieve all of the different chunks, all the different nodes, and all the different entities from our results. and then return them as items in values and then we see here and then we're going to format it as associated entities. When I said entities, we have entities and relationships, entities are the nodes, just representation of all of that data. After that, we create a vector cipher retriever using a chunk embedding index with a Neo4j driver. That's just a way to interact with your Neo4j graph database and then the embedder itself. You can define the tool function to search out the lesson content for that specific retriever and then update the tools and modify the query. We have the completed code there as well. Lastly, we would be able to query the database and then officially create the entire agent as well. Maybe we have a little bit of time just to go through what these steps would do. Querying the entire database, we'd use a tool called TextDecipher Retriever to retrieve and to convert those user queries into cipher statements. It's like TextDecipher takes a natural language query and turns that 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 uh llm knowledge graph builder tool which takes in um 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 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 what a human readable 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. We'd have the tool, which is query database, and then we've defined what that tool actually is. It's a catch-all specifically here. It's a catch-all tool to get answers for specific questions about lesson content. And then we would return the result from there. You can update whatever tools that you want to use, get schema, search lessons, or query the database. You can include whatever tools that you're looking for and then modify the query as well. Then we'd run the agent and see the output. Then the last piece of the puzzle here, because we have a little bit of time. This is an optional challenge. For this challenge, you're going to apply what you've learned with all of this. When you go through the entire course yourself, um you'd apply what you've learned from this and try to create an agent that creates a custom set of tools so you can do the the um lesson knowledge graph, which is this entire lesson. Use your own knowledge graph as well. You can augment to use your own knowledge graph still within Neo4j sandbox database. So you don't have to worry about any hosting problems or anything like that with your data, but from either the previous challenge or your own information. And then creating an agent using the example code from this workshop to... 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 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 actually go through this course because it's actually very very detailed on taking you from zero to 100. right on building was graph rag was 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 is that 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 you know making whatever decisions that it makes 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 um uh area then um and you're building an agent that um is asking oh you know should we provide a credit line increase to jessica right for example 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 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 nother 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 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 4j we have a blog post on how to build out context graphs um done by my colleague william lyon 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 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. So I do want to save some time for questions, but you can see for this, if we were to, yeah, here, like if we were to approve a credit limit increase for this person named Jessica, 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 is because she actually has a lot of fraud that has happened on her account where someone was trying to get access to her data not her data her money really um and they were trying to um trying to ask you know for this credit line increase so that they can take more money out of her account and we wouldn't have known that unless we had been able to do this decision trace and all and understand that actually there was a big fraud piece that happened three months ago we should not approve this credit line increase to protect jessica so this isn't jessica this is a fraudulent player right now so excellent um excellent demo um excellent uh resource to learn more about how context graphs make your agents more successful more trustworthy um and um uh more auditable as well so i'd highly recommend that you um check out the context graphs demo um here which shows you everything live so yes i'll let this run and then um if we have any questions happy to take them and hello from texas It's a super cool demo. Yeah, I appreciate y'all so much. Thank you. Perfect. But yeah, you can see all the decisions that are made. I'm not going to dive into this demo because it's a completely separate one. But if you have any questions, either based on this, and then we see the recommendation to reject. So yeah. Perfect. Cool. Thank you so much, Naya. That was incredible. And yeah, hopefully everyone managed to do some part of that. again if not don't worry uh you can you can catch up with the recording make sure you register for uh the session so we can send you the recording as well so we've got a couple of questions from the audience if you have any questions uh everyone please do ask us them uh now and we'll get to them very shortly in the meantime though i've got a couple of my own questions so uh is it possible to provide too much context to an agent good question okay so Yes. And there's reasons for that. There's reasons for that. So I would say that there's ways to provide too much context to an agent because it would be based on how you're providing that context. Right. So like I had mentioned in the slides like. there's different ways that you can do context engineering in general, right? Either by structuring and ordering the context within the prompts that you're providing to your agent or uh using knowledge graph augments in context but there's lots of different ways that you can provide context and when you do if you provide quote unquote too much context or the wrong context you can um result in what's called like so for example if we're doing normal rag right normal rag there's ways to provide context within the normal rag process um there's this thing called context poisoning which happens when you are trying to base your agent on which normally works, normal vector-based RAG. However, sometimes when you're using normal vector-based RAG, your system is going to provide off-target information. You're actually looking for this information right here, but it's providing off-target information. It's just slightly adjacent to the thing that you need, but it's actually not what you need. And so what happens there is this concept called context poisoning, where you are getting um where you're providing context but it's actually giving you back the wrong thing um so yes i would say that there's ways that you can provide too much context um i even seen people when they're developing agents and when they're just using agents they're just trying to like overload the context window and just provide as much information you know give feed it the world right like the agent will be fine it's going to figure itself out but actually no this is why my entire um definition of doing proper context and is providing the right information and the right structure at the right time. That at the right time is also key. I think that that's really important when we're thinking about building out context. Got it. We've got a few questions from the audience. First one is from someone on LinkedIn whose name I'm not allowed to show. Why is ordering associated with memory? Why is ordering associated with memory? That's a good question. i would say that structuring and ordering context one could think of them as completely separate um but i would say that they're part of the same field and that's because like if you're ordering your context um within and let me see if i can bring it up yeah thank you oh you're the best thank you yeah perfect so when you're um ordering out your context right you have to and it's a part of this like thinking about memory management piece they're not the same they're not the same concepts but they work with each other right so the structure and the the order of your context is going to matter because it's uh your agent is then still thinking about like okay what is the priority here And especially when you're thinking about memory as well, it's very similar to memory management. We're thinking about like, what do I, what can I forget? So I'd say that they're not the entire same concept, but they are related to each other. Got it. We had a question from Glenn on YouTube. He said, any thoughts on the rig framework in Rust for building out AI agents or vector stores? I'm not familiar with the rig framework. Usually I'm a JavaScript and Python developer, so I haven't worked in Rust. But I'm going to do some research on the rig framework in Rust and then get back to you on that. You're on LinkedIn, so I think that I can. Yeah. Recommendation there would be follow Naira on LinkedIn and then for an update very soon. Then the final question that we've got from the audience is from Dan. Dan says, do you mean to save the reasoning output as a node? in your graph so this was a little bit later on oh do you mean to save the reasoning output as a node you can you can save it um there's also so yeah i think that that that actually be an intuitive way to um uh to display and show that information is by showing it as a particular node on your graph um so that it's actually stored within your graph and you're able to query that information or or rerun that information later and actually have that that um uh that output for later queries so it doesn't have to do a big traversal yet again so yeah that's a definitely a way to do it cool and then i'll sneak in one final question from omar so omar asks What does context at the right time mean? Good question. Good question. What does context at the right time mean? So when you're building out your agent, again, looking at all these different ways that you are providing context, context at the right time is focused on when you and thank you for showing the for showing the slides, but it's focused on like when is your. Let me think of a good answer to this. I'd say it's like when you're developing out your agent, where are you providing that context? I'll say like, where are you providing that context? and when are you providing it as well um so if you're building your agent and you're thinking about like providing your uh context either in the prompt window or um uh as you are building out your knowledge graph for example understanding when to include all this context so that you're not just overloading your context um i think it's something that's important to focus on but i'm happy to um to have more conversation about it either offline Perfect. Cool. So I think that's all the questions that we've got from the audience. So actually, we've had Dan follow up regarding. So he says, regarding my earlier question, context was about recording the agent's decision for auditing. Might you capture the model's output verbatim, including the reason? The context was about recording the agent's decision for auditing. Might you capture the model's output verbatim, including the reason? Yes, within the knowledge graph, yes. But yeah, happy to get into it. Cool. Brilliant. So that's it for today. However, before we shut up shop today, we do have a few more things coming up if you are interested. We have our AI-powered Python series that's coming up very soon if you head over to DataCamp. uh we have a big banner on the top of the website uh allowing you to sign up for that so yeah if you're a relative python beginner but you want to uh infuse your journey with ai and learn it better and yeah do it probably a little bit easier then head over there and we also on april 1st have our flagship virtual conference radar so please do check that out You can just search for DataCamp Radar and you'll find the link for that. Make sure you sign up for those. That one's on April 1st. And then AI Powered Python is a similar time as well. So make sure you check those out. And obviously, please do also check out datacamp.com forward slash webinars where you'll find this session as well as all of our future sessions as well. So yeah, Naya, thank you so much for taking us through that today. I certainly learned a lot and hopefully everyone else did today. We'll be back again on Wednesday where we're talking through. dashboards uh so check out that session and we're also going to be looking at healthcare dashboards uh soon as well so please do check out datacamp.com forward slash webinars uh so yeah thank you to everyone who tuned in now thank you so much for uh taking us through things today and yeah hopefully we'll see you again soon

⚙️ Pipeline jobs

StageStatusAtt.UpdatedError
download done 1/3 2026-07-20 11:57:05
transcribe done 1/3 2026-07-20 11:57:48
summarize done 1/3 2026-07-20 11:58:31
embed done 1/3 2026-07-20 11:58:33

📄 Описание YouTube

Показать
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.