共用方式為


微調和工具呼叫

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

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

備註

function_callfunctions 已被棄用,建議使用 tools。 建議改用 tools 參數。

建構定型檔案

建構工具呼叫範例的定型檔案時,您會採用如下的函式定義:

{
    "messages": [
        { "role": "user", "content": "What is the weather in San Francisco?" },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "id": "call_id",
                    "type": "function",
                    "function": {
                        "name": "get_current_weather",
                        "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"
                    }
                }
            ]
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "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","tool_calls":[{"id":"call_id","type":"function","function":{"name":"get_current_weather","arguments":"{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"}}]}],"tools":[{"type":"function","function":{"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", "tool_calls": [{"id": "call_id", "type": "function", "function": {"name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"}}]}
        {"role": "tool", "tool_call_id": "call_id", "content": "21.0"},
        {"role": "assistant", "content": "It is 21 degrees celsius in San Francisco, CA"}
    ],
    "tools": [] // same as before
}

如同先前的範例,此範例會被人為擴充以方便閲讀。 .jsonl 定型檔案中的實際輸入會是單行:

{"messages":[{"role":"user","content":"What is the weather in San Francisco?"},{"role":"assistant","tool_calls":[{"id":"call_id","type":"function","function":{"name":"get_current_weather","arguments":"{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"}}]},{"role":"tool","tool_call_id":"call_id","content":"21.0"},{"role":"assistant","content":"It is 21 degrees celsius in San Francisco, CA"}],"tools":[]}

函數呼叫

建構定型檔案

建構函式呼叫範例的定型檔案時,您會採用如下的函式定義:

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

下一步