[00:00:00] Okay, in this video we're gonna
look at some of the new tools that have come out recently, and using them with LangChain. So specifically I'm gonna look at three, tools
in here. the first one being Wikipedia. Second one being Duck Duck Go. And the third one being the Python. Read, Evaluate, Print, Loop. that we've got going on here. as always, I've just installed, LangChain. I'm using OpenAI in this case, which I'll talk about why when
we go through, and the first thing, there are gonna be other packages that we need to
install for these tools. So Wikipedia is one of those. so we can just pip install that. and then to set up the tools you're gonna
find for each of the tools to setting them up is actually pretty easy. So here I can basically just, bring in this
Wikipedia API wrapper, set it up, and this is a chain in itself. That when I run this and I pass in, a query,
it will look for the Wikipedia page for that. So, for [00:01:00] example, Wikipedia run LangChain, it's gonna go and find the LangChain page, and get the info, about that. if I was to put in someone's name, it would go and look for a, a Wikipedia page
about them. the next one I've actually, I've got is the Read Evaluate Print Loop. this is very simple. This one, you don't even need to pip install
anything. here we are just, again, we've got the idea
of just bringing it in. We've got the wrapper and then this is makes
a chain, and then whatever we run in Python in here. we will evaluate and print out. So you can see here I've just got 17 times two. It's gonna print out 34. if I wrote a whole function in here, it would evaluate that and print that out as
well. So one of the key things I'm trying to go
for here is that in future videos, in the next video, we're gonna look at doing custom
tools. So one of the key things that you want to
do a lot is doing custom tools, but to have a, just a good understanding of how these
work, I'm [00:02:00] going through some of these
basic ones and I'm gonna show you how you could put them together as well. okay, we've got, that for running Python code. And the last one we're gonna look at in, this
video is Duck Duck Go. So duck Duck Go is a search engine. the great thing with this one compared to
the Google SERP API is we don't have to pay for any API calls with this. we can just bring this in, we've got the wrapper
and then we can just do a search on this. And it will basically go and find out information
and return it back to us. So each of these things is powerful in their
own right, but what we wanna do is by putting them together,
we're gonna make an agent. That can actually use all three of these tools,
and it's going to decide when it uses which tool by itself. Now, this is the reason why I am using the
older, open AI API in here. If we were gonna use the new chat one, and
we just ran it just like this, you'll find that it actually will get things wrong.[00:03:00]
so that one needs to be, we need to sort of recalibrate the prompt for the chatGPT api. maybe that's something I'll look at doing
in another video. here though, we are just going with the basic
one to, look at this. and we're gonna set up the tools. So tools, basically just set up as a list
of tools. and we are going to create a tool. So this is just a class that creates a tool. the tool has the name, of the tool. it has the function that's gonna run it. So just like up here when we did the Python, REPL, that was our wrapper function. And we did, Python rEPL.run and we passed in whatever we wanted there. the same is gonna be here. this is gonna get, the output of a language
model passed into it, to run. Now, the last thing is we need to define a
description, for the tool. So this is also really important, is that
you want to basically, describe how your tool would be used, in the agent. So [00:04:00] here I'm saying that, okay,
this is useful for when you need to use Python to answer
a question. you should input Python code in there. So that's for the Python one. All right, so I've got that tool set up. I can set up tools outside, the actual tools,
list. So you can see I'm setting up the Wikipedia
one. So again, we've got a name, we've got the
function for running it. and here I've got, the descriptions. The description in this case is gonna be useful
for when you need to look up a topic, country or person on Wikipedia. And the last one, is gonna be Duck Duck Go. So this is, the name is Duck Duck Go Search
useful for when you need to do a search on the internet to find information that another
tool can't find. Be specific with your input. So the idea is that it's gonna put keywords
in to do a search on duck dot go there. okay, we've defined these three tools. I need to add the other tools to the list. So that's just simply just simple python,
appending these to the list.[00:05:00] So then basically I've got all of these in
a list of tools, which I'm gonna pass into the agent. So next up, we've gotta define our agent. So here we are basically going for a very
simple agent where we're gonna have a zero shot agent. and we're gonna initialize this agent, with
the zero shot react description. So you'll see what that is. When we look at the prompt, we're gonna pass in the tools. We're gonna pass in the LLM. we're gonna passing the verbose=true throughand we're gonna pass max iterations. So max iterations is the number of times it
can kind of think to itself about is this the right tool? And then if the tool doesn't work, it might
decide that, no, I actually want to use a different tool or something like that. Okay. So, in a second we'll look at the prompt and
we'll see what's going on with this react description here. But first, let's just try it out. So we've got our zero shot agent run. When was Barack Obama born? So this kicks off this agent executor chain. [00:06:00] And this It's gonna pass in our
query. And then that's gonna sort of redefine it. It's gonna work out like what does it need
to do? So it says, okay, well I need to find out
when Barack Obama was born. So the action that it decides on this is it's
gonna use Duck Duck Go search. And then the search that it's gonna do is
gonna be action input Barack Obama birthdate. The observation is what it got back. And so we can see that it got quite a lot
of information back. but from that, it's gone through and it's
decided that, okay, well, the thought is Barack Obama was born on August 4th, 1961, and then
it gives us the final answer and it finishes out the chain. So this used one tool of going to Duck duck
go for that. If I asked it something like, what is, 17
by six? Okay. So in this case,it's gotta calculate what
it is, so it says that I need to do a calculation, but rather than just sort of try to use the
[00:07:00] language model to calculate it, it knows that, okay,
this calculation could be run in Python. So it basically decides that the action it's
gonna take is the Python rEPL or read Evaluate Print Loop. and its input is 17 time six that
we've got there, but written in the way that Python would understand that, it gets the
answer back. It says, I now know the answer, the final
answer is 102. So we can see that it's managed to do both
of those, really well. So let's have a look at the prompt. What actually is going on here. So when we look at this prompt, we can see
that the prompt that it's sending in, in this case, let me just make sure that's the latest
one up to date. it's gonna be answer the following questions. As best you can, you have access to the following tools. And this is where you see our names, the Python REPL and then the description. Useful for when you need to use Python. To answer a question, you should input Python
code. Duck Duck Go search useful for when you need
to search the internet, et cetera like we [00:08:00] looked at before. Wikipedia, useful for when you need to look
up a topic, country, or personal Wikipedia. so it's got that and then it's got the use
the following format and then it passes in the format for this. So, we're not giving it really a lot of examples
in this. So there are some other react agents that
you can run where it passes in some examples as well. and that's something that we could do if we
wanted to try with the Turbo api. Maybe we will look at doing that in the future. the idea here is that, it's gonna basically
use the format question, the input question you must answer. sort of rephrase what the question is that
you've gotta answer, thought you should always think about, what to do. Action. The action to take should be one of, and then
we pass in that list of the three tools. And then the, action input, the input to the
action observation, the result of the action. and it shows that, this can be done for a
number of times. And then the final answer, and then you would
give the [00:09:00] final answer. So then we pass in the question and we pass
in the scratch pad. So the scratch pad is what's taking the information
that we've received or already from one of the tools. So now you can see if I ask it. Okay. tell me about langChain. And this time it didn't use the Wikipedia
one. So the problem with these three examples is
that the Wikipedia is actually very similar to Duck Duck go, right? They can, they both have the ability to look
things up. so in this case, it's decided to use duck
duck go for this. So it's gone and done a search. It's done a search for langChain. It's got some information back, and it comes
back with our final answer. LangChain is an open source Python library
the name anyone who can write code to build LLM powered applications. All right. so I wanted to try and get it to do at least
one where it shows you Wikipedia. I knew that I had country in, one of the things about Wikipedia, in my description of Wikipedia. So the base model, when I said, tell me about
[00:10:00] Singapore. the base model obviously has some understanding
is maybe a little bit of a stretch, but it obviously has some representation of Singapore
being a country. So when it sees that, it's able to then work
out. Singapore is a country, so I should look it
up on Wikipedia and sure enough, it chooses the action Wikipedia, the input Singapore,
it gets the Wikipedia page back and you can see that there's, on that page there's stuff
about Singapore dollar, Singapore Army, Singapore, other things, but it realizes no that we are
focused in on the country. So Singapore is an island, and city state,
in Southeast Asia. And it gives, goes on to give us a bunch of different information about
this. So this is an example of it using, Wikipedia. for this, if we didn't have the Duck Duck
Go, it would've gone to Wikipedia for the langChain one because it doesn't have, another
tool for doing basic search. All right, what if I give it, let's try some
other things. What is the current price of bitcoin? Okay, in this [00:11:00] case, it realizes
the best place to get this information. Duck Duck go. current price of BTC, goes for it and just
returns it. then I was thinking, okay, well let's show
one more with the Python REPL library. So I tried, is, 11 a prime number? And sure enough, it decided that yes, you
know, that the Python REPL library was the way to go. but it, it kind of didn't do a great job. So you can see what it did was it wrote out
11, divide by two, what's left over? if there's nothing, it might be a prime number. You can see that this is what it's got back
is that, oh, it might be a prime number. So in this case, it decides well, I've still
got two possible iterations I can do. I'm gonna go and do another search and I'm
gonna use duck Duck Go. I'm going to do another tool and use Duck
Duck Go search and it searches. There is 11, a prime number, and it obviously
finds it in a list of, prime numbers somewhere on the internet, that it's able to return
that. So I wasn't that happy with that, right? Because I [00:12:00] wanted the Python REPL
to do it. So I thought, okay, well nevermind, we'll
just try rephrasing a little bit. And so we write, write a function to check
if 11, is a prime number and test it. So here you can see it's right. I need to write a function to check if if
a number is prime. Okay, what do I need for that? I need the Python, read Evaluate print Loop. So then it writes this function. and then it obviously runs then I need to test this function. So then it tests this function, is it prime,
it prints back that it's true. And then so then it finally comes back and
says, 11 is a prime number. So you see this is done multiple calls to
get the answer in this case, for both of these, they've done multiple calls in there, so I
encourage you to play around with it. you can, you can certainly try with the GPT
3.5 Turbo api. You'll just find that it doesn't work as well
for some of these react things where it has to choose, the tool. you can improve it [00:13:00] by rewriting the
prompt a bit and also rather than using a zero shot agent, giving it some examples,
for the agent. But anyway, this shows you, some examples
of running tools, in langChain. In the next video, we're gonna look at how
to make custom tools so that you could make your own tool, if you
wanted to do something, just by writing some code in Python and then using that with LangChain, as you go along. All right. as always, if you've got any questions, please
put them in the comments below. if you found this useful, please click like
and subscribe. I've got a bunch of videos coming up on langChain,
over the next week or so. Bye for now.