Xây dựng Định nghĩa Tool cho OpenAI
Bạn đã tạo hàm convert_timezone() dùng OpenTimezone API để chuyển đổi datetime giữa các múi giờ. Giờ bạn cần tạo một định nghĩa tool theo định dạng của OpenAI để LLM hiểu cách sử dụng hàm này. Định nghĩa tool hoạt động như một sổ tay cho mô hình, mô tả mục đích, tham số và các yêu cầu của hàm.
Bài tập này là một phần của khóa học
Làm việc với OpenAI Responses API
Hướng dẫn bài tập
- Định nghĩa một tool kiểu
"function"tên"convert_timezone". - Tạo định nghĩa cho từng tham số trong ba tham số của hàm:
"date_time","from_timezone", và"to_timezone". - Đảm bảo cả ba tham số đều được đánh dấu là bắt buộc bởi tool.
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.
tools = [
{
# Define a function tool called convert_timezone
"type": "____",
"name": "____",
"description": "Convert a datetime from one timezone to another using the OpenTimezone API.",
"parameters": {
"type": "object",
# Define the parameter names, types, and descriptions
"properties": {
"____": {
"type": "____",
"description": "The datetime string in ISO format (e.g., '2025-01-20T14:30:00')"
},
"____": {
"type": "____",
"description": "The source timezone (e.g., 'America/New_York', 'Asia/Tokyo')"
},
"____": {
"type": "____",
"description": "The target timezone (e.g., 'Europe/London', 'Australia/Sydney')"
}
},
# Ensure that all three parameters are required
"____": ["date_time", "from_timezone", "to_timezone"],
"additionalProperties": False
}
}
]