開始使用免費開始

建立 OpenAI 工具定義

你已建立使用 OpenTimezone API 將日期時間在不同比時區之間轉換的 convert_timezone() 函式。現在你需要依照 OpenAI 的格式建立一個工具定義,讓 LLM 知道如何使用這個函式。這個工具定義就像是給模型看的說明書,描述函式的用途、參數與必要條件。

本練習屬於課程

使用 OpenAI Responses API

檢視課程

練習說明

  • 定義一個 "function" 類型、名為 "convert_timezone" 的工具。
  • 為該函式的三個參數分別建立定義:"date_time""from_timezone""to_timezone"
  • 確保這三個參數都被標記為此工具的必要參數。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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
        }
    }
]
編輯並執行程式碼