Check, Please! Cho agent truy cập dữ liệu
Tại AgentsCafé, tất cả order đồ uống được lưu trong tệp orders.csv, gồm các cột: table_id, drink_name, và size.
Thay vì phải cuộn xem tệp mỗi khi khách yêu cầu tính tiền, bạn sẽ xây dựng một tool để tra cứu toàn bộ order hiện tại cho một bàn cụ thể.
Lưu ý: tool và pandas đã được import sẵn cho bạn.
Bài tập này là một phần của khóa học
AI Agents với Hugging Face smolagents
Hướng dẫn bài tập
- Dùng
table_idlàm tham số của hàm để agent biết cần lấy order của bàn nào. - Đọc tệp
orders.csv, nơi chứa tất cả order đồ uống. - Trả về danh sách các order đồ uống của bàn đó.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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 ____