你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

微调和函数调用

使用聊天补全 API 的模型支持函数调用。 遗憾的是,聊天补全调用中定义的函数并不总是按预期执行。 使用函数调用示例微调模型可以通过以下方式改进模型输出:

  • 即使不存在完整的函数定义,也可以获得类似格式的响应。 (可以在提示令牌上节省资金。)
  • 获得更准确且一致的输出。

重要

随着 API 的 2023-12-01-preview 版本的发布,functionsfunction_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 训练文件中的一行,如下所示:

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

后续步骤