Hello guys.
So in this video we are going to develop a multi-agent generative AI application using line graph uh,
wherein we will try to create a chatbot.
Along with the chatbot, we will also be integrating with some third party tool so that whenever we
ask a query with the chatbot, if the query is being able to handle by the chatbot, uh, which is specifically
being using some llms, then we will directly get the response.
But if it does not has the answer, it will go ahead and ask this specific external tool.
So, uh, the entire flow or the architecture looks something like this.
Here we will go ahead and start this process here.
Any user if if any user is specifically asking any query, first of all the query will go to the chat
bot.
Then the chat bot will decide whether it should specifically provide the answer, or should it.
Probably go ahead and ask some third party tool.
Right?
The third party tool can be, uh, Wikipedia.
It can be Google Search anything.
It can be right.
And this chatbot that you will be seeing, it will also be interacting with some open source LLM models.
And this open source LLM model.
We will be accessing it from the grok API.
So now we are going to go ahead with respect to one additional step with respect to land graph like
how we can integrate some tools.
You know here I will be going showing an example with respect to one tool like this.
You can add any number of tool.
And when the interaction is done with the tool you get the response.
And then you finally end this entire flow okay.
So let's go ahead and implement this step by step.
Now first of all, what we really need to do is that we need to go ahead and install all these libraries.
That is uh, land graph lang smith lang chain lang chain underscore grok.
You know why lang chain underscore grok is specifically used so that you can actually communicate with
the grok API.
Then you have Lang chain community.
Right.
So I will go ahead and execute this.
So once you specifically execute this here line graph is going to get this.
All these libraries will be installed.
Okay.
Now uh, after installing this we will go ahead and import two more libraries.
One is annotated and type dict.
The reason is very simple.
Uh, if you have already seen my previous video, we are going to use this two libraries in our state
class which will be managing the state of the line graph.
Right.
So let's go ahead and execute this.
So once we have executed this I will go ahead and start writing my code right now.
First thing first.
What we really need to do is that I will also go ahead and install some tools, you know, and Lang
already provides you some amazing tools and tools here that we are going to use is specifically like
Wikipedia or RCF, you know.
So if you have already explored Lang, so already there are a lot of third party tools specifically
available, right?
So for that, I will go ahead and write a comment here.
We are going to work working with tools or external tools.
So first of all, I will go ahead and write from long chain underscore community dot utilities dot utilities.
I'm going to go ahead and import two tools I can actually use.
But right now I will just show you an example with respect to one tool.
One is rc API wrapper.
And the second tool that I'm actually going to use is something like Wikipedia API wrapper.
Right now RCF is basically if you are looking for any research papers, you can basically go ahead and
interact with this particular wrapper instead.
If you just want to get the search from Wikipedia, you can go ahead and take it from this particular
library.
Okay.
Then the next step will be that from Lang chain underscore community dot tools import rc query run.
This will be responsible in running any queries that you specifically give.
And then you have this Wikipedia query run okay.
So this is for the RC tool.
This is specifically for the Wikipedia tool.
Okay, then, uh, after this, I'm just going to go ahead and call my RCF, uh, and Wikipedia tools.
So let's say Wikipedia tools okay.
Then here I'm going to write my RCF underscore wrapper is equal to RCF, rcf API wrapper here.
Uh, let's say in this API wrapper how many results I want, I will go ahead and provide a parameter
that is top-k results.
Let's say top-k results.
I just want one result and I will go ahead and write my document content character Max.
Like how many characters I specifically want.
Let's say I will go ahead and write 300.
Okay.
Then uh, with the help of this RCF, what I'm actually going to do, I'll go ahead and create this
specific tool.
I will use this R safe query run, and I will give the wrapper as a parameter over here.
Right.
One amazing thing about Google Colab is that it is now providing you code suggestion, which will be
very helpful whenever you are writing any code.
Similarly, if I go ahead and write my API underscore wrapper, I will go ahead and use my Wikipedia
wrapper.
Here also, I will go ahead and write my top k results and document content characters.
Max characters.
I really want 300, right?
That basically means whenever we are interacting with Wikipedia, I'm going to get the response as one
document at a time.
And inside that document, the maximum number of characters will be 300.
So finally, I will go ahead and also write about Wikipedia tool where I'm going to use this Wikipedia
run query run.
And we are going to run this particular API wrapper.
So two things I have actually imported it.
Now let's say let's go ahead and just uh okay.
One uh error is basically there.
Let's say okay, I have not installed.
So we need to install RCF and Wikipedia okay.
So without this this will not work okay.
So let's go ahead and execute this quickly.
So this is going to get installed.
Here you can see that it is getting installed.
Because without this particular packages we will not be able to execute this code.
Right now it should basically run up here I'm getting an error saying that base tool underscore unit
okay RC tools takes RC wrapper okay.
This is my RC wrapper.
I need to provide the parameter because over here I will go ahead and give my API underscore wrapper
as my parameter.
Okay.
So I'm not going to edit any of this particular errors.
So please make sure that and this is the key that is specifically used.
That is the reason it was giving an error.
Now it should run okay.
Now this is run perfectly.
Now let's go ahead and run any of this.
So I will go ahead and write Wikipedia dot tool and let me go ahead and ask who is who is.
I'll ask any question who is a famous actor?
Let's say Shahrukh Khan.
Okay.
So here I'm going to write Wiki wiki wiki underscore two.
It should be dot invoke.
Okay, so if I really want to invoke any of this invoke and let me go ahead and execute it now, it
should probably go ahead and search from Wikipedia.
And I'm getting the response over here.
The maximum number of characters will be 300.
Similarly if you can go ahead and use this RCF tool also.
So if I go ahead and write RCF underscore tool dot invoke okay.
Attention is all you need.
So this is one of the research paper related to Transformers.
So here you can see I'm getting all the information.
You do not need all the information.
So this is basically coming from the RCF itself right.
And all the author research paper names everything is basically available over here.
So perfect.
That basically means my external tools are working absolutely fine.
Right.
But since I'm just going to use one tool, you can also use any number of tools.
So I'll just go ahead and write this as wiki over here or wiki underscore tool.
So this will basically be my tool.
Uh, afterwards let's say if you want to go ahead and add any number of tool, just go ahead and write
comma and keep on adding all the tool.
Right.
So now my third party tool is done.
Now I'm going to create my line graph application okay.
Line graph applications.
Now in order to start line graph application already in our previous video we have seen I'm going to
create class state.
And here I'm going to use my type dict.
Okay.
The reason why I'm using this because I'm going to create this messages variable which will be a list
of variables okay.
And I'm going to use the annotated.
And I have to make sure that whenever the execution of a specific node gets completed, I'm going to
go ahead and just make sure to update this messages variable.
So here I'm going to get my list.
And here I'm going to get my add underscore messages okay.
So once this is done I add underscore messages.
Also I need to import and this add underscore messages.
If you remember from my previous video already it is already available in the line graph.
So let me just go ahead and import this okay line graph dot message import add message.
And here I'm going to basically use this specific state.
Right.
So all these are completed.
And we have this particular add messages and we are good to go okay great.
Now the next thing uh along with this that we really need to import is three important things from line
graph.
That is your state graph start and end I need to import all these things so that I get all the information
that I really want okay.
Uh, with respect to this.
So this is done.
Now let me go ahead and first of all create my chatbot.
Okay.
Uh, because uh, already we have created this particular state class.
Now we need to probably design our entire workflow Right.
So here I'm going to go ahead and execute this.
Now let me just go ahead and right quickly I will go ahead and create my graph builder okay is equal
to graph Builder I will create this specific variable.
And here I'm going to go ahead and write state graph or state graph.
And here we are specifically going to take this state.
Now this state graph is nothing but it is imported from here.
This state is nothing but my class.
The state is going to maintain all the all the all those different different states in this particular
messages variable.
Okay.
So this will basically be my graph builder.
Now it's time to probably call my LM model.
Right.
Because at the end of the day we if we really want to create any chatbot, we really need to use a specific
LM model.
Now, I have already shown you in our previous video how we can actually go ahead and use LM models,
right?
Or how you can use or how you can call the LM model from Chad Brock.
Right.
So here I will go ahead and right from Lange Lang chain.
Underscore.
Grok import chart.
Grok okay.
Chart.
Grok.
And inside this chart grok I'm just going to go ahead and execute this.
And then I'm going to create my LM variable which is nothing but which is my chart grok.
And here you can actually go ahead and see my grok underscore API underscore key.
This API key I should be getting from my uh, secret keys.
Right?
I have already added the secret keys, and I've already shown you how you can basically add it in the
previous video.
Right.
So in order to add this, I will go ahead and write one more code.
I will go ahead and write from Google Dot Colab, import user data, and then we will go ahead and write
grok underscore API.
Underscore key is equal to user data dot.
Get And here I'm going to specifically use my grok underscore API underscore key, which is the key
name that I've written it over here in my secret keys.
Right.
Grok underscore API underscore key.
Perfect.
So this is fine.
Now once I get my grok underscore API underscore key I will just go and click on grant access.
And finally I will keep the value over here.
So this should be grok underscore API underscore key.
And here finally this is my LLM model.
And this LLM model by default is using no no models.
So I have to provide my model name.
So the model name will be nothing.
But I will go ahead and write model name is equal to gamma.
I can use gamma.
So let's go ahead and use gamma.
Gamma two nine be it.
Okay.
So I've specifically used this particular model.
And let's go ahead and execute this.
Perfect.
This is done.
My graph builder is also ready.
Now it's time that we go ahead and create our chat bots, right?
So we need to go ahead and define our chat bot.
Now with the help of this LLM, we can basically call or we can call with our chat bot itself, whatever
nodes we are specifically creating.
Right.
So let's go ahead and go to go with the next step wherein we will go ahead and create our chat bot,
which will be doing a functionality of managing the state and also interacting with the LLM model.
So let me go ahead and create my definition chat bot because see uh, if, if I show you over here right.
We need to create this first.
Right.
My LLM is ready.
It is interacting with the grok API.
Now I'll create my chat bot.
I will be interacting with this.
But I will also be adding one very important thing that is nothing but my tools, right?
My LLM should also be integrated with the tools.
So let's see how to probably do that.
So first of all, uh I will go ahead and create a cell over here.
I've created my graph builder.
Everything is ready.
I will take this LLM and I will say hey, go ahead and bind underscore tools, whatever tools I'm going
to use.
So this will be my tools is equal to tools.
And if you remember this tools has specifically what does it has a binding as Wikipedia tool.
Right.
So I'm going to basically bind this particular tool itself.
And after binding I will store this in a variable.
This is just giving a additional functionality to the LM that it has this particular tool access also.
Right.
So this LM with underscore tools will be used.
Now once I'm creating this particular functionality I will first of all derive my uh, I will inherit
this particular state inside this chatbot so that it has all the information related to the message.
And here I'm going to probably return my output with, with the help of this messages variable, which
is the key that is present inside the state colon.
And here I'm going to probably call or invoke with the help of my LM model.
But this time my LM model will also have the functionalities from the tools, right?
Because it also has the tools functionality which is nothing but the Wikipedia tool.
And here I will go and write Dot invoke.
And I'm going to probably take this particular state, this state which I'm inheriting, which is coming
from this and inside this state.
You know, what exactly is present messages variable is present.
And if I take the state of messages, this is basically going to have the query of the user.
Right.
So once I execute this this is what it is basically going to return.
Right.
So my chatbot functionality is ready okay.
This chatbot functionality is ready.
This LM is being integrated with the LM tool.
So it is basically going to interact.
So whatever query comes, if this chatbot is able to probably provide the response from the LM, then
it is going to get the output.
Otherwise it is going to interact with this particular tool.
Right.
And finally we are just going to end this.
Now we need to create this entire flow the flow of the execution.
Right.
So in order to create the flow of the execution I will again take my graph builder.
So let me go ahead and write Graph builder dot add underscore node.
Okay.
And then we are basically going to write chatbot.
So this will basically be my node name chatbot.
And finally you will also be able to see this chatbot.
Right.
So this is my node.
I will add the start with this chatbot node.
Right.
I will add that particular edge because we need this specific edge right from start to the chatbot.
Perfect.
This node is basically added.
Now I have to also make sure that I need to add uh the tool node okay.
So see whenever a tool is basically needed to add because here we will be adding this tool over here.
Right.
So this tool needs to be added to the chatbot.
Then only the LM will be able to interact with the tool.
So for that we basically use another library over here which is called as tool node.
Okay.
So let me just go ahead and import one more library.
So I will go ahead and right from Landgraaf.
In order to add any third party tools or any custom tools, you can also create this custom tools.
I will go ahead and import one tool node and then along with this I will also give one tool condition.
Like for this particular tool node there should be some kind of conditions we need to add right now.
Now let's see what is the error tool node prebuilt from tool node okay.
It should be tool conditions tools condition okay.
No worries I will just go ahead and import this.
Perfect.
So this is done.
Now what I have done.
I have created a chat bot.
I have connected from start node to the chat bot node.
Then I will go ahead and call this tool node.
So let me just go ahead and write tool node.
And this tool node will basically have my tools.
My tools is nothing.
But whatever tools I am going to use that is nothing but Wikipedia.
And I'll create a variable which is called as tool node.
Okay.
So this tool node is also created that basically means this tool node is also created.
Now we need to connect from this chatbot.
To the tool nodes.
How do I connect it?
It is very simple.
I will go ahead and write.
Graph graph builder dot add underscore node.
And here I'm going to basically go.
Ahead and create a node which is called as tools.
And this tools is going to get connected.
To tool underscore node right.
So this way what we have done this entire node is getting.
Getting created.
Right.
So here this entire tool node is basically getting connected.
Now we need to probably connect this tool to our chatbot now in order to connect it.
Obviously we need to write some conditions.
That basically means what does this basically mean.
Like if I'm getting a query, this chatbot may either interact with the tools or it may.
Interact with the LM and give you the output.
Right.
So this kind of edges if there is a.
Bidirectional edge like this, we really need to use something called as add conditional edges.
Right.
So here what we are going to use is that I will write graph builder dot add add conditional edges.
See add conditional edges.
That is the reason we had imported somewhere.
Let's see where we have imported it.
Have we not imported this?
Let's see.
Add conditional edges.
Okay, we have not imported it.
So first of all we really need to import it.
But no no need to import it because this is an inbuilt function inside Graph Builder.
Okay.
So here I'm going to basically go ahead and write add graph add conditional edges.
And inside this particular edges here what edges will specifically add from chat bot to to tools underscore
condition.
Right.
And this tools underscore condition is nothing.
But it is basically coming from here.
Now understand over here why we are adding this conditional edges because just from the diagram you
can see that from the chatbot.
I need to interact with the tool and again get it back.
So whenever we have this kind of bidirectional we really need to add this particular edge which is called
as conditional edges.
I need to give my node name and the tool conditions are the default conditions that is available over
here.
This tools condition is going to make that bidirectional directions itself right from the chatbot.
Now after that, uh, you know that my tools also needs to get connected to the chatbot.
See this tools when I'm using this, add conditional edges, the chatbot, tilde tools, the communication
will happen, but I need to reconnect it back.
So for in order to reconnect it back I will just go ahead and write graph underscore builder dot.
Add edge from tools to chatbot see from tools to chatbot.
And finally we are going to add an edge which is from chatbot till the end.
That is what we really want this end.
So it is very simple.
First of all we went this is the entire flow.
And this is really important.
Inside this flow we use three important things.
One is add edges, add edges, add edges, add node.
Right.
And then you also had conditional edges.
Right.
All these three nodes we had.
Right.
So first of all we connected start to chatbot.
So we created uh we created a node for chatbot.
And we add this edge from start to chatbot.
Then we created this LM tool integration.
Then from chatbot to tools we specifically went right.
So if we go ahead and just revise this step by step, see first of all we added a node chatbot which
is having this particular functionality.
And this functionality is nothing, but it is using this LM underscore with tools dot invoke.
It is just trying to get the output from this particular message right.
Whatever message is there, LM will either give the response or the tool will give the response.
Perfect.
Then we added the edges with start to chatbot at this specific node.
Then we created our tool node.
Then this tool node got added as a node in the graph builder.
Then we added conditional edges between chatbot and we applied a tools condition.
Then we added the edges back from tools to chatbot.
And then finally we have this graph builder dot add edge where we connected the chatbot with the end
right.
So all these things were executed perfectly.
Now let's go ahead and compile this.
I'm going to basically use graph underscore builder dot compile.
Okay.
Compile.
And then this will basically be my graph graph.
And we will just go ahead and execute this.
Okay.
Now finally in order to display this graph I can go ahead and use the same thing which we have used
earlier in our videos with respect to IPython dot display.
This is just from the documentation.
Now we can see the entire flow.
See start chatbot tools end right.
Everything is basically here.
So finally you can see the flow over here after executing it.
Let's go ahead and test it.
So see from start to chatbot.
If the tool is required then again it is connected back.
And finally we go to the end.
Right.
So all these things are specifically connected and we are good to go with respect to this right now.
Let's go ahead and try out some of the flow like with respect to any kind of messages okay.
And let's see how we are going to get the output right.
So first of all I will just go ahead and give my user underscore input is equal to I will write uh hi
there.
Let me just go ahead and write hi there.
And here I'm just going to write.
My name is Krish okay.
I'm just going to probably provide some basic input from the user.
Right.
Then I'm going to go ahead and use an events variable.
And let me go ahead and write graph dot stream.
And here inside the stream I'm going to give my messages in this format.
This is really important because this messages will follow in the C any user message.
We need to probably set it up in this way.
Write messages is equal to user user input stream mode values okay, stream mode values.
And then we are going to probably get our response.
So I will go ahead and write for event in events.
Let's display the event of messages minus one dot pretty print.
We are just trying to print it in a better way.
So whenever I give this particular message you can see uh, by default it is probably going ahead with
Wikipedia call.
Okay, this should not happen because it is just connecting my name with some famous personality name.
So it is probably giving you some information, right?
Sangeetha.
Krish.
It has selected.
No worries.
I will just go ahead and write.
Hey.
Hi there.
My name is John.
Let's execute this now.
Okay.
So query John Wikipedia.
See it is going to probably Wikipedia.
Uh, so no good Wikipedia.
Uh search result was found.
So it is saying hey.
Hello, John.
It's nice to meet you.
Now see it is probably going and checking the tool message.
Also whether I'm going to find this specific message or this particular name over there.
So it did not get a good result, then it is probably interacting with us.
Hey.
Saying that hello John, it's nice to meet you.
So guys, I hope you like this particular video.
Uh, this was about building chatbot applications with third party tools.
Uh, with the help of line graph.
Uh, just go ahead and try it out.
Try it out with multiple tools and see, uh, whether it goes to RC or not, just keep on adding and
keep on asking queries.
You should be able to get all the solutions over there, right?
So yes, this was it from my side.
I will see you all in the next video.
Thank you.
Take care.