Hello guys.
So let's go ahead and build our end to end generative AI application with respect to the demo that we
have already seen.
So quickly, to go ahead with what I will be doing is that I will be requiring the Streamlit library
again.
I'm going to create this entire end to end project with the help of Streamlit.
Then, uh, we are just going to go ahead and import, uh, Chad Grok because we are going to use Google
Gamma model.
Now, along with this, uh, I will be using, uh, two important chain.
One is called as LM chain and one is LM Mac chain.
Okay.
Since, uh, we need to do the entire mathematical calculation.
So this will be the library that will be useful for us.
Okay.
Uh, along with that, as you know, that, uh, we also need we will be since, uh, I also want that
thought of chain to have an interaction between the agents also.
So I will be using Wikipedia.
I will be using, uh, prompt template.
Uh, I will be using Agent type, I'll be using a initialize agent.
And all the tools will be created with this specific library called as uh, tools itself.
And this is basically coming from Langston.
Underscore agents uh load underscore dot env needs to be used if you really want to use the API key.
But since we are completely giving the API key from the website itself.
So I'll not be going to use that.
And from launching callbacks, we'll be using the Streamlit callback handler.
Now, the main importance of the Streamlit callback handler will be that the thought of chain, right?
The kind of interaction that is basically going to happen between the tool, every thing we should be
able to see it.
And we will also be able to understand that how the entire conversation has started.
Okay.
So these are some of the libraries that we will be using.
And I think in all our previous session we have discussed about all the specific libraries.
Now quickly, uh, let's go ahead and set up the Streamlit lab or Streamlit app.
So I'm going to go over here and go here and create our Streamlit app.
Then I'm going to write SD dot set page config.
And we are going to specifically use our page title which is called as text to Math Problem Solver,
okay, and Data Search Assistant, since I'm also going to create some tools.
Right.
So this will basically be my, um, you know, the page title itself.
And with respect to this, I will also go ahead and use some page icons so that whenever, uh, I see
in my browser.
Right.
I should be able to see this.
Right.
So this will basically be my page icon that I'm actually going to use along with this.
Uh, the next thing that I'm going to basically do is that provide my s t dot title.
So let me just go ahead and write s t dot title is equal to.
And here I'll say, hey this is my text to math math problem solver okay.
Okay.
And, uh, I'll just go ahead and write using Google Gamma two, because the linear model that I'm going
to use is Google Gamma two.
Okay.
And, uh, you know, uh, I can still write more information that you want, like, uh, whatever things
you really want, uh, you can go ahead and write it with the help of St Dot write if you want more
description over here.
Now quickly, let's go ahead and create our grok underscore API underscore key.
Now with respect to this grok underscore API underscore key what I am actually going to do I'll just
go ahead and write t dot sidebar.
Sidebar dot text underscore input so that I get my input for the grok API.
Right.
So here I'm going to basically go ahead and write grok uh API key.
Okay.
Once I get this grok API key then the next thing that I'm actually going to do is my password.
I'll go ahead and say, hey, it is basically a password.
I really need to get this now my entire application start with this.
So I will be giving a condition if grok underscore API underscore key colon I'll just go ahead and write
sc.info okay.
And I'll say hey please add your grok API key to continue.
Okay.
So we'll go ahead and write this.
And here I'm also going to write SD dot stop okay.
If we are not providing our grok API key that should really be the condition.
If I'm saying hey uh, if grok API key, that won't make sense, I'll go ahead and write.
If not, if the grok API key is not provided, you have to.
First of all, go ahead and provide this.
Now let's go ahead and initialize our language model.
Now here my language model will be again by using chat grok.
The model.
That name that I'm actually going to specifically use is nothing but gamma.
So this information I hope you will be able to get it from the, uh, the cloud API, the Grok Cloud
API.
Right.
So here I'll be going and writing 9BIT And here you can go ahead and see grok with respect to grok API
key.
Here we are going to initialize the grok API key.
Perfect.
So this basically becomes my language large language model okay.
Or I will just go ahead and write LLM LLM LLM over here.
So we will be able to refer it anywhere we want okay.
Now uh the next thing will be that we will start initializing our tools.
So let's start initializing the tools over here.
Now, first of all, the tool that I'm actually going to use is Wikipedia.
So I'll go ahead and create my Wikipedia wrapper, which we have again discussed in the tools and agent
types.
So here I'm going to basically go ahead and call this Wikipedia wrapper.
Whatever is the default value.
The one result or two result, whatever things you really want to use it you can use it over here.
Then I will go ahead and create my Wikipedia underscore two, which will be nothing, but this will
be my tool over here, which I've initialized it.
And then I'm going to go ahead and write name is equal to Wikipedia.
Okay.
So once I go ahead and create this then I will just go ahead and write my function.
So my function will be equal to uh I'll just go and say Wikipedia dot run.
Okay.
Or dot search.
I think it should be search dot run.
Okay.
So run is my function name.
Uh, so there is a parameter with respect to that.
If you really want to run this particular wrapper I can just go ahead and use this Wikipedia dot run.
And I'll provide some description for this so that I'll be able to see some information while I'm doing
the chat.
Uh, tool for searching the internet and solving your math problem.
Okay, something like this.
So Wikipedia is, uh.
No, just searching the internet should be the math.
Solving the math problem and, uh, searching the internet to find the various info on the on the topics
mentioned.
So this should be my entire thing.
Okay, so Wikipedia tool is there and I will just go ahead and initialize.
Initialize the math tool okay.
So let's go ahead and initialize the see Wikipedia is there.
This is just used for search right.
Now if I really want to solve the math problem.
So there is a tool over here uh, even in long chain which is called as LM matching, which we have
actually imported.
So I'll just go ahead and write math underscore chain is equal to LM math LM math chain okay.
And here uh, I'm just going to say from underscore LMS uh from underscore LMS okay.
So this is an inbuilt method.
And here I'm going to pass my LM model okay.
So LM is equal to LM okay.
So what it is going to do is that uh, this specific math chain is going to interact with our LM model,
uh, with respect to the any kind of calculation or any kind of arithmetic operation that needs to be
performed.
Then I will go ahead and create a variable.
Let's say I'll go ahead and write calculator.
So I'll just name it as calculator.
right.
And I will initialize my tool.
Uh I will give some name parameter over here.
So this will be nothing but calculator.
Um the function that we will be using is nothing but math underscore chain dot run again okay.
Like how we used it for Wikipedia.
And finally we will go ahead and give our description which will be nothing but a tool for answering
math related questions.
And here, uh, I will just make a note.
We only need to provide only input, mathematical expression.
Nothing else than this.
Otherwise it will give us an error.
Okay.
Expression needs to be provided.
Needs to be provided.
Okay.
So all this information is basically there.
Uh, this is basically my calculator tool.
Now it's time that, uh, we start, you know, solving this entire problem.
Um, and first of all, I will just go ahead and define my prompt, my prompt, and I'll say, hey,
um, let's go ahead and do this.
Uh, let's create our prompt.
So I'll say, hey, you are, uh, uh, agent tasked for solving, okay, users mathematical questions.
Okay, I'll just go ahead and write like this.
Mathematical, um, questions.
Okay.
Uh.
Mathematical questions.
Uh, logically.
Arrive at the solution and provide a detailed explanation.
Explanation.
Okay.
Um.
And display it and display it.
Point wise.
Okay.
Point wise.
Okay, so I've just written some basic command prompt, and here, um, point wise.
So for the for the question below okay.
So here I will just go ahead and write my question and I'll give my input as question.
Okay.
So this is what I am actually going to do.
And finally, I should be able to get my answer like this and just close my brackets.
Okay.
So this is basically my prompt, uh, which I'm actually going to use.
And now let me just go ahead and define my prompt template.
Okay.
So I will go ahead and write prompt template is equal to.
And here also we'll go ahead and initialize our prompt template.
Uh the input variables that we are going to use over here is nothing but question, but I hope you are
much more familiar by seeing all the tutorials before now everything looks like needs to be familiar,
right?
I, I think you will be definitely familiar with all these things.
So template will nothing be it will be my prompt.
Okay now let's uh create now the chain which will combine all the tools that we have created like Wikipedia
and all.
Okay, so first, quickly let me do one thing.
Let me just go ahead and first I will say Matt problem tool okay.
So with respect to the math problem tool, uh, you know, what is the tool that I'm actually using
that is nothing but LM chain.
So I will go ahead and initialize my LM chain and uh, okay.
LM first of all, see when I really want to, I'll say combine all the tools into chain.
Okay.
Now, what I'll be doing is that I will be using this Wikipedia.
I'll be using this map chain and I'll combine with the entire prompt template.
Okay.
But before combining them.
Right.
First of all I need to create my LM chain.
Now LM chain usually has two important things.
One is your LM model.
So I'll go ahead and initialize it.
And the second one is something called as prompt.
Prompt template okay.
So once we have this both the important things that basically means my chain is ready okay.
So here I'll go ahead and create my chain.
Let me do one thing.
Instead of writing prompt template over here.
Let me first of all give my prompt over here okay.
Since this prompt should be equal to my entire prompt template that I have actually created over here.
Okay.
The same thing.
So this is the first thing that I really need to create is my chain.
And then uh, one more.
Um, see, I also need to make sure that I add reasoning inside this.
Okay.
So for that I will create a reasoning tool and I'll say, hey, let's create this particular tool.
And this tool name should be given as reasoning tool.
Okay.
I want to add this one more thing and my function will be nothing but whatever chain that I am actually
creating this I will run this chain okay.
So I'm running this entire chain from a specific tool.
Uh, the reason why I have named it as reasoning because since they are, they are multiple things.
That is probably going to happen in this specific chain.
So, uh, we will also be running this in the form of a tool.
Okay.
And here I'll be writing my description.
And let's say I'll say hey tool for answering logic based and reasoning reasoning questions okay.
So this is done over here.
So this basically becomes my description.
Description okay.
This is all good.
Now now we are just going to go ahead and initialize our agents.
So let's initialize the.
The agents okay.
Now in order to initialize the agent I will first of all go ahead and write assistant underscore agent
is equal to initialize the agent.
And here I will go ahead and say tools what all tools I want I will just go ahead and combine it over
here like Wikipedia tool okay.
The second tool will basically be my calculator tool or callback manager.
My calculator okay.
So this will be another one tool.
And along with this we will go ahead and add one more which is called as reasoning tool, which will
be responsible in running the entire chain.
So in short, what we have done is that my LM chain, I have converted this into a tool.
Okay.
And I'm saying that, hey, this selenium chain should actually give you the entire answers.
But I'm saying, hey, it is a reasoning tool because it has some sort of chain that may be going right
because it is interacting with the LM model.
Now finally I will go ahead and create my LM is equal to LM.
And let me just go ahead and create my agent.
So here I'll be using agent type dot zero short react description.
And I have already discussed what exactly this is.
I will also go ahead and write verbose is equal to true just to see my detailed, um, uh, detailed
uh, uh, detail in uh, thought of chain process.
But I'll keep it as false because this will get, uh, printed in the console itself.
Right.
Sorry.
In the terminal.
Right.
So here, uh, I will also go ahead and write handle underscore parsing underscore error is equal to
true.
Okay.
So I'm also going to keep this particular parameter over here.
Right.
So almost each and every thing is basically being used.
And over here we have also used the initialized agent.
So this is good to go now okay.
But always remember we also need to make sure that we create our session state.
So let me just go ahead and write if messages.
So here I have written if messages are not in st dot session underscore st dot session underscore state
so that I will create this particular state session underscore state okay.
And here I am just going to write st dot session underscore state if for messages.
Okay.
Now inside this particular messages I am going to go ahead and add my role.
So let's say by default uh the role that I'm adding is two roles.
One is assistant.
And uh this assistant will also be in string.
And the second thing that I'm actually going to add is my content by default content I'll say hey,
content colon.
I'll say, hey, um, hi, I'm a math chat bot.
Who can answer?
Who can answer all your maths question?
Okay, something like this.
So yes, this was my role that I've actually added it over here.
Now quickly I'll just go ahead and say for message in s t s t dot session underscore state dot messages.
And here we are specifically going to use this s t dot chat underscore message okay.
Chat underscore message.
And here I'm going to use my, my uh my message my message of role okay comma dot right of message of
content.
So I'm just going to go ahead and write this entire content with respect to all the conversation inside
this chat dot chat underscore message.
Okay.
Now finally I'm going to go ahead and create.
And this is something that we do commonly whenever we want to use session state over in every project
that you really want to do.
Now I will say hey function to handle uh or to generate the response, I'll go ahead and create my own
function.
And for this I will write definition generate underscore.
Response will be nothing, but it will be user underscore question okay.
And here I'll go ahead and create my response.
Assistant underscore agent.
And here I'm just going to go ahead and invoke okay.
Um so this assistant agent that you'll be seeing right.
What it exactly it is this is it.
Right.
And I'm writing dot invoke over here and I'll be giving my input.
Right.
So this will specifically be my input.
And here I'm going to give my whatever question that I have.
Let me just go ahead and remove this and let me write it over here.
So this will basically be my question.
Okay.
Um, now, uh, along with this, I will go ahead and write return response.
Okay.
And, uh, this are all my values.
Remember one thing, guys, I forgot to tell you that when we initially imported.
Right?
I'm considering that all these things are have installed in your requirement dot txt.
So from the starting session till now, whatever requirement dot txt we have actually created, I've
just kept it everything in one place so that you can just go ahead and install it and you can start
using it.
Okay.
So you can also go ahead and do that.
Now this will basically give us the response.
Now let's start the interaction.
So I will say hey uh let's start the let's start the interaction okay.
Now here what I'm actually going to do I'll write start button, uh, and I'll say uh, find my answer.
Find my answer.
But before this, uh, before I start clicking the specific button, I will also go ahead and create
my question.
And here I'll say t dot text area.
And by default I will put one question over here.
I'll say enter your question Okay.
And here you'll be able to see that I will go ahead and write my question okay.
Now one of the question that I have taken over here, I have to created over here is like this question,
right?
The same demo one which we saw.
I have five bananas, seven grapes.
I eat two bananas and gave away all these things.
Right.
So all this, uh, information will be given by default.
And we can also use it if you want.
Otherwise leave it.
Okay.
So here if I go ahead and write if question after cat, after clicking the button write get answer,
I'll say hey, if there is a question, write.
If there is a question then what do you do?
Run a TT dot spinner okay, so these are some components in uh Streamlit.
So here I will write with TT dot spinner.
And let me say that there is some message that is basically getting generated.
So I'll say generate response.
And here I will go ahead and write like this.
And then I will just go ahead and write TX dot session underscore state dot messages dot append okay.
And here I'm going to basically write role colon user.
Right.
So this will basically be my role that I have to probably set it up before we start this conversation.
And the content will be equal to this specific question that I'm passing.
Okay.
So this is what we want first of all appended.
Then we will go ahead and start SD dot chat message.
Okay.
Chat message.
And here I will set my user along with that.
Uh.
uh, sorry user and I will.
Write the user question.
So it should be able to it should be visible in that particular.
Uh chat message.
Right.
So that is the reason I'm putting this entire information.
So this will basically be my question.
Now quickly let's go ahead and write s t dot CB where I will be creating my Streamlit callback handler.
And inside this I will say TX dot container.
I'll initialize the container, which will basically display the entire thoughts of action and I'll
say hey, go ahead and make the tab false.
It should not be visible in the initial stages.
Later on I can click and I can probably expand it.
Now I will go ahead and get my response.
So for this I will say assistant.
Uh, I can either see I can either call this function like generate response.
Okay.
It is up to you what you really want to use it.
Okay.
Or, uh, what you can do is that you can directly call assistant.
So let me just remove this because again why to create this particular function I know it is not a best
practice, but I will just go ahead and write it over here because I just need to call it from here
itself.
So assistant Dot run.
But if you are probably developing um, bigger application where you have front end, back end, many
things, as such, you have to probably go ahead and create a function.
So here I will write SD dot session.
Session underscore state dot messages okay.
And here I'm going to also integrate with it callbacks.
So callback will be s t comma cb okay so this is it.
Uh and then finally I will just open my new braces.
Uh, sorry.
New line.
I will say TX dot session underscore state dot messages.
And I'm just going to append each and everything over here.
I'll go ahead and write append.
So it should be messages.
Let's see messages over here.
Messages over here.
Uh messages also over here okay.
So it should be matching each and everything.
Okay so append and here I'm just going to go ahead and add my role.
So the role will be with respect to assistant.
Because once I get my information right what role we need to assign it needs to be assistant.
It needs to be assistant.
And then we will go ahead and add our content which will be response.
Okay.
So finally let's go ahead and print the response if you want.
Okay.
So I'll go ahead and say print the response.
Uh just to print it in the terminal.
Otherwise instead of writing this I'll just go ahead and say s t dot.
Right.
And let me add my response.
colon.
And here I'll say TX dot success.
This will be my final response.
Okay, uh, if the question is given, that is fine.
Otherwise I'll say else.
SD dot warning array by input --.
Please enter your input.
Please enter the question.
Okay.
That's it.
So quickly.
I think within 10 to 15 minutes, I was able to do the entire coding.
And step by step, I've actually showed you over here.
Uh, let me just go ahead first of all and show you what all things we did.
We had one grok API key, uh, text input so that we can put it all the information.
Then we have this.
We initialized our, uh, LLM model.
Then we created our tools like Wikipedia math.
Then we had our prompt.
Then we had our prompt template.
Then finally we combined the prompt and the LM model using the LM chain.
Then we converted this LM chain into a tool.
Right.
That is what we really did.
And then we initialized the agent by combining all these tools uh with the LM model.
And then type what we really need to do.
Then we created the session state.
And we are making sure that every time whatever input specifically comes, we append in this particular
state session state.
And now here I have my question.
And once we click this particular question it should probably go ahead and interact with the agent and
probably give you the output okay.
So this was overall the entire thing.
So let me do one thing.
Let me just go ahead and write CD dot dot and I will go ahead to CD.
If you see over here, this is my Mac GPT eighth lesson.
So I will go ahead and write math CD eight Mac GPT.
Okay.
And now in order to run it Streamlit run app dot pi or app dot pi.
Now before that I will go to my environment variable, take my grok API key and just run it.
Let's press enter.
So here you can see it.
Let me close this.
So here I have my API.
So I'll go ahead and press enter over here.
So here you can see one positional argument is missing in Wikipedia because of description.
I know what is the error.
I think there should be a spelling mistake.
Okay.
Uh, this is description.
Wikipedia.
That was a spelling mistakes.
Okay.
I will say always rerun.
Okay.
It is asking for installing num expression.
So let's go ahead and do that so quickly.
I will go ahead and open my another command prompt.
And this will be one of the requirement.
Uh, I will just go ahead and update in my requirement dot txt.
Let me go ahead and paste it over here and let me press enter.
So I'll say pip install.
Minus our requirement dot txt.
So let's go ahead and do this installation.
We one of the dependency I think this is just for the numerical expression.
You know how to probably use this numerical expression how to calculate it.
So once it is successfully installed I'll go over here.
Now let's reload this okay.
Let's go back to my env.
I will go ahead and copy my grok API key.
So two errors we have faced till now I'll press enter.
And here you have this.
Hi, I'm a mad chat bot.
Uh, who can answer all your mathematical questions or maths question?
I'll just go ahead and click on Find answer.
So here you can see thinking let's break down.
Reasoning is basically happening right.
And over here some must funny funny logos has also been used.
Okay.
So finally you can see 69 is my answer.
Okay I'll say what is what is.
Um, let's say, um, I have 20 apples, I divide, I divide, I, I have five friends, I have five
friends, and I give each friend.
two apples, are two apples and later takes one apple back from them.
So this is most like a logical reasoning, right?
Uh, so how many Apple do I have?
Okay.
I'm just trying to make.
So okay so I'll go ahead and click on find my answer.
So let's see.
See over here all this question response will also be over here right.
So here you can see I have got 69.
Now the next question over here I'll go ahead and see this one -1010 plus one.
Uh okay.
You take one apple back one apple back from them.
Okay.
Did not understand from each of them okay.
So they're saying okay I have 11 apples have left.
If I write from each of them, I think I should be from each of them.
Okay.
Now I think it should be able to give the answer.
Let's see.
So it should be able to give you 15 right.
So yes go ahead and try it out.
This is another amazing project that we have actually developed.
Uh, you can give any kind of complexity.
Just try to give it in the form of sentence and uh, then try to see that whether you are able to get
all the answers or not.
So yes, this was it from my side.
I hope you like this particular video.
Uh, this is amazing text to maths problem solver using Google Gemini to, uh, sorry, Google Gamma
to uh.
I hope you like this particular video.
I'll see you in the next video.
Thank you.
Have a great day.
Bye bye.