Agents 课程文档

创建您的盛会 Agent

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

创建您的盛会 Agent

现在我们已经构建了 Alfred 的所有必要组件,现在是时候将所有内容整合到一个完整的 agent 中,它可以帮助举办我们奢华的盛会。

在本节中,我们将把嘉宾信息检索、网络搜索、天气信息和 Hub 统计工具组合成一个强大的 agent。

组装 Alfred:完整的 Agent

我们将从各自的模块中导入我们在前面部分中创建的所有工具,而不是重新实现它们,这些模块保存在 tools.pyretriever.py 文件中。

如果您尚未实现这些工具,请返回 toolsretriever 部分来实现它们,并将它们添加到 tools.pyretriever.py 文件中。

让我们从前面的部分导入必要的库和工具

smolagents
llama-index
langgraph
# Import necessary libraries
import random
from smolagents import CodeAgent, HfApiModel

# Import our custom tools from their modules
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
from retriever import load_guest_dataset

现在,让我们将所有这些工具组合成一个 agent

# Initialize the Hugging Face model
model = HfApiModel()

# Initialize the web search tool
search_tool = DuckDuckGoSearchTool()

# Initialize the weather tool
weather_info_tool = WeatherInfoTool()

# Initialize the Hub stats tool
hub_stats_tool = HubStatsTool()

# Load the guest dataset and initialize the guest info tool
guest_info_tool = load_guest_dataset()

# Create Alfred with all the tools
alfred = CodeAgent(
    tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool], 
    model=model,
    add_base_tools=True,  # Add any additional base tools
    planning_interval=3   # Enable planning every 3 steps
)

您的 agent 现在可以使用了!

使用 Alfred:端到端示例

既然 Alfred 已经完全配备了所有必要的工具,让我们看看他如何在盛会期间帮助处理各种任务。

示例 1:查找嘉宾信息

让我们看看 Alfred 如何帮助我们处理嘉宾信息。

smolagents
llama-index
langgraph
query = "Tell me about 'Lady Ada Lovelace'"
response = alfred.run(query)

print("🎩 Alfred's Response:")
print(response)

预期输出

🎩 Alfred's Response:
Based on the information I retrieved, Lady Ada Lovelace is an esteemed mathematician and friend. She is renowned for her pioneering work in mathematics and computing, often celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. Her email address is ada.lovelace@example.com.

示例 2:查看烟花天气

让我们看看 Alfred 如何帮助我们了解天气。

smolagents
llama-index
langgraph
query = "What's the weather like in Paris tonight? Will it be suitable for our fireworks display?"
response = alfred.run(query)

print("🎩 Alfred's Response:")
print(response)

预期输出(会因随机性而异)

🎩 Alfred's Response:
I've checked the weather in Paris for you. Currently, it's clear with a temperature of 25°C. These conditions are perfect for the fireworks display tonight. The clear skies will provide excellent visibility for the spectacular show, and the comfortable temperature will ensure the guests can enjoy the outdoor event without discomfort.

示例 3:给 AI 研究人员留下深刻印象

让我们看看 Alfred 如何帮助我们给 AI 研究人员留下深刻印象。

smolagents
llama-index
langgraph
query = "One of our guests is from Qwen. What can you tell me about their most popular model?"
response = alfred.run(query)

print("🎩 Alfred's Response:")
print(response)

预期输出

🎩 Alfred's Response:
The most popular Qwen model is Qwen/Qwen2.5-VL-7B-Instruct with 3,313,345 downloads.

示例 4:组合多个工具

让我们看看 Alfred 如何帮助我们准备与尼古拉·特斯拉博士的对话。

smolagents
llama-index
langgraph
query = "I need to speak with Dr. Nikola Tesla about recent advancements in wireless energy. Can you help me prepare for this conversation?"
response = alfred.run(query)

print("🎩 Alfred's Response:")
print(response)

预期输出

🎩 Alfred's Response:
I've gathered information to help you prepare for your conversation with Dr. Nikola Tesla.

Guest Information:
Name: Dr. Nikola Tesla
Relation: old friend from university days
Description: Dr. Nikola Tesla is an old friend from your university days. He's recently patented a new wireless energy transmission system and would be delighted to discuss it with you. Just remember he's passionate about pigeons, so that might make for good small talk.
Email: nikola.tesla@gmail.com

Recent Advancements in Wireless Energy:
Based on my web search, here are some recent developments in wireless energy transmission:
1. Researchers have made progress in long-range wireless power transmission using focused electromagnetic waves
2. Several companies are developing resonant inductive coupling technologies for consumer electronics
3. There are new applications in electric vehicle charging without physical connections

Conversation Starters:
1. "I'd love to hear about your new patent on wireless energy transmission. How does it compare to your original concepts from our university days?"
2. "Have you seen the recent developments in resonant inductive coupling for consumer electronics? What do you think of their approach?"
3. "How are your pigeons doing? I remember your fascination with them."

This should give you plenty to discuss with Dr. Tesla while demonstrating your knowledge of his interests and recent developments in his field.

高级功能:对话记忆

为了使 Alfred 在盛会期间更加有用,我们可以启用对话记忆,以便他记住之前的互动

smolagents
llama-index
langgraph
# Create Alfred with conversation memory
alfred_with_memory = CodeAgent(
    tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool], 
    model=model,
    add_base_tools=True,
    planning_interval=3,
    memory=True  # Enable conversation memory
)

# First interaction
response1 = alfred_with_memory.run("Tell me about Lady Ada Lovelace.")
print("🎩 Alfred's First Response:")
print(response1)

# Second interaction (referencing the first)
response2 = alfred_with_memory.run("What projects is she currently working on?")
print("🎩 Alfred's Second Response:")
print(response2)

结论

恭喜!您已成功构建了 Alfred,这是一个配备了多种复杂工具的 agent,可帮助举办本世纪最奢华的盛会。 Alfred 现在可以

  1. 检索有关嘉宾的详细信息
  2. 查看天气状况以计划户外活动
  3. 提供有关有影响力的 AI 构建者及其模型的见解
  4. 在网络上搜索最新信息
  5. 使用记忆保持对话上下文

凭借这些功能,Alfred 已准备好确保您的盛会取得圆满成功,以个性化的关注和最新的信息给嘉宾留下深刻印象。

< > 在 GitHub 上更新