Hello guys.
So we are going to continue the discussion with respect to creating this amazing end to end search engine,
an AI application, with the help of tools and agents.
Already in our previous video, we have seen that how we can use Lang chain in order to create tools
and how we can execute it with the help of agents.
And uh, in our previous video, we have already seen about creating your own custom tools.
Along with that, we have also seen how to use the inbuilt tools that are supported by Lang Chain.
Now let's go ahead and quickly start our project.
So here in my search engine folder you will be able to see that I have created my app Dot Pi project.
Uh, so folder by folder I am trying to execute this specific project.
So let's go ahead without wasting any time.
So first of all we will go ahead and import Streamlit as st along with this.
Uh, what we are basically going to do is that we are also going to import all the other things that
we require.
Okay, so I'll just remove this.
I don't know why this suggestion usually comes, but can't help.
Then uh, quickly I will go ahead and use something called as lang chain underscore grok.
We are going to use grok over here to access the open source models.
So I'm just going to go ahead and write Chad Grok.
And then from this I'm also be using lang chain underscore uh, community dot utils.
Okay.
I've already shown you how to probably go ahead and create your wrappers.
Right.
So here also we are going to use the same wrappers.
So I'll go ahead and write utilities import.
One is the RCF wrapper.
And then I'm going to use the Wikipedia wrapper.
So let's go ahead and use this two wrappers for right now.
And again you can go ahead and explore other tools.
Also in this I will also add one search engine tool.
Okay.
Let let's see that also.
Okay.
Along with this you will be seeing that I will go ahead and import from lang chain underscore community
dot tools.
Inside the tools we will go ahead and import our chef query.
And then I'll also have this uh Wikipedia query run.
Okay.
So our query run, and then I'll also be having this Wikipedia query run.
Along with this I will also add one like capabilities to search from the internet.
Right.
So for that I have this duck duck duck duck duck go search run okay.
So I'll be using this also which will actually help you to search anything in the internet.
Right.
So all these, uh, are the important imports that we are specifically going to do now, I will also
be importing something called as from Lang Chain.
I will be importing something called as agents.
So let's first of all go ahead and initialize the agent over here.
Along with this there is an option of putting something called as agent type which I will be discussing.
What is the importance of this?
I will go ahead and discuss about this also along with this.
So let's go ahead and import our next library.
So here we are also going to use something called as lang sched dot callbacks.
And I'm going to import something called as streamlet callback handler.
And this is important because this will allow you to communicate with all this kind of tools within
themselves.
Right.
So all this libraries will be using initially right now uh streamlet has also been imported.
I will go ahead and import OS if I want over here.
Along with this, I will go ahead and write import OS, then from dot env import load underscore dot
env.
I'm also going to use this okay.
Uh, if you remember in our chatbot also right.
We specifically did not even have to call any APIs.
Right.
I was directly copying and pasting in the, uh, web application itself.
So I will try to also develop in that specific way.
So if I go ahead and open this OpenAI chat bot here, you'll be able to see I had created this all slide
bars.
Right.
So here I will also use one of the slide bar okay.
And I will keep this handy when I will be using it okay.
So I'll just paste it over here to keep this sidebar settings and I'll say hey, enter your grok API
key okay.
So I will go ahead and enter the grok API key.
So I'll keep this also handy so that I don't have to even call my load underscore dot EMV right for
the same thing.
Now, the next thing, uh, after this, what I am actually going to do is that after importing all
these things, let's also, you know, take this entire things.
And first of all, I will go ahead and, you know, create my RCF wrapper, API wrapper and uh, the
Wikipedia API wrapper.
And already I have actually shown you how to do that.
This is for my RCF tools, which I had actually shown you in my previous video, Agip and Wikipedia
tools.
Right.
So here we have initialized it, uh, along with this, as I said that I'm also going to use, uh,
a duck.
Duck go search.
So that is what I'm actually going to do.
In order to use that particular tool, I will go ahead and create a variable which is called as search
is equal to duck, duck, go, search, run.
And here the kind of task that I'm telling it to do, or the name that I'm actually going to give,
this is something called as search.
So here the name will be Wikipedia.
Here the name will be RCF and And this will be my search functionality, which I really need to search
from the internet itself.
Right.
And uh, after doing all these things now let's go ahead and create my Streamlit app.
So first of all, I will go quickly.
Go ahead and write s t dot title.
I'll just go ahead and write link chain with chat with search.
Okay.
Um, let me just give a simple reminder or a comment over here saying that, hey, in this example,
we are using something called as Streamlet callback handler to display the thoughts and action of the
agent in an interactive Streamlet app.
Okay, so this will actually help you to, uh, this Streamlet callback handler will actually help you
to understand, like what thoughts and action of the agents will be when they are executing all this
kind of tools, or when they are interacting with the tools.
Okay then, I have imported this specifically for my grok API.
Okay.
Now what I'm actually going to do, since I also need to make sure that now my entire conversation over
here should happen along with the chat history.
So I will go ahead and create one simple.
I'll just say, hey, if message is I'll, I'll basically create my session state.
So I'll go ahead and write session underscore state over here.
And along with this I'll write hey s t dot session state of messages okay.
So I'm just going to go ahead and create this messages equal to.
And here I'll just go ahead and give some of the important uh, message, uh roles that I'm actually
going to provide.
So first one information that I really need to put is something about role.
So role will be nothing but assistant.
And then I will say, hey, this is my content.
So let me just go ahead and write the content.
It will be again colon I'll say, hi, I am a chat bot who can search the web.
Okay.
Uh, or I'll also put one message.
How can I help you?
Okay, so this will be the default assistant, uh, information that you have.
So this.
I'm saving it in the session state.
Okay.
Then I will go ahead and say for every messages, we will go ahead and traverse it.
So I'll write for message in s t dot session state session s t dot session.
Underscore state dot messages.
Okay.
And here I'm just going to use state dot chat underscore message.
I'm basically going to give my message role.
Okay.
Now inside this message role I'm just going to go ahead and write dot write right dot right over here
as my message content.
So with respect to any conversation that I get because I'm going to put all this specific role information
like how I put over here inside my session state as we go ahead.
Right.
So right now in this key value pairs I will keep on adding it.
And we will be making sure that we add all these values in our session state.
Okay.
Now, uh, let's go ahead and do one thing.
Let's go ahead and create my prompt.
Okay.
Now prompt is something, uh, you know, by default some text message will be there.
So I'll write from prompt colon equal to okay I'll go ahead and say TX dot chat underscore input okay.
And here I'm going to just go ahead and see chat.
Underscore input is also one very important uh component in Streamlit.
And here we are just going to go ahead and put our placeholder.
So I will go ahead and write my placeholder.
And I'll say hey what is machine learning.
I'm just going to put a simple basic message, okay.
Uh, regarding this chat underscore input.
Now this prompt by default will be having this specific placeholder.
Now I will go inside this.
If this particular prompt is specifically written, I will go ahead and say, hey, let's go ahead and
create this SD dot session state session underscore state.
And I'm going to take this messages.
Um let's see.
Yes messages dot I'm going to append some role.
Right.
And initially whenever I try to put any if I try to ask any query the role, you know what it should
be added.
It should be added as a user.
So this will be my role.
Okay.
And the content that I'm actually going to add is nothing but my prompt.
Okay.
So here I'm going to probably go ahead and create my prompt.
Perfect.
So once we add this then the next step will be that I will go ahead and say TT dot chat underscore message.
And here I'm going to basically use user.
And we are just going to write this prompt dot right of prompt.
Okay.
So what we are basically doing is that whenever we write any prompt, it is going to get appended in
this particular message so that we will be able to read this.
And.
All right.
Uh, so this is also done.
Uh, now the next step will be that, uh, inside this right inside this prompt itself, we will go
ahead and create our LM.
So I'll write chat grok.
And.
I will go ahead and use chat grok underscore API underscore key okay.
Which will be equal to I think I've written right chat grok.
And here have I imported chat grok or not.
Yes I have imported Chat Grok.
So here I will be using my grok API key.
So let's see.
The grok API key has been imported or not.
It's okay.
So we are going to get this grok API key from our uh parameters right.
Which we will be getting it through from the input itself.
Right.
So here what I will do, I will just use this API key that I'm actually going to use it over here okay.
So once this is done, uh, this will basically be my model.
And here I'm just going to use my model underscore name.
You can use any model for now I'm using llama three 8 billion parameter.
And this will be with 8912 okay.
So this will be my model.
And here I'm also going to keep my parameter as streaming is equal to true.
So let me just go ahead and write it over here.
And streaming will be equal to true okay.
Okay.
So this basically becomes my LM model altogether.
Um, I have initialized all the all the tools, whichever I really want to use it.
Okay.
And these are the tools that I initialized.
Right.
Uh, one is uh, the wiki, one is RCF and one is search.
So I will go ahead and create a variable called as tools.
And I will initialize all these tools over here.
Okay.
So let's say I will go ahead and write.
One is search.
Then one is are safe.
Then one is wiki.
Okay.
All these tools is specifically been added.
Now I need to convert this entire tools into an agent so that I will be able to, you know, invoke
this specific agent.
So I will say, hey, this is my search agent.
And here I'm going to basically use this initialize agent function.
And along with this I will be using my uh my tools will be going over here.
So this will be my tools.
This tools will be in the form of list that you have over here.
So all the search search Ar7 wiki we are going to specifically use over here.
And along with this the second parameter will be my LM which I'm actually going to use.
The third will be something called as agent.
So the agent that we are going to basically initialize over here is my agent type dot.
There will be something called as zero short react description.
And there is also something called as chat zero short react description.
And there is one more something called as structured chat zero short react description.
You can definitely see the documentation what exactly this means, but let me give you a brief idea.
What is the main difference between zero shot react description and zero shot react description?
It basically lies in how they handle our context and memory, how they generate prompts for the language
model.
See in the case of zero shot, zero shot, uh, react description, they don't rely on the chat history,
right?
Uh, it makes a decision based on the current input only without considering any previous action.
Similarly, on the other hand, if I use Chad zero shot react description, it uses a chat history to
remember the context of the chat and the history of the conversation, and it accepts a certain structure
in the chat history and might raise an error if it does not find it.
Okay, so in our case, I will just go ahead and use this zero shot react description.
And here I'm also going to make sure that I give one more additional parameter which is called as handling
pass.
So here I'm going to go ahead and write handling underscore pass is passing.
Sorry.
Passing underscore errors is equal to true.
I'm also saying that if you face any errors please make sure that you also pass those errors okay.
So this are the basic parameters that I specifically require over here.
Right.
So here inside all these things I've actually given this Let me make the spelling right.
Search underscore agent.
Okay.
And this is how we basically create this agent.
Now it's time that we go ahead and have this kind of conversation.
And it should be able to give us an answer.
So I'll try it with s t dot chat underscore message.
And I'm going to give the history over here or sorry message of assistant.
Right.
So I'll go ahead and give like assistant.
Okay.
Whenever the assistant is giving me any kind of response.
So what I'm actually going to do is that I'm also going to call this Streamlit callback handler.
I know there are multiple things that I'm actually showing you, but this is the best way of creating
this, right?
So once I use the Streamlit callback handler, understand what it is doing it is displaying.
It is used to display the thoughts and action of an agent in an interactive Streamlit app.
So now whenever my agent is basically communicating with itself, right?
It should be.
We should be able to see that and that will be able to see once I will execute it.
So here I will be using an SD dot container.
So container over here.
And if you see what exactly this container is it in turn insert a multi element container.
Inserts an invisible container into your app that can be used to hold multiple elements.
This allows you to, for example, insert multiple elements into your app out of order.
Okay, that is the reason we are specifically using.
You'll be seeing will be able to see that block wise.
You'll be having that entire information.
And then we will go ahead and create one more parameter called as expand underscore new underscore thoughts.
And I'll say, please, whatever thoughts you have show me that detailed explanation.
And if I say expand, right, so every thought will probably just get opened.
We can also open this, but right now I'll just keep it to false.
So again this expand new thoughts you'll be able to see once I probably, uh, run my entire application.
You'll be seeing that by default it will be closed.
You can also open it okay.
Now let's go ahead and get the response so quickly I will go ahead and write.
Response is equal to search underscore agent dot run.
I will run this and while running I will also give all my session state dot messages okay.
And I will say hey you can also make a callback.
So there is a parameter which is called as callbacks with whatever callback we have actually created,
right?
Streamlit callback handler.
So I'll go ahead and say TX comma cb okay, once I do this now the next step will be that I will go
ahead and again append it's t dot session, underscore state dot messages dot append.
And here I'm going to specifically use my role as assistant because this will be given by the assistant
itself okay.
And my whatever response I'm actually specifically getting it will be inserted into the content part.
So here I have this response okay.
Now quickly let's go ahead and write SD dot write response okay.
So this is what it is.
And uh I think we have executed this entire thing.
Uh, let's execute this entire thing and see whether everything is working fine or not.
But I think it should work.
Just to give you a quick revision, we have created all the tools wrapper.
Then we use this tools parameter over here which will be included.
We created our search agent LM agent type this, this this and we are good to go with respect to this
okay.
And this is completely with the open source uh LM models.
Now I will quickly go ahead and write CD which folder it is in five search engine.
So I will go ahead and write right five tab search engine.
Now quickly let's go ahead and run it.
Streamlit run app.py okay.
So once I run this I hope so.
It should get executed.
Okay, so here it is.
Um, HDR object has no attribute.
Let's see.
So it says on line 36 I'm having this particular error.
Let's see okay.
Line 36 I am I'm having this particular error.
Let me go ahead and check to this.
What is exactly the problem over here.
So if I probably see this message 36 it is chat underscore message message dot okay I made one specific
simple mistake.
I should have close the brackets over here.
And no need of this specific brackets.
So whenever you get an error, don't feel afraid of the things that you are seeing.
Instead, just try to fix it.
Now this looks absolutely fabulous, right?
Uh, quickly, let me just go ahead and take my grok key.
I will copy this entire thing over here.
I'll go over here and press enter, and let's go ahead and do.
Okay.
I did not write one more condition over here.
Okay.
Um, and that is the reason it may give us an error.
I'll say if prompt and and write and the API key, I have to probably give this API key.
Okay.
Now I think it should run.
Oh let's go ahead and copy and paste it.
I will press enter.
Now I will go ahead and write what is generative AI?
Oh God, it is getting displayed.
Why it is getting displayed, we'll see to it.
But at least the functionality is working over here, right?
So this is my key.
The key should not get displayed over here.
But it got displayed.
Okay.
Let's see.
Uh, am I printing somewhere the API key?
So API key is over here.
Okay.
It is also getting appended in this specific message okay.
But let's see whether everything is working fine or not.
So here you can see I asked what is machine learning okay.
Um or here you can see what can I search.
Okay.
Let's see I'll ask what is machine learning.
Let's hit one more time okay.
So again this is getting displayed.
How can I help you search okay.
There there is some problem over here.
Let's see some problem.
Some problem.
So whenever I'm hitting a query write prompt, then it is writing the specific prompt.
The simple reason is that because it is considering this as a prompt.
Okay.
So uh, that is the reason you will be able to see that when I'm hitting this and I'm putting this particular
condition.
Okay, let's do one thing.
Let's remove this for now.
Onwards.
Okay.
And let's reload it.
We'll go ahead and reload it.
I will quickly do one thing.
I will put my API key, I'll press enter.
And now I will just go ahead and write what is machine learning.
So now I'm getting the input because the API key was basically getting uh, you know hidden over there.
So now you see, see see the perfect thing As soon as I searched what is machine learning?
It it went to directly to Wikipedia.
Now, see, the interaction is also basically happening right here.
You can see automatically Wikipedia is able to give some more information.
Okay.
Then it automatically took the examples.
Then Wikipedia is saying hey I have more details.
Go ahead and see this.
Right.
Uh, and here you can see, hey, Wikipedia is also there.
Right.
And all the information is here is the response.
All the information is basically coming up.
Right.
And finally this is the output that I'm able to get.
See, so much interaction is basically happening right.
Uh this content is basically getting displayed I'm printing it right.
Action.
Everything is going on okay okay.
Tell me about the research paper.
Okay.
Um, attention is all you need.
and I'll go ahead and press enter.
See, it went to arXiv.
Amazing.
Right.
So here now it is thinking now Wikipedia came into picture.
Okay.
Then suddenly it went to search.
Attention is all you need arXiv paper.
Then again it went to search.
See how the interaction is basically happening, right?
I thought I understood that the user wants to know more about the research paper right over here.
This is the thought.
Here is the continuation.
The Wikipedia search found a relevant way, but it is not a research paper.
See, attention is all you need.
You don't want to know more about this.
Or I say attention is no good or save result was found.
Amazing right?
So this automatically this is like you know you have independent tools who are interacting with the
outside world.
It's like I'm having four people who's probably doing all the search and who is probably, you know,
making sure that all the things basically happen.
And one error is over here handling parsing errors.
Okay.
An output parser, this is error could not pass LM thought I have found multiple research paper okay.
So let's see.
We'll try to fix this also.
But uh uh I think uh, this is just because uh, they have actually got multiple results, like no one
is coming to the right purpose right over here.
So now let's see.
I'll go ahead and write.
Attention is all you need.
Okay.
And I will just go and search for this research paper.
Okay.
I will say hey.
Provide a summary of.
So here you can see that automatically all this summary will be found out by ourself.
Right here all the information once a summary of attention is all you need.
Then again it is doing this.
It did not have to probably go with or ask any other tools.
The user is asking title this, this this.
All the information is over here.
The user is asking for the summary of the research paper, which is a published paper on arXiv.
Right.
So this is going on and this interaction will keep on going ahead.
And finally you'll be able to see some output.
It will be able to probably provide you okay.
But uh, here I see that it is also going into that entire loop I agent stopped due to iteration limit.
Yes.
So this can also be one type of problem.
Uh, we'll see in the upcoming videos how we can fix this kind of problem also.
Okay, so let me just go ahead and ask, just like this okay, I want.
Tell me about the research paper.
Nothing but this.
So here you can see the user is asking about a specific I have a tool to search for it on arXiv right
then Wikipedia, then all this interaction is coming.
And this paper is this authored by so and so.
All the information is over here.
Right.
Let me just go ahead and search for something like what is generative AI.
Now I think it should probably go with Wikipedia or search.
Right.
So search is duck duck go search right.
Thought the user has multiple ranges ranging from general topics like what is the machine learning to
specifically see.
It is also using the chat history.
Right.
And based on that it is giving you the entire response.
Right?
I've already provided a brief overview.
See, it's saying that again.
I'll not go ahead and provide it.
I've already provided it right.
Generative AI right.
And here are all the answers.
So I hope you are able to understand this.
Uh, again, um, if you want to go ahead and explore more examples, you can go ahead and check it
out over here.
Right.
So yes, this was it about this entire end to end projects.
Uh, we'll see, uh, some more errors that we are getting.
I think I need to have a look why we got that specific error, but I don't think so.
It is a major issue with respect to that.
Unless and until we are having a good interaction between the agents, I think we can work out.
One assignment that I really want to give you is that try to integrate a PDF document Q&A along with
these tools, and try to add it also and try to see that how the interaction will basically happen.
So yes, this was it for my side.
I'll see you all in the next video.
Thank you.