ReAct 代理
现在动手创建您自己的 ReAct 代理吧!请回忆,ReAct 代表 Reason and Act(推理与行动),描述了其决策方式。在本练习中,您将加载内置的 wikipedia 工具,把来自 Wikipedia 的外部数据与您的 LLM 集成。我们已为您定义了一个使用 OpenAI 的 gpt-4o-mini 模型的 llm。
注意:wikipedia 工具需要将 wikipedia Python 库作为依赖进行安装。在本环境中已为您安装完毕。
本练习是课程的一部分
使用 LangChain 开发 LLM 应用
练习说明
- 使用
load_tools()加载"wikipedia"工具。 - 使用
create_react_agent()定义一个 ReAct 代理,并传入llm和要使用的工具。 - 在给定输入上运行代理,并打印
response中最终消息的内容。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define the tools
tools = load_tools(["____"])
# Define the agent
agent = create_react_agent(____, ____)
# Invoke the agent
response = agent.____({"messages": [("human", "How many people live in New York City?")]})
print(response['messages'][-1].____)