开始使用免费开始使用

构建 OpenAI 工具定义

您已经创建了使用 OpenTimezone API 在不同时区之间转换日期时间的 convert_timezone() 函数。现在,您需要按照 OpenAI 的格式创建一个工具定义,让 LLM 理解如何使用该函数。工具定义相当于模型的使用手册,描述函数的用途、参数和要求。

本练习是课程的一部分

使用 OpenAI Responses API

查看课程

练习说明

  • 定义一个 "function" 类型的工具,名称为 "convert_timezone"
  • 为函数的 3 个参数分别创建定义:"date_time""from_timezone""to_timezone"
  • 确保这 3 个参数都在工具中被标记为必需。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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
        }
    }
]
编辑并运行代码