So in this chapter, we are going to talk a little bit more about agents in long Chain.
For me, the most important thing of this chapter is a.
To tell you a few warnings regarding agents.
So it is important for you to be aware that agents are not still a mature functionality in launching.
So they are still in in a experimental phase, and until they improve, it is not advisable to use them
in a professional application.
So they are very popular.
They created a lot of excitement at the beginning, but now we are seeing that in many occasions they
are failing when we are trying to do serious things with them.
So one thing is like, you know, like a toy application, like a game application, like a non serious
application and like a hobby application.
Another thing is a professional application where a customer is paying you to develop it or whatever,
right, or for your startup or whatever.
So careful with agents are appealing, are interesting, but they are still not 100% mature.
Maybe they will soon, but at this time they are still not 100% mature.
So using agents A also is relatively expensive in terms of OpenAI.
This is another thing you need to be aware of, uh, when you, uh, make a different operations with
agents in OpenAI, with ChatGPT, this is going to be more expensive than usual operations.
A and finally, agents tend to break or get lost in infinite loops if they are not ask a question related
to the tools they have access to.
We are going to see an example now.
But this is very important because.
If your application goes into an infinite loop and you have this application connected to OpenAI or
any other private LM model, that is going to cause you a lot of money.
So you have to be extremely careful with that.
And every time you work with agents, you need to limit the maximum number of iterations.
We are going to see that in the example that they can make in order to prevent a possible disasters,
like the one I told you.
So in the exercise of today, A, we are going to play around a little bit with agents.
Again, we want to create an LM application that autonomously autonomous, autonomously decides which
tool it will use to solve the problem we present to to it.
So this is the keyword autonomously okay.
So we are going to use a long chain agents.
Initially we are going to create a very simple one very similar to one you already know.
And then we will create a customized agent based on a very simple drag application.
So in the right side of the screen first you have our classic, you know, a m file connection.
So we can, uh, communicate with the OpenAI API and use ChatGPT.
The initial part of this exercise is just a very simple drag application.
So you are already familiar with it.
Uh, we we create the instance of the LM model.
We load a text file, which is going to be our private document.
We divide this document into small chunks with a text splitter.
And finally we are going to load these, uh, chunks of text converted into numbers to embeddings.
We are going to load the embeddings into a vector database.
So.
Once we have that here, you have our typical chain.
The retrieval QA chain in order to make questions to to this private document.
Okay.
So now we are going to see how we can do that with an agent instead of using the default chain we were
using before.
Before doing that, we are going to create a simple agent.
So in order to one of them, the most popular uses of agents, if you remember, is that we provide
the agent with a number of tools, and the agent is going to decide, uh, which tool it will use depending
on the question we make.
So in this case, to simplify that to the maximum level, we are going to provide just one tool.
So our tool is going to be the LM math which is like a math calculator tool.
In order to to to be able to play with it, you need to install this, uh, plugin before.
And once you have it, you know, you just define the, the tools.
And once you have defined the tools, you can initialize the agent, as you remember with the LM, the
tools, the LM model, etc., etc., etc. the agent type is going to be the most popular, one is going
to be true.
So we can see what is happening in the background and we are going to set.
Max iterations to three, so we are preventing possible loops.
So you will see one loop here.
So first we are going to check this agent with a math question.
If we make a math question the agent doesn't have any problem.
Immediately is going to use the tool.
You know the calculator is going to provide a good response in a very fast a way.
But what happens if we make a question that is not related with math?
So the agent is going to try to find a tool able to respond.
Our question he is trying.
If you see here he's trying to use the Google tool, but Google tool is not installed for this agent.
So he's, you know, hitting errors, but you see that he's hitting error and trying again and trying
again.
And he will continue until the infinite if we haven't, uh, configured the max iteration number to
three.
So once it hits the third, uh, iteration, it stop.
Okay.
So here you have the error message agent stop due to iteration limit okay.
So after this very simple example, just a reminder of how agents work.
We are going to create a custom agent that is going to.
A work as our, uh, default chain in the in the in the beginning of this, uh, exercise.
So I am not going into the details, but the only point you need to keep in mind is that our tool,
we are creating a custom tool here, this custom tool, what it does is it is going to search, you
know, through our private document.
And the most important thing to to keep in mind is that it is making use of our vector database.
This store embeddings is the vector database we have created.
Here in our rug application.
So if we a.
Uh, proceed.
We define the agent, you know, with initialize agent.
Uh, now, and we are using the tools with with the custom tool we have provided.
So a maximum iteration three.
If we ask a question, this, uh, agent is going to make use of the, uh, article search tool, which
is the name of our custom tool and is going to find the answer we are looking for, which is a summary
of this article.
So apart from having this, uh, working code for you to play around and to experiment, the most important
thing of this chapter is to remember the warnings around agents.
So careful with agents are very interesting, a very appealing to most developers until you find out
that in a production environment they can fail.
So careful with them right now.