智慧體課程文件
建立您的盛會助手
加入 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 上更新