让代理搜索网页
您的朋友想给她在纽约的妹妹汇款 $500,但不确定当前汇率是否仍然划算。她记得上周看到过 "1 GBP → 1.24 USD",但汇率每天都会变动。
为帮助她做出明智决定,您决定构建一个代码代理,该代理可以:
- 查询实时 GBP -> USD 汇率
- 总结过去 7 天汇率的变化情况
您将使用内置的 VisitWebpageTool 为代理赋予网页搜索能力。
注意:model 已为您初始化。CodeAgent 类也已导入。
本练习是课程的一部分
使用 Hugging Face smolagents 的 AI Agents
练习说明
- 从
smolagents库中导入VisitWebpageTool。 - 使用提供的模型和包含
VisitWebpageTool()实例的工具列表来创建代理。 - 使用
.run()方法运行代理,并传入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(____)