ファインチューニングと関数呼び出し

チャット入力候補 API を使用するモデルは、関数呼び出しに対応しています。 残念ながら、チャット入力候補呼び出しで定義されている関数は、期待どおりに動作しないことがあります。 関数呼び出しの例を使用してモデルをファインチューニングすることで、次の機能が可能になり、モデル出力を向上させることができます。

  • 完全な関数定義が存在しない場合でも、同様の形式が設定された応答を取得します。 (プロンプト トークンのコストを節約できる可能性があります。)
  • より正確で一貫性のある出力を取得します。

重要

API の 2023-12-01-preview バージョンのリリースに伴い、functions および function_call パラメーターは非推奨になりました。 ただし、現在、ファインチューニング API ではレガシ パラメーターを使用する必要があります。

トレーニング ファイルの構築

関数呼び出しの例のトレーニング ファイルを構築する場合は、次のような関数定義を使用します。

{
    "messages": [
        {"role": "user", "content": "What is the weather in San Francisco?"},
        {"role": "assistant", "function_call": {"name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"}
    ],
    "functions": [{
        "name": "get_current_weather",
        "description": "Get the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "The city and country, eg. San Francisco, USA"},
                "format": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["location", "format"]
        }
    }]
}

また、次のように .jsonl トレーニング ファイル内で情報を 1 行で表現します。

{"messages": [{"role": "user", "content": "What is the weather in San Francisco?"}, {"role": "assistant", "function_call": {"name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"}}], "functions": [{"name": "get_current_weather", "description": "Get the current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "The city and country, eg. San Francisco, USA"}, "format": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["location", "format"]}}]}

すべてのファインチューニング トレーニングと同様に、呼び出し例のファイルの場合も少なくとも 10 個の例が必要です。

コストを最適化する

完全な関数定義でモデルをファインチューニングした後、使用するプロンプト トークンを減らすために最適化しようとしている場合、OpenAI は次を試すことを推奨しています。

  • 関数とパラメーターの説明を省略する: 関数とパラメーターから説明フィールドを削除します。
  • パラメーターを省略する: パラメーター オブジェクトからプロパティ フィールド全体を削除します。
  • 関数全体を省略する: 関数配列から関数オブジェクト全体を削除します。

品質を最適化する

また、関数呼び出し出力の品質を向上させようとしている場合は、ファインチューニング トレーニング データセットに存在する関数定義と、後続のチャット入力候補呼び出しを同じままにすることが推奨されています。

関数出力に対するモデルの応答をカスタマイズする

関数呼び出しの例に基づくファインチューニングを使用して、関数出力に対するモデルの応答を改善することもできます。 これを実現するには、関数の応答メッセージとアシスタントの応答メッセージから構成される例を含めます。それにより、アシスタントによって関数の応答が解釈され、コンテキストに配置されます。

{
    "messages": [
        {"role": "user", "content": "What is the weather in San Francisco?"},
        {"role": "assistant", "function_call": {"name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celcius\"}"}}
        {"role": "function", "name": "get_current_weather", "content": "21.0"},
        {"role": "assistant", "content": "It is 21 degrees celsius in San Francisco, CA"}
    ],
    "functions": [...] // same as before
}

前の例と同様に、この例も読みやすくするために人為的に拡張されています。 .jsonl トレーニング ファイルの実際のエントリは 1 行になります。

{"messages": [{"role": "user", "content": "What is the weather in San Francisco?"}, {"role": "assistant", "function_call": {"name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celcius\"}"}}, {"role": "function", "name": "get_current_weather", "content": "21.0"}, {"role": "assistant", "content": "It is 21 degrees celsius in San Francisco, CA"}], "functions": []}

次のステップ