【GPT入门】第28课 手把手教你:用 LangChain 搭建双工具 ReACT 代理

【GPT入门】第28课 langchain React agent的概念、作用与代码案例

1.react agent的概念与作用

ReACT 代理的概念
ReACT(Thought, Action, Observation)是一种框架,核心思想是让代理能够进行思考、执行动作并根据动作结果进行观察,以此循环迭代,最终完成复杂任务。在该框架下,代理会产生一系列思考步骤,每步思考决定采取的行动,行动执行后得到相应观察结果,这些结果又影响下一步思考。
create_react_agent 的作用
create_react_agent 函数是 LangChain 提供的用于创建 ReACT 代理的便捷工具。它接收多个参数,如工具列表、语言模型等,通过这些参数配置并返回一个可执行任务的 ReACT 代理。
ReACT 代理的作用
–处理复杂任务:ReACT 代理能将复杂任务分解为一系列子任务,通过循环的思考 - 行动 - 观察过程逐步完成。例如,在需查询多个数据源、进行-计算和推理的任务中,代理可依次调用合适工具,最终得出结果。
–集成多种工具:它可集成多种工具,如搜索引擎、数据库查询工具、数学-计算工具等。代理会根据任务需求自动选择合适工具完成任务,提高任务处理效率和准确性。
–动态决策:根据每步行动的观察结果,代理可动态调整后续的思考和行动,适应不同情况和变化。

2.调用两个工具的react agent代码

import os

from langchain import hub
# from langchain.agents import load_tools #过期
from langchain_community.agent_toolkits.load_tools import load_tools
from langchain.agents import initialize_agent, create_react_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI # 过期
from langchain_community.chat_models import ChatTongyi
from langchain.agents import AgentExecutor
# 加载语言模型
#llm = OpenAI(temperature=0)
llm = ChatTongyi(
    model="qwen-max",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url=os.getenv("DASHSCOPE_BASE_URL")
)
# 加载工具
tools = load_tools(["serpapi", "llm-math"], llm=llm)
# 下载一个现有的 Prompt 模板
prompt = hub.pull("hwchase17/react")

print(prompt.template)
# 创建ReACT代理
# agent = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True) 过期
agent = create_react_agent(llm, tools, prompt)

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# 执行任务
result = agent_executor.invoke({
    
    "input": "获取跳水奥运会金牌最多的女运动员是谁,他的年龄的平方是多少?"})
print(result)

3. 代码执行结果

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

{tools}

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

Begin!

Question: {input}
Thought:{agent_scratchpad}


> Entering new AgentExecutor chain...
我需要先找到获得跳水奥运会金牌最多的女运动员是谁,然后才能计算她的年龄的平方。但是,考虑到运动员的年龄可能会随时间变化,并且直接搜索可能不会立即给出当前的具体年龄信息,我可能还需要进一步查找该运动员的出生年份来估算其当前年龄。首先,我将通过搜索来确定这位运动员的名字。
Action: Search
Action Input: 跳水奥运会金牌最多的女运动员2016年12月15日,吴敏霞正式宣布退役。 吴敏霞是世界跳水历史上获得最多奥运金牌的运动员,是奥运史上首位在跳水项目中四连冠的运动员;与邹凯及陈若琳以5枚金牌的成绩并列为获得奥运金牌数第二多的中国运动员,只仅次于获得6枚金牌的马龙;与黄雪辰以7枚奖牌的成绩并列为获得奥运奖牌数最多的中国运动员。根据搜索结果,获得跳水奥运会金牌最多的女运动员是吴敏霞。但这里并没有直接给出她的具体年龄信息。我需要进一步查找吴敏霞的出生日期以估算她当前的大致年龄,之后才能计算其年龄的平方。
Action: Search
Action Input: 吴敏霞 出生日期November 10, 1985 (age 39 years), Shanghai, China根据搜索结果,吴敏霞出生于1985年11月10日。假设当前年份为2023年,则她现在的年龄大约是37岁(如果现在还没到11月10日)或38岁(如果已经过了11月10日)。为了简化计算过程并提供一个大致的答案,我们可以取38岁作为她的近似年龄。接下来,我将使用计算器工具来计算38的平方。
Action: Calculator
Action Input: 38 ** 2Answer: 1444我通过计算得知38的平方是1444。
Final Answer: 获得跳水奥运会金牌最多的女运动员是吴敏霞,她的年龄大约为38岁(基于2023年的估算),其年龄的平方约为1444。

> Finished chain.
{'input': '获取跳水奥运会金牌最多的女运动员是谁,他的年龄的平方是多少?', 'output': '获得跳水奥运会金牌最多的女运动员是吴敏霞,她的年龄大约为38岁(基于2023年的估算),其年龄的平方约为1444。'}

Process finished with exit code 0