เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

สร้าง Tool Definition สำหรับ OpenAI

คุณได้สร้างฟังก์ชัน convert_timezone() ที่ใช้ OpenTimezone API เพื่อแปลงวันและเวลาระหว่าง timezone ต่าง ๆ แล้ว ขั้นตอนต่อไปคือการสร้าง tool definition ในรูปแบบของ OpenAI เพื่อให้ LLM เข้าใจวิธีใช้ฟังก์ชันนี้ tool definition ทำหน้าที่เหมือนคู่มือสำหรับโมเดล โดยอธิบายจุดประสงค์ พารามิเตอร์ และข้อกำหนดของฟังก์ชัน

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การทำงานกับ OpenAI Responses API

ดูคอร์ส

คำแนะนำการฝึกหัด

  • กำหนดเครื่องมือประเภท "function" ชื่อว่า "convert_timezone"
  • สร้างคำจำกัดความสำหรับพารามิเตอร์ทั้งสามของฟังก์ชัน ได้แก่ "date_time", "from_timezone" และ "to_timezone"
  • ระบุให้พารามิเตอร์ทั้งสามเป็น required ในเครื่องมือนี้

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

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
        }
    }
]
แก้ไขและรันโค้ด