Skip to content

Commit 13fb5dd

Browse files
Merge pull request #202 from mrbullwinkle/mrb_09_09_2024_pm_fine_tuning
[Azure OpenAI] Fine-tuning tutorial updates
2 parents 74e2662 + 5ad1632 commit 13fb5dd

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

articles/ai-services/openai/tutorials/fine-tune.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
---
2-
title: Azure OpenAI Service fine-tuning gpt-3.5-turbo
2+
title: Azure OpenAI Service fine-tuning gpt-4o-mini
33
titleSuffix: Azure OpenAI
4-
description: Learn how to use Azure OpenAI's latest fine-tuning capabilities with gpt-3.5-turbo.
4+
description: Learn how to use Azure OpenAI's latest fine-tuning capabilities with gpt-4o-mini-2024-07-18
55
#services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-openai
88
ms.topic: tutorial
9-
ms.date: 05/15/2024
9+
ms.date: 09/09/2024
1010
author: mrbullwinkle
1111
ms.author: mbullwin
1212
recommendations: false
1313
ms.custom: devx-track-python
1414
---
1515

16-
# Azure OpenAI GPT-3.5 Turbo fine-tuning tutorial
16+
# Azure OpenAI GPT-4o-mini fine-tuning tutorial
1717

18-
This tutorial walks you through fine-tuning a `gpt-35-turbo-0613` model.
18+
This tutorial walks you through fine-tuning a `gpt-4o-mini-2024-07-18` model.
1919

2020
In this tutorial you learn how to:
2121

@@ -24,7 +24,7 @@ In this tutorial you learn how to:
2424
> * Create environment variables for your resource endpoint and API key.
2525
> * Prepare your sample training and validation datasets for fine-tuning.
2626
> * Upload your training file and validation file for fine-tuning.
27-
> * Create a fine-tuning job for `gpt-35-turbo-0613`.
27+
> * Create a fine-tuning job for `gpt-4o-mini-2024-07-18`.
2828
> * Deploy a custom fine-tuned model.
2929
3030
## Prerequisites
@@ -33,13 +33,12 @@ In this tutorial you learn how to:
3333
- Python 3.8 or later version
3434
- The following Python libraries: `json`, `requests`, `os`, `tiktoken`, `time`, `openai`, `numpy`.
3535
- [Jupyter Notebooks](https://jupyter.org/)
36-
- An Azure OpenAI resource in a [region where `gpt-35-turbo-0613` fine-tuning is available](../concepts/models.md). If you don't have a resource the process of creating one is documented in our resource [deployment guide](../how-to/create-resource.md).
36+
- An Azure OpenAI resource in a [region where `gpt-4o-mini-2024-07-18` fine-tuning is available](../concepts/models.md). If you don't have a resource the process of creating one is documented in our resource [deployment guide](../how-to/create-resource.md).
3737
- Fine-tuning access requires **Cognitive Services OpenAI Contributor**.
3838
- If you do not already have access to view quota, and deploy models in Azure OpenAI Studio you will require [additional permissions](../how-to/role-based-access-control.md).
3939

40-
4140
> [!IMPORTANT]
42-
> We strongly recommend reviewing the [pricing information](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/#pricing) for fine-tuning prior to beginning this tutorial to make sure you are comfortable with the associated costs. In testing, this tutorial resulted in one training hour billed, in addition to the costs that are associated with fine-tuning inference, and the hourly hosting costs of having a fine-tuned model deployed. Once you have completed the tutorial, you should delete your fine-tuned model deployment otherwise you will continue to incur the hourly hosting cost.
41+
> We recommend reviewing the [pricing information](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/#pricing) for fine-tuning to familiarize yourself with the associated costs. In testing, this tutorial resulted in 48,000 tokens being billed (4,800 training tokens * 10 epochs of training). Training costs are in addition to the costs that are associated with fine-tuning inference, and the hourly hosting costs of having a fine-tuned model deployed. Once you have completed the tutorial, you should delete your fine-tuned model deployment otherwise you will continue to incur the hourly hosting cost.
4342
4443
## Set up
4544

@@ -106,7 +105,7 @@ source /etc/environment
106105

107106
### Create a sample dataset
108107

109-
Fine-tuning `gpt-35-turbo-0613` requires a specially formatted JSONL training file. OpenAI provides the following example in their documentation:
108+
Fine-tuning `gpt-4o-mini-2024-07-18` requires a specially formatted JSONL training file. OpenAI provides the following example in their documentation:
110109

111110
```json
112111
{"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "What's the capital of France?"}, {"role": "assistant", "content": "Paris, as if everyone doesn't know that already."}]}
@@ -206,7 +205,10 @@ First example in validation set:
206205

207206
In this case we only have 10 training and 10 validation examples so while this will demonstrate the basic mechanics of fine-tuning a model this in unlikely to be a large enough number of examples to produce a consistently noticeable impact.
208207

209-
Now you can then run some additional code from OpenAI using the tiktoken library to validate the token counts. Individual examples need to remain under the `gpt-35-turbo-0613` model's input token limit of 4096 tokens.
208+
Now you can then run some additional code from OpenAI using the tiktoken library to validate the token counts. Token counting using this method is not going to give you the exact token counts that will be used for fine-tuning, but should provide a good estimate.
209+
210+
> [!NOTE]
211+
> Individual examples need to remain under the `gpt-4o-mini-2024-07-18` model's current training example context legnth of: 64,536 tokens. The model's input token limit remains 128,000 tokens.
210212
211213
```python
212214
# Validate token counts
@@ -216,7 +218,7 @@ import tiktoken
216218
import numpy as np
217219
from collections import defaultdict
218220

219-
encoding = tiktoken.get_encoding("cl100k_base") # default encoding used by gpt-4, turbo, and text-embedding-ada-002 models
221+
encoding = tiktoken.get_encoding("o200k_base") # default encoding for gpt-4o models. This requires the latest version of tiktoken to be installed.
220222

221223
def num_tokens_from_messages(messages, tokens_per_message=3, tokens_per_name=1):
222224
num_tokens = 0
@@ -268,27 +270,27 @@ for file in files:
268270
Processing file: training_set.jsonl
269271
270272
#### Distribution of total tokens:
271-
min / max: 47, 62
272-
mean / median: 52.1, 50.5
273-
p5 / p95: 47.9, 57.5
273+
min / max: 46, 59
274+
mean / median: 49.8, 48.5
275+
p5 / p95: 46.0, 53.599999999999994
274276
275277
#### Distribution of assistant tokens:
276-
min / max: 13, 30
277-
mean / median: 17.6, 15.5
278-
p5 / p95: 13.0, 21.9
278+
min / max: 13, 28
279+
mean / median: 16.5, 14.0
280+
p5 / p95: 13.0, 19.9
279281
**************************************************
280282
Processing file: validation_set.jsonl
281283
282284
#### Distribution of total tokens:
283-
min / max: 43, 65
284-
mean / median: 51.4, 49.0
285-
p5 / p95: 45.7, 56.9
285+
min / max: 41, 64
286+
mean / median: 48.9, 47.0
287+
p5 / p95: 43.7, 54.099999999999994
286288
287289
#### Distribution of assistant tokens:
288290
min / max: 8, 29
289-
mean / median: 15.9, 13.5
290-
p5 / p95: 11.6, 20.9
291-
**************************************************
291+
mean / median: 15.0, 12.5
292+
p5 / p95: 10.7, 19.999999999999996
293+
****************************
292294
```
293295

294296
## Upload fine-tuning files
@@ -304,7 +306,7 @@ from openai import AzureOpenAI
304306
client = AzureOpenAI(
305307
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
306308
api_key = os.getenv("AZURE_OPENAI_API_KEY"),
307-
api_version = "2024-05-01-preview" # This API version or later is required to access seed/events/checkpoint features
309+
api_version = "2024-08-01-preview" # This API version or later is required to access seed/events/checkpoint features
308310
)
309311

310312
training_file_name = 'training_set.jsonl'
@@ -381,7 +383,7 @@ In this example we're also passing the seed parameter. The seed controls the rep
381383
response = client.fine_tuning.jobs.create(
382384
training_file = training_file_id,
383385
validation_file = validation_file_id,
384-
model = "gpt-35-turbo-0613", # Enter base model name. Note that in Azure OpenAI the model name contains dashes and cannot contain dot/period characters.
386+
model = "gpt-4o-mini-2024-07-18", # Enter base model name. Note that in Azure OpenAI the model name contains dashes and cannot contain dot/period characters.
385387
seed = 105 # seed parameter controls reproducibility of the fine-tuning job. If no seed is specified one will be generated automatically.
386388
)
387389

@@ -404,7 +406,7 @@ print(response.model_dump_json(indent=2))
404406
response = openai.FineTuningJob.create(
405407
training_file = training_file_id,
406408
validation_file = validation_file_id,
407-
model = "gpt-35-turbo-0613",
409+
model = "gpt-4o-mini-2024-07-18",
408410
)
409411

410412
job_id = response["id"]
@@ -435,7 +437,7 @@ Status: pending
435437
"batch_size": -1,
436438
"learning_rate_multiplier": 1
437439
},
438-
"model": "gpt-35-turbo-0613",
440+
"model": "gpt-4o-mini-2024-07-18",
439441
"object": "fine_tuning.job",
440442
"organization_id": null,
441443
"result_files": null,
@@ -540,7 +542,7 @@ Status: pending
540542
"batch_size": -1,
541543
"learning_rate_multiplier": 1
542544
},
543-
"model": "gpt-35-turbo-0613",
545+
"model": "gpt-4o-mini-2024-07-18",
544546
"object": "fine_tuning.job",
545547
"organization_id": null,
546548
"result_files": null,
@@ -564,7 +566,7 @@ Found 4 fine-tune jobs.
564566

565567
## List fine-tuning events
566568

567-
API version: `2024-05-01-preview` or later is required for this command.
569+
API version: `2024-08-01-preview` or later is required for this command.
568570

569571
While not necessary to complete fine-tuning it can be helpful to examine the individual fine-tuning events that were generated during training. The full training results can also be examined after training is complete in the [training results file](../how-to/fine-tuning.md#analyze-your-customized-model).
570572

@@ -728,7 +730,7 @@ This command isn't available in the 0.28.1 OpenAI Python library. Upgrade to the
728730

729731
## List checkpoints
730732

731-
API version: `2024-05-01-preview` or later is required for this command.
733+
API version: `2024-08-01-preview` or later is required for this command.
732734

733735
When each training epoch completes a checkpoint is generated. A checkpoint is a fully functional version of a model which can both be deployed and used as the target model for subsequent fine-tuning jobs. Checkpoints can be particularly useful, as they can provide a snapshot of your model prior to overfitting having occurred. When a fine-tuning job completes you will have the three most recent versions of the model available to deploy. The final epoch will be represented by your fine-tuned model, the previous two epochs will be available as checkpoints.
734736

@@ -753,7 +755,7 @@ This command isn't available in the 0.28.1 OpenAI Python library. Upgrade to the
753755
{
754756
"id": "ftchkpt-148ab69f0a404cf9ab55a73d51b152de",
755757
"created_at": 1715743077,
756-
"fine_tuned_model_checkpoint": "gpt-35-turbo-0613.ft-372c72db22c34e6f9ccb62c26ee0fbd9",
758+
"fine_tuned_model_checkpoint": "gpt-4o-mini-2024-07-18.ft-372c72db22c34e6f9ccb62c26ee0fbd9",
757759
"fine_tuning_job_id": "ftjob-372c72db22c34e6f9ccb62c26ee0fbd9",
758760
"metrics": {
759761
"full_valid_loss": 1.8258173013035255,
@@ -770,7 +772,7 @@ This command isn't available in the 0.28.1 OpenAI Python library. Upgrade to the
770772
{
771773
"id": "ftchkpt-e559c011ecc04fc68eaa339d8227d02d",
772774
"created_at": 1715743013,
773-
"fine_tuned_model_checkpoint": "gpt-35-turbo-0613.ft-372c72db22c34e6f9ccb62c26ee0fbd9:ckpt-step-90",
775+
"fine_tuned_model_checkpoint": "gpt-4o-mini-2024-07-18.ft-372c72db22c34e6f9ccb62c26ee0fbd9:ckpt-step-90",
774776
"fine_tuning_job_id": "ftjob-372c72db22c34e6f9ccb62c26ee0fbd9",
775777
"metrics": {
776778
"full_valid_loss": 1.7958603267428241,
@@ -787,7 +789,7 @@ This command isn't available in the 0.28.1 OpenAI Python library. Upgrade to the
787789
{
788790
"id": "ftchkpt-8ae8beef3dcd4dfbbe9212e79bb53265",
789791
"created_at": 1715742984,
790-
"fine_tuned_model_checkpoint": "gpt-35-turbo-0613.ft-372c72db22c34e6f9ccb62c26ee0fbd9:ckpt-step-80",
792+
"fine_tuned_model_checkpoint": "gpt-4o-mini-2024-07-18.ft-372c72db22c34e6f9ccb62c26ee0fbd9:ckpt-step-80",
791793
"fine_tuning_job_id": "ftjob-372c72db22c34e6f9ccb62c26ee0fbd9",
792794
"metrics": {
793795
"full_valid_loss": 1.6909511662736725,
@@ -848,7 +850,7 @@ Alternatively, you can deploy your fine-tuned model using any of the other commo
848850
| resource_group | The resource group name for your Azure OpenAI resource |
849851
| resource_name | The Azure OpenAI resource name |
850852
| model_deployment_name | The custom name for your new fine-tuned model deployment. This is the name that will be referenced in your code when making chat completion calls. |
851-
| fine_tuned_model | Retrieve this value from your fine-tuning job results in the previous step. It will look like `gpt-35-turbo-0613.ft-b044a9d3cf9c4228b5d393567f693b83`. You'll need to add that value to the deploy_data json. |
853+
| fine_tuned_model | Retrieve this value from your fine-tuning job results in the previous step. It will look like `gpt-4o-mini-2024-07-18.ft-b044a9d3cf9c4228b5d393567f693b83`. You'll need to add that value to the deploy_data json. |
852854

853855
[!INCLUDE [Fine-tuning deletion](../includes/fine-tune.md)]
854856

@@ -862,7 +864,7 @@ token = os.getenv("TEMP_AUTH_TOKEN")
862864
subscription = "<YOUR_SUBSCRIPTION_ID>"
863865
resource_group = "<YOUR_RESOURCE_GROUP_NAME>"
864866
resource_name = "<YOUR_AZURE_OPENAI_RESOURCE_NAME>"
865-
model_deployment_name = "YOUR_CUSTOM_MODEL_DEPLOYMENT_NAME"
867+
model_deployment_name = "gpt-4o-mini-2024-07-18-ft" # Custom deployment name you chose for your fine-tuning model
866868

867869
deploy_params = {'api-version': "2023-05-01"}
868870
deploy_headers = {'Authorization': 'Bearer {}'.format(token), 'Content-Type': 'application/json'}
@@ -872,7 +874,7 @@ deploy_data = {
872874
"properties": {
873875
"model": {
874876
"format": "OpenAI",
875-
"name": "<YOUR_FINE_TUNED_MODEL>", #retrieve this value from the previous call, it will look like gpt-35-turbo-0613.ft-b044a9d3cf9c4228b5d393567f693b83
877+
"name": "<YOUR_FINE_TUNED_MODEL>", #retrieve this value from the previous call, it will look like gpt-4o-mini-2024-07-18.ft-b044a9d3cf9c4228b5d393567f693b83
876878
"version": "1"
877879
}
878880
}
@@ -911,11 +913,11 @@ from openai import AzureOpenAI
911913
client = AzureOpenAI(
912914
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
913915
api_key = os.getenv("AZURE_OPENAI_API_KEY"),
914-
api_version = "2024-02-01"
916+
api_version = "2024-06-01"
915917
)
916918

917919
response = client.chat.completions.create(
918-
model = "gpt-35-turbo-ft", # model = "Custom deployment name you chose for your fine-tuning model"
920+
model = "gpt-4o-mini-2024-07-18-ft", # model = "Custom deployment name you chose for your fine-tuning model"
919921
messages = [
920922
{"role": "system", "content": "You are a helpful assistant."},
921923
{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
@@ -937,11 +939,11 @@ import openai
937939

938940
openai.api_type = "azure"
939941
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
940-
openai.api_version = "2024-02-01"
942+
openai.api_version = "2024-06-01"
941943
openai.api_key = os.getenv("AZURE_OPENAI_API_KEY")
942944

943945
response = openai.ChatCompletion.create(
944-
engine = "gpt-35-turbo-ft", # engine = "Custom deployment name you chose for your fine-tuning model"
946+
engine = "gpt-4o-mini-2024-07-18-ft", # engine = "Custom deployment name you chose for your fine-tuning model"
945947
messages = [
946948
{"role": "system", "content": "You are a helpful assistant."},
947949
{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},

0 commit comments

Comments
 (0)