Original text:LangChain Agent (study notes) - Chen Ye Xinfan's column - TNBLOG
LangChain Agent (study notes)
Introduction
Agent
is an agent. Its core idea is to use a language model to select a series of actions to be performed. The chain of LangChain
hardcodes a series of actions into the code.
In Agent
, the language model is used as an inference engine to determine which actions should be performed and in what order.
This involves several key components:
Agent
actingTool
toolToolkit
ToolkitAgentExecutor
Agent Executor
Next we will introduce them one by one.
Agent
Agent
is driven by a language model and a prompt word to decide what action to take next.
Prompt words can include the following:
- The agent's personality (used to make it respond in a specific way)
- The agent's background (used to give it more contextual information about the type of task it is performing)
- Guidance strategies (used to inspire better reasoning skills)
LangChain
Different types of proxies are provided:
Zero-shot ReAct
Use the ReAct framework to decide which tool to use based on the tool's description. Multiple tools can be used, but description information needs to be provided for each tool.
The selection of the tool depends solely on the tool description information.
For more information about the ReAct framework, please refer to ReAct.
Structured Input ReAct
Compared to agents that take a single string as input, this type of agent can create structured action input through the tool's parameter schema.
OpenAI Functions
This type of agent is used to work with the OpenAI Function Call mechanism.
Conversational
This type of agent is designed for conversational scenarios, uses conversational prompts, leverages the ReAct framework selection tools, and leverages memory functionality to save conversation history.
Self ask with search
Such agents use tools to find factual answers to their questions.
ReAct document store
Use the ReAct framework to interact with document storage. When using it, you need to provide Search
tools and Lookup
tools, which are used to search for documents and find recently. Find terms in documents.
Plan-and-execute agents
The agent plans what to do and then executes subtasks to achieve the goal.
Here we have mentioned "tools" many times, which is Tool
. Next, we will introduce what Tool
is.
Tool
Tool
Tool is a function called by the agent, usually used to interact with the external world, such as Wikipedia search, database access, etc. LangChain
For the list of built-in tools, please refer to Tools.
Toolkit
Typically, a set of tools is required to achieve a specific goal. LangChain
provides the concept of Toolkit
toolkit, which combines multiple tools together.
AgentExecutor
The agent executor is the runtime of the agent.
When the program is running, it calls the agent and performs the actions it chooses.
Component instance
Tool
LangChain
provides a series of tools, such as Search
tools, AWS
tools, Wikipedia
tools, etc.
These tools are all subclasses of BaseTool
.
Execute the function of the tool by calling the run
function.
Let’s take the LangChain
built-in tool DuckDuckGoSearchRun
as an example to see how to use the tool.
Note: To use the DuckDuckGoSearchRun tool, you need to install the following python packages:
pip install duckduckgo-search
1. Create tool instances through tool classes
This class provides the functionality to search through the DuckDuckGo search engine.
from langchain.tools import DuckDuckGoSearchRun
search = DuckDuckGoSearchRun()
search.run("Who is winner of FIFA worldcup 2018?")
You should expect output like this:
Note: Due to space limitations, the text of the answer to the model is excerpted from this lecture.
2. Loading through auxiliary function load_tools
LangChain
provides functions load_tools
to load tools based on their names.
Let’s first take a look at the definition of the DuckDuckGoSearchRun class:
class DuckDuckGoSearchRun(BaseTool):
"""Tool that adds the capability to query the DuckDuckGo search API."""
name = "duckduckgo_search"
description = (
"A wrapper around DuckDuckGo Search. "
"Useful for when you need to answer questions about current events. "
"Input should be a search query."
)
name
The variable defines the name of the tool.
This is exactly what we need when using the load_tools
function to load the tool.
Of course, what is currently tricky is that the implementation of load_tools
maps the tool names, so not all tools use the ones defined in the tool class truthfully a> to load the tool. function needs to use , but the is For example, the name of name
. DuckDuckGoSearchRun
duckduckgo_search
load_tools
ddg-search
Please refer to the source code load_tools.py for details on tool data initialization.
usage
from langchain.agents import load_tools
tools = load_tools(['ddg-search'])
search = tools[0]
search.run("Who is winner of FIFA worldcup 2018?")
You should expect similar output to method 1.
Finally, share a helper function get_all_tool_names
for getting the names of all tools.
from langchain.agents import get_all_tool_names
get_all_tool_names()
In the current LangChain
version 0.0.235
, we should see the following list:
Agent
Agent
usually needs Tool
to work together, so we put the Agent
instance after Tool
.
Let’s take the Zero-shot ReAct type Agent
as an example to see how to use it. The code is as follows:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
import os
os.environ['OPENAI_API_KEY'] = "您的有效openai api key"
llm = OpenAI(temperature=0)
tools = load_tools(["ddg-search", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("What is the height difference between Eiffel Tower and Taiwan 101 Tower?")
Code explanation:
Refer to the implementation of initialize_agent
, we will see that it returns an instance of type AgentExecutor
.
This is also a common use of proxy executors.
Please go to the source code initialize.py for more details on initializing the agent executor.
def initialize_agent(
tools: Sequence[BaseTool],
llm: BaseLanguageModel,
agent: Optional[AgentType] = None,
callback_manager: Optional[BaseCallbackManager] = None,
agent_path: Optional[str] = None,
agent_kwargs: Optional[dict] = None,
*,
tags: Optional[Sequence[str]] = None,
**kwargs: Any,
) -> AgentExecutor:
"""Load an agent executor given tools and LLM.
You should expect output like this:
Summarize
In this lesson, we learned what Agent
agents are, Tool
tools, and AgentExecutor
agent executors , and learned their basic usage. In the next lecture we will learn about Callback
callbacks.
For the complete sample code of this course, please refer to 08_Agents.ipynb.