Langchain Agents [2024 UPDATE] - Beginner Friendly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so essentially what agents allow us to do is combine our large language models with tools that Lang chain has available and why this is super important is you can build out better applications one of the biggest flaws right now between open Ai and even like chat GPT is the lack of upto-date information if you're searching things that happen in late 2022 or 23 or early 24 that data is not available but we have a lot of tools available that have that information such as Wikipedia Google search or even DuckDuckGo search so by utilizing our large language model and also a tool and connecting them with the agent we can build a lot better applications hey guys welcome back to another video in this series that I am developing on a large language models open AI API and Lang chain today we're going to be covering agents as well as the agent executor now recently with the new Lang chain update a lot of things with agents and agent executors have been depreciated I've updated my code so that way this is the most upto-date video here on YouTube as of early 2024 we're going to be going over a few examples uh the first one which only covers one tool and another example which covers two tools so a lot of information in this video I highly recommend if this is your first time jumping into Lang chain or open AI API watch some of the other videos in this playlist first and also set up your API I key you have watched some of those videos or have the basic understandings of how linkchain works you should be ready to go and we're going to start coding in Google collab all right so we're going to start off in this Google collab notebook we're actually have to install quite a lot of things so stay with me on this one install duck ducko search then we're going to pip install Wikipedia install Wikipedia then we're we're going to pip install Lang chain so pip install Lang chain then after that we're going to pip install Lang chain Hub pip install Lang chain Hub then we're going to pip install Lang chain open Ai and that should be good there I'm only going to do one other thing real quick is I'm going to import OS in I'm going to say os. Environ and then put open aior API _ key and we're going to set this equal to whatever your open AI API key is it should start with an S I'm going to paste mine in but I'm going to hide it from you guys so that way you don't use my key um but I'll be pasting that there so make sure you run each of these like that or you run your your cell with your API key and we be back in probably about a minute or two once all these have been installed all right so let's bring in some dependencies now so from linkchain open AI import chat open AI from linkchain [Music] import hub from link chain. agents import create open AI functions agent from Lang chain. agents import agent executor from linkchain schema import human message system message M and then last one from linkchain [Music] actually that should be the last one there I believe so just run that okay awesome let's set up our llm so I'm going to say llm equals chat open AI then what I'm going to set is the temperature to 0.5 I could be doing throughout this series and if you want to Define your model here go ahead I'm just not going to do that build a few more cells so what I'm gonna do first is I'm gonna say try to find try to find the 2023 World Series champion now if you don't know what the World Series is if you're not a baseball fan it's the winner of all the different baseball teams in America Major League Baseball level there's one team that wins it well well the problem with right now open AI API is we don't have that type of data 2023 has not been trained on these models yet so it's going to be non-existent so essentially what I'm just going to show you is that this data is missing so message and we're going to say over here system message like that and we're going to say inside over here content equals and I'm going to say a user will input in a year and you will get the Baseball World Series champion okay then we're going to set up our human message which this needs to have a comma but then we're goingon to say human message like that we're going to say content equals 2023 we'll put another comma over here right and then should be good and then I'm going to set up lm. invoke and we're going to say our message so that and let's see how this ends up work so here we go it says content the Baseball World Series champion for the year 2023 is not known as it is a future event well we're sitting here in January 2024 so it is not a future event so let's build out a way to do this so let's see how we can set up a tool with our large language model and the tool that we're going to be using is going to be Wikipedia is Wikipedia perfect no by absolutely means it is not perfect um essentially what this tool will do and I've covered in the other video is it'll find a Wikipedia page and then give you some information from it so essentially we're going to use that information from the page to determine the winner from the 2023 World Series could technically pull other pages and possibly might not find the information from that Wikipedia page but we're going to try and I know it's going to work because I already uh prepped this code beforehand but it might have to take a few times running this LM so we're going to say prompt equals and this is new for um this version of Lang chain I'm not too familiar with hub. Poole I got to do some research on it uh so it's HW Chase 17 slop AI functions agent I saw this in the documentation again I I'm not too sure what's specifically going on with this and I'll probably cover another video when I research it um but the old way to initialized agents has completely changed so unfortunately when I first prepped this video versus uh when I'm recording it now what I did was completely outdated and I had to restart from scratch so sad but I don't want to have something that does not work for you guys in like a few months so from Lang chain. tools we're going to import a few things by the way uh we're going to import in Wikipedia query run and then from God that was a horrible spell from uh link chain Community utilities import our Wikipedia API wrapper like that okay should be good there and then what I'm going to set up next is our API wrapper equals Wikipedia API wrapper like that we're going to say top K results equals 1 doc content Max I'm going to say 500 so we can actually read what's going on in the article so Wiki tools now or Wiki tool equals Wikipedia query run dang it didn't auto correct or Auto finish on that one and we're going to say our API wrapper equals our API wrapper so I guess I could just copy that over here and lastly we're going to set up our tools so tools equal and what we're going to say this is equal to the wiki tools should be fine there great that ran so now what we're going to say is Agent equals create open AI functions agent and you're going to pass in an LM you're going to pass in tools you're going to pass in a prompt have our agent now and then you have to build out your agent executor it's going to be equal to agent executor and we're say agent equals agent and tools equals tools and there's a few other optional parameters I'll show you in our next example um but we're going to have that over here awesome and now what we're going to do is we're going to call invoke so agent executor invoke and believe this is new um based off the documentation old way was depreciated so we're going to say inputs then we're going to say more time who won the 2023 World Series like that and let's see how it goes and here we go look so our input is who won the 2023 World Series and then the output is the Texas Rangers won the 2023 World Series was their first world series title since they're founding in 1961 so because we were able to implement this Wikipedia tool with our large language model we're able to get that information right earlier above not known but now we have that information all you got to do essentially right is set up our prompt which we grabbed over here Import in our Wikipedia tool which I set this up over here set this equal to tools cre our agent and our executor and then invoke your message and we're able to build that out so I want to just show you an example if we're going to do multiple tools so we're going to say multiple tools and this time what I want to use is LM math but I also want to use duck ducko and we're not going to use Wikipedia for this example just to give you guys a little bit more experience with some other tools but let's do that so from Lang chain import LM math [Music] chain okay we'll run that and from link chain. agents Import in tool and essentially what we're going to do is set up our math chain and I got this code for this math chain from another YouTube video I can't remember whose it was but I was looking up this over here let's see from LM m equals LM verbos equals true and there are other ways that you can set up math as well you can take a look at some toolkits that probably have those in there but I haven't covered toolkits in a video yet be doing that in the future tool. from function again I found this another video so function equals LM Math chain. Run then name equals calculator and then just put your description and I know I need I have a comma there too and we'll put description next equals useful for answering math questions only math questions and nothing else let me just see what this other verbiage was then let say only input math expressions all right so now we have our math tool I believe that's all the code for this one great now let's bring in our duck. go so so duck. go should be easier from Lang chain. tools import duck doc capital D doc go search results then we're going to do search equals and we're going to do essentially this duck. go search over here duck duck go it's going be search run not results like that and actually this should have been search run here not search results so I do apologize all right so that should be there and then what I'm going to do is we're going to set up our second version of tools so I'm going to say tools 2 equals and what I'm going to input in over here is search as well as our math tool so set that up over here awesome then we're going to set up our agent two so agent two equals and it's going to be the same uh thing over here this create the open AI so I'm just going to copy that and we should have over here on this side of things I'm just going to clarify things equal LM and we're going to say tools equal tools 2 and then prompt equals prompts so it doesn't get confused right and then we need to set up our second agent executor so agent executor like that we're going to say two equals agent executor and then agent equals agent 2 tools equal tools two and this time what I'm going to say is verbose equals true and the reason why I want to do verbose equals true is it will show you step byep what's going on with our agent so you can see how it thinks I think it's personally helpful so we threw that in over here and then you can do another invoke so I'm gonna say agent executor 2 and invoke okay and it's going to be very similar like what we put in over here right so let's just copy our input and I'm going to slightly modify it so I'm going to say who won the 2021 World Series and how many years was it since their last World Series went and there's a few different ways that this will run so first it's probably going to look for some results on duck. goo and potentially on there it could tell you the last time that team whoever it is in 20121 won their World Series so might get that information from there or it could take a look at who won the 2021 World Series and then look up that team and look up like World Series wins and do some math based around it so there's there's a few different approaches I'm not too sure how it's going to work when I run it this time but it should give us some answers so let's run this executor and see if it works properly so invoking duck. go search right query 2021 World Series winner says Nathan evaldi pitch six gutsy Innings right Mitch Garver first time so it's saying what's going on over here right finish chain so we on the 2021 World Series how many years was it since our last World Series win we get the output that l Braves won the 2021 World Series it's been 26 years since their last World Series win which was 1995 you do some basic math 5 plus 6 is 1 then you're at 2001 add 20 years 2021 and that is the correct answer so to just to recap what's going on there are some few complicated sections on here if you're coming here from the past when initialized agent was used it's no longer used it's depreciated so essentially what we're doing right as after we import quite a lot of stuff is we set up our first tool in this example over here I set up Wikipedia we set up our tool over here our Wiki tool then we set up an agent and we set up an agent executor our agent over here combines our llm the tools and the prompt which The Prompt right now is from this hub. Poole and I saw it all throughout the document ation so this does change in the future which it might just as a heads up that's what it is right now in early 2024 so that's how we build our agent and then our agent executor takes in an agent and then also takes in our tools we can also set up that verbose property which we have down over here which if we do the invoke um it'll show you step by step what's going on I Didn't Do It for this one just to show you an example just like in a basic level how this works but if we want to invoke it shows you entering over here duck dock go search and gets you the final answer on that side of things and again you can add in multiple tools if you want in this one I had the duck ducko search as well as the math tool if that was needed uh sometimes it makes sense to have multiple tools so that way you get a better answer but because we were able to set up these different tools we were able to find information that we wouldn't have been able to find normally right uh so because of the lag and information between open Ai and also these tools we can't find a lot of information like who won the 2023 World Series and that's where tools kind of just unlocks that and there's a lot of tools out there I believe there's 52 right now um can't remember from the tools video but there's a lot of opportunities to utilize tools and make better llms hey you made it this bar which means you learned something new about agents I appreciate you guys watching this video now if you aren't subscribed to the channel which most of you guys aren't I'd really appreciate it take a lot of time to build out these videos and subscribe is free and it shows YouTube that people like these videos and are interested in data science or AI now the next video that I would recommend in the series that I'm developing is going to be right over here and this is going to be covering uh agents with CSV files which can show even more capabilities
Info
Channel: Ryan Nolan Data
Views: 2,285
Rating: undefined out of 5
Keywords: Data Analyst, Data Scientist, langchain depracated, langchain 0.1, LangChain, Agents, Language Models, AI, Artificial Intelligence, NLP, Natural Language Processing, Programming, LangChain Agents Tutorial, Language Model Applications, python, python tutorial, coding tutorial, openai, openai api, llm
Id: WVUITosaG-g
Channel Id: undefined
Length: 20min 48sec (1248 seconds)
Published: Mon Jan 29 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.