请结账!让 Agent 访问数据
在 AgentsCafé,所有饮品点单都保存在名为 orders.csv 的文件中,其中包含列:table_id、drink_name 和 size。
与其在顾客询问账单时每次都手动翻阅文件,您将构建一个工具,用于查询某个餐桌的所有当前点单。
注意:已为您导入 tool 和 pandas。
本练习是课程的一部分
使用 Hugging Face smolagents 的 AI Agents
练习说明
- 使用
table_id作为函数参数,让 agent 知道要检索哪个餐桌的点单。 - 读取包含所有饮品点单的
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 ____