Skip to content

Commit 18f2037

Browse files
Merge pull request #260050 from mrbullwinkle/mrb_12_01_2023_fine_tuning_tutorial
[Azure OpenAI] fine-tuning 1.x tutorial
2 parents f05fd2f + 8a95e05 commit 18f2037

File tree

3 files changed

+161
-25
lines changed

3 files changed

+161
-25
lines changed

articles/ai-services/openai/includes/fine-tuning-python.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ import os
151151
openai.api_key = os.getenv("AZURE_OPENAI_API_KEY")
152152
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
153153
openai.api_type = 'azure'
154-
openai.api_version = '2023-09-15-preview' # This API version or later is required to access fine-tuning for turbo/babbage-002/davinci-002
154+
openai.api_version = '2023-10-01-preview' # This API version or later is required to access fine-tuning for turbo/babbage-002/davinci-002
155155

156156
training_file_name = 'training_set.jsonl'
157157
validation_file_name = 'validation_set.jsonl'
@@ -241,7 +241,7 @@ print(response)
241241
response = client.fine_tuning.jobs.create(
242242
training_file=training_file_id,
243243
validation_file=validation_file_id,
244-
model="gpt-35-turbo", # Enter base model name. Note that in Azure OpenAI the model name contains dashes and cannot contain dot/period characters.
244+
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.
245245
)
246246

247247
job_id = response.id

articles/ai-services/openai/includes/fine-tuning-rest.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ For large data files, we recommend that you import from an Azure Blob store. Lar
150150
### Upload training data
151151

152152
```bash
153-
curl -X POST $AZURE_OPENAI_ENDPOINT/openai/files?api-version=2023-09-15-preview \
153+
curl -X POST $AZURE_OPENAI_ENDPOINT/openai/files?api-version=2023-10-01-preview \
154154
-H "Content-Type: multipart/form-data" \
155155
-H "api-key: $AZURE_OPENAI_KEY" \
156156
-F "purpose=fine-tune" \
@@ -160,7 +160,7 @@ curl -X POST $AZURE_OPENAI_ENDPOINT/openai/files?api-version=2023-09-15-preview
160160
### Upload validation data
161161

162162
```bash
163-
curl -X POST $AZURE_OPENAI_ENDPOINT/openai/files?api-version=2023-09-15-preview \
163+
curl -X POST $AZURE_OPENAI_ENDPOINT/openai/files?api-version=2023-10-01-preview \
164164
-H "Content-Type: multipart/form-data" \
165165
-H "api-key: $AZURE_OPENAI_KEY" \
166166
-F "purpose=fine-tune" \
@@ -172,7 +172,7 @@ curl -X POST $AZURE_OPENAI_ENDPOINT/openai/files?api-version=2023-09-15-preview
172172
After you uploaded your training and validation files, you're ready to start the fine-tuning job. The following code shows an example of how to create a new fine-tuning job with the REST API:
173173

174174
```bash
175-
curl -X POST $AZURE_OPENAI_ENDPOINT/openai/fine_tuning/jobs?api-version=2023-09-15-preview \
175+
curl -X POST $AZURE_OPENAI_ENDPOINT/openai/fine_tuning/jobs?api-version=2023-10-01-preview \
176176
-H "Content-Type: application/json" \
177177
-H "api-key: $AZURE_OPENAI_KEY" \
178178
-d '{
@@ -187,7 +187,7 @@ curl -X POST $AZURE_OPENAI_ENDPOINT/openai/fine_tuning/jobs?api-version=2023-09-
187187
After you start a fine-tune job, it can take some time to complete. Your job might be queued behind other jobs in the system. Training your model can take minutes or hours depending on the model and dataset size. The following example uses the REST API to check the status of your fine-tuning job. The example retrieves information about your job by using the job ID returned from the previous example:
188188
189189
```bash
190-
curl -X GET $AZURE_OPENAI_ENDPOINT/openai/fine_tuning/jobs/<YOUR-JOB-ID>?api-version=2023-09-15-preview \
190+
curl -X GET $AZURE_OPENAI_ENDPOINT/openai/fine_tuning/jobs/<YOUR-JOB-ID>?api-version=2023-10-01-preview \
191191
-H "api-key: $AZURE_OPENAI_KEY"
192192
```
193193
@@ -266,12 +266,12 @@ Azure OpenAI attaches a result file named _results.csv_ to each fine-tune job af
266266
The following Python example uses the REST API to retrieve the file ID of the first result file attached to the fine-tune job for your customized model, and then downloads the file to your working directory for analysis.
267267
268268
```bash
269-
curl -X GET "$AZURE_OPENAI_ENDPOINT/openai/fine_tuning/jobs/<JOB_ID>?api-version=2023-09-15-preview" \
269+
curl -X GET "$AZURE_OPENAI_ENDPOINT/openai/fine_tuning/jobs/<JOB_ID>?api-version=2023-10-01-preview" \
270270
-H "api-key: $AZURE_OPENAI_KEY")
271271
```
272272
273273
```bash
274-
curl -X GET "$AZURE_OPENAI_ENDPOINT/openai/files/<RESULT_FILE_ID>/content?api-version=2023-09-15-preview" \
274+
curl -X GET "$AZURE_OPENAI_ENDPOINT/openai/files/<RESULT_FILE_ID>/content?api-version=2023-10-01-preview" \
275275
-H "api-key: $AZURE_OPENAI_KEY" > <RESULT_FILENAME>
276276
```
277277

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

Lines changed: 153 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,22 @@ In this tutorial you learn how to:
4747

4848
### Python libraries
4949

50+
# [OpenAI Python 0.28.1](#tab/python)
51+
5052
If you haven't already, you need to install the following libraries:
5153

5254
```cmd
5355
pip install "openai==0.28.1" json requests os tiktoken time
5456
```
5557

58+
# [OpenAI Python 1.x](#tab/python-new)
59+
60+
```cmd
61+
pip install openai json requests os tiktoken time
62+
```
63+
64+
---
65+
5666
[!INCLUDE [get-key-endpoint](../includes/get-key-endpoint.md)]
5767

5868
### Environment variables
@@ -273,6 +283,8 @@ p5 / p95: 11.6, 20.9
273283

274284
## Upload fine-tuning files
275285

286+
# [OpenAI Python 0.28.1](#tab/python)
287+
276288
```Python
277289
# Upload fine-tuning files
278290
import openai
@@ -281,7 +293,7 @@ import os
281293
openai.api_key = os.getenv("AZURE_OPENAI_API_KEY")
282294
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
283295
openai.api_type = 'azure'
284-
openai.api_version = '2023-09-15-preview' # This API version or later is required to access fine-tuning for turbo/babbage-002/davinci-002
296+
openai.api_version = '2023-10-01-preview' # This API version or later is required to access fine-tuning for turbo/babbage-002/davinci-002
285297

286298
training_file_name = 'training_set.jsonl'
287299
validation_file_name = 'validation_set.jsonl'
@@ -302,6 +314,41 @@ print("Training file ID:", training_file_id)
302314
print("Validation file ID:", validation_file_id)
303315
```
304316

317+
# [OpenAI Python 1.x](#tab/python-new)
318+
319+
```python
320+
# Upload fine-tuning files
321+
322+
import os
323+
from openai import AzureOpenAI
324+
325+
client = AzureOpenAI(
326+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
327+
api_key=os.getenv("AZURE_OPENAI_KEY"),
328+
api_version="2023-10-01-preview" # This API version or later is required to access fine-tuning for turbo/babbage-002/davinci-002
329+
)
330+
331+
training_file_name = 'training_set.jsonl'
332+
validation_file_name = 'validation_set.jsonl'
333+
334+
# Upload the training and validation dataset files to Azure OpenAI with the SDK.
335+
336+
training_response = client.files.create(
337+
file=open(training_file_name, "rb"), purpose="fine-tune"
338+
)
339+
training_file_id = training_response.id
340+
341+
validation_response = client.files.create(
342+
file=open(validation_file_name, "rb"), purpose="fine-tune"
343+
)
344+
validation_file_id = validation_response.id
345+
346+
print("Training file ID:", training_file_id)
347+
print("Validation file ID:", validation_file_id)
348+
```
349+
350+
---
351+
305352
**Output:**
306353

307354
```output
@@ -313,6 +360,8 @@ Validation file ID: file-70a3f525ed774e78a77994d7a1698c4b
313360

314361
Now that the fine-tuning files have been successfully uploaded you can submit your fine-tuning training job:
315362

363+
# [OpenAI Python 0.28.1](#tab/python)
364+
316365
```python
317366
response = openai.FineTuningJob.create(
318367
training_file=training_file_id,
@@ -330,6 +379,27 @@ print("Status:", response["status"])
330379
print(response)
331380
```
332381

382+
# [OpenAI Python 1.x](#tab/python-new)
383+
384+
```python
385+
response = client.fine_tuning.jobs.create(
386+
training_file=training_file_id,
387+
validation_file=validation_file_id,
388+
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.
389+
)
390+
391+
job_id = response.id
392+
393+
# You can use the job ID to monitor the status of the fine-tuning job.
394+
# The fine-tuning job will take some time to start and complete.
395+
396+
print("Job ID:", response.id)
397+
print("Status:", response.id)
398+
print(response.model_dump_json(indent=2))
399+
```
400+
401+
---
402+
333403
**Output:**
334404

335405
```output
@@ -350,26 +420,12 @@ Status: pending
350420
}
351421
```
352422

353-
To retrieve the training job ID, you can run:
354-
355-
```python
356-
response = openai.FineTuningJob.retrieve(job_id)
357-
358-
print("Job ID:", response["id"])
359-
print("Status:", response["status"])
360-
print(response)
361-
```
362-
363-
**Output:**
364-
365-
```output
366-
Fine-tuning model with job ID: ftjob-0f4191f0c59a4256b7a797a3d9eed219.
367-
```
368-
369423
## Track training job status
370424

371425
If you would like to poll the training job status until it's complete, you can run:
372426

427+
# [OpenAI Python 0.28.1](#tab/python)
428+
373429
```python
374430
# Track training status
375431

@@ -402,6 +458,42 @@ response = openai.FineTuningJob.list()
402458
print(f'Found {len(response["data"])} fine-tune jobs.')
403459
```
404460

461+
# [OpenAI Python 1.x](#tab/python-new)
462+
463+
```python
464+
# Track training status
465+
466+
from IPython.display import clear_output
467+
import time
468+
469+
start_time = time.time()
470+
471+
# Get the status of our fine-tuning job.
472+
response = client.fine_tuning.jobs.retrieve(job_id)
473+
474+
status = response.status
475+
476+
# If the job isn't done yet, poll it every 10 seconds.
477+
while status not in ["succeeded", "failed"]:
478+
time.sleep(10)
479+
480+
response = client.fine_tuning.jobs.retrieve(job_id)
481+
print(response.model_dump_json(indent=2))
482+
print("Elapsed time: {} minutes {} seconds".format(int((time.time() - start_time) // 60), int((time.time() - start_time) % 60)))
483+
status = response.status
484+
print(f'Status: {status}')
485+
clear_output(wait=True)
486+
487+
print(f'Fine-tuning job {job_id} finished with status: {status}')
488+
489+
# List all fine-tuning jobs for this resource.
490+
print('Checking other fine-tune jobs for this resource.')
491+
response = client.fine_tuning.jobs.list()
492+
print(f'Found {len(response.data)} fine-tune jobs.')
493+
```
494+
495+
---
496+
405497
**Output:**
406498

407499
```ouput
@@ -432,6 +524,8 @@ Found 2 fine-tune jobs.
432524

433525
To get the full results, run the following:
434526

527+
# [OpenAI Python 0.28.1](#tab/python)
528+
435529
```python
436530
#Retrieve fine_tuned_model name
437531

@@ -441,6 +535,19 @@ print(response)
441535
fine_tuned_model = response["fine_tuned_model"]
442536
```
443537

538+
# [OpenAI Python 1.x](#tab/python-new)
539+
540+
```python
541+
#Retrieve fine_tuned_model name
542+
543+
response = client.fine_tuning.jobs.retrieve(job_id)
544+
545+
print(response.model_dump_json(indent=2))
546+
fine_tuned_model = response.fine_tuned_model
547+
```
548+
549+
---
550+
444551
## Deploy fine-tuned model
445552

446553
Unlike the previous Python SDK commands in this tutorial, since the introduction of the quota feature, model deployment must be done using the [REST API](/rest/api/cognitiveservices/accountmanagement/deployments/create-or-update?tabs=HTTP), which requires separate authorization, a different API path, and a different API version.
@@ -504,6 +611,8 @@ It isn't uncommon for this process to take some time to complete when dealing wi
504611

505612
After your fine-tuned model is deployed, you can use it like any other deployed model in either the [Chat Playground of Azure OpenAI Studio](https://oai.azure.com), or via the chat completion API. For example, you can send a chat completion call to your deployed model, as shown in the following Python example. You can continue to use the same parameters with your customized model, such as temperature and max_tokens, as you can with other deployed models.
506613

614+
# [OpenAI Python 0.28.1](#tab/python)
615+
507616
```python
508617
#Note: The openai-python library support for Azure OpenAI is in preview.
509618
import os
@@ -527,6 +636,33 @@ print(response)
527636
print(response['choices'][0]['message']['content'])
528637
```
529638

639+
# [OpenAI Python 1.x](#tab/python-new)
640+
641+
```python
642+
import os
643+
from openai import AzureOpenAI
644+
645+
client = AzureOpenAI(
646+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
647+
api_key=os.getenv("AZURE_OPENAI_KEY"),
648+
api_version="2023-05-15"
649+
)
650+
651+
response = client.chat.completions.create(
652+
model="gpt-35-turbo-ft", # model = "Custom deployment name you chose for your fine-tuning model"
653+
messages=[
654+
{"role": "system", "content": "You are a helpful assistant."},
655+
{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
656+
{"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},
657+
{"role": "user", "content": "Do other Azure AI services support this too?"}
658+
]
659+
)
660+
661+
print(response.choices[0].message.content)
662+
```
663+
664+
---
665+
530666
## Delete deployment
531667

532668
Unlike other types of Azure OpenAI models, fine-tuned/customized models have [an hourly hosting cost](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/#pricing) associated with them once they are deployed. It is **strongly recommended** that once you're done with this tutorial and have tested a few chat completion calls against your fine-tuned model, that you **delete the model deployment**.

0 commit comments

Comments
 (0)