讓 Agent 搜尋網路
你的朋友想匯出 $500 給她在紐約的妹妹,但不確定匯率是否仍然有利。她記得上週看到「1 GBP → 1.24 USD」,但匯率每天都可能變動。
為了幫她做出明智的決定,你決定建立一個能夠:
- 查詢即時 GBP -> USD 匯率
- 摘要過去 7 天匯率的變化情況
你會使用內建的 VisitWebpageTool 讓你的 agent 具備網頁搜尋能力。
注意:model 已為你初始化,CodeAgent 類別也已匯入。
本練習屬於課程
使用 Hugging Face smolagents 的 AI 代理
練習說明
- 從
smolagents函式庫匯入VisitWebpageTool。 - 使用提供的 model 與包含一個
VisitWebpageTool()實例的工具清單建立 agent。 - 使用
.run()方法執行 agent,並傳入task。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the VisitWebpageTool class
from smolagents import ____
# Create agent with web search capabilities
agent = CodeAgent(
tools=[____],
model=model
)
task = "Find GBP to USD exchange rates and summarize how this rate has changed over the past 7 days. A good source is usually Wise."
# Run the agent with the task
result = agent.run(____)