LangChain Agent execution process analysis OpenAI

LangChain Agent execution process analysis

What is LangChain Agent

In simple terms, the content entered by the user like LangChain is unknown. At this point, you can have a set of tools (you can also customize tools), host this set of custom tools to LLM, and let it decide to use one of the tools (if it exists)

example

First, here are two simple tools customized

from langchain.tools import BaseTool

# 天气查询工具 ,无论查询什么都返回Sunny
class WeatherTool(BaseTool):
    name = "Weather"
    description = "useful for When you want to know about the weather"

    def _run(self, query: str) -> str:
        return "Sunny^_^"

    async def _arun(self, query: str) -> str:
        """Use the tool asynchronously."""
        raise NotImplementedError("BingSearchRun does not support async")

# 计算工具,暂且写死返回3
class CustomCalculatorTool(BaseTool):
    name = "Calculator"
    description = "useful for when you need to answer questions about math."

    def _run(self, query: str) -> str:
        return "3"

    async def _arun(self, query: str) -> str:
        raise NotImplementedError("BingSearchRun does not support async")

Next is a simple call for the tool: Note that the use of OpenAI here temperature=0needs to be limited to 0

from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from CustomTools import WeatherTool
from CustomTools import CustomCalculatorTool

llm = OpenAI(temperature=0)

tools = [WeatherTool(), CustomCalculatorTool()]

agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

agent.run("Query the weather of this week,And How old will I be in ten years? This year I am 28")

Take a look at the complete response process:

I need to use two different tools to answer this question
Action: Weather
Action Input: This week
Observation: Sunny^_^
Thought: I need to use a calculator to answer the second part of the question
Action: Calculator
Action Input: 28 + 10
Observation: 3
Thought: I now know the final answer
Final Answer: This week will be sunny and in ten years I will be 38.

It can be seen that LangChain Agent analyzed each step in detail, and correctly called each available method, got the corresponding return value, and even fixed the error of 28+10=3 at the end.
Let's see how LangChain Agent does this

working principle

First look at what the question I entered is:
Query the weather of this week,And How old will I be in ten years? This year I am 28
query the weather this week, and how old will I be in ten years, I am 28 this year

In LangChain Agent, there is a set of templates that can be applied:

PREFIX = """Answer the following questions as best you can. You have access to the following tools:"""
FORMAT_INSTRUCTIONS = """Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question"""
SUFFIX = """Begin!

Question: {input}
Thought:{agent_scratchpad}"""

With this template, plus our questions and custom tools, it would look like this, with explanations:

Answer the following questions as best you can.  You have access to the following tools: #  尽可能的去回答以下问题,你可以使用以下的工具:


Calculator: Useful for when you need to answer questions about math.
 # 计算器:当你需要回答数学计算的时候可以用到
Weather: useful for When you want to know about the weather #  天气:当你想知道天气相关的问题时可以用到
Use the following format: # 请使用以下格式(回答)


Question: the input question you must answer #  你必须回答输入的问题
Thought: you should always think about what to do
 # 你应该一直保持思考,思考要怎么解决问题
Action: the action to take, should be one of [Calculator, Weather] #  你应该采取[计算器,天气]之一
Action Input: the input to the action #  动作的输入
Observation: the result of the action # 动作的结果
...  (this Thought/Action/Action Input/Observation can repeat N times) # 思考-行动-输入-输出 的循环可以重复N次
T
hought: I now know the final answer # 最后,你应该知道最终结果了
Final Answer: the final answer to the original input question # 针对于原始问题,输出最终结果


Begin! # 开始
Question: Query the weather of this week,And How old will I be in ten years?  This year I am 28 #  问输入的问题
Thought:

Through this template, a series of specifications are stipulated to openai, including which tool sets are currently available, what questions you need to think about answering, which tools you need to use, what content you need to input for the tools, and so on.

If it's just this, openAI will completely complete your answer, and nothing can be inserted in the middle. Therefore, LangChain uses the stop parameter of OpenAI to cut off the current conversation of AI."stop": ["\\nObservation: ", "\\n\\tObservation: "]

After making the above settings, OpenAI will only give andAction be stopped early. The following is the content of OpenAI's response:Action Input

I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.
Action: Weather
Action Input: This week

Here is the response result of OpenAI. It can be seen that the Action and Action Input are easily obtained.
Here we find the tools from Tools name=Weather, and then pass This Week into the method. See details for specific business processing. Only Sunny is returned here.

Since Action and Action Input are currently found. On behalf of OpenAI, it is determined that the current task chain is not over. So concatenate the result after the request body: Observation: Sunnyand let him think againThought:

Start the second round of thinking:
the following is the complete request body of the request again:

Answer the following questions as best you can. You have access to the following tools:

Calculator: Useful for when you need to answer questions about math.
Weather: useful for When you want to know about the weather


Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Calculator, Weather]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: Query the weather of this week,And How old will I be in ten years? This year I am 28
Thought: I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.
Action: Weather
Action Input: This week
Observation: Sunny^_^
Thought:

As in the first round, OpenAI thought again, and afterAction returning and , it was stopped early again.Action Input

I need to calculate my age in ten years
Action: Calculator
Action Input: 28 + 10

Since the calculator tool will only return 3, the result will be concatenated into a wrong result, and a new request body will be constructed
for the third round of requests:

Answer the following questions as best you can. You have access to the following tools:

Calculator: Useful for when you need to answer questions about math.
Weather: useful for When you want to know about the weather


Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Calculator, Weather]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: Query the weather of this week,And How old will I be in ten years? This year I am 28
Thought: I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.
Action: Weather
Action Input: This week
Observation: Sunny^_^
Thought:I need to calculate my age in ten years
Action: Calculator
Action Input: 28 + 10
Observation: 3
Thought:

At this time, the results of both questions have been obtained. According to the restrictions at the beginning, OpenAi will return after the results are completely obtained I now know the final answer. And in full context. Summarize multiple results: the following is the complete corresponding result:

I now know the final answer
Final Answer: I will be 38 in ten years and the weather this week is sunny.

can be seen. ai strictly returns the desired content according to the setting, and also corrected the mathematical error of 28+10=3

The above is the complete workflow of LangChain Agent

Guess you like

Origin blog.csdn.net/qq_35361412/article/details/129797199