Okay, so in this chapter we are going to build a simple agent using language that is the problem we
want to solve.
We want our LM application to be able to decide which tool is the most suitable to answer the question
we pose.
And we want our application in this case, to be able to search on Google and to search for information
on a specific URL.
So we are going to build a very simple agent with these two tools.
He's going to be able to search on Google, and he's going to be able to search for information on a
specific URL.
And as an agent, we want him to decide on autonomously which tool is he going to use, depending on
the question we have.
So the agent is going to decide if he wants to search on Google, or if he wants to search on a particular
URL, depending on on the question we provide to to the application.
So what we are going to do is we are going to create an agent that knows how to decide between two tools
doing a search on Google or connecting to a specific web page.
This is the process we are going to follow.
We have the code here on the right.
So first, as always, we load the OpenAI credentials from our dot env file.
This is the confidential file we use to store our passwords, etc., etc..
After that.
What we do is load OpenAI, the module OpenAI, in order to create an instance of an LM.
And once we have that, we are going to load the two.
Tools we are going to use.
The first tool we are going to use is Google Search.
In order to make use of Google Search in your LM application, first you want to get a couple of private
keys in Google.
So here you have the links to go to Google in order to sign up to create your Google API key and your
Google CC ID, and without any question you may have in the process of getting these keys, you know
where to go ChatGPT, Google, StackOverflow, etc. it's very easy.
It's free and it's very easy.
Once you have these keys, you store these keys in your dot m file.
And in this case, what we are doing here is we are getting these keys from our dot m file.
Then in order to, uh, create a tool that is able to search on Google, we need to install the Google
API, Python client, plugin or module to do that.
If we want to do that from the notebook, we write this and execute this command.
If we want to do it from the terminal, we execute this command from the terminal.
It's different if you do it from the notebook.
From the notebook you add the exclamation mark and from the terminal you just write this down.
Since we have already installed this, we comment this and we don't want to do anything.
So then we are going to create the variable.
In this case, we name the variable search in order to make use of that functionality.
So now we are able to use this functionality in the variable search.
So first thing cover we now are able to search on Google from our LM application.
The second functionality we want to be able to connect with an external URL.
In order to do that, we are going to load or import a module called Text Requests wrapper from the
Lang Chain Utilities library.
And once we have this module, we can create a new variable.
We will call it request.
You can call it whatever you want in order to make use of this new functionality.
Once we have these two functionalities in our in our application, we are going to set the tools that
we that will use our agent.
So the first thing we do is we import the tool module.
And with this tool module we are going to create a new variable called tools that is going to store
our two tools okay.
So this is the first tool which is the Google search.
And this is the second tool which is the request what we have called requests.
What is doing this is going to connect with a particular URL on the internet okay.
So once we have the tools defined we are going to initialize and configure our agent okay.
In order to do that we are going to load three more modules initialize agent agent type and load tools.
And we are going to create our agent.
So we are going to call our agent my agent you can call it whatever you want.
And we are going to initialize it with this functionality.
And we are going to tell them okay, you are going to use these tools we have created.
We are going to use this LM instance we have created here.
Okay.
And, uh, this is the type of agent we want to use from long chain.
As you will see, if you go to the long chain documentation, long chain provides a few, uh, types
of agent you can use.
This is by far the most popular, the one we use, like, 99% of the time.
But you have other options as well.
We are going to define this new agent with the option verbose as true because we want to see what is
happening, you know, on the on the background whenever we execute a command we'll see how very soon.
And this is very important, we are setting a max iterations to three because we want to avoid here
one of the main.
One of the most common troubles you are going to find when working with agents, which is that in some
cases, if the agent is not able to find an answer from your questions right away, he's going to loop
over and over trying to get the response.
So if you do not limit the number of attempts your agent is going to make in order to solve your questions,
your agent can be looping, you know, forever, and your OpenAI bill is going to be high.
So be very careful with this.
Okay, so this is the note we make here.
Operations with agents are more expensive than regular operations in OpenAI.
And especially if the number of iterations is high okay.
So be careful with this.
So once we have defined our agent we can start asking questions.
And we will see how our agent decides which tool to use.
So we are going to start asking our agent which team won the 2010 soccer World Championship.
Is the question.
And this is the way we run the question through our agent.
Okay.
And.
We are going to store this response into this new variable we create.
So here as we have defined variables through here, we can see what are the operations our agent is
doing in the background.
So.
Our agent reads the question, and once he reads the question, he needs to decide which of the two
tools is more, uh, adequate for him to use.
So in this case, you can see that our agent is thinking and is is thinking, okay, I should look up
the result online.
So I'm going to use Google Search in order to solve this question.
So what he is doing here is querying Google asking this to Google okay Google.
Who is the world champion in in this year.
Okay.
So the first response gives the answer to our agent.
So once he.
Process this information.
He knows the final answer.
So here you have the thought of our agent.
I know the final answer.
So he has the final answer set.
If we print the response, we now see how our agent is answering.
Spain won the 2010 soccer World Championship.
If we make another type of question asking for example, okay, go to this particular URL and tell me.
What are the comments in this web page about?
Okay, so.
The.
Agent.
Once he starts processing our question, he immediately guessed that the best way to solve this question
is to use the second tool.
The tool we have named request.
And as you know, the tool that allows our agent to connect with one particular URL, one particular
web page on the internet.
So he's connecting with this web page and he is downloading all the HTML, all the code that is in the
background of this web page.
And he's processing.
He's learning from all this code.
As you know, a web page on the background is HTML, but this HTML is wrapping all the text that is
in a web page.
So our agent is reading the website, the website, it is this web page.
And you will see that after reading the information in this web page he says, I can see the comments
in the web page which will give me the subjects.
So he he processes the information, blah blah blah.
And immediately he finds out that the subjects in the comments of the web page are related to OpenAI's
embedding API.
La la la la la.
Okay, so if we print the response now we know, uh, what our agent has found found out, right?
So as you have seen.
We have learned how to build a simple agent, and we have also learned.
Start learning about the limitations of agents and how you have to be careful with them, especially
when you are working in a professional environment.