LangChain Agent 实验过程剖析 OpenAI

[复制链接]
查看1381 | 回复0 | 2023-8-23 12:14:13 | 显示全部楼层 |阅读模式
什么是LangChain Agent

简单来说,用户像LangChain输入的内容未知。此时可以有一套工具聚集(也可以自界说工具),将这套自界说工具托管给LLM,让其自己决定利用工具中的某一个(假如存在的话)
例子

首先,这里自界说了两个简单的工具
  1. from langchain.tools import BaseTool
  2. # 天气查询工具 ,无论查询什么都返回Sunny
  3. class WeatherTool(BaseTool):
  4.     name = "Weather"
  5.     description = "useful for When you want to know about the weather"
  6.     def _run(self, query: str) -> str:
  7.         return "Sunny^_^"
  8.     async def _arun(self, query: str) -> str:
  9.         """Use the tool asynchronously."""
  10.         raise NotImplementedError("BingSearchRun does not support async")
  11. # 计算工具,暂且写死返回3
  12. class CustomCalculatorTool(BaseTool):
  13.     name = "Calculator"
  14.     description = "useful for when you need to answer questions about math."
  15.     def _run(self, query: str) -> str:
  16.         return "3"
  17.     async def _arun(self, query: str) -> str:
  18.         raise NotImplementedError("BingSearchRun does not support async")
复制代码
接下来是针对于工具的简单调用:留意,这里利用OpenAI temperature=0需要限定为0
  1. from langchain.agents import initialize_agent
  2. from langchain.llms import OpenAI
  3. from CustomTools import WeatherTool
  4. from CustomTools import CustomCalculatorTool
  5. llm = OpenAI(temperature=0)
  6. tools = [WeatherTool(), CustomCalculatorTool()]
  7. agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
  8. agent.run("Query the weather of this week,And How old will I be in ten years? This year I am 28")
复制代码
看一下完备的响应过程:
  1. I need to use two different tools to answer this question
  2. Action: Weather
  3. Action Input: This week
  4. Observation: Sunny^_^
  5. Thought: I need to use a calculator to answer the second part of the question
  6. Action: Calculator
  7. Action Input: 28 + 10
  8. Observation: 3
  9. Thought: I now know the final answer
  10. Final Answer: This week will be sunny and in ten years I will be 38.
复制代码
可以看到LangChain Agent 详细分析了每一个步调,而且准确的调用了每一个可用的方法,拿到了相应的返回值,甚至在末了还修复了28+10=3这个错误。
下面看看LangChain Agent是怎样做到这点的
工作原理

首先看看我输入的问题是什么:
Query the weather of this week,And How old will I be in ten years? This year I am 28
查询本周气候,以及十年后我多少岁,今年我28
LangChain Agent中,有一套模板可以套用:
  1. PREFIX = """Answer the following questions as best you can. You have access to the following tools:"""
  2. FORMAT_INSTRUCTIONS = """Use the following format:
  3. Question: the input question you must answer
  4. Thought: you should always think about what to do
  5. Action: the action to take, should be one of [{tool_names}]
  6. Action Input: the input to the action
  7. Observation: the result of the action
  8. ... (this Thought/Action/Action Input/Observation can repeat N times)
  9. Thought: I now know the final answer
  10. Final Answer: the final answer to the original input question"""
  11. SUFFIX = """Begin!
  12. Question: {input}
  13. Thought:{agent_scratchpad}"""
复制代码
通过这个模板,加上我们的问题以及自界说的工具,会酿成下面这个样子,而且附带表明:
  1. Answer the following questions as best you can.  You have access to the following tools: #  尽可能的去回答以下问题,你可以使用以下的工具:
  2. Calculator: Useful for when you need to answer questions about math.
  3. # 计算器:当你需要回答数学计算的时候可以用到
  4. Weather: useful for When you want to know about the weather #  天气:当你想知道天气相关的问题时可以用到
  5. Use the following format: # 请使用以下格式(回答)
  6. Question: the input question you must answer #  你必须回答输入的问题
  7. Thought: you should always think about what to do
  8. # 你应该一直保持思考,思考要怎么解决问题
  9. Action: the action to take, should be one of [Calculator, Weather] #  你应该采取[计算器,天气]之一
  10. Action Input: the input to the action #  动作的输入
  11. Observation: the result of the action # 动作的结果
  12. ...  (this Thought/Action/Action Input/Observation can repeat N times) # 思考-行动-输入-输出 的循环可以重复N次
  13. T
  14. hought: I now know the final answer # 最后,你应该知道最终结果了
  15. Final Answer: the final answer to the original input question # 针对于原始问题,输出最终结果
  16. Begin! # 开始
  17. Question: Query the weather of this week,And How old will I be in ten years?  This year I am 28 #  问输入的问题
  18. Thought:
复制代码
通过这个模板向openai规定了一系列的规范,包罗如今现有哪些工具集,你需要思考答复什么问题,你需要用到哪些工具,你对工具需要输入什么内容,等等。
假如仅仅是如许,openAI会完全补完你的答复,中心无法插入任何内容。因此LangChain利用OpenAI的stop参数,截断了AI当前对话。"stop": ["\\nObservation: ", "\\n\\tObservation: "]
做了以上设定以后,OpenAI仅仅会给到Action和 Action Input两个内容就被stop早停了。
以下是OpenAI的响应内容:
  1. I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.
  2. Action: Weather
  3. Action Input: This week
复制代码
到这里是OpenAI的响应效果,可见,很简单就拿到了Action和Action Input。
这里从Tools中找到name=Weather的工具,然后再将This Week传入方法。详细业务处理看详细环境。这里仅返回Sunny。
由于当前找到了Action和Action Input。 代表OpenAI认定当前使命链并没有竣事。因此像请求体后拼接效果:Observation: Sunny 而且让他再次思考Thought:
开启第二轮思考:
下面是再次请求的完备请求体:
  1. 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 weatherUse the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Calculator, Weather]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: Query the weather of this week,And How old will I be in ten years? This year I am 28Thought: I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.
  2. Action: Weather
  3. Action Input: This week
  4. Observation: Sunny^_^Thought:
复制代码
同第一轮一样,OpenAI再次举行思考,而且返回Action 和 Action Input 后,再次被早停。
  1. I need to calculate my age in ten years
  2. Action: Calculator
  3. Action Input: 28 + 10
复制代码
由于计算器工具只会返回3,效果会拼接出一个错误的效果,构造成了一个新的请求体
举行第三轮请求:
  1. 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 weatherUse the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Calculator, Weather]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: Query the weather of this week,And How old will I be in ten years? This year I am 28Thought: I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.
  2. Action: Weather
  3. Action Input: This week
  4. Observation: Sunny^_^Thought:I need to calculate my age in ten years
  5. Action: Calculator
  6. Action Input: 28 + 10
  7. Observation: 3Thought:
复制代码
此时两个问题全都拿到了效果,根据开头的限定,OpenAi在完全拿到效果以后会返回I now know the final answer。而且根据完备上下文。把多个效果举行归纳总结:下面是完备的相应效果:
  1. I now know the final answer
  2. Final Answer: I will be 38 in ten years and the weather this week is sunny.
复制代码
可以看到。ai严格的按照设定返回想要的内容,而且还以外的把28+10=3这个数学错误给改正了
以上,就是LangChain Agent的完备工作流程

来源:https://blog.csdn.net/qq_35361412/article/details/129797199
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则