始める無料で始める

OpenAI ツール定義を作る

OpenTimezone API を使ってタイムゾーン間で日時を変換する convert_timezone() 関数を作成しました。次は、この関数をLLMが正しく使えるように、OpenAI の形式でツール定義を作成します。ツール定義はモデル向けの取扱説明書として、関数の目的、パラメータ、必須条件を説明します。

この演習はコースの一部です

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
        }
    }
]
コードを編集して実行