結帳囉!讓 Agents 讀取資料
在 AgentsCafé,所有飲品訂單都儲存在名為 orders.csv 的檔案中,包含欄位:table_id、drink_name 和 size。
與其每次顧客要結帳時都手動翻閱檔案,你將建立一個工具,能查出指定餐桌目前的所有訂單。
注意:tool 和 pandas 已為你匯入。
本練習屬於課程
使用 Hugging Face smolagents 的 AI 代理
練習說明
- 使用
table_id作為函式參數,讓代理能知道要取回哪一桌的訂單。 - 讀取包含所有飲品訂單的
orders.csv檔案。 - 回傳該餐桌的飲品訂單清單。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a tool that receives the table_id as input
@tool
def lookup_orders(____: str) -> list[str]:
"""
Retrieves the current drink orders for a café table.
Args:
table_id (str): The table's identifier (e.g., "T5").
Returns:
list[str]: A list of drink orders, each formatted like "Latte (Large)".
"""
# Read the orders.csv file
df = pd.read_csv('____')
orders = df[df['table_id'] == table_id].apply(lambda row: f"{row['drink_name']} ({row['size']})", axis=1).tolist()
# Return the table orders
return ____