共用方式為


微調和函式呼叫

使用聊天完成 API 的模型支援函式呼叫。 不幸的是,聊天完成呼叫中定義的函式不一定會如預期般執行。 使用函式呼叫範例微調模型,可以透過以下方式改進模型輸出:

  • 即使完整函式定義不存在,也取得類似的格式化回應。 (讓您在提示權杖上節省資金。)
  • 取得更精確且一致的輸出。

重要

functionsfunction_call 參數已隨著 API 2023-12-01-preview 版本的發行而淘汰。 不過,微調 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 定型檔案內的單行,如下所示:

{"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 定型檔案中的實際輸入會是單行:

{"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": []}

下一步