In this video, we ll discuss React, which is short from reasoning and acting.
We will also create and use a React agent.
React is a new approach that combines
reasoning and acting capabilities of LLMs.
It does this by allowing LLMs to generate
reasoning traces and task -specific actions in an interleaved manner.
This means that the model can reason about a task and then take actions in the
real world to gather more information or complete the task.
This combination of reasoning and acting allows React to overcome some of the
limitations of existing LLMs, such as hallucinations and error propagations.
Lang chain agents are one of the key components that make Lang chain so useful.
They take Lang chain tools, which we ve looked at in some of the other videos,
and chains and combine them into one agent.
We will create and use a React agent.
The big problem when using only tools or chains is that you probably won t want to
use the same tool with every query you send.
In our previous examples, we used as
tools Wikipedia and DuckDuckGo, but not both.
Using a React agent, it will check the
input and then decide what is the best tool to use based on the user s query.
Let s get started.
I will install the LangChainHub package.
pip install LangChainHub -q.
The LangChainHub is a hub for sharing and
discovering resources related to Lang chain.
It stores and allows access to pre
-trained prompts, chains, and agents that can be used in Lang chain projects.
I am loading the OpenAI API key from .env.
Next, let s import the modules for
prompts, agents, tools, and external services.
I am pasting the imports.
I am setting up the LLM.
LLM equals chat -open -ai -of.
And I ll choose gpt4 -turbo, a powerful language model for processing text and
generating responses.
If you don t have access to gpt4 -turbo,
use gpt3 .5 -turbo instead.
Model name equals gpt4 -turbo -preview.
And the temperature equals 0.
Once again, if you are using a free plan
and don t have access to gpt4 -turbo, use gpt3 .5 -turbo instead.
In the next step, I will prepare the prompt template for asking questions.
This template will structure the way the questions are presented to the agent.
I will define a multi -line string, answer the following questions as best
you can, and the questions, colon, and the Q in curly braces.
I am converting the template into a prompt template equals the prompt
-template constructor from template of template.
I am retrieving the structure of the
react prompt from the online -hub.
Prompt equals I am running the code.
Let's see the input variables of the prompt.
Print prompt .inputVariables.
If you want to see the template, use prompt .template.
Print prompt .template.
You can see the chain of docs.
Take a look here.
Ok, let's move on.
In this example, I will use three tools, the Python -Apple tool for executing
Python code, the Wikipedia tool for searching Wikipedia, and the DuckDuckGo
search tool for general web searches.
The agent will choose the best tool based
on the user's input query.
I am creating the first tool.
I am creating an object of type Python -Apple tool.
Python -Apple equals and I am calling the constructor of the class.
And now the tool.
Python -Apple tool equals tool of and the
arguments are name equals Python -Apple.
Give the tool a descriptive name.
The second argument will be func.
This is the function to run when using
this tool.
Python -Apple .run.
And finally, a description.
Useful when you need to run Python to
answer a question.
You should input Python code.
Let's do the same for the Wikipedia tool.
I am creating an API wrapper.
I am calling the constructor and an Wikipedia query run object.
Wikipedia equals Wikipedia query run.
And the API wrapper equals API wrapper,
the object I have just defined.
Next, I will create the Wikipedia tool.
Wikipedia tool equals tool of and the arguments name equals Wikipedia, func
equals Wikipedia run and the description.
Very well.
Let's create the DuckDuckGo search tool.
Search equals DuckDuckGo search run and
the tool.
DuckDuckGo tool.
I am calling the constructor of the tool class and the arguments are name equals
DuckDuckGo search, func equals search .run and the description.
Pay attention to single quotes if you define the string using single quotes.
Use a backslash before it.
We have created the tools, the Python or
Apple tool, the Wikipedia tool and the DuckDuckGo search tool.
Next, I will collect all the tools into a list.
Tools equals and the list.
Python or Apple tool, Wikipedia tool and the DuckDuckGo tool.
To create the agent, there are basically two main things you have to do.
Load the tools the agent is going to use and initialize the agent using the agent
executor and those tools.
First, agent equals createReactAgent of
LLM, tools and prompt.
The createReact function was imported
from langchain .agents.
This line of code combines the LLM, the
tools and the prompt to create a React agent.
Next, I will set up an agent executor to
run the agent.
The executor examines the input and
determines the best tool to use based on the user's query.
I am creating the executor to manage the agent's actions and output.
AgentExecutor equals and I am calling the constructor of the AgentExecutor class
and the arguments, agent equals agent, this is the object returned by the
createReactAgent function, tools equals tools, my list of tools and the verbose
equals true.
I will add two more important arguments,
handle parsingErrors equals true and maxIterations equals 10.
HandleParsingErrors controls how the agent executor handles potential parsing
errors that might occur during the execution of tools or the agent itself.
If it is set to true, the executor will attempt to gracefully handle parsing
errors by logging the error message for debugging purposes, continuing with the
execution process, potentially skipping the affected tool or part of the agent's response.
It will also try to recover and proceed with the remaining steps.
If it is set to false, which is the default, parsing errors will cause the
execution to stop immediately, raising an exception.
Using this argument can make agents more
robust and less prone to unexpected failures due to parsing issues,
especially when working with external tools or unpredictable input.
MaxIterations sets a maximum limit on the number of iterations or steps the agent
can take during a single execution.
Using this argument prevents agents from
running indefinitely, which could lead to performance issues, resource exhaustion
or getting stuck in loops.
Once the specific number of iterations is
reached, the execution will stop, even if the agent hasn't fully completed its task.
Note that the optimal value for MaxIterations depends on the complexity
of the task and the expected execution time, you might need to adjust it based
on the specific use case.
Ok, we've successfully built the React
agent, let's take a break and in the next video we'll test it.
I'm really excited to see what it can do, see you