Skip to content

Commit 2043c94

Browse files
authored
Merge pull request #3147 from likebupt/update-ft-tool-calling
[Azure AI Svcs] update fine-tuning tool calling
2 parents a400235 + 83b8159 commit 2043c94

File tree

1 file changed

+103
-8
lines changed

1 file changed

+103
-8
lines changed

articles/ai-services/openai/how-to/fine-tuning-functions.md

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,118 @@
11
---
22
title: Fine-tuning function calls with Azure OpenAI Service
3-
description: Learn how to improve function calling performance with Azure OpenAI fine-tuning
3+
description: Learn how to improve tool calling performance with Azure OpenAI fine-tuning
44
#services: cognitive-services
55
manager: nitinme
66
ms.service: azure-ai-openai
77
ms.topic: how-to
8-
ms.date: 09/05/2024
8+
ms.date: 02/20/2025
99
author: mrbullwinkle
1010
ms.author: mbullwin
1111
---
1212

1313

14-
# Fine-tuning and function calling
14+
# Fine-tuning and tool calling
1515

16-
Models that use the chat completions API support [function calling](../how-to/function-calling.md). Unfortunately, functions defined in your chat completion calls don't always perform as expected. Fine-tuning your model with function calling examples can improve model output by enabling you to:
16+
Models that use the chat completions API support [tool calling](../how-to/function-calling.md). Unfortunately, functions defined in your chat completion calls don't always perform as expected. Fine-tuning your model with tool calling examples can improve model output by enabling you to:
1717

1818
* Get similarly formatted responses even when the full function definition isn't present. (Allowing you to potentially save money on prompt tokens.)
1919
* Get more accurate and consistent outputs.
2020

21-
## Constructing a training file
21+
> [!NOTE]
22+
> `function_call` and `functions` have been deprecated in favor of `tools`.
23+
> It is recommended to use the `tools` parameter instead.
24+
25+
26+
## Tool calling (recommended)
27+
### Constructing a training file
28+
29+
When constructing a training file of tool calling examples, you would take a function definition like this:
30+
31+
```json
32+
{
33+
"messages": [
34+
{ "role": "user", "content": "What is the weather in San Francisco?" },
35+
{
36+
"role": "assistant",
37+
"tool_calls": [
38+
{
39+
"id": "call_id",
40+
"type": "function",
41+
"function": {
42+
"name": "get_current_weather",
43+
"arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"
44+
}
45+
}
46+
]
47+
}
48+
],
49+
"tools": [
50+
{
51+
"type": "function",
52+
"function": {
53+
"name": "get_current_weather",
54+
"description": "Get the current weather",
55+
"parameters": {
56+
"type": "object",
57+
"properties": {
58+
"location": {
59+
"type": "string",
60+
"description": "The city and country, eg. San Francisco, USA"
61+
},
62+
"format": { "type": "string", "enum": ["celsius", "fahrenheit"] }
63+
},
64+
"required": ["location", "format"]
65+
}
66+
}
67+
}
68+
]
69+
}
70+
```
71+
72+
And express the information as a single line within your `.jsonl` training file as below:
73+
74+
```jsonl
75+
{"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"]}}}]}
76+
```
77+
78+
As with all fine-tuning training your example file requires at least 10 examples.
79+
80+
### Optimize for cost
81+
82+
OpenAI recommends that if you're trying to optimize to use fewer prompt tokens post fine-tuning your model on the full function definitions you can experiment with:
83+
84+
* Omit function and parameter descriptions: remove the description field from function and parameters.
85+
* Omit parameters: remove the entire properties field from the parameters object.
86+
* Omit function entirely: remove the entire function object from the functions array.
87+
88+
### Optimize for quality
89+
90+
Alternatively, if you're trying to improve the quality of the tool calling output, it's recommended that the function definitions present in the fine-tuning training dataset and subsequent chat completion calls remain identical.
91+
92+
### Customize model responses to function outputs
93+
94+
Fine-tuning based on tool calling examples can also be used to improve the model's response to function outputs. To accomplish this, you include examples consisting of function response messages and assistant response messages where the function response is interpreted and put into context by the assistant.
95+
96+
```json
97+
{
98+
"messages": [
99+
{"role": "user", "content": "What is the weather in San Francisco?"},
100+
{"role": "assistant", "tool_calls": [{"id": "call_id", "type": "function", "function": {"name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"}}]}
101+
{"role": "tool", "tool_call_id": "call_id", "content": "21.0"},
102+
{"role": "assistant", "content": "It is 21 degrees celsius in San Francisco, CA"}
103+
],
104+
"tools": [] // same as before
105+
}
106+
```
107+
108+
As with the example before, this example is artificially expanded for readability. The actual entry in the `.jsonl` training file would be a single line:
109+
110+
```jsonl
111+
{"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":[]}
112+
```
113+
114+
## Function calling
115+
### Constructing a training file
22116

23117
When constructing a training file of function calling examples, you would take a function definition like this:
24118

@@ -51,19 +145,19 @@ And express the information as a single line within your `.jsonl` training file
51145

52146
As with all fine-tuning training your example file requires at least 10 examples.
53147

54-
## Optimize for cost
148+
### Optimize for cost
55149

56150
OpenAI recommends that if you're trying to optimize to use fewer prompt tokens post fine-tuning your model on the full function definitions you can experiment with:
57151

58152
* Omit function and parameter descriptions: remove the description field from function and parameters.
59153
* Omit parameters: remove the entire properties field from the parameters object.
60154
* Omit function entirely: remove the entire function object from the functions array.
61155

62-
## Optimize for quality
156+
### Optimize for quality
63157

64158
Alternatively, if you're trying to improve the quality of the function calling output, it's recommended that the function definitions present in the fine-tuning training dataset and subsequent chat completion calls remain identical.
65159

66-
## Customize model responses to function outputs
160+
### Customize model responses to function outputs
67161

68162
Fine-tuning based on function calling examples can also be used to improve the model's response to function outputs. To accomplish this, you include examples consisting of function response messages and assistant response messages where the function response is interpreted and put into context by the assistant.
69163

@@ -85,6 +179,7 @@ As with the example before, this example is artificially expanded for readabilit
85179
{"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": []}
86180
```
87181

182+
88183
## Next steps
89184

90185
* [Function calling fine-tuning scenarios](https://techcommunity.microsoft.com/t5/ai-azure-ai-services-blog/fine-tuning-with-function-calling-on-azure-openai-service/ba-p/4065968).

0 commit comments

Comments
 (0)