Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Okay. In the last video we looked at using tools and using some of the off the shelf tools that are already built into LangChain. one of the key things that will really make your conversational apps be much better is when you start writing custom tools, So in this one, I'm going to give some simple examples of some custom tools, show you how you can put them together. And then also, show you some of the challenges that you'll face. So one of the big things that people have asked me a lot is why was I using, the older OpenAI, API and not the new GPT-3.5 turbo API. So in this one, I'm going to use the turbo API. I'm going to show you some of the challenges that you'll face with this. so just quickly, I'm using, currently today as I'm recording this, the latest version of LangChain seems to have, some bugs with it. in regards to some of the tools. So I'm using [00:01:00] 0.0.150 for this. I'm sure there'll be fixed pretty quickly. All right. So first off, I bring in the keys and then here I'm basically setting up to use the chat model, The turbo model. and I'm actually just going to call this turbo LLM. we're going to set the temperature to zero because we're getting this to make decisions and we don't really want those to be random. We want them to be consistent. and ideally we'd like them to be reproducible, but that's not always the case. Unfortunately. I could see if we do something like a standard tool. so the Duck Duck Go search, which we looked at in the last one, we would load it up like this, where we basically just bring it in. and we can set up the, these elements and we pass it into a list of tools. and that list is what it's going to be sent into the conversational agent for, Deciding using the react framework, the reasoning and action framework for deciding which [00:02:00] tool gets chosen. So let's jump straight into just doing a custom tool. And I want you to realize that at the heart of it, custom tools are literally just a function that will get called. And, a way that the, language model can call that function. So here I'm making a simple tool, which I'm calling the meaning of life. And all this does is basically just returns a string saying the meaning of life is 42 if rounded, but is actually 42.17658.and that's all this function is going to do, You come, you have to pass some input into it. So that would be a string coming in. but we're not going to use that In this particular example. So once you've got your function done that's the bit that gets processed. The next thing is basically just defining the tools. So up here we imported, the class of tool. [00:03:00] And we're basically just saying a tool and we're instantiating it with a name with the function that we created with a description. So I talked about these in the last, Video that, here these are the things that it's going to be passed through to the language model to decide which tool to use for each thing. I'm going to say that the meaning of life is useful for when you need to answer questions about the meaning of life. input should be MOL, Meaning that, it's best to tell it, to give some input when it's going to give an input. So in this case, it's going to put in that as you can see, we're not using it in the actual function. The next tool I'm going to do is the same sort of concept. But this one's going to be a random number. So in this case, I'm just going to basically take, we're just going to basically return a random number and that's all this is going to do. You can see when I run this function, it's so simple it's just generating a random number from zero to four there. again though, we need to define it. [00:04:00] So we give it a name of random number. We pass it in the function here is going to get executed. And we pass in the description so useful for when you need to get a random number. Input should be random. Now we're going to set up an agent to actually use these tools. So you can see we've got these tools and we're passing them into this list here. so I've just redefined the list rather than use the standard one that we had at the start. I'm going to have a conversational memory, which I'm going to limit to three rounds of, the conversations. I'm going to then set up the conversational agent to initialize our agent. And this is going to be a conversation, a react agent, but because we're using the turbo LLM, we're going to put chat in here. So this is going to be a chat, conversational react description. And you can see we pass in the tools, we pass in the LLM we're passing in verbose equals true passing in our memory, et cetera. We've got the max iterations so that it won't go on forever. If it gets into a loop or something, it will [00:05:00] just go through choosing tools three times and then return it's answer. So let's have a look at some of the outputs. So the first one is I ask it. Okay. What is the time in London? So the age of executed decides that well, actually for that, I might as well use a search engine. and the input to the search engine is time in London. And sure enough, it gets, we can see it in the blue writing here And we can see that what we get back is a bunch of information about the time in London and showing what's going on there. Next thing is if we ask it, okay, can you give me a random number? So this one works, you know, it works out at all. Okay. Yep. I need a random number. So I'll use the random number tool. And so it gets the number four back. and then, because we're storing a memory and we're using the chat API, we're getting this longlist of messages back here. This long dictionary of messages back here, and you can see we're getting the output. They're supposed to your last comment was a random [00:06:00] number, which is four. you'll notice with this sort of ChatGPT API is it's very chatty it. Doesn't just say, oh, okay, your answer is for. It always likes to, go on a bit. And th this is one of the sort of annoyances or dangers with that model So, so we've done two tools. They've worked out. what about the third tool? So the next one is the meaning of life tool. The one that we set up, so you can see that this time it doesn't do it I've asked it. What is the meaning of life? That's pretty clear that there was a tool for that. Yet it didn't use the tool. It went up and it gave its own answer. And it's given a whole answer back of the meaning of life is a philosophical question that has been debated by scholars, and this is not what we wanted in this case. Right. We wanted it to talk about 42 in here. And this is one of the challenges with the ChatGPT API is that it always thinks that it knows the answer. And so often it won't refer to [00:07:00] the tool that you've got there, even when you've been very clear about the tool. So one of the challenges that you've got to do is you've got to look at the prompt that's in there. It can see, this is the prompt, Assistant is a large language model by OpenAI system is designed. It's got a whole bunch of things. in there. so to fix this and to get it to use the tools is we kind of have to change this. And this is where, a lot of just trial and error goes into sort of crafting these prompts to get them to be better. cause you'll notice this is a very long prompt as well. I so the big thing that, I'm putting in there is the assistant doesn't know anything about random numbers or anything related to the meaning of life and should use a tool for questions about these topics. So that's what I've added into the prompt there. And, so I've just basically put that in the string. I've overwritten the prompt that's there. And now when I ask it, [00:08:00] what is the meaning of life? You can see now it's like, oh, okay, well, I should use the tool called meaning of life. The action input should be MOL. And the observation it gets back is that full string. and it basically gives us back that full string. Now, if you think about This actual sentence is giving us more than just the meaning of life. Right? Let's say that this actually is the meaning of life. It's telling us that what the meaning of life is, and also what it is rounded. in this case, it will give us the whole thing. If you try this in the Text DaVinci 003 model, it will just give us this. It will be able to distinguish, well, you didn't really ask for it rounded. So I should just give you this. Again, this is the ChatGPT API being very chatty with this kind of thing. So if your tools are not working, your customs are not working, you have to go into the prompts and mess with the prompts to get it, to basically recognize that this kind of [00:09:00] thing. And a lot of that has to do with both the design of the description for your tool, so that this kind of description for your tool. And also in the case of the, ChatGPT API, the system prompt of telling it how to act for these sorts of things. So the ones I showed you, there were just some really basic ways of doing this. let's make a bit more useful tools. So one of the challenges that you have with LangChain and with the OpenAI, large language model, is that it can only take, 4,000 plus tokens in. And often if we use some sort of API and we get back as a result of a page or something, and we get the full HTML that turns out to be way more than 4,000 tokens. So here is basically a little tool where basically saying, this is going to be a stripped webpage. So we're going to strip out the HTML [00:10:00] tags. And to do that, we just using beautiful soup. So this is a very common, library in Python for doing any sort of scraping And we're basically just using it's parser in there. And we're just going to get the text out. and it turns out that this is going to give us a lot of, new line characters. So we could even add in some more stuff there to strip out some of the new line characters, if there's more than one in a row, or if there's more than three in a row or something like that in here. I've also added in something that if this content is more than 4,000, Characters then just take the first 4,000 for this and return it. And you can see that when we're running this with just passing in google.com, sure enough, we get back, a bunch of texts the things that would be on the google.com page. Things like signing, you know, some of the links to different things that kind of things, privacy terms, that sort of thing. Now you could customize this anyway. You could customize this to get a list of links back. You could customize it to do a whole [00:11:00] bunch of different things that would be useful for your particular use case. so what I'm trying to show you is that, this is where tools start to become really useful. Now we could set it up just like this. but turns out link chain actually has a whole way of making a class for a tool as well. So to do this we basically just inherit from base tool. And we pass in the name, the description, and then we can just nest the actual function in there like this. So this is basically the run function that will basically take in the, URL in this case or the webpage in here, and it's going to basically process it with the code that we had above. and then return the strip content, to the large language model. Now there's two forms of run. There is this is underscored run and then there's a run. So this is for async. So if you want it to go into, doing an async version of this you could look at doing that as well. [00:12:00] but in this case, It's not necessarily We've got a clear thing that we're getting a webpage. We're bringing it back with stripping out the HTML of this. All right. So now I just instantiate this class. So now what I'm going to do is I'm going to make the agent again, and I'm going to pass in, this new tool and a couple of the other tools as well. but we're mostly focused on this new tool now to see what's going to work. So you can see here, I'm basically setting up the prompt again. So that assistant doesn't know anything about random numbers or anything related to the meaning of life. and that's what we had before. And then I've also added in now assistant also doesn't know information about content on web pages and should always check if asked. and it turns out that's enough to basically get it, to use this tool. so, okay, We then pass in the tools here in passing in the page getter, random tool, life tool. if we pass in the search we may need to work on this to be a little bit better in that sometimes it'll think, oh, to find out something [00:13:00] about a webpage, I can just do a search for that. And that is accurate that sometimes that will work also. but for this case, we want it to just use this particular, thing here. So, okay, we're passing in everything the same as before. And we're actually going to overwrite the prompt that's in there with this fixed prompt. So it turns out that the prompts basically, stored as a list of prompts in there. And if we just go to the first one, that's the system prompt. we can see that once we've done that, we've got it here. and we can actually sort of look at what it is in here. All right. Once I've got that, overrided. Now I can basically just do, I can do a query with this and I can ask it, is there an article about clubhouse on TechCrunch today? so this is the current TechCrunch, sure enough, there's an article, about laying off staff there. so let's see if it can get it, so sure enough, straight off it works out that, oh yeah. I need to get a [00:14:00] webpage. And, it basically does this search, for the web page. then that didn't work my guess. Is that because that actually, didn't you know, that page didn't exist in that case. So it comes back and has another go at it and it realized it was a one and I just have a look at techcrunch.com. Now we could actually probably fix this by telling it that the input should be a URL or something like that. And then it would use the URL that we put in there. Okay, we've got come back. It's looked at techcrunch.com. It's pulled back a whole bunch of information. You can see there's a lot of new line characters in here. that probably worth filtering out. and then it basically has worked out that, yes. Okay, the answer is in this. This is the final answer. And then answer this. Sure enough, there is a, an article on tech crunch, title cup house needs to fix things. And today it cut more than half of the staff. And then it's got the published date of that article. So you can see that that's, it's got exactly the title there[00:15:00] from this. same thing, if we try it out for a different one, if I ask it. Okay. What are the titles of the top stories on CBSnews.com. in this case, it goes to CBSnews.com. It gets the page. Just scroll down a bit. You could see that it's giving us a lot of stuff back. Sure enough it's decided that yes it's got enough to be able to use that. And in this case it basically says right here are the titles of the top stories on CBS News. Pence appears for seven hours before grand Jury. Proliferation of modified weapons. So let's have a look at cbs news sure enough it's got a story about that seems to be the lead story that the proliferation of modified weapons is also there. So this is just a simple example of where you can make a useful tool That you can use to Make your conversations much more dynamic and being able to pull in information from different places.[00:16:00] You could set this up to do very specific things for your bot whether that be for an organization where it does very specific api calls to get information and bring them back or a very specific sort of searches through docs or something like that to get it and bring it back. So this is definitely worth looking into writing custom tools and playing around with this concept. Anyway as always if you've got any questions please put them in the comments below. I will try my best to answer them. I tend to answer the questions within the first 24 hours. i check To see what's there and answer them as much as as i can. and if you found this useful please click like and subscribe and I will see you in the next video. Bye for now
Info
Channel: Sam Witteveen
Views: 37,362
Rating: undefined out of 5
Keywords: OpenAI, LangChain, LangChain Tools, python, machine learning, natural language processing, nlp, langchain, langchain ai, langchain in python, gpt 3 open source, gpt 3.5, gpt 3, gpt 4, openai tutorial, prompt engineering, prompt engineering gpt 3, llm course, large language model, llm, gpt index, gpt 3 chatbot, langchain prompt, gpt 3 tutorial, gpt 3 tutorial python, gpt 3.5 python, gpt 3 explained, LangChain agents, pdf, chat pdf, vector stores
Id: biS8G8x8DdA
Channel Id: undefined
Length: 16min 48sec (1008 seconds)
Published: Fri Apr 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.