将自定义工具集成到代理中
现在您已经准备好了工具,是时候搭建您的代理式工作流了!您将再次使用 ReAct 代理。回顾一下,它会推理应采取的步骤,并根据上下文与工具描述来选择工具。我们已为您定义了一个 llm,使用的是 OpenAI 的 gpt-4o-mini 模型。
本练习是课程的一部分
使用 LangChain 开发 LLM 应用
练习说明
- 使用
create_react_agent()和llm以及包含retrieve_customer_info工具的列表来创建一个 ReAct 代理。 - 使用
agent.invoke()对给定输入调用该代理。 - 打印最终消息中的内容。
交互式实操练习
通过完成这段示例代码来试试这个练习。
@tool
def retrieve_customer_info(name: str) -> str:
"""Retrieve customer information based on their name."""
customer_info = customers[customers['name'] == name]
return customer_info.to_string()
# Create a ReAct agent
agent = create_react_agent(____, [____])
# Invoke the agent on the input
messages = agent.____({"messages": [("human", "Create a summary of our customer: Peak Performance Co.")]})
print(messages['messages'][-1].____)