So guys, now finally we will go ahead and start our development of our application.
So here inside this I will go ahead and create my first app.py file.
And this will be my Streamlit app.py file.
So initially what I will do is that quickly go ahead with installing some of the basic libraries.
So first of all I will go ahead and import Streamlit okay as St.
So once we import the Streamlit uh, which we are basically going to use for our entire application,
I'm also going to install one more library, which is called as Pathlib, so that I will be able to
get my absolute path over here.
Um, in, uh, c in Lang Chain, we have an agent, right.
Which will actually help us to create the sequel agent itself.
As I told you, at the end of the day, there will be a kind of sequel toolkit that we are going to
run, and that entire toolkit will be able to run with the help of SQL agent itself.
So in long chain you have something called as create sequel agent.
So you can probably go ahead and just see the definition, uh, around it.
So let me just show you once, like how you should probably go ahead and refer it.
So here I'm going to go ahead and write long chain sequel agent okay.
Create sequel agent.
So let me just go ahead and write create sequel agent.
So this is just like creating a different kind of agents itself, right?
So once I click this here you will able to see about the create sequel agent.
So that is what we are going to use over here.
Right.
And with the help of this create sequel agent.
Right.
So here you can see, uh, while using OpenAI function call API to drive this, we use this.
And specifically if I go ahead and see this particular reference of create SQL agent.
So here uh we are going to use this.
It creates a SQL agent from an LM and toolkit or a database.
Right.
So this is what we are going to basically use over here right in our application.
So for this we will be using uh create uh SQL agent.
One more important uh thing that we use specifically for the SQL databases, this particular library,
I'll talk about it.
Why do we use it now after importing this particular library?
Let's see what all other libraries we will be requiring.
And as I said, you first of all, uh, there will be an agent that we really need to create and, uh,
uh, the inbuilt sequel toolkit is also available.
So for this we will be first of all using one agent type, then this Streamlit callback handler, which
we have also used in our previous project.
Uh, and this is what I'm actually talking about, the agent toolkits, which is nothing but the sequel
database toolkit, which we are going to specifically use.
Okay.
Again, uh, you can go ahead and see in the documentation about this entire toolkit, more detail if
you really want to know and understand.
Okay.
Now finally, what I will do is that I will go ahead and write from SQL alchemy.
Okay.
This is also what I'm actually going to use.
Uh, SQL alchemy will actually help you to map with respect to the output that is specifically coming
from your SQL database.
Okay.
So here we are going to import this something called as Create engine.
Uh this is one of the another library that we will be using.
Okay.
Uh, along with this since we are also going to work with SQLite three.
So I'm going to write SQLite three over here.
Okay.
And then finally since we are using uh lang chain underscore grok I'm going to go ahead and import Chad
Grok okay.
So this is what we will be using to interact with our open source LM model okay.
Now quickly uh we will go ahead and set our page configuration of our Streamlit and our, our title.
I'll say, hey Lang in chat with SQL DB.
This will be the page title icon.
I've used this particular bird symbol, which is very much famous in Streamlit.
Almost everybody is creating this kind of application, uses this kind of bird symbol.
Okay, uh, one major problem with, uh, Streamlit is that with the chat with SQL DB is that there
may be some kind of vulnerable prompt injection.
Okay, so prompt injection kind of warnings I can actually give it.
So use a DB role with limited permission.
So please make sure that you give this kind of warning so that it will be shown in the red color.
Okay.
So till now I'll just remove it.
I will just try to add it in the later stages.
Now quickly uh, I will go ahead and create my two local variables.
So two global variables.
One is my local DB.
I'll say, hey, uh, I'll create this particular variable.
I'll say, hey, use local underscore db, local DB over here okay.
And then I will say for my sequel I will create this particular variable value use underscore my sequel.
Okay.
The reason is very much simple because I am trying to implement with respect to two databases okay.
Now what we will do on the left hand side we will create some radio option.
Okay.
So here I'm going to basically go ahead and define some radio options radio button like kind of thing.
So here I will go ahead and write radio opt.
And here we are going to specifically use SQLite three database.
By default.
I will give two options with respect to this particular radio option.
I'll say hey, the first DB is interact with the student dot db, and the second DB is nothing but connect
to your SQL database.
Okay, so these are the two things that I am going to specifically use.
Okay.
Then you have this selected underscore opt okay selected underscore opt.
And here I'm going to basically use um I'm going to say hey create this two radio button on the side
wise.
So I'll say use dot sidebar dot radio.
And here I'm going to basically use.
Label I'll say hey choose the DB.
Which you want to chat.
Okay.
Something like this.
And here I will give two options which I've actually defined in my radio opt.
Okay.
So here my second parameter is something called as option.
And here I'm going to basically define it to radio opt.
So this in the left hand side in the sidebar, it just gives you a kind of two radio buttons which you
can probably select.
Okay.
So all this information is done till here.
Now let's say if I am selecting.
So I will go ahead and write.
If radio opt dot index what.
Whenever I probably select uh this radio buttons I will be getting some kind of index.
So here I'm going to write.
If this opt dot selected operation or subtracted option is double equal to one, I am going to say that.
See if you are selecting an.
Every index in Python starts from zero.
Similarly in radio option also.
But my first option is with respect to the student dot db first, uh, sorry zeroth option.
This is my zeroth index and this is my first index since I'm selecting, uh, the first index over here,
then my DB or I will be sorry DB underscore URI will be nothing, but it will be.
I'll be I'll just set it to my sequel.
Okay.
My sequel uh, over here I had created this particular variable.
So my sequel is nothing, but it is saying use my sequel.
Okay.
And whenever we select my sequel, we need to show four more option.
And this, my sequel is with respect to my local database that I've actually installed the the MySQL
workbench.
Okay.
So here first of all I will be requiring MySQL host.
And with this I will go ahead and write SD dot sidebar.
Uh, again I see for this MySQL host.
Also I will be requiring some information.
Right?
Without this information I will not be able to connect it to my MySQL workbench.
So for connecting it I will be requiring some information like MySQL SD dot sidebar.
I'll be using a text input and then say hey, provide my provide MySQL hostname.
Okay, I will say like this.
Okay, so this will be the first parameter that I will be requiring in order to connect it to my SQL
workbench.
Okay.
Then the next thing that I actually require is my MySQL user.
And here we are basically going to again go ahead and write SD dot sidebar dot text underscore input.
And here I'm going to basically use my SQL user okay.
What is my username.
Okay.
So this will be the second parameter that I will require.
First provide my SQL host then my SQL user.
Along with this I will also be requiring my password Write my sequel underscore password.
These are the basic information, and I'll try to show you how we can map this with our MySQL workbench.
Okay, so here I will just go ahead and create this particular variable MySQL password.
And here also I will go ahead and write my dot sidebar dot text underscore input.
And here I'm actually going to give my SQL password.
And with respect to this I will go ahead and write type is equal to password okay.
Here we are going to specifically give the password itself.
Finally I will go ahead and write MySQL underscore db is equal to.
SD dot sidebar.
Because here also I have to give my database name which database I really need to interact.
So text underscore input.
And here I will be having my SQL database.
So this all information uh the user needs to provide from the application itself okay.
So that is the reason we are taking this information.
Now what does this basically mean.
Like if I go ahead and select connect to your MySQL database okay.
If I go ahead and write like this, that basically means we are going to connect to our MySQL workbench.
So for this you require information like MySQL host user password and db.
Okay.
Uh, this is all the information that will be definitely required if we select the first option.
So I will go ahead and write in the else block.
If we select the first option then my DB URI will be nothing, but it will be just pointing to this
local DB.
Local DB is nothing, but it says use local underscore DB.
Okay Now finally, if this selection is done and you know that this all information will be displayed
by taking all this information, I just need to connect it in the later stages.
Now I also require one important API key that is my grok API key right.
So here what I will do I will use this, uh I will say hey let's use this sidebar sidebar st.
Dot sidebar.
And I'm just going to say, hey, while we are using this, let's go ahead and use our text input.
And inside this text input I'm going to give my label.
The label will be grok API key.
And the type will be password over here because I really want to keep this all things hidden.
Okay.
So this through this, we will be, uh, getting our API key.
We'll be getting our DB ora and.
All right, so now, uh, first of all, I need to make sure that all this radio option button needs
to be selected, and this API key needs to be given.
Right.
Unless and until these two are not given, we cannot chat with the database.
So what we'll do over here is that we'll write two important conditions.
Okay.
One is if not db underscore URI, then I will just go ahead and write s3.info.
So inside s3.info I will go ahead and say please enter the database information and URI.
Okay.
So I'm just going to go ahead and tell you this information that I really want.
Other than this if the API key is not given.
So if I go ahead and write API underscore key I'll say s t.info.
And here also I'll say hey please add the grok API key.
Okay.
So some kind of message if I'm not giving this to information okay.
Now the next thing is that I will quickly go ahead and call my LM model.
So here I will just go ahead and call my LM model.
And here what we are going to basically use is nothing.
But we are seeing chat grok.
Uh, first of all I need to give my API key, which is nothing, but uh, the API key that I have actually
created.
Okay.
So this will basically be my API key.
Okay.
It should be grok.
Grok API key I guess.
Okay.
And this will basically be set to my API key over here.
Then you have this model underscore name I will try to give my model underscore name over here.
Uh for this case I will just use llama three.
Again.
It is up to you with respect to grok you can use any model that you want.
Okay, so I have written llama three 8,000,000,008 192.
And finally I'll go keep one more parameter that is streaming is equal to true okay.
So once I get this LM model I'm going to use this LM model over here with my sequel toolkit.
Right now I will go ahead and create one very important function that is called as configure.
So here I will say configure underscore db I will go ahead and configure our db itself.
And for this I will use a decorator which is called as SD dot cache a resource.
We want to probably cache this because we don't have to connect each and every time.
And here I will take the TTL.
Time is equal to turnaround time.
Sorry.
Total time limit I guess.
Yes.
So this will be somewhere around two hours.
I'm saying that till two hours.
You can keep it in the cache here.
The first parameter that I'm actually going to give is my db underscore uri.
The second parameter that I really want to give is my SQL underscore host is equal to none.
Okay.
Because all this information I definitely require right.
My sequel host I require then I require this my SQL sequel user.
So I will go ahead and copy it over here.
I'll set it to none.
Right along with this, uh, I also require my sequel password.
Right.
So I will just keep this as none, because all this information, we are just going to explore it or
take it from there.
And by default we will be keeping it at none.
Okay.
And then you have also my SQL DB.
So this will also be none.
Okay, so all this information I'm actually specifically giving over here.
And then we will close the bracket and open the function from here.
Right.
So I will go ahead and open the function.
So this will basically be my configure DB.
Now here I will just go ahead and write.
If my db uri okay is equal to.
If my db underscore URI whatever db uri that I'm assigning, if it is double equal to local DB, okay,
if it matches the local DB, what we will do, we will go ahead and set up the file path to our DB file
that is present over here.
Because here this local DB is nothing but your SQLite DB right.
So here we'll go ahead and write db file path db file path.
File path is equal to.
Here I'm going to quickly write path.
And let me just give the file path like this.
By default uh file path.
And I'll say uh this this is just mainly to get your, uh, if you really want to understand what we
are specifically doing this, if I just go ahead and write dot parent okay.
This dot parent, uh, if you just go ahead and print this particular information with respect to this
particular file and the file information that I'm actually giving is student dot db.
Okay.
This I will try to give it in the string wise.
So this in short is creating a absolute path okay.
Absolute path.
So this will basically be your parent path itself right.
Above this what exactly is present.
And then here I will just write absolute get the path complete path and actually get the absolute path
of it okay.
So if you just go ahead and print it you will be able to understand.
So if I go ahead and print over here I'll say DF file path okay.
So this is my son DF.
It should be DB okay db file path.
So here also I will go ahead and write my db file path.
I'll just go ahead and print it to just show you what will be the path that will be connected over here.
Then uh, again uh, there are two lines of code that I'm going to execute after this.
First of all, I will go ahead and write creator Lambda SQL connect, and this will be the same path
that I will be using over here.
Uh, the mode will be on read bytes write.
We will be basically reading bytes from this particular SQL.
So for this we use SQL three dot connect to connect to that particular file.
And you are I is equal to true.
And for this we'll be using a lambda function okay.
Finally after executing this you will be seeing that we'll be returning the SQL database which will
be engine inside this.
So that is the reason you can see that we have imported lang chain dot SQL database import SQL database
over here.
So it is nothing but create engine SQL light over here.
All the information uh, with respect to this and creator is equal to creator.
Whatever information is present with respect to this particular file path, okay.
And that is the parameter that is required in create engine.
Now similarly here I will just go ahead and write l f db underscore URI is double equal to my SQL.
Okay, if it is double equal to my SQL, then what we do over here, if it is not equal, uh, my uh,
if it is double equal to my SQL.
And first of all I will say condition if any of the parameter is missed, like if my SQL host, my SQL
user, or my SQL password, or my SQL db, If this any of the information is not provided, then we
are just going to go ahead and display state error.
Okay.
Uh, this is important so that we'll be able to handle the exception over here.
Otherwise we will go ahead and return.
And this is how you connect to the MySQL database okay.
So this is important okay.
So here uh what we will specifically say that if this is working absolutely fine I'm just going to say
hey, return this entire information.
So what we are returning over here SQL database Create engine.
But here we are going to use my SQL connector.
Usually when you are connecting to the MySQL workbench we basically use the MySQL connector.
After this this entire URL will be basically created.
Here you will be having your MySQL user.
Then you will have your MySQL password.
Then you will have your MySQL host.
And finally you have to have your MySQL DB.
Okay.
And this is the way how you specifically connect to your MySQL database.
And for that we use this particular create engine along with the SQL database.
Right.
So uh, this is how you basically go ahead and connect to the MySQL okay.
So this all information is done.
Either of these two options when we select uh this is how we configure the entire DB okay.
Now uh, in our uh next video okay.
In our next video.
And before this, uh, I go ahead I have to call this configure DB.
Right.
So let's say if I go ahead and say if our DB underscore URI is a double equal to my SQL, then what
we are going to do is that we are going to basically configure it.
I'm going to write DB is equal to configure.
I'll call this configure db function.
And inside this I will give my db uri.
First parameter is my db uri.
The second parameter is my SQL host okay my SQL host over here.
The third parameter is specifically called as my SQL user.
The fourth parameter that I am actually giving is MySQL password.
And the fifth parameter that I have is nothing but MySQL db.
Okay, so all these parameters, we are passing it over here for this particular database and automatically
it will configure this particular database.
I will be getting this particular database from this particular function.
Right.
It is returning the SQL database itself in the else section.
What I will do, I will just go ahead and say DB is equal to configure underscore db.
And here I'm going to give my db array.
So in this particular local DB case I just need to give my db URI.
And automatically it will be able to take this entire information and it will be able to execute it.
Okay.
Uh, yes.
Uh, I think uh, all the information is specifically done over here till here.
Now in our next video, uh, I will start creating our toolkit.
So this is for our whatever work we have basically done.
This is mostly about connecting your database which database you are specifically connecting okay.
Now whenever I try to use, uh, this toolkit, what I will do is that I'm actually using an LM model.
I'll say, hey, please display the records of, uh, all the records in the student table.
Then it should.
This LM model will probably create a query for us.
And then with the help of that particular query, we will try to interact with this.
Right.
Interact with our database.
So that work is basically done by the toolkit itself.
So that is what we are going to see in the next video.
So I hope you are able to understand till here I hope we are going on.
Good.
So yes, in the next video we'll be discussing more about it.
So yeah, I'll see you all in the next video.
Thank you.