top of page
Writer's pictureThe Tech Platform

Fine Tuning Operations in Azure OpenAI Service

Updated: Sep 14, 2023

Unlocking the full potential of artificial intelligence (AI) models often requires a degree of fine-tuning—tailoring them to specific tasks and objectives.


Azure OpenAI Services, a dynamic platform for AI-driven solutions, empowers developers with the tools to perform fine-tuning operations efficiently and effectively.


In this article, we will learn fine-tuning within Azure OpenAI Services, exploring key operations such as creating, canceling, deleting, retrieving, monitoring events, and listing—all of which play pivotal roles in shaping the capabilities of AI models. Understanding these operations is essential for optimizing AI models to meet your unique requirements and deliver top-notch performance.


Let's begin!


Table of Contents:

CREATE OPERATION

CANCEL OPERATION

DELETE OPERATION

GET OPERATION

GET EVENTS OPERATION

LIST OPERATION


What is Fine-Tuning?

Fine-tuning in Azure OpenAI Service is the process of adjusting the parameters of a pre-trained model to improve its performance on a specific task. This is done by training the model on a dataset of examples that are relevant to the task.


Fine-tuning can be used to improve the performance of Azure OpenAI Service models on a variety of tasks, such as text generation, translation, code generation, question answering, summarization, and creative writing.


To fine-tune a model in Azure OpenAI Service, you need to provide the following:

  • A base model: This is the pre-trained model that you want to fine-tune.

  • Training data: This is a dataset of examples that are relevant to the task that you want the model to perform.

  • Hyperparameters: These are settings that control the fine-tuning process.

Once you have provided these inputs, you can create a fine-tuning job using the Azure OpenAI Services REST API. The fine-tuning job will train the model on the training data and adjust the model's parameters. Once the fine-tuning job is complete, you can deploy the fine-tuned model to production.


Also Read:


Fine-Tuning operations in Azure OpenAI Services

Fine-tuning operations in Azure OpenAI Services are the process of adjusting the parameters of a pre-trained language model to make it more specialized for a specific task. This can be done by providing the model with a dataset of labeled examples that are relevant to the task. The model will then learn to associate the input data with the desired output labels.


Fine-tuning operations are used to improve the performance of the language model on a variety of tasks, such as:

  • Question answering

  • Text summarization

  • Code generation

  • Creative writing

  • Translation

To fine-tune a language model in Azure OpenAI Services, you can use the CREATE fine-tune operation. This operation takes a base model identifier and a training data identifier as input. The Azure OpenAI Service will then create a fine-tune job and start the fine-tuning process.


Once the fine-tuning process is complete, you can use the GET fine-tune operation to get the fine-tuned model identifier. You can then use the fine-tuned model to generate text, translate languages, and perform other tasks.


Below is the list of fine tune operations in Azure OpenAI Services:

  1. Create

  2. Cancel

  3. Delete

  4. Get

  5. Get Events

  6. List


1. Create

The CREATE operation allows you to create a job that fine-tunes a specified model from a given training file. The response includes details of the enqueued job, including job status and hyper parameters. The name of the fine-tuned model is added to the response once complete.


To create a fine-tune job, you can use the following HTTP request:

POST {endpoint}/openai/fine-tunes?api-version=2023-05-15 

The request body for the CREATE operation in Azure OpenAI Service is a JSON object that contains the following information:

  • model: The identifier of the base model to be fine-tuned.

  • training_file: The identifier of the file that contains the training data.

The response body contains the following information:

  • id: The identifier of the fine-tune job.

  • status: The status of the fine-tune job.

  • hyperparameters: The hyperparameters that were used for the fine-tuning process.

  • fine_tuned_model_id: The identifier of the fine-tuned model, once the fine-tuning process is complete.

Below is the list of some additional parameters:

  1. batch_size: The batch size to use for training.

  2. classification_betas: The classification beta values.

  3. classification_n_classes: The number of classes in a classification task.

  4. classification_positive_class: The positive class in binary classification.

  5. compute_classification_metrics: A value indicating whether to compute classification metrics.

  6. learning_rate_multiplier: The learning rate multiplier to use for training.

  7. n_epochs: The number of epochs to train the model for.

  8. prompt_loss_weight: The weight to use for loss on the prompt tokens.

  9. suffix: The suffix used to identify the fine-tuned model.

  10. validation_file: The file identity (file-id) that is used to evaluate the fine tuned model during training.

If you specify the compute_classification_metrics parameter, you must also specify the classification_n_classes parameter for multiclass classification or the classification_positive_class parameter for binary classification.


Here is an example of how to create a fine-tune job in Python:

import requests

endpoint = "https://aoairesource.openai.azure.com"
model_id = "my-base-model-id"
training_file_id = "my-training-file-id"

headers = {
    "api-key": "YOUR_API_KEY",
}

data = {
    "model": model_id,
    "training_file": training_file_id,
}

response = requests.post(
    f"{endpoint}/openai/fine-tunes?api-version=2023-05-15",
    headers=headers,
    json=data,
)

if response.status_code == 201:
    print("Fine-tune job created successfully.")
else:
    print(response.content)

The above code will create a fine-tune job using the my-base-model-id base model and the my-training-file-id training file.


You can use the optional parameters to create a fine tune job. Consider the syntax below:

POST /openai/fine-tunes?api-version=2023-05-15 
Content-Type: application/json  
{   
    "model": "my-base-model-id",   
    "training_file": "my-training-file-id",   
    "batch_size": 32,   
    "learning_rate": 0.001,   
    "epochs": 10,   
    "warmup_steps": 1000,   
    "validation_file": "my-validation-file-id",   
    "classification_betas": [0.25, 0.75],   
    "compute_classification_metrics": true 
}

2. Cancel

The Azure OpenAI Service provides an operation called cancel that can be used to cancel the processing of a fine-tune job. The cancel operation takes the fine-tune ID as input and returns a response indicating whether or not the fine-tune job was successfully canceled.


To cancel a fine-tune job, you can use the following HTTP request:

POST {endpoint}/openai/fine-tunes/{fine-tune-id}/cancel?api-version=2023-05-15 

where:

  • {endpoint} is the Azure OpenAI endpoint for your account.

  • {fine-tune-id} is the identifier of the fine-tune job to be canceled.

You must also include the api-version query parameter in the request, and the api-key header in the request header.


The response to the cancel operation will be a FineTune object, or an ErrorResponse object if the fine-tune job could not be canceled.


You can cancel Fine tuning Job in Azure OpenAI Services:

  1. Using Python

  2. Using Azure OpenAI SDK

Cancel a fine-tune job using Python:

import requests  

endpoint = "https://aoairesource.openai.azure.com" 
fine_tune_id = "12345678-abcd-efgh-ijkl-mnopqrstuvw"  

headers = {     
    "api-key": "YOUR_API_KEY", 
}  

response = requests.post(     
    f"{endpoint}/openai/fine-tunes/{fine_tune_id}/cancel?api-version=2023-05-15",     
    headers=headers, 
)  
    
if response.status_code == 200:     
    print("Fine-tune job canceled successfully.") 
else:     
    print(response.content) 

Benefits of Canceling Fine Tune job using Python:

  1. Can manually construct the request URL and header and parse the response

  2. Can customize the process as per your needs. For example, can add additional error handling or logging.

Limitations of canceling Fine Tune job using Python:

  1. Can be complex

  2. You need to handle errors yourself, as the Python standard library does not provide any built-in error handling for HTTP requests.

  3. You need to keep your code up to date with the latest Azure OpenAI Service API changes.


Cancel a fine-tune job using the Azure OpenAI SDK

import azure.cognitiveservices.openai as openai

endpoint = "https://aoairesource.openai.azure.com"
fine_tune_id = "12345678-abcd-efgh-ijkl-mnopqrstuvw"

client = openai.OpenAIClient(endpoint, api_key="YOUR_API_KEY")

response = client.cancel_fine_tune_job(fine_tune_id)

if response.status_code == 200:
    print("Fine-tune job canceled successfully.")
else:
    print(response.content)

The cancel_fine_tune_job() method takes the fine-tune ID as input and returns a response indicating whether or not the fine-tune job was successfully canceled.


If the fine-tune job is successfully canceled, the response will be a FineTune object. If the fine-tune job could not be canceled, the response will be an ErrorResponse object.


Benefits of Canceling Fine Tune job using Azure OpenAI SDK:

  1. You can simply call the cancel_fine_tune_job() method, and the Azure OpenAI SDK will take care of the rest.

  2. The Azure OpenAI SDK is up to date with the latest Azure OpenAI Service API changes, so you do not need to worry about making changes to your code.

Limitations of canceling Fine Tune job using Azure OpenAI SDK:

  1. Less control over customization

  2. No additional error handling or logging

The Difference:

The main difference between canceling a fine-tune job in Azure OpenAI Service using Python and using the Azure OpenAI SDK is that the Azure OpenAI SDK provides a higher-level abstraction for canceling a fine-tune job.


When canceling a fine-tune job using Python, you need to make an HTTP request to the Azure OpenAI Service endpoint. This requires you to manually construct the request URL and headers, and to parse the response.


When canceling a fine-tune job using the Azure OpenAI SDK, you can simply call the cancel_fine_tune_job() method. The Azure OpenAI SDK will take care of constructing the request URL and headers, and parsing the response.


Things to keep in mind:

Below are the things to keep in mind while canceling a fine-tune job in Azure OpenAI Service:

  • The fine-tune job may not be canceled immediately. The cancellation request may take some time to be processed, and the fine-tune job may continue to run for a short period of time after the request is made.

  • The fine-tune job may not be canceled if it is already in the completed state. Once a fine-tune job is completed, it cannot be canceled.

  • Canceling a fine-tune job may result in some data loss. The fine-tune job may have already processed some of the training data, and this data may be lost when the job is canceled.

If you are unsure whether or not to cancel a fine-tune job, it is best to contact Azure OpenAI support for assistance.


3. Delete

If you no longer need the fine-tune job, then you can use DELETE operation to delete a particular fine-tune job.


To delete a fine-tune job, you can use the following HTTP request:

DELETE {endpoint}/openai/fine-tunes/{fine_tune_id}?api-version=2023-05-15 

If the fine-tune job is already in the Completed state, it will be deleted immediately. If the fine-tune job is in the Running or Pending state, it will be cancelled and then deleted.


Here are some of the benefits of deleting fine-tune jobs in Azure OpenAI Service:

  • Free up resources: Deleting fine-tune jobs that are no longer needed can free up resources on the Azure OpenAI Service platform.

  • Prevent unnecessary charges: Deleting fine-tune jobs that are no longer running can prevent unnecessary charges.

  • Improve performance: Deleting fine-tune jobs that are no longer needed can improve the performance of the Azure OpenAI Service platform.

Things to keep in mind when deleting fine-tune jobs:

  • You cannot delete a fine-tune job that has already been deployed.

  • You cannot delete a fine-tune job that is currently being used by another process.

  • If you delete a fine-tune job, you will lose all of the training data and results associated with the job.


4. Get

The GET operation will give you detailed information about a specific fine-tune job. You can use this to monitor the progress of the job, identify any potential problems, or get the results of the fine-tuning process.


To get a fine-tune job, you can use the following HTTP request:

GET {endpoint}/openai/fine-tunes/{fine_tune_job_id}?api-version=2023-05-15

To get the fine-tuned model identifier for a specific fine-tune job, you can use the following code:

import requests

endpoint = "https://aoairesource.openai.azure.com"
fine_tune_job_id = "my-fine-tune-job-id"

headers = {
    "api-key": "YOUR_API_KEY",
}

response = requests.get(
    f"{endpoint}/openai/fine-tunes/{fine_tune_job_id}?api-version=2023-05-15",
    headers=headers,
)

if response.status_code == 200:
    fine_tuned_model_id = response.json()["fine_tuned_model_id"]
    print(f"Fine-tuned model identifier: {fine_tuned_model_id}")
else:
    print(response.content)

The above code will get the fine-tuned model identifier for the my-fine-tune-job-id fine-tune job. You can then use the fine_tuned_model_id variable to deploy the fine-tuned model and use it to generate text, translate languages, and perform other tasks.


The response body contains the following information:

  • id: The identifier of the fine-tuned model.

  • status: The status of the fine-tune job.

  • hyperparameters: The hyperparameters that were used for the fine-tuning process.

  • created_on: The date and time when the fine-tune job was created.

{
  "id": "my-fine-tuned-model-id",
  "status": "Completed",
  "hyperparameters": {
    "batch_size": 32,
    "learning_rate": 0.001,
    "epochs": 10,
    "warmup_steps": 1000
  },
  "created_on": "2023-08-04T12:00:00Z"
}

Benefits of using GET operation:

  • Easy to use: The GET operation is a simple and easy-to-use way to get the fine-tuned model identifier for a specific fine-tune job.

  • Flexible: The GET operation can be used to get the fine-tuned model identifier for any fine-tune job, regardless of the base model or training data used.

  • Accurate: The GET operation returns the accurate fine-tuned model identifier for the specified fine-tune job.


5. Get Events

The GET EVENTS operation in Azure OpenAI Service allows you to get a list of events for a specific fine-tune job. This can be useful for monitoring the progress of the fine-tuning process and identifying any potential problems.


To get the events for a fine-tune job, you can use the following HTTP request:

GET {endpoint}/openai/fine-tunes/{fine_tune_id}/events?api-version=2023-

To get a list of events for a specific fine-tune job, you can use the following code:

import requests

endpoint = "https://aoairesource.openai.azure.com"
fine_tune_job_id = "my-fine-tune-job-id"

headers = {
    "api-key": "YOUR_API_KEY",
}

response = requests.get(
    f"{endpoint}/openai/fine-tunes/{fine_tune_job_id}/events?api-version=2023-05-15",
    headers=headers,
)

if response.status_code == 200:
    events = response.json()["events"]
    for event in events:
        print(f"Event type: {event['type']}")
        print(f"Event timestamp: {event['timestamp']}")
        print(f"Event message: {event['message']}")
else:
    print(response.content)

This code will get a list of events for the my-fine-tune-job-id . The events are returned in chronological order, with the most recent event at the top of the list.


Each event contains the following information:

  • type: The type of event.

  • timestamp: The date and time when the event occurred.

  • message: A message describing the event.

You can use the information in the events to monitor the progress of the fine-tuning process and identify any potential problems. For example, if you see an event with the type "ERROR", this indicates that an error has occurred during the fine-tuning process.


6. List

The LIST operation in Azure OpenAI Service allows you to get a list of all the fine-tune jobs that you own. This can be useful for monitoring the progress of your fine-tune jobs, identifying the jobs that you want to get more information about, or deleting jobs that are no longer needed.


To list fine-tune jobs, you can use the following HTTP request:

GET {endpoint}/openai/fine-tunes?api-version=2023-05-15

The response body contains a list of fine-tune jobs, each of which contains the following information:

  • id: The identifier of the fine-tune job.

  • status: The status of the fine-tune job.

  • hyperparameters: The hyperparameters that were used for the fine-tuning process.

  • created_on: The date and time when the fine-tune job was created.

{
  "fine_tune_jobs": [
    {
      "id": "my-fine-tune-job-id-1",
      "status": "Completed",
      "hyperparameters": {
        "batch_size": 32,
        "learning_rate": 0.001,
        "epochs": 10,
        "warmup_steps": 1000
      },
      "created_on": "2023-08-04T12:00:00Z"
    },
    {
      "id": "my-fine-tune-job-id-2",
      "status": "Pending",
      "hyperparameters": {
        "batch_size": 64,
        "learning_rate": 0.0005,
        "epochs": 20,
        "warmup_steps": 2000
      },
      "created_on": "2023-08-04T12:01:00Z"
    }
  ]
}

You can use the information in the response body to filter and sort the list of fine-tune jobs, or to get more information about a specific fine-tune job.


Best practices for Fine Tuning operations

In the above section, we discussed the concept of Fine Tuning and its operational aspects. However, during practical usage, errors may arise, or the responses may not align with our desired outcomes. To address these issues effectively and efficiently, we present a set of best practices for working with Fine Tuning in your Azure OpenAI service.


Best Practice 1: Use a large enough training dataset. The more training data you have, the better the fine-tuned model will perform.


Best Practice 2: Choose a suitable base model. Some base models are better suited for certain tasks than others. For example, the davinci-codex base model is good for general-purpose tasks, while the curie base model is good for creative writing tasks.


Best Practice 3: Experiment with different hyper parameters. The hyper parameters of the fine-tuning process can have a significant impact on the performance of the fine-tuned model. Experiment with different hyper parameters to find the best settings for your specific task.


Best Practice 4: Use a GPU if possible. Fine-tuning models can be computationally expensive. If possible, use a GPU to speed up the fine-tuning process.


Best Practice 5: Be patient. Fine-tuning models can take some time, especially if you are using a large training dataset. Be patient and let the fine-tuning process complete before evaluating the performance of the fine-tuned model.


Best Practice 6: Use a good quality training dataset. The quality of the training dataset will have a significant impact on the performance of the fine-tuned model. Make sure that your training dataset is clean, well-labeled, and representative of the data that you will be using the fine-tuned model to generate.


Best Practice 7: Use a balanced training dataset. If your training dataset is imbalanced, the fine-tuned model may learn to favor the majority class. To avoid this, use a balanced training dataset.


Best Practice 8: Use a variety of training data. The fine-tuned model will learn to perform better if it is trained on a variety of data. Try to include a variety of different types of data in your training dataset, such as text, code, and images.


Best Practice 9: Use a validation dataset. A validation dataset is a set of data that is not used to train the model but is used to evaluate the performance of the model during the fine-tuning process. Use a validation dataset to ensure that the model is not overfitting to the training dataset.


Best Practice 10: Monitor the fine-tuning process. You can use the GET EVENTS fine-tune operation to monitor the progress of the fine-tuning process and identify any potential problems.


Best Practice 11: Evaluate the performance of the fine-tuned model. Once the fine-tuning process is complete, evaluate the performance of the fine-tuned model on a held-out test dataset. This will help you to determine how well the fine-tuned model will perform on new data.


Conclusion

To sum it up, fine-tuning operations in Azure OpenAI Services, like creating, canceling, deleting, getting information, checking events, and listing, are important tools for improving your AI models. By using them carefully and following the best methods, you can make your AI systems work better.


As you keep using these operations, remember that staying flexible, keeping an eye on how things are going, and making improvements are the keys to getting great results in your AI-powered applications. Fine-tuning in Azure OpenAI Services allows you to customize AI models for your needs and provide excellent experiences for users.

コメント


bottom of page