Chat with a CSV - LangChain CSV Agents Tutorial For Beginners (OpenAI API)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so essentially all you have to do is write some text and S it to an agent we reading our CSV file kind of like in a way that a data analyst would do hey guys welcome back to another video in the series where I'm developing on a l chain open AIS API and llms and today I'm going to be teaching you guys how we utilize agents as well as our large language models to read over a CSV file and find specific results best part is there's not a ton of coding involved with it and you can pretty much pick up the Concept in 15 to 30 minutes if you practice it a little bit now I do have other videos that are covering agents large language models so if you're brand new to the space I'd probably recommend watching a few videos before jumping into this one but you still should be able to grasp a lot of these Concepts pretty easily with that being said all that you need for this video is an open AI Epi key then like a Google collab or a vs code all right time to code all right so I have my Google collab notebook in first we're going to have to actually pip install a few things so first we're going to do is PIP install L chain so make sure that is in over here then up next we're going to have to pip install L chain open AI so pip install Lang chain and then underscore open AI like that and then lastly what we're going to have to also Import in is L chain experimental so pip install L chain that and thenar mental and what other thing I'm going to do really quick is I'm going to set up my open AI API key so we're going to import OS and we're going to say os. Environ equal oh let's put over here uh open AI key so open AI API key I'm going to say that's going to be equal to and then put your key over here it should start with SK if you don't have a key make sure to grab it from the the open ai's API website I also showed how we can build that in our first video in the series but make sure you put that there I'm not going to show mine in this video I'm just actually going to build out a few new cells down below but what I'm going to do is install these three real quick Import in my AI key and then we should be good to go all right so now that's working let's uh actually look at our CSV file so we're going to say import pandas as PD okay run that and then now I'm going to show you where we get this CSV so essentially I go to a website called baseball reference very basic for baseball statistics I'm going to grab Walter Johnson one the best pitchers of all time and he's all Famer two time MVP three Triple Crowns and five ER titles 165 On's above replacement which is phenomenal um regardless you go down over here to standard pitching you can share an export and I believe you can get table as CSV or get Excel workbook you can change it to CSV if you really want to but that way you can export that out now I will say also in Excel make sure you remove these last three over here otherwise you're going to have some issues with that so just to show you what these look like real quick I just go over here to file I'm going to open up this Walter Johnson right it's going to be a CSV file right now I'm just going to read this comma finish so this is what my file looks like right just remove these over here and this is optional you can also remove Awards honestly I probably should have done it but I already have all the code prepped so I'm not going to do that for this video but there you go we don't need these remove the files and um that's what the CSV should look like so the way that you upload your file is you just go over here to files and then click over here at upload then grab it I have Walter Johnson right here on the desktop me a warning that it's not going to save if I restart this that's fine um this is for the video but just if you run the code again in the future you have to upload your CSV that won't store over there so let's just take a look what this CSV looks like here in Python so pretty easy pd. read CSV then just throw in the name now kind of a shortcut for you guys you don't want to type all this out three dots rename file grab all that control C it throw it over here contrl V do that all the time it's so easy to make typos and we have that and then if you just go DF head right here if you just want to make sure it's working properly great we have this imported in over here now let's build out our CSV agent so that way we can essentially use our llm with our uh CSV file so first thing we're going to do is from Lang chain experimental. agents. agent toolkit import create CSV agent and I hope that's proper and make no mistakes there that work awesome and then we're going to do is from linkchain open AI we're going to say import chat open Ai and hopefully that works as well and boom we have that so all right so now what we're going to do is set up an llm so we're going to say LM equals this chat open AI I'm lazy I'm just going to copy and paste it then you can set your temperature you want to have something close to zero I'm just going to do 0.5 because I've been using it a lot in the series and put this if you want to use a specific model you can clarify that down below but uh for this video is purposes I'm not going to and then of course I do have an error and it says did not find an open AI API key apparently I didn't run that so I'm just going to pause real quick rerun that cell and and it was correct I didn't run that that so this time it actually did work so make sure you run yourselves otherwise you're going to have the same issue I did so now what we're going to do and this this next step I was a little bit iffy on the documentation if this is technically an agent executor or not so it didn't really explain it too much or just a straight up agent um agent executors are supposed to run the agents so I'm like 5050 on this one so I do apologize if this should just be an agent code still works but I'm going to say create our CSV agent CSV agent and then what we're going to pass inside over here is first our LM which we defined above then we're going to put in our CSV file so I have Walter johnson. CSV like that and then I want to see how the LM is getting our information so I'm going to use verbose equals true and it'll spit out essentially every every line that's going through so we're going to run that over here great that worked as well and let's take a look at our first thing in our document like this was as easy as it was just to get all this set up right just few lines of code so first thing I want to do is find the total amount of wins now winds is going to be over here this W column so I'm not telling it that wins is W but I'm curious if the natural language will pick that up so to show you how I do this in pandas and again I'm going very basic just to make sure that this works in the tutorial as we're going to set up a w like that so we're grabbing our winds column over here with our W and we're just going to sum it up so sum of that and 417 if we want to check that right you go on over here 417s great so now what we can do is we can do this to agent executor so we can Agent executor then you're going to put do invoke and that's the new proper term for saying that a lot of stuff has been changed and depreciated in the past so I tried updating this tutorial where I can and invoke is the way to do it now but let's put in our natural language over here so how many total wins are there so let's see if this picks up the W I know it should because I prepped it before but let's just run this and we have an error funny enough now putut parsing error occurred in order to pass this airor back so that's kind of funny let's retry that over here make sure everything invoke my total wins are there that is literally the same exact thing that I put in our pre code so let's try rerunning this again and here we go and that's why I'm saying this is still a little bit buggy but you can see entering the new agent executor chain which is also why I think this was supposed to be set up as an agent executor and not just as an agent again documentation was very iffy on this um we can find the total wins by summing up the values in the W column of the data frame okay that's awesome action so it goes python action input same thing I put over here DFW sum right observation 417 the total amount of wins is 417 Final Answer 417 finish the chain input is how many total wins are there and our output is 4 17 awesome so another one that we can find over here let's say we're going to find the most wins so like find the most wins in a season so let's look how we would want to do that let's add some new code over here so again we can just take this W over here and let's find our Max so we just put Max on this side of things 36 if we go over here we can see 36 is the most right right awesome so that did work properly well I mean that should because that's our normal python code but let's grab our agent executor again let's throw this in over here and say what's the most wins what is the most wins in a year I'll add in a year I didn't do that with my practice code but let's see find the max right and we get 36 over here I need to find the maximum value in the W column use a Max function Max and calculates that awesome so let's do one more over here and let's find find the average amount of wins so find the average mins I said mins but wins over here let's grab this and this time we're going to do a mean I guess mins would be mean and wins but no idea so let's grab our mean like that you can see the average is 19.8 so let's do this agent executor so same exact things what's the average wins I'm GNA say also round up to whole number which I didn't do this in my practice code but I think it'd be kind of fun to do over here so let's see if it does that as well so it's trying to import math it looks like on here action input we'll see but it is saying over here use the mean from that not a valid tool so this might crash out I might try to rerun it but not working the best in this example we have one more line and then if it doesn't work yeah let's stop that so I'm just going to say what's the average wins we'll remove this over here and we'll run that still looks like we're having some errors let's put this question mark here at the end third time that's exactly what I put my example code and the chain is finished over here what's the average wins output 19.85% videos right over here with Lang chain and I recommend that you check them out I'm uploading two to three videos every single week and I'm going to continue to build out this full playlist in 2024 and 2025
Info
Channel: Ryan Nolan Data
Views: 7,077
Rating: undefined out of 5
Keywords: Data Analyst, Data Scientist, OpenAI API, langchain, langchain agents, csv agents, talk with csv, data analyst automation, csv automation, excel automation, python, langchain tutorial, ai tutorial, automation tutorial, python tutorial, openai, llm, large language model, llm langchain, llm csv, llm app, ai python, python automation
Id: VVdzQs-FeHE
Channel Id: undefined
Length: 13min 10sec (790 seconds)
Published: Fri Feb 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.