LangChain SQL Database agent with OpenAI to query PostgreSQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello gonna be looking at something quite specific  today we're looking at how to use the SQL database   agent with Lang chain Lang chain is a way to chain  commands together to query things with open Ai and   chat gbt I'm no expert I've only been learning it  for a short while one thing I have found which I   just want to share with you is this bit of the  documentation for link chain when you are trying   to connect to a database now you may have read  that you can only connect to SQL Lite or that   you need to use SQL can me that was my initial  impression but after a bit more delving I found   out you can connect to anything MySQL postgres  so what I thought was how can I query postgres   using open AI chat EBT and also I thought well  if it's got thousands of Records is that going   to use me lots of tokens and one thing I just  want to show with you share is the token usage   and you can see it's not insignificant so um I've  been using it for about a week and already I'm up   to nearly a dollar so that's just purely with  two or three tests a day um if you want to use   this in production then I guess you will need to  have a large budget so already today I've used six   cents which may not sound much but that's just me  testing if you multiply that by thousands of users   customers whatever then it will add up you can  also add in code to check how many tokens you're   using um I won't give her that here but it is  possible to put that into your code so as you're   running it from python you can see exactly  what you're going to be using uh right okay Imports so Lang chain I'm not going to really run  through that there's many better videos out there   than what I can do I'm using Lang chain Lang chain  will call open AI and the key thing here is I'm   using load.emv to extract my my API key from  my dot EMV file which is in my home directory   and this is all you need you may see examples  where there's much more than this but this   is all you need you need from.mv importload.emv  load.mv once you've done that all your environment   I can't even say it environment variables are  available so when you need to call on them   Lang chain will actually look for something called  open AI underscore API underscore key which is   very specific it has to be that when you create  your EMV file which you create with a text editor   again lots of videos out there then what I  will be showing you this so we've done the   Imports this is the Crux of the video  and I just wanted to really just share   this with you in case it was of use so  I am connecting to a postgres database   postgresql I'm using psycho pg2 which is the  connector which connects python to postgres   my username is admin this is the username  to connect to postgres with nothing to do   with chap GPT or python as such password one two  three that's whatever your password is localhost   because I'm running it locally again if you're  running this code on a production server it would   probably typically still be localhost because the  python code would be running on the server as well   then admin and that's the database so I know I've  got admin there admin's the user and then admin is   the um it's the database that I'm connecting  to right from Lang chain chat models import   chat and why have I got that plus there  did I put that in I don't think I did   um so llm large language model equals chat  open AI model name gpt-3.5 turbo as of May   2023 now toolkit SQL database toolkitdb  equals DB that was in the documentation   what wasn't in documentation was uh where was it with um what wasn't in the  documentation was the actual   um I showed you at the start there  we go here it just says DB equals DB um the way I found out was actually testing it in   um just as a normal python file I was testing  it here and then I got the helpful hints and   I was getting a type value missing error  and then you have to pass in llm equals llm good okay so this is to connect  to SQL which happens on this line   then we run the agent or create the agent and  then we run the agent so without further Ado   I'm just going to show you the SQL which I  created which is based on the teachers table   and some values which if you've got the book by  Anthony De Barros practical SQL you will have   access to that so if you've got the online version  um the electronic version you can copy and paste   and you've got all of this I believe it's got  to get I'm pretty sure he's still got the uh   GitHub repo where you can actually get that from  as well so without further Ado let's run all   and what this is going to do is it's going  to query my postgres database that I've got   running on this particular computer which I'm  actually also recording this video with and I've got an error because action list tables SQL DVD could  not find could not pass the text I do not know why that happened perhaps it's  because I clicked something whilst talking   apologies for that so here you can see this is  not instant and it's doing several things so   um really this is just to show you how you  can connect it's not to show you there's much   better videos out there showing you how you  can use agents and how you can run multiple   queries and you can chain agent chain the tools  together um I just want to show you this so I've   got a table called teachers and chat GPT or  open AI has gone off and checked the table so it's listed tables it's   I should use the teachers table to find the first  name and last name columns and calculate the mean   salary which is why ask that I ask you to find the  first and last name of teachers who earn less than the mean salary so what it went off and found was  Janet Smith's mother bush company   and if we look at the original database  you can see the mean salary is going to be   somewhere around actually it found it finds it  for us doesn't it let me just um scroll across mean salary says somewhere or it was earlier anyway means salary so an actual fact everyone  earns less than the mean salary   because the mean salary is distorted by the fact  that Lee Reynolds and sixty five thousand dollars   so I think the mean salary is about 40 4 000  something like that so in actual fact you've got   um one above the mean and Five Below also note  that it converted mean to average so I asked   it to use the mean and it was smart enough to  know that mean me mean equals average so yeah   just a quick video to show you how to connect  to SQL using SQL database with Lang chain so if   you've not used Lang chain check it out because  the power really is with bank chain once you've   I mean otherwise you could just use the chat GPT  web page and just keep asking it silly questions   but um yeah once you can connect to a database and  do all sorts of interesting things then it opens   up a lot more new possibilities so yeah there we  go so just remember the documentation mentions DB   equals DB and I had to go in and put in this llm  equals llm because you've had to specify ULM I've   used gpt-3.5 turbo you could use you don't have  to use open air to open AI you could use something   from poking face Etc you could use gpt4 if you  want a different model so that's kind of um thus   that's who you're using and that's which of  their models you're using if that makes sense   so thanks for watching and I'll be back again  once I've done some more experimentation and   learned a few more things don't forget  to subscribe and all that thank you
Info
Channel: Python 360
Views: 8,609
Rating: undefined out of 5
Keywords:
Id: qWSeDgun6HQ
Channel Id: undefined
Length: 10min 22sec (622 seconds)
Published: Thu May 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.