现在我们已经为 Alfred 构建了所有必要组件,是时候将它们整合成一个完整的智能体来协助举办我们的奢华盛会了。
在本节中,我们将把宾客信息检索、网络搜索、天气信息和 Hub 统计工具整合成一个强大的智能体。
我们不需要重新实现之前章节创建的所有工具,只需从保存的tools.py和retriever.py模块中导入它们即可。
让我们从之前章节导入必要的库和工具:
# 导入必要的库
import random
from smolagents import CodeAgent, InferenceClientModel
# 从自定义模块导入工具
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
from retriever import load_guest_dataset现在让我们将所有工具组合成一个智能体:
# 初始化 Hugging Face 模型
model = InferenceClientModel()
# 初始化网络搜索工具
search_tool = DuckDuckGoSearchTool()
# 初始化天气工具
weather_info_tool = WeatherInfoTool()
# 初始化 Hub 统计工具
hub_stats_tool = HubStatsTool()
# 加载宾客数据集并初始化宾客信息工具
guest_info_tool = load_guest_dataset()
# 创建包含所有工具的 Alfred
alfred = CodeAgent(
tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
model=model,
add_base_tools=True, # 添加额外的基础工具
planning_interval=3 # 每 3 步启用规划
)您的智能体现已准备就绪!
现在 Alfred 已配备所有必要工具,让我们看看他如何协助处理晚会中的各种任务。
展示 Alfred 如何协助获取嘉宾信息:
query = "Tell me about 'Lady Ada Lovelace'"
response = alfred.run(query)
print("🎩 Alfred's Response:")
print(response)预期输出:
🎩 Alfred's Response:
根据检索到的信息,Ada Lovelace 女士是位备受尊敬的数学家兼好友。她因在数学和计算领域的开创性工作而闻名,常因其在 Charles Babbage 分析机方面的工作被誉为第一位计算机程序员。她的电子邮箱是 ada.lovelace@example.com。展示 Alfred 如何协助天气查询:
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:
已为您查询巴黎天气。当前天气晴朗,气温 25°C。这样的条件非常适合今晚的烟花表演。晴朗的夜空将为壮观表演提供绝佳能见度,舒适的温度也能确保宾客们愉快享受户外活动。展示 Alfred 如何协助与 AI 研究者互动:
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:
Qwen 最受欢迎的模型是 Qwen/Qwen2.5-VL-7B-Instruct,下载量达 3,313,345 次。展示 Alfred 如何协助准备与 Nikola Tesla 博士的对话:
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:
我已收集信息帮助您准备与 Nikola Tesla 博士的对话。
嘉宾信息:
姓名:Dr. Nikola Tesla
关系:大学时期的老友
描述:他是您大学时期的老友,最近刚获得新型无线能量传输系统的专利,非常乐意与您讨论。请记住他对鸽子情有独钟,这可能是很好的闲聊话题。
邮箱:nikola.tesla@gmail.com
无线能源最新进展:
根据网络搜索,以下是无线能量传输领域的最新发展:
1. 研究人员在使用聚焦电磁波进行远距离无线输电方面取得进展
2. 多家公司正在开发用于消费电子的谐振感应耦合技术
3. 无物理连接的电动汽车充电新应用
对话切入点:
1. "我很想听听您关于无线能量传输新专利的情况,与大学时期的原始概念相比有何改进?"
2. "您是否关注近期消费电子谐振感应耦合技术的发展?对他们的方法有何看法?"
3. "您的鸽子最近好吗?我记得您对它们特别着迷"
这些内容将为您与 Tesla 博士的对话提供充足话题,同时展现您对他兴趣领域和专业发展的了解。为了让 Alfred 在晚会中更智能,我们可以启用对话记忆功能使其记住先前交流:
# 创建带记忆的 Alfred
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
)
# 首次交互
response1 = alfred_with_memory.run("Tell me about Lady Ada Lovelace.")
print("🎩 Alfred's First Response:")
print(response1)
# 二次交互(引用首次内容)
response2 = alfred_with_memory.run("What projects is she currently working on?", reset=False)
print("🎩 Alfred's Second Response:")
print(response2)注意到这三种智能体框架都没有直接集成记忆模块,这种设计有何特殊考量?🧐
恭喜!您已成功构建 Alfred——配备多种工具的智能体助手,可协助举办本世纪最盛大的晚会。Alfred 现在能够:
凭借这些能力,Alfred 已准备就绪,确保您的晚会取得圆满成功,通过个性化服务和实时信息给宾客留下深刻印象。
< > Update on GitHub