Full transcript of “Agentic AI With Langgraph And MCP Crash Course-Part 1”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 Hello guys. So welcome to this amazing crash course on building agentic AI application with the help of Langraph. This entire crash course has been divided into three important parts and each and every part will be somewhere around 2 to three hours of videos. Right. And here you can basically see what in which way we are going to cover all the topics and uh where we are going to aim once we reach to the part three. Okay. So in the part one you'll be able to see that we will be covering various fundamental techniques which are really really important in order to build agentic AI application. some of the important topics like how to build a chatbot, how to integrate tools, how to integrate multiple tools in a chatbot, you know, how to add memory, how to add human in the loop like human feedbacks
0:47 when you're executing the entire graph state, how to use different streaming technique, how to probably go ahead and use MCP, how to build MCP completely from scratch, right? So this part also we'll be discussing about along with this u there will be various topics like states what are graphs nodes edges how do you go ahead and use this with the help of graph API you know so all these things will be covered in part one so part one will be approximately around 2 hour 50 minutes maybe okay but I'm just making an approximate suggestion along with that once we complete this then we go to the part two in the part two cover advanced langraph concept. Now here we are going to focus on various kind of
1:34 workflows and agents. Here is the topic where we will be developing applications where agents will be communicating with other agents. Right? And why they will be communicating to solve a complex workflow. Okay. Solve a complex workflow. Right? Along with this, we will try to see how we'll be handling the multistate management even in multi- aents. Then we'll also introduce you to how to directly use functional API instead of just directly going through graph APIs itself. And then I will also be showing you how you can debug and monitor them in the langraph studio. Okay, langraph studio and for this we will also be using lang. So this all fundamentals is put up in
2:20 the advanced part because uh this will be like one step towards developing some amazing production grade application and finally this part two will also be somewhere around 2 hours of video and then we have in part three where we'll focus on building completely end to end projects we'll focus on LMOS pipeline we'll focus on deployment techniques and recently I have also explored all the evaluation techniques metrics specifically in LLM and how you can use along with langraph uh and some open- source tools right like MLflow how you can use AWS to track all that kind of metrics along with that how you can use graphana to probably display all those particular reports that is where we will be moving in the part three right we'll
3:06 also be using hugging face spaces to do the deployment so this is just a tentative plan in order to cover lang graph crash course and these all are like long recorded videos so I definitely definitely require your entire support. Yes, now part one is ready. You can go ahead and watch this entire video and make sure that you also download the material from the description and keep on practicing and definitely do share it in various platforms like LinkedIn and all. I definitely want to see how your learning is. Definitely do tag me in LinkedIn, Twitter, wherever you can. Right? So yes, let's go ahead and enjoy this particular session. So guys, now let's go ahead and build a basic chatbot using Langraph. So this is my entire empty folder. So this will be my project workspace. Uh from this I will go ahead and open my command prompt. So let's go
3:52 ahead and open my command prompt. Um as I said that this is my uh working directory uh with respect to my project workspace. I will just go ahead and open my VS code cuz I'm going to use VS code for my coding purpose. Uh once I open my VS code uh this is how my VS code looks like. Um you know whenever we go ahead and start any kind of projects or you build any applications right it is necessary that you start creating an environment. Um most of my videos I've actually shown how to create environments with the help of but in this particular video we are going to use something called as UV package manager. Okay. Yes, you can also use cond. Uh but if you don't know about UV package manager, it is a really fast, extremely fast Python package and
4:38 project manager and it is completely written in Rust. Since it is written in Rust, it is very very fast. So you can probably compare over here from UV to poetry to pdm and pipsync. This has the least time. Uh that means that whenever you're trying to create an environment or do any kind of installation of the packages, that happens really really fast. Okay, some of the highlights that you can see over here. It is 10 to 100 times faster than pip. Uh it is a single tool to replace pip, pip tools, pipex, poetry, pyenv, twine, virtually envir. Uh it provides comprehensive project management and universal lock file. It installs and manages different kind of python versions also. You can do it in the same project itself. Right? And uh to start with the installation, if you are in Mac OS or Linux from the
5:24 terminal, you just need to go ahead and copy this particular command and execute it. If you are on Windows, go and open your PowerShell, copy this particular command and uh paste it over there. And if you're using pi, uh just go ahead and write pip install uv. Okay. Uh once that is done, your uh you know the entire project repository will be initialized. Okay. So first of all, what I'm actually going to do is that I'll just go ahead and open my terminal. Now inside this terminal I will open my command prompt. I've already done the installation of UV package manager. So I will just go ahead and quickly initialize my uh project workspace. In order to initialize all I have to do is that I have to write uv init. Okay. As soon as I write u init will happen in the project workspace. Okay. So here you can see in the project
6:10 workspace there are some files that has got created like get ignore python version main.py pipro.2ml 2 mm uh and and if you probably go ahead and see in Python version which Python version you have actually created it is nothing but 3.13 then you also have this main py so that you can start the program execution directly from here then you have this pi project toml wherein you have the project brief information uh you can change the version you can add your own description and all um here you can see that it is requiring a python of minimum 3.13 okay this dependency is right now empty because we have not installed any kind of packages is now what I'm actually going to do is that I'm going to go ahead and create my requirement.txt. Now inside my requirement.txt I will go ahead and install some of the libraries.
6:55 Let's say lang graph lang chain. Then along with this I will also go ahead and use my lang. Okay. Uh all our libraries that will be specifically useful. Uh and I'll tell you as we go ahead lang and lang chain we're going to use various functionalities in order to build generative AI applications. chat bots along with that agent AI applications also uh lang is basically used for uh tracking and evaluation of your applica applications you know directly in the langraph cloud so these are the basic libraries that we're going to use now since I have already initialized this working space now the next thing is that I need to go ahead and create my virtual environment so quickly I will go ahead and write uv venv um with the help of this command you'll be able to create a
7:41 virtual environment this venv is nothing but your uh virtual environment name. Okay. So once I go ahead and write uvnv here you can see that it has got created with the help of this particular version that is 3.13.2. The virtual environment is at this location.v over here. Now in order to activate the environment I'll just go ahead and copy this quickly. I will paste it over here. Okay. So once I activate this here you can clearly see that hey uh my my uh environment has got activated. Okay. So here agentic lang graph has got activated. Now this is perfect till here everything looks good. Now the next step is that we'll go ahead and do the installation of all the libraries. So in order to do it u like if you're using pip it is like pip
8:26 install minus r requirement.xt. But here we are going to write uv minus r requirement.xt. Okay. So once you do this installation here you can quickly see that the installation has been completed. And now if you go ahead and open this particular file. All the libraries that has got installed will be visible over here. Okay. Now as I said uh this is my first tutorial. I'll go ahead and just write one folder name one and I'll say hey uh basic chatbot. Okay. And we'll we'll just learn some of the basic stuffs over here. Okay. Now with respect to this particular basic chatbot uh here we are going to go ahead and create our um you know applications. So we'll go ahead and create our basic chatbot itself. Um we'll just go ahead
9:12 and open my one file. Let's say I'll go ahead and write basic chatbot ipynb. Okay. So as soon as I open this basic chatbot ipynb, it will tell me to select a kernel. I will go ahead and select a kernel. And since I'm using Jupyter notebook for the initial stages, um I also have to go ahead and add one more library UV add ipi kernel. Okay. So follow the steps step by step like you have to just follow this steps as we go ahead because IPI kernel will be required in order to run anything in the Jupyter notebook. Okay. Now once this is done I will start writing my code over here. Okay. U now with respect to the code let's check whether this is working or not. Okay. It should give an error because oneplus exclamation is something happening over here. Here you can see it
9:58 is connecting to the kernel agentic. Okay. So yeah invalid syntax. Now if I go ahead and write 1 + 1, it is working fine. Perfect. Now here as I said um let me just quickly go ahead and write here we are going to build a basic basic chatbot. Okay. Now building a basic chatbot uh um you know this this chatbot is like a basic chatbot that basically means uh and here whenever I'm talking with respect to lang graph okay with lang graph I'll go ahead and write that here we are going to use the graph API functionality okay there is one more API which is called as functional API as we go ahead we'll also try to learn about it but what I felt is that the most efficient way of learning lang
10:43 graph is specifically using this graph API Okay. Okay. Um, so let's start with this and uh let me go ahead and write some information you know how you should actually go ahead and start and all the things you know and what we are basically going to develop. Okay. So guys, now let's go ahead and build a basic chatbot with the help of Langraph. Now before we go ahead, we need to understand some of the important components of Langraph so that you will be able to understand how to build a basic chatbot. So let's go ahead and talk about the components of Langraph. There are three important components of lang graph. Number one edge, number two nodes and number three which is called as
11:29 state right now what are these right? What are these components? So in order to explain you I would like to probably take a use case. Okay let's say that and I have I had this use case a long time and I solved it. You know as you all know that I also upload a lot of YouTube videos right YouTube videos. Now what I wanted was that I as soon as I upload a YouTube video I should be able to convert or create a blog out of it. Okay. So this is a kind of task that I really wanted to do. Now in considering this particular task if we consider this workflow how this workflow needs to be executed. You know let's understand this. If I want to solve this task, the first thing is that from my YouTube videos,
12:17 I have to take out my transcript. Okay, transcript, right? So from this YouTube videos, I want to first of all take out the transcript. Then I will use this transcript. Transcript. And with the help of this particular transcript since I need to start writing my blog I will go ahead and create the title of the blog. Okay. And in third step I want to take both title and transcript and we will go ahead and create the content of the blog. Right? So if I want to solve this use case, you know, this will
13:02 be my workflow to solve this use case. You know, first of all, I need to go ahead and take out the transcript from the YouTube video. Then I need to go ahead and based on the transcript, we need to go ahead and generate a title. And then based on the title and transcript, we need to generate a content. Right now I am alone uploading the videos and it is not possible that I also go ahead and create the blog out of it because it will take more of time. But since when like LLMs right now is the buzz word, right? We definitely have LLMs. Now with respect to LLMs, you know that these are really really good at content generation. It is very very good at content generation. Right? Now whenever we talk about content generation that basically means LLM it can take an input. Let's
13:48 say if I say hey what is machine learning? It'll be able to generate what is exactly machine learning. Right? Now can we use LLM in order to solve this particular workflow with the help of langraph. Now in order to solve this problem what I will be doing is that I will follow some kind of graph structure. Okay. And yes in langraph if you want to solve this kind of workflows. There are two ways. Okay. One is directly using graph API. graph API and second one is directly by using functional API functional API but according to my experience I feel graph API is the most
14:33 easiest and most best way yes if you have lot of expertise with respect to the graph API you can directly go ahead and use the functional API the difference between them we will get to know as we go ahead okay so first of all what we'll do in order to solve this complex workflow I will go ahead and create some kind of graphs. Okay. And this graph will show that how my flow of execution will happen. Okay. So let's say that I have this node I have one more node. Okay. So these are my two nodes. As I said the components of langraph are edges, nodes and state. Okay. So initially let's say we are going to go ahead and start over here. Okay. So here I will be having my start
15:19 node. Okay. In this start node we give our input right. Let's say in this particular case in my use case obviously I need to give some kind of input. Now what input I will give? I will give my YouTube URL. Okay let's say this is my input YouTube URL. Then it goes to this phase from start it goes to this node. This node should be responsible in taking out the transcript from my YouTube video. So here I can go ahead and write, hey, this is my transcript. Okay, this is my transcript generator. Now, how do I go ahead and generate the transcript in Langchain?
16:06 In Langchain, we have some third party libraries. No, I think there is something like YT loader or what it does is that we give our input videos of the YouTube and output we will be able to get the transcript. So here output of this particular node should be that we should be able to get a transcript. Okay. Now understand one thing over here. So what is this? This is nothing but this is my node. What is this? This is nothing but this is my edge. Right? So this is nothing but edge. Edge main fundamental is that the flow of information should go from here to here or node to node. Right? So this is also my edge.
16:52 Right? Now whenever we talk about nodes, right? As soon as you create a node, we also have to create a node implementation, right? Some functionality with respect to this particular node. Like what does this node actually do? Now in this particular case this node functionality should be that it should take a YouTube URL and it should generate a transcript. Okay. And the output of this node should be this transcript. Okay. Now in my workflow I have completed this YT video to transcript by that node. Now based on this transcript I should be generating the title. So what this node will be doing this is nothing but this will be title generator. And here the input will be transcript
17:39 right. The input will be transcript. And this will be my next node. And this node functionality should be that it should take this transcript and it should generate the title. Right? This is what is my functionality. Very simple functionality. Right? Now after this the output that we're going to give right should be my title. Along with the title I also want to give my transcript and we go to the next step. What is the next step over here which is nothing but content generation. So my third node
18:25 that you'll be able to see over here is nothing but it is content generator content generator right. So this will again be my edge and this node will have a functionality which will take this information title and transcript and it will generate content right and finally you go to the next step which is end in the end you get the output right now see now you may be thinking how do we generate transcript to title. Now if you have a fundamental idea of LLM. So here in my title generator I
19:13 will have an LLM along with one prompt and then when we give this input of transcript it should be able to generate the output. Right? Similarly for this content generator which is the node. If I give the title and transcript here again I will be having some kind of LLM plus some prompt which will be able to generate the content. Here we give the input as transcript and we get the output over here. Right? And finally all this output is combined and we get display it over here. Right? So this is an example of a workflow and this is entirely with the help of graph API. We
19:58 will be able to see the graph uh we'll be able to see the execution. We'll be able to see the output. Okay. Now coming to this right I have told you already about edges and nodes right now where does state come into existence. Okay. Now see based on this particular use case state we can define something right. So here this state will have some values or some variables we can define some variables and that variables will like that variables can be accessed by any of this node in this particular graph. Okay. So let's say for this particular use case you know that I require transcript. So I will go ahead and create a transcript variable.
20:45 As soon as this node is executed the output will be saved in this variable. Okay. So let me just go ahead and write it down over here. So state means what right? Whenever we define any kind of state our main aim is that whatever variables we define over here right. So let's say one of the variable I want to define is transcript because as soon as I execute this node my transcript will get generated right and this transcript will also be required in my third node. So what I can do when I create this state right this state will have one variable which will have the information about the transcript maintained. Okay. Similarly title is my third second output that I really want because here
21:31 in this node I want to go ahead and save the title right. So here title information will be saved and then here you have content. So let's say that if I go ahead and create this three variables as soon as we generate those we can save in this right and the advantages of saving that values inside this state will be that inside this entire graph every node or any of these node will be able to access this variable. Okay. So that is the importance of state. Okay. And this entire graph we basically say it as state graph. So that is the reason we say it as state graph because it is able to maintain the context of the state at every node. Yes, don't get confused with external memory or memory.
22:17 Right? So memory can also be used over here and that part we'll discuss in the later stages. But here we want to focus more on the state graph. State it is able to maintain the state within the specific nodes. Now I hope you got a clear idea about the components of lang graph. Now what we'll do? We will build a basic chatbot. In this basic chatbot what we'll do I will be having a start. From this start I will create one node. Let's say this particular node is nothing but chatbot. And from this we will go ahead and end it. Now this chatbot will be integrated with some kind of LLM press prompt and the work is take the input and give the output. Right? So this is the basic
23:06 chatbot what we are going to build and as we go ahead you know we will go ahead and add tools external tools. We will go ahead and see that how we can integrate this external tools along with the chatbot. Then as we go ahead we'll again discuss about react agent. Okay. So react agent is something more amazing with respect to the tools. I know there are so many topics that we have discussed but let's now focus on understanding how to build this basic chatbot. So for this I will again go back to my code and now you have understood what is state graph. You have understood what exactly is nodes and what exactly is edges. Okay. Now step by step we will go ahead and do this. As usual, what we are going to do is that first of all, before starting building a
23:52 chatbot using uh uh state graph or graph APIs, you know, first of all, we will go ahead and import some important libraries. Okay. So, one important library is something called as from typing import annotated. I'll talk about annotated. What exactly annotated is? It is just to add context specific metadata to a type. Okay. uh it is better that I show you an example in order to make you understand one more important library that I'm going to use is typing extension import type okay now along with this since you know that every graph starts with a start node and ends with the end node okay so for this I will go ahead and write from lang graph dot graph
24:38 import state graph since we need to go ahead and also create a state graph state graph will be the entire graph right entire graph that you have seen over here. If I want to represent this entire graph, we can represent it with the help of state graph. Okay? And then comma start and then we will also have end. Okay. Start and end are just like my start node and end node. Along with this we will also go ahead and add from langraph dotgraph dot message import add messages. Okay then let's go ahead and execute this. Okay now we have imported all the libraries. Now you may be thinking kish
25:24 what exactly this add messages is. These are called as reducers. Okay. Now what is the importance of reducers? Okay, I will talk about it. Let's say that if I want to create this chatbot, right? If I want to create this chatbot, you know that we also have a state, right? Now in this state, what is the kind of variable that I really need to create so that any output that is generated by the chatbot will be saved it in one variable itself. So let's say that if I go ahead and create a variable called as messages inside this messages can I make this as a list type and inside this list as soon as I keep on asking any input
26:10 automatically it should keep on getting appended. So again let me repeat it what I'm trying to say over here. Let's say if I'm creating this basic chatbot as soon as I give an input this should be able to generate an output. But again in that session if I give another input it this graph will again get executed and it'll give me the output right. So we can execute this graph as many number of times. Right? So when we are creating this state graph okay state graph so every conversation can I save that inside my state right which will be available to this particular node at any point of time yes. So for that what we'll do we'll we'll create one variable. We'll make it as a list type and inside this list we should keep on
26:57 adding this messages. When I say adding it should be appending this messages. It should not replace the previous message. Okay. When I say replacing the previous message let's say in the first instance I had one message. I said hi how are you? Then the chatbot replied I am good. Then my next question is hey uh tell me what is your name? Then the chatbot replies hey I do not have any name I'm just a basic chatbot so this message should not get replaced instead it should get appended you know as every conversation goes ahead so that we will be able to maintain this information and that is the reason we say it as state graph okay so in order to probably append it we can use something called as reducers okay one of the example of the reducers
27:44 there are different types of reducers that we can specifically use one of The red reducer is nothing but add messages. Now this add messages what it is going to do is that its work is only to add the messages instead of replacing in any kind of variable that we define. Okay. So now let me just go ahead and execute this. And now I will go ahead and start creating my state. So here I will write class state is equal to and here we are going to use this type dictionary. That basically means the state class is going to return type of a dictionary right. So here let me just go ahead and provide you some basic dock string so that you should be able to understand it as we go ahead. So here you can see messages have
28:29 the type list. The add message function in the annotation defines how the state key should be updated. In this case it appends messages to the list rather than overwriting them. I hope everybody has understood why we are inheriting type deck because this state is going to return right this class is basically going to return of this type that is nothing but dictionary type right so if you see what is type dick it is a simple type name space at runtime it is equivalent to a plain dictionary right if I'm going and writing class point 2D type dick right so x is int y is int label is str so what we can do we can provide values in the form of dictionaries right key value pairs It's like x is equal to 1, y is equal to two, label is equal to good, right? Something like this. Now in the next step, what we
29:16 are going to do is that we create one variable. Let's say messages. Inside this messages, we will go ahead and use annotated. Now annotated is just like a kind of label. Okay, this annotated class that we have inherited or we are basically writing it is nothing but it is it indicates the hypothetical runtime check model. This type is an unsigned integer. every other consumer of this type can ignore this metadata and treat this type as integer. So if you see some of the examples over here, you should definitely be able to understand these are something like in Python what exactly this basically means right now inside this I will say hey you have to go ahead and add the messages inside a list type with the help of add message. So this add message is called as a
30:02 reducer. Please remember this information. When we say reducer, that basically means it is not going to replace this list with respect to every conversation we have. Instead, it is going to append. Append right. So here you can see that how this state key should be updated. In this case, it appends messages to the list rather than overwriting them. So this is the basic information. But I will show you how this looks like as we go ahead because we will go ahead and just display this with respect to the state. Now I will go ahead and build my graph. So in order to build my graph I'll say graph builder. I'll use the state graph and I'll give this class right. I'll give this class. That basically means when I give this specific class over here, this state
30:48 graph uh when we are creating the entire graph uh at any point of time we can provide this specific information to our different different nodes. Okay, so this basically becomes my graph builder. Here I'm just going to go ahead and give show me my graph builder. It is nothing but it is of a type state graph. Okay, so my state information has got completed. Okay. Now in the next step what we are going to do is that we are going to build our entire graph itself. Right. We going to go ahead and build our entire graph. Okay. Now for this first of all what we are basically going to do is that I will go ahead and put one more libraries. So for this I will use python.env since we are going to go ahead and use uh you know uh grock
31:34 models. You can use openAI models. You can use any kind of model. So here what I'm actually going to do I'll go ahead and write uv add uh minus r requirement txt. So once I go ahead and install this the installation has been done. Now once I go over here right so here you can see that um now we can go ahead and quickly import all the libraries that we want. So I will go ahead and write import OS then I will go ahead and write from lo from env import load env right and then we're going to go ahead and initialize this load env right so the reason why we are doing this is that whatever keys we specifically write in our enenv it should be able to load it so here I'm
32:19 going to go ahead and create my env file right now with respect to the env um the Next step uh that we are going to specifically do is that whatever keys that we specifically want with respect to the gro API, we'll paste it over here. So this is my env. I hope everybody knows how to create a gro API key. In order to do that, just go to console.grock. Okay. So here you go to console.grock.com, right? And here you just go ahead and create your API keys. You can go ahead and create your API key, write the API key name and start using it. Okay? So this API key we'll be using it and we can use different different um you know models LLM models in order to develop your generative AI applications. Okay. So once this is done
33:06 I will quickly go ahead and again execute this since my env. So first of all I will show you one very easy way. So I will go ahead and write from langchain or sorry from langchain grock. Okay. So for this we need to install this library. It's called a lang grock. So I will go ahead and write lang chain gro. Okay. I will open my terminal requirement.txt. Now here you can see langchen gro has got installed and I
33:52 will go ahead and minimize this. So from langun grock I will be importing chat gro. Okay. So this is one way you can directly initialize the gro model. The other way is more common and generic way. So where you can just give the model name and automatically it should be able to do it. So for that you will be using from langchain langchain um dot chat models import init chat model right so here if you want to directly go ahead and use your lm with the chat gro you can just go ahead and write like this and with respect to this you can just provide your uh model name okay so models it is up to you whatever models you specifically uh want to use or you want
34:39 to go ahead with it, you know, you can definitely go ahead and just use that. Okay. See, at the end of the day, it's all about how you are using some specific models and which model you really want to use. Okay. So here, let's say that I want to go ahead with some other model, right? Uh for this, I will again open my let's see my playground is over here. So let's say I will be using some models like llama 3 8 billion8192. So here all you have to do is that you have to go ahead and give your model is equal to uh lama 3 lama 3 is the names correct 8b 8b 8192 right so you can basically give this particular model and if you execute it this is nothing but this becomes your llm right this becomes your llm right
35:26 you can either initialize in this way or you can also directly go ahead and write something like this so here I'll be using llama llm Initiate chat model. Here we are going to give the model name. The model name will start with something like this. Grock colon you know llama 3 8 billion 9 sorry 8192. Okay. So here also you can use this and it'll also give you the same llm right. So these are both ways how you can initialize this. Uh and again if you are using OpenAI then you can use uh Langchain OpenAI and here you can just mention OpenAI colon whatever OpenAI model name you are specifically going to use. Okay now this is my LLM. So here if I go back to my graph right we have created our
36:12 LLM. Our LLM is ready. Now we will go ahead and create this chatbot. The chatbot is nothing but it is just like a node right now with for every node you need to create a node definition. Right? So in order to create a node definition, I will go ahead and write definition chatbot. Let's say this is my node. And here uh here I'm going to go ahead and define my state colon state. Okay. And here what I'm actually going to do is that I'll go ahead and write return messages colon. Now see this since this why I'm returning in this particular variable because whenever I define this chatbot right it should be inheriting this state because at the end
36:57 of the day I need to keep on appending inside this particular variable right and you know the state return type is type dictionary so that is the reason we are inheriting over here state colon state and when we write return message colon here we are going to invoke it with our llm. So here I'm going to go ahead and write llm.invoke and with respect to the invoke here we're going to use the state of messages. Okay, state of messages. So we are going to basically go ahead and return this uh to give you a very brief understanding. This is what is my node functionality is okay this is what is my node functionality. Here we have defined a node called as chatbot. This llm.invoke Invoke is basically giving right based
37:43 on this input message. See the state of messages is what it will be my input message right as soon as we get an input message we are giving to our chatbot node and that chatbot node is going to provide the response from this from my llm and it will append inside this messages variable. This messages variable is nothing but it is the same variable that we defined in the class state. Okay. Now this is done. Now in my next step what we are basically going to do is that we are going to go ahead and quickly start building our graph. So for this we will be using our graph builder. If you remember uh what is graph builder? So graph builder is nothing but it's my state graph. So I will just remove this quickly over here and I'll just paste it over here itself. Okay. So this is my graph builder and uh with respect to the graph builder how we need
38:29 to build it. Right. In my graph builder I have to have one chatbot node, one start and one end. Right? And there should be edges connected to both of them. And as I told you that we are going to use the graph API. Right? So uh for this what I'm actually going to do is that I'm quickly going to write graph builder dot add node. Okay. So this will basically be my first node. My first node name will be chatbot. You can mention anything. Let's say I will go ahead and write llm chatbot. Okay. But the second parameter that I'm going to write is about my node definition. So which is nothing but chatbot. Right? So this every node will have some node implementation. That node implementation you should be specifying it over here. Okay. Then uh coming to the next uh
39:16 option is that in my graph right I definitely have only one node right this is the node that is there. But along with this I will go ahead and create start and end as my starting and end point right. So in order to create that we need to go ahead and create edges right. So first of all uh what we basically going to do is that I'll go ahead and write graph builder dot add edge. So this was my adding node. Adding nodes this is my adding edges. Add edges. Now with respect to add edges and add node here is my start. So from the start I have to go to my LLM chatbot
40:01 right. So from my start I'm going to the LLM chatbot and from the LLM chatbot I should basically go where to the end right? So I will go ahead and add one more edge and this edge is going from llm chatbot llm chatbot and remember here you need to specify the node name instead of a node functionality right and this will basically go to my end node. Okay, perfect. Now see that is what it is matching right from start I have created an edge to chatbot then again it is going to the end. So this is my entire graph right. Finally what we do is that we compile the graph. So these are some of the steps when we define the graph. The compilation is necessary so that we
40:47 can execute the graph. Right? Unless and until the graph is not compiled you will not be able to execute it. Right? So for this I will go ahead and use graph builder dot compile. And here we are basically going to just go ahead and execute it. Okay. Now the question rises can we go ahead and see how this graph looks like? Okay. Yes. Obviously you can see it. So for this we will be using some visualization graph. So I will just go ahead and write visualize the graph. Okay. So from visualization graph I will go ahead and write from I python dot display okay import image comma display okay so we are going to use this and uh again we're going to go ahead and use try
41:33 catch block where we're going to use this display method which is responsible in displaying the graph with respect to any image object that you give and if I go ahead and write graph get graph I should be able to get the graph itself and this we will try to draw it in some mermaid png. Okay, these are some of the functionalities that were provided over there in the documentation. So I'll go ahead and write except exception. Okay, and here I can just go ahead and write pass. So here you can see this is how my chatbot looks like. So here I have start, this is my LLM chatbot and this is my end, right? So when I give my input from here, my llm my start will be sending this and I should be able to get this. Okay. Now the time is that how do
42:20 we run this? You know we we really need to run this right at any point of time and if you are running it how does it basically looks like you know so for this I can directly use this graph dot invoke. Okay. And I will say hey u hi. So let's say this is the message that I'm giving. So what will happen? Hi will go from here. it'll go to the LLM chatbot. It'll give you the output and it'll end. That's it. Right? So when I say hi uh got hi. Okay. So one problem over here that you'll be able to see that uh when it is trying to retrieve the details there we are facing some kind of problems. Okay. Now what is the exact problem that we are facing? I will just try to uh resolve this uh as we go ahead you know. So let's go ahead and do this.
43:06 So here one very important thing is that in the state you remember that what is the variable that we created right messages. So what I will do I will go ahead and create a dictionary called as messages. And now inside this I will give my message saying as hi before I had not given this. So it is not able to pick it up right because here if you see inside my functionality of LLM chatbot it is invoking from this particular variable right from state of messages where it is basically saved right so here now let's go ahead and execute this now it should execute it let's see invalid API key during the task okay so my env is ready okay no worries see the problem
43:53 over here is that we need to restart the kernel because my API key I added it in the later stages right so that is the reason so quickly I will execute all these things sorry graph builder is not required over here now it should execute it because I just needed to reload this you know by restarting my kernel then only it'll get reloaded okay no worries now it should work so my visualization graph is there and now I'm invoking the messages of high. Now here you can see that I have got graph.invoke messages of high human message. Now you see this high that is going right. It is being treated as a human message. And now your response is with respect to the AI message. Hi, it's nice to meet you. So let's go ahead and
44:39 save this as my response. Okay. Now in order to check the response, right? What was the final response here? You can see that I can go ahead and read inside my messages variable. Now this is what is really important. See inside my class state right I told you that we are going to create a variable right over here this is my messages variable annotated was there list was there and add message was there this add messages is acting as a reducer reducer work is to append inside this list see initially human gave high then AI message gave high right and this has got added inside this list understand one very very important thing And this is in the messages variable. Right? Now
45:25 you may be thinking what is this annotated? Annotated basically means what? As soon as I gave hi. See over here automatically this messages got converted to human message. Right? Human message is just like one kind of annotation. We uh the the the the the graph is making sure that it is annotating and it is appending in this specific list. And the reason it is getting appended because here you can see that directly that my messages are getting appended with the help of those reducers itself add messages itself. Right? Now I hope you are able to understand it right why we have specifically defined it. Now the question rises how do I go ahead and retrieve the last message? It is nothing but response of message minus one. Okay. So here you can see that I have got this and if you just go ahead and write dot
46:11 content you should be able to get hi it's nice to meet you. Is there something I can help you with? Okay, so this is the most easiest way of probably uh reading all the stuffs. Okay, now there are two more way of streaming it, right? Streaming your specific data or or running your entire graph and uh you know displaying the information, right? So that is what we will discuss now and understand one thing guys if you are able to understand this right trust me as you go ahead any kind of graph any kind of complex workflow that you have in your mind you should be able to execute it okay now what I will do I will go ahead and write for event in graph dot stream okay so this time
46:58 instead of directly using graph.invoke invoke I am using something called as graph stream we will understand about this as we go ahead but I just want to give you some kind of information how things work in this so now here I will give you messages okay and colon let's say here I go ahead and give hi how are you okay so this is what is my message let's see whether everything looks fine uh yeah this is my for loop yeah now what I'm actually going to do I will just go ahead and write print event. Okay. Now let's execute this. So here you can see that inside this I have got an output which looks something like this AI messages messages AI message all these information and I'm getting this right now when I am doing graph stream
47:47 with this particular input right so here I'm getting with llm chart lm chart is nothing but my uh node which you are able to see this okay now let's say that I will go ahead and write one more for loop I'll write for event or so for value in event dot values event dot values now what will happen if I just go ahead and print this okay see I will print my value now if I execute this here you can see that I'm getting this AI message right so that basically means now whenever we try to stream from this graph stream and whenever we try to see the event values only AI messages will be getting displayed Right now in order to display
48:32 this what I can basically do is that I can also go ahead and write value of messages uh which will be my last message minus one and here I'll just use dot content and this will basically display the same thing like what it was displayed over here right hi I'm just language model so I don't have feelings or like human do and all this is just one specific example I've told about streaming but don't worry because this streaming we will discuss more about it there are multiple types of streaming. With respect to streamings, you can also provide different different parameters what exactly it means, you know. So, we will discuss about it as we go ahead. But here this was just an example of how you can go ahead and build a basic chatbot. Okay. Now, it's time that we start thinking crush can we go ahead and
49:19 integrate some kind of external tools. Okay. So, for this let me go ahead and talk about a use case. Let's say I have a chatbot. Okay. Now this chatbot I have a question I can basically go ahead and ask a question for this chatbot saying that hey let's say this this chatbot I have and this chatbot you know what does it have it basically has a llm with some kind of prompt and it is taking an input from the start and it is basically ending it right so here start end now if I ask a question provide
50:05 me the recent AI news. Do you think the chatbot with the help of this LLM will be able to provide the output? The answer is simple. No, it is not able to provide it. Why? Because LLM will not have any information related to live, right? Any live information it will not have. It may have not trained with the recent data right. So here the dependency on external tool comes right external tools comes right. So what we can basically do is that for this chatbot as soon as we give an input this chatbot should understand hey we are not able to answer it. So I definitely have to make a tool call. I definitely have to make a tool call.
50:51 And when I'm making this specific tool call this tool call let's say this can be any third party API it can be uh Google search engine it can be let's say one of the search engine that we going to use is tavi tavi is nothing but it is a web search it provides a web search API okay and with respect to this tavi we will be able to get some kind of response over here okay so as soon as we make a tool call or in order to define it like this I will I will just make this graph a little bit longer now. Okay. So what what happens as soon as I get an input the next thing is that the chatbot is understanding it is a tool call. So it goes and makes a tool call and here we will define another node which will be called as tool node. Okay.
51:36 And then based on this tool call I should be able to get the response in the end. Okay. So instead of chatbot not able to give you the output let's say if I give any input provide me the recent AI news this request will go to the chatbot chatbot will understand hey we do not we do not have that information so definitely I have to make a tool call so here what it will do it will make a tool call and then from here it'll go to end okay it'll go to end right and here in this tool node I may have multiple tools I may have tools like tabuli. I may also go ahead and define some custom tools. Let's say add, subtract
52:23 or some custom implementation also you can go ahead and write. Right now the question arises how does this chatbot knows about the tool node? See there is something called as LLM. Okay, inside this chatbot we use LLM, right? LLM is actually the brain behind taking this decision. Why this LLM can be binded with this tools. When LLM is binding with this tools, what does this basically mean here? It means that let's say I go ahead and create one custom function. This custom function is called as addition. Let's say this is my addition. This can be added as a tool to the LLM. It can be binded with LLM itself. Then the LLM
53:11 here whenever you define any custom tool you also need to provide the dock string. You need to provide the dock string. Now with the help of this dock string the LLM will know what are the inputs and what are the arguments that is required over here. So if this inputs and arguments matches with the input that we are giving in this chatbot then automatically this is going to make that particular tool call right so this same thing I will try to show you it in the practical way we will go ahead and create some some tools we'll also go ahead and create some of the custom tools and once we do that what we are basically going to do is
53:57 that we also going to go ahead and create tool node and There is one more additional condition which is called as tool condition. So we will discuss about all these things with respect to this particular implementation. But our main point over here is that the chatbots can also be integrated with a separate tool node. And here it can also make a tool call based on a specific input that we get. Okay. How that can be implemented? by binding tools with the LLM and also defining your custom functions if it is required and this LLM will be able to understand whether it has any tool or not through this dock strings. Okay. So now let's go ahead and implement those functionality.
54:42 So guys, now let's go ahead and start building a chatbot with tools with the help of langraph. Now first of all, I'll just show you like what we are trying to build over here. Okay. So here is one graph uh you know uh which we will try to create. Now just observe this graph. Okay. This graph is quite amazing because here uh we have a separate set of tools. Okay. Here we have a tool calling LLM. Okay. So from here uh we are definitely going to give our input. Now from this input as you know these are my edges. This tool calling LLM is my first node and this node has LLM. LLM with binding tools. Okay, when I say binding tools, what
55:29 does this basically mean? So this means that I have LLMs and tools integrated with themselves. We will use couple of tools. One of the most famous tool that we will try to use let's say we will use Tavly API. or tably search. This is just like an internet search. Along with this, we'll also create some of our custom functions. Okay, we will uh create some of the custom functions or custom tools. Okay, tools. And remember here when I can also combine multiple tools in one tool node. Okay, so here what we doing is that we're going to combine this in one tool node. Okay. So this is nothing but this is a tool node and here you can observe
56:16 one more very amazing thing right. So from this particular tool from this particular node here we have two paths either we can go there here or either we can go over here. So let's say if my question is hey what is the recent AI news? So the input will go over here. Then this tool calling LLM will decide whether it can give the answer or whether it is dependent on some tools. Since we have binded this tools and remember how LLM will be able to understand from the dock string, right? So in every tool there is some kind of dock string. Dock string is nothing but some brief information about what that tool actually does. Okay, I will also define one custom and show it to you. Then this tool calling LLM you know
57:01 since it has those tool information it will take a decision whether it has to make a tool call or whether it can just answer it and go to end. Let's say if it makes a tool call the tool will then provide some kind of output message and it will end. If it is not a tool call it is just going to go and give you the output and go to the end state. Okay. So this is uh fundamentally a simple problem things that we are going to solve right now. Okay. And we'll solve it step by step. Okay. how how do we go ahead and solve it? Uh that I will discuss as we go ahead. Okay. So now let me go back to my code. So first of all in my requirement txt I will go ahead and import one library which is called as langchain_tavly. Okay. Now langchen tavly is nothing but
57:48 uh if you see in my envi api we need a tavly api. Okay. And in my requirement.txt we need to first of all install this. So quickly let's go ahead and open my terminal and here I will go ahead and write uv add minus r requirement txt. Okay so once we do the installation the installation will be completed and uh we are good. We have this lang tabi. Uh the next step will be that I will just go ahead and open this website called as tabi. Okay. So here you can just go ahead and search for tabi.com. It empowers your AI application with real-time accurate search results tailored for LLM and RAG. It's just like an internet search. Okay. So, I will just go ahead and log in.
58:34 Once I log in here, you can see that it'll give you one key. I will copy this key and it is free for free. Uh you can probably hit many number of requests with the help of this. So, I think you don't have to be dependent on my API key. Right. So, I will go ahead and write tab API key and I'll paste it over here. Right now the next thing is that uh since I'm working over here with with the help of this you know I will just go ahead and restart my kernel. Okay you have to restart your kernel otherwise things will not work you know the reason is very simple because we need to import this again. So I will first of all go ahead and execute this. This will basically be my lm. Okay this or this can be your lm no worries. Okay now I'll go back over here.
59:19 Now let me go ahead and import some of the libraries. Right. So for tabi first of all I will go ahead and import this tool. So I'll write from langchain tavly. Okay. Uh I'm going to go ahead and import tavly search. Okay. I will go ahead and create this tool wherein I initialize the tavly search and here my max results is equal to two. Okay. And then I will define my tools. Let's say this will be my list of tools. Okay. I can still define many number of tools I like. Okay. But I'll use this tool. Let's say I will go ahead and just invoke with one message. Let's say I will write what is no or what is lang graph. Okay. So this will basically
1:00:05 be my question. Now once I execute this here you should be able to see some kind of response. So here you can see what is lang graph results. You are able to see all these values title from different different source and URL you are able to see this right langraph is a Python library and all this information is specifically coming up. So once uh we have created this sav tavly search tool now our next step will be that uh we will just go ahead and try to create uh our custom method okay custom function so that gives you an idea like how you can also integrate a custom function and how lm is able to understand because I spoke about something called as dock string right so how do we write this dock string everything we'll discuss right so let's let's take a basic function so here uh I will define one
1:00:52 custom function And this custom function here we're going to just go ahead and write definition multiply and let's say here I will go ahead and write a int b col int and let's say this is going to return type of int right so u now the question rises how do we go ahead and write our document string so this basically gives you a document string example okay here in the summary let's say I will go ahead and write multiply a and b okay and And then here uh let's say a will be my first int b will be my second int and it returns an
1:01:37 output int. Right? So it is something like this. I've just written some information. Now this is what is called as dock string. Okay. Now this dock string will be very important because once we bind any functions right with our LLM or this functions can also be converted as a tools and bind it right and then LLM will be able to understand what this tool will be able to do it now what I will do I will go ahead and create my variable tools here I'm going to use first tool multiply and let me just go ahead and execute it right now as I told you that I need to bind this entire tools this list of tools with my LLM So I will go ahead and write llm dot bind tools and here we're going to basically go ahead and write tools and
1:02:24 this will be nothing but llm with tools. So once I go ahead and write so here now if you go ahead and see this is nothing but llm with tool right. So this is nothing but it is a run runnable binding chad grock. It has all the information over here and uh what all functions it is basically connected to like it is connected to tavly search it is connected to multiply you can find out all those specific information over here itself right and this is how uh things work in this. Now once we have defined this llm with tool this tool we are going to use inside our chatbot node. Okay. So now let's go ahead and create the entire state graph right remember the structure of the state graph how it
1:03:10 will be I have start I have tool calling lm this is connected to tools and this is end okay now our question is that how do we basically create this tool nodes also and for this also we have some predefined uh packages uh available in langraph okay so I will quickly go ahead and write from langraph from langraph graph dot graph as usual. I'm going to go ahead and import state graph. See again I'm importing all these things so that you get to know like I know in the top already we have imported it but you should know what all things are there. So from langraph dotp pre-built I'm going to go ahead and import tool node right see we have anyhow binded this llm with all the
1:03:57 specific tools right so binding will play a very important role see there are two important things one is binding when we are binding llm with tools this actually helps the llm to understand which all tools it has which all tools it has right so whenever an input comes it's just like just imagine LLM has some kind of weapons to solve your input right if I ask hey provide me the recent AI news obviously LLM will not be able to do it it will do an internet search and it will try to provide you the response right when we do this binding it is just trying to
1:04:43 give you an information that LLM has all the specific tools but further when an LLM makes a tool call it has to make a call to this tool that is what we really need to understand how that tool call will be happening okay so I'll go back over here we have imported something called as tool node now all the tools that we have created these all tools it has some kind of functionalities right these needs to get converted into a tool node Okay, because each tool node will be having some kind of implementation. Along with this, we will also go ahead and import one more library from langraph.prebbuilt. import tools condition. Okay. Now, first of
1:05:31 all, we will go ahead and start with the node definition. Okay. Here we are going to create a uh definition. Okay. And before creating a node definition also first let's start creating the graph. Okay. So you know first of all we going to use a builder. This will be of type state graph. State graph. And inside the state graph we will be using something called as state. Okay. This will be our class specifically state class. Then in the next step is that we going to go ahead and create our builder. Add node. So two one node uh two nodes we definitely require if you see in this graph one is a tool calling llm and one is the tools right. So first we will go ahead and create this node. Inside this node we
1:06:18 will give the name as tool calling llm. Okay, and then I have something called as tool or I have to define the functionality of this node. Right? So this in the later stages will define still I'm not defined because I will be defining it over here. The other edge that we really need to create or other node that we need to create is nothing but add node is nothing but tools and remember this tools will be nothing but it will be of node type of tool and here we are going to give all our tools itself. So this node is nothing but it is this specific nodes and inside this node if I want to go ahead and write a definition it will
1:07:03 be of tool nodes. If you go ahead and see the definition of this tool nodes, it is how all the list of tools that we specify, it will be implemented as a tool node itself. Okay. So this is my node name and this is the definition. This is my node name and this is the definition. Now let's go ahead and create the definition. So for creating the definition, I'll go ahead and write tool_calling lm. And here we are going to define state colon state. And here we are going to go ahead and write return messages colon. Again what we are going to go ahead and write here we are not directly going to call lm but instead we are going to call lmit tool right. So here
1:07:48 llm tool dot invoke and where do we get the input from? from state of messages. Right? So here we are going to go ahead and define state of messages. Perfect. So here you can see very clear. Now in the next step we are going to go ahead and add the edges. Now adding the edges is really important. If you understand this any kind of complex use cases you'll be able to understand it. Okay. So the first edge is from start to tool calling LLM. Okay. So first of all let's create that. In order to create it uh we will just go ahead and write something like this. See builder.addage start to tool calling llm. Now from tool calling llm there are two nodes that are going on right sorry two edges. One edge
1:08:35 is going to the end and one edge is going to the tools. Right. Now this kind of edges are called as conditional edges. Okay. So in order to add a conditional edges it will be like builder dot add conditional edges. And inside this we are going to call our tool calling LLM. From two calling LLM this will happen right from this specific node it is going to happen. So
…