LangChain Agent (study notes)

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 acting
  • Tool tool
  • Toolkit Toolkit
  • AgentExecutor 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.


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:

 
 
  1. pip install duckduckgo-search

1. Create tool instances through tool classes


This class provides the functionality to search through the DuckDuckGo search engine.

 
 
  1. from langchain.tools import DuckDuckGoSearchRun
  2. search = DuckDuckGoSearchRun()
  3. 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:

 
 
  1. class DuckDuckGoSearchRun(BaseTool):
  2. """Tool that adds the capability to query the DuckDuckGo search API."""
  3. name = "duckduckgo_search"
  4. description = (
  5. "A wrapper around DuckDuckGo Search. "
  6. "Useful for when you need to answer questions about current events. "
  7. "Input should be a search query."
  8. )


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.
DuckDuckGoSearchRunduckduckgo_searchload_toolsddg-search

Please refer to the source code load_tools.py for details on tool data initialization.


usage

 
 
  1. from langchain.agents import load_tools
  2. tools = load_tools(['ddg-search'])
  3. search = tools[0]
  4. 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.

 
 
  1. from langchain.agents import get_all_tool_names
  2. 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:

 
 
  1. from langchain.agents import load_tools
  2. from langchain.agents import initialize_agent
  3. from langchain.agents import AgentType
  4. from langchain.llms import OpenAI
  5. import os
  6. os.environ['OPENAI_API_KEY'] = "您的有效openai api key"
  7. llm = OpenAI(temperature=0)
  8. tools = load_tools(["ddg-search", "llm-math"], llm=llm)
  9. agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
  10. 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.

 
 
  1. def initialize_agent(
  2. tools: Sequence[BaseTool],
  3. llm: BaseLanguageModel,
  4. agent: Optional[AgentType] = None,
  5. callback_manager: Optional[BaseCallbackManager] = None,
  6. agent_path: Optional[str] = None,
  7. agent_kwargs: Optional[dict] = None,
  8. *,
  9. tags: Optional[Sequence[str]] = None,
  10. **kwargs: Any,
  11. ) -> AgentExecutor:
  12. """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.

Guess you like

Origin blog.csdn.net/javastart/article/details/134488537