智能体课程文档
为您的智能体构建和集成工具
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
为您的智能体构建和集成工具
在本节中,我们将授予阿尔弗雷德访问网络的权限,让他能够找到最新的新闻和全球动态。此外,他将能够访问天气数据和 Hugging Face Hub 模型下载统计数据,以便他能够就新话题进行相关对话。
让您的智能体访问网络
请记住,我们希望阿尔弗雷德以一个真正博学多才的主人的身份出现,对世界有着深刻的了解。
为此,我们需要确保阿尔弗雷德能够获取世界的最新新闻和信息。
让我们首先为阿尔弗雷德创建一个网页搜索工具!
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。
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 界的知名人物。阿尔弗雷德希望通过讨论他们最受欢迎的模型、数据集和空间来给他们留下深刻印象。我们将创建一个工具,根据用户名从 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 统计工具,阿尔弗雷德现在可以通过讨论他们最受欢迎的模型来给有影响力的 AI 开发者留下深刻印象。
将工具与阿尔弗雷德集成
现在我们拥有了所有的工具,让我们将它们集成到阿尔弗雷德的智能体中
smolagents
llama-index
langgraph
from smolagents import CodeAgent, InferenceClientModel
# Initialize the Hugging Face model
model = InferenceClientModel()
# 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.
结论
通过集成这些工具,阿尔弗雷德现在能够处理各种任务,从网络搜索到天气更新和模型统计。这确保他在晚会上保持信息最灵通和最引人入胜的主人身份。
尝试实现一个可以获取特定主题最新新闻的工具。
< > 在 GitHub 上更新
完成后,在 tools.py
文件中实现您的自定义工具。