開始使用免費開始

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].____)
編輯並執行程式碼