Agents 课程文档
为你的 Agent 构建和集成工具
加入 Hugging Face 社区
并获得增强的文档体验
开始
为你的 Agent 构建和集成工具
在这一节中,我们将授予 Alfred 访问网络的权限,使他能够查找最新的新闻和全球更新。此外,他还将有权访问天气数据和 Hugging Face Hub 模型下载统计信息,以便他可以就新鲜话题进行相关对话。
让你的 Agent 访问网络
请记住,我们希望 Alfred 确立他作为真正的文艺复兴时期主持人的地位,对世界有深刻的了解。
为此,我们需要确保 Alfred 能够访问有关世界的最新新闻和信息。
让我们从为 Alfred 创建一个网络搜索工具开始!
smolagents
llama-index
langgraph
from smolagents import DuckDuckGoSearchTool
# Initialize the DuckDuckGo search tool
search_tool = DuckDuckGoSearchTool()
# Example usage
results = search_tool("Who's the current President of France?")
print(results)
预期输出
The current President of France in Emmanuel Macron.
创建用于获取天气信息以安排烟花的自定义工具
完美的庆典应该在晴朗的天空下燃放烟花,我们需要确保烟花不会因恶劣天气而取消。
让我们创建一个自定义工具,该工具可用于调用外部天气 API 并获取给定位置的天气信息。
为了简单起见,我们在此示例中使用虚拟天气 API。 如果你想使用真实的天气 API,你可以实现一个使用 OpenWeatherMap API 的天气工具,就像单元 1中那样。
smolagents
llama-index
langgraph
from smolagents import Tool
import random
class WeatherInfoTool(Tool):
name = "weather_info"
description = "Fetches dummy weather information for a given location."
inputs = {
"location": {
"type": "string",
"description": "The location to get weather information for."
}
}
output_type = "string"
def forward(self, location: str):
# Dummy weather data
weather_conditions = [
{"condition": "Rainy", "temp_c": 15},
{"condition": "Clear", "temp_c": 25},
{"condition": "Windy", "temp_c": 20}
]
# Randomly select a weather condition
data = random.choice(weather_conditions)
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
# Initialize the tool
weather_info_tool = WeatherInfoTool()
为有影响力的 AI 构建者创建 Hub 统计工具
参加庆典的是 AI 构建领域的知名人士。 Alfred 希望通过讨论他们最受欢迎的模型、数据集和 Spaces 来给他们留下深刻印象。 我们将创建一个工具,用于根据用户名从 Hugging Face Hub 获取模型统计信息。
smolagents
llama-index
langgraph
from smolagents import Tool
from huggingface_hub import list_models
class HubStatsTool(Tool):
name = "hub_stats"
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
inputs = {
"author": {
"type": "string",
"description": "The username of the model author/organization to find models from."
}
}
output_type = "string"
def forward(self, author: str):
try:
# List models from the specified author, sorted by downloads
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
if models:
model = models[0]
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
else:
return f"No models found for author {author}."
except Exception as e:
return f"Error fetching models for {author}: {str(e)}"
# Initialize the tool
hub_stats_tool = HubStatsTool()
# Example usage
print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook
预期输出
The most downloaded model by facebook is facebook/esmfold_v1 with 12,544,550 downloads.
借助 Hub 统计工具,Alfred 现在可以通过讨论有影响力的 AI 构建者最受欢迎的模型来给他们留下深刻印象。
将工具与 Alfred 集成
现在我们有了所有工具,让我们将它们集成到 Alfred 的 agent 中
smolagents
llama-index
langgraph
from smolagents import CodeAgent, HfApiModel
# Initialize the Hugging Face model
model = HfApiModel()
# Create Alfred with all the tools
alfred = CodeAgent(
tools=[search_tool, weather_info_tool, hub_stats_tool],
model=model
)
# Example query Alfred might receive during the gala
response = alfred.run("What is Facebook and what's their most popular model?")
print("🎩 Alfred's Response:")
print(response)
预期输出
🎩 Alfred's Response:
Facebook is a social networking website where users can connect, share information, and interact with others. The most downloaded model by Facebook on the Hugging Face Hub is ESMFold_v1.
结论
通过集成这些工具,Alfred 现在能够处理各种任务,从网络搜索到天气更新和模型统计信息。 这确保了他仍然是庆典上最博学和最吸引人的主持人。
尝试实现一个可用于获取有关特定主题的最新消息的工具。
< > 在 GitHub 上更新
完成后,在 tools.py
文件中实现你的自定义工具。