開始使用免費開始

將自訂工具整合到代理中

現在你已經備妥工具,是時候設定你的代理式工作流程了!你將再次使用 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].____)
編輯並執行程式碼