Transcription
Hello everybody, this is Leno Tadros. I'm very excited about this three-part video I'm going to be making. My goal is to demonstrate the ability of querying a SQL database using a large language model.
So, we're going to have three parts to this:
1. The first part will be to set up a project, maybe in Visual Studio Code, and explain the database that we are going to use. It can be any SQL database; it doesn't really matter.
2. The second part will be to use LangChain directly as an orchestrator to allow us to pass the schema of the database with the question that we are trying to ask to the prompt. This will allow the LLM and the orchestration to negotiate together until they can provide us with the correct answer.
3. The final and third part will use LlamaIndex, which is actually better at tracking the retrieval of generated content. The reason for this is that it has special classes for SQL databases that can be very useful. Not only that, but you can also embed and vectorize your entire schema for that database to make it available for questions and queries in the future at any time.
I'm very excited about this! Let's go ahead and start with the first part, which is to set up and get everybody ready to go.
All right, so there is my Visual Studio Code. I created a completely empty folder on my hard drive called "YouTube SQL LLM." There is nothing in it, and the first thing I usually like to do is create a virtual environment. This way, when I go ahead and bring in packages for Python and so on, I don't mess this up for the entire machine. I might actually have other projects on my machine that need different version numbers of specific things.
So, whenever you want to clean this up, the first thing you want to do is create a virtual environment. How do you do that? We will go ahead and do a Control + Shift + P here in Visual Studio Code. Notice we have something called "Python: Create Environment," and we have a choice: we can use the virtual environment or Conda. Either one of them will work, depending on what you prefer to use. I'm going to use a virtual environment here.
Then, it's going to ask me which version of Python I'm going to use. The latest and greatest one! I mean, I tried it with 3.11 and 3.10, and it still worked as well, so I'm going to bring in 3.12 here, 64-bit. We will give it a few seconds, and it will create a brand new folder called "VNV," and this is where all my libraries will end up going.
You will notice here there are not too many, but once I start bringing in some files and some packages using pip, for instance, it will automatically bring this in and make it work. Sounds good? I'll come back after it finishes.
And indeed, it finished! Visual Studio Code is saying the following environment is selected, so I can actually make sure to bring up a terminal here at the bottom, and your virtual environment will be successfully activated. A lot of people like to see the word "VNV" here on the left side of the command line, but Visual Studio is telling you that even though you cannot see it, it is activated and is being used. So, we are in pretty good shape.
You can actually start doing that at this time. This is the part where you will go, for instance, here in the command line, and you'll say "pip install" and start bringing in one after the other all the stuff that we're going to be needing, like for instance, LangChain, LangChain Community, LangChain OpenAI, SQLAlchemy, and all of these things.
But I usually like to make this a lot cleaner, especially since I'm going to be including this code on GitHub for everybody to have. So, my favorite thing to do, of course, is to click on the plus sign here. No, not here. Let me go ahead and make it at the same level at the root of that folder. So, I'm going to go ahead and say "No," and we click outside of it. We click on this plus sign. There you go!
I'm going to call this one the "requirements.txt" file. So, we'll say "requirements.txt," and inside of this requirements.txt, I'm going to be pasting in all the different packages in Python that I'm going to need: LangChain, LangChain Community, LangChain OpenAI, SQLAlchemy. I'm also going to use PyODBC, and later on for part three of this video series, I'm going to use LlamaIndex and LlamaIndex LLM OpenAI.
All right, how do I actually make this work? First of all, let's do a Control + S to save this file. I can actually run now the "pip install -r" to make sure everything I've placed inside of my requirements.txt will get installed. Let's go ahead and do that.
I'm going to go ahead and say "pip install -r," and notice actually the co-pilot is so nice; it automatically knows that this is exactly what I'm trying to do, and it points to my requirements.txt. Let's push enter here, and we'll give it about 3-4 minutes. It's going to install the kitchen sink for all of these things. I will come back after all of the stuff has been installed, specifically in this folder, not globally to my machine.
And voila! It took about 4 and a half minutes or so, but it installed the kitchen sink of all of these things. If I want to see exactly where it occurred, if I open up the "lib" again for the libraries, you’ll notice there were only two packages, but now I have tons of different packages because a lot of these have, of course, dependencies and so on. But I know that LlamaIndex got installed, LangChain got installed, and the community for LangChain got installed. So, we are in pretty good shape now.
I can actually close down my virtual environment and start focusing on my projects. I'm going to create a couple of Python files, but before I get into LangChain and all that, I want to finish up part one to explain exactly what I'm going to do with my database.
So, I'm going to open up Microsoft SQL Server Management Studio. I have a lot of databases in here; this is my local SQL Express. Again, it could be any database. I've done this with Azure SQL databases, I've done it on data centers, and I did it locally on my machine using SQL Express. All of them will work.
For this specific example, I wanted to actually use one of the great CMS systems in the world, which is Sitefinity. Again, this could be a Kentico database for a CMS, Sitecore, or it could be Sitefinity. I choose to do this with Sitefinity here.
So, there is a database that I have locally on my machine. This is a snapshot of one of my databases that I'm using for my website, and there are over 250 tables. So, it's a pretty significant database. It has all my blogs, all my press releases, all the users, all the dynamic comments, and everything. There is tons of information in here right away.
So, it would be nice, of course, that I can actually chat with this database and all the content and the relationships it has between all the different tables in English. A business analyst in my company can just go in and ask questions like, "Hey, what are the most used or viewed blogs in the last 30 days?" Or I can actually say, "How many admins are there, and what are their email addresses available in the database?"
For instance, "What is the most used press release in the last year?" All of these things that you might be interested in could be done very easily by a business analyst who doesn't know SQL or is not technical enough but definitely would like to get the most out of the entire database.
So, this is what I wanted to show you so far. Now that it is all set up, we can actually start working with part two for LangChain. I'll see you again in the next video!