智能体课程文档
创建您的盛会助手
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
创建您的盛会助手
现在我们已经为阿尔弗雷德构建了所有必要的组件,是时候将所有内容整合到一个完整的助手中,它可以帮助我们举办这场盛大的盛会。
在本节中,我们将宾客信息检索、网络搜索、天气信息和 Hub 统计工具组合成一个强大的单一助手。
组装阿尔弗雷德:完整的助手
我们不会重新实现前面章节中创建的所有工具,而是从它们各自的模块中导入它们,这些模块我们保存到了 `tools.py` 和 `retriever.py` 文件中。
让我们从前面的章节导入必要的库和工具。
smolagents
llama-index
langgraph
# Import necessary libraries
import random
from smolagents import CodeAgent, InferenceClientModel
# Import our custom tools from their modules
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
from retriever import load_guest_dataset
现在,让我们将所有这些工具组合成一个单一的代理。
# Initialize the Hugging Face model
model = InferenceClientModel()
# 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
)
您的助手现在可以使用了!
使用阿尔弗雷德:端到端示例
现在阿尔弗雷德已配备所有必要的工具,让我们看看他如何帮助处理盛会期间的各种任务。
示例 1:查找宾客信息
让我们看看阿尔弗雷德如何帮助我们处理宾客信息。
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:查看烟花天气
让我们看看阿尔弗雷德如何帮助我们查看天气。
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:给人工智能研究人员留下深刻印象
让我们看看阿尔弗雷德如何帮助我们给人工智能研究人员留下深刻印象。
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:组合多个工具
让我们看看阿尔弗雷德如何帮助我们准备与尼古拉·特斯拉博士的对话。
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.
高级功能:对话记忆
为了让阿尔弗雷德在盛会期间更有帮助,我们可以启用对话记忆,这样他就能记住之前的互动。
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
)
# 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?", reset=False)
print("🎩 Alfred's Second Response:")
print(response2)
请注意,这三种代理方法都没有直接将内存与代理耦合。这种设计选择是否有特殊原因🧐?
- smolagents:内存不会在不同的执行运行中保留,您必须使用 `reset=False` 明确声明。
- LlamaIndex:需要明确添加上下文对象以进行运行中的内存管理。
- LangGraph:提供检索先前消息或利用专用MemorySaver组件的选项。
结论
恭喜!您已成功构建了阿尔弗雷德,一个配备多种工具的复杂智能体,可帮助您举办本世纪最奢华的盛会。阿尔弗雷德现在可以:
- 检索宾客的详细信息
- 检查天气状况以规划户外活动
- 提供关于有影响力的人工智能构建者及其模型的信息
- 在网络上搜索最新信息
- 通过记忆维护对话上下文
凭借这些能力,阿尔弗雷德将确保您的盛会取得圆满成功,通过个性化的关注和最新的信息给宾客留下深刻印象。
< > 在 GitHub 上更新