使用聊天补全 API 的模型支持工具调用。 遗憾的是,聊天补全调用中定义的函数并不总是按预期执行。 使用工具调用示例微调模型可以通过以下方式改进模型输出:
- 即使不存在完整的函数定义,也可以获得类似格式的响应。 (可以在提示令牌上节省资金。)
- 获得更准确且一致的输出。
注释
function_call
和 functions
已弃用,支持 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": []}
后续步骤
- 函数调用微调场景。
- 在 Azure OpenAI 微调教程中了解微调功能。
- 查看微调模型区域可用性。