Fine-tuning a model with Azure Open AI Studio
Tags: ai, azure, openai, aiadvent
Published: 2023-12-01 03:20:40

_This publication is part of the **AI Advent Calendar 2023**, an initiative led by Héctor Pérez, Alex Rostan, Pablo Piovano, and Luis Beltrán. Check **[this link](https://dev.to/ppiova/advent-artificial-intelligence-2881)** for more interesting articles about AI created by the community._

In preparation to my upcoming participation at the **[Global AI Conference 2023](https://globalai.community/events/global-ai-conference-december-2023/)** with the topic _Fine-tuning an Azure Open AI model: Lessons learned_, let's see how to actually customize a model with your own data using **Azure Open AI**.

## First of all, a definition
I like the definition presented [here](https://deeplizard.com/learn/video/5T-iXNNiwIs). **Fine-tuning is:** 

> _the process that takes a model that has already been trained for one given task and then tunes or tweaks the model to make it perform a second similar task._ 

It is a way of applying _transfer learning_, a technique that uses knowledge which was gained from solving one problem and applies it to a new but related problem.

## Azure Open AI and Fine-tuning
**Azure Open AI** is a cloud-based platform that enables everyone to build and deploy AI models quickly and easily. One of the capabilities of this service is fine-tuning pre-trained models with your own datasets. Some advantages include:

* Achieving better results than prompt-engineering. 
* Needing less text sent (thus, fewer tokens are processed on each API call)
* Saving costs, improving request latency.

## What do you need?

- An **Azure subscription** with access to **Azure Open AI** services.
- An Azure Open AI **resource** created in one of the [supported regions](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models#fine-tuning-models-preview) for fine-tuning, with a supported deployed model.
- The **Cognitive Services OpenAI Contributor** role.
- The most important element to consider: **Do you really need to fine-tune a model?** I'll discuss about it during my talk next week, for the moment you can read about it [here](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/fine-tuning-considerations).

Let's fine-tune a model using **Azure OpenAI Studio**.

## Steps

1. Create an Azure Open AI resource.
2. Prepare your data.
3. Use Azure OpenAI Studio's Create custom model wizard.
4. Wait until the model is fine-tuned.
5. Deploy your custom model for use.
6. Use it.

## Let's do it!

### Step 1. Create an Azure Open AI resource
Use the wizard to create an Azure Open AI resource. You only need to be careful about the region. Currently, only _North Central US_ and _Sweden Central_ support the fine-tuning capability, so just choose any of them.

![Azure Open AI resource in North Central US region](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xespytqvfd0fabtlp7kg.png)

Once the resource is created, go to **Azure OpenAI Studio**. 

### Step 2. Prepare your data.
You must prepare two datasets: one for training and a second one for validation. They each contain samples of inputs and its expected output in **JSONL** (JSON Lines) format. However, depending on the base model that you deployed, you will need specific properties for each element:

* If you are fine-tuning recent models, such as **GPT 3.5 Turbo**, here's an example of the file format. 

```
{"messages": [{"role": "system", "content": "You are a helpful recipe assistant. You are to extract the generic ingredients from each of the recipes provided."}, {"role": "user", "content": "Title: No-Bake Nut Cookies\n\nIngredients: [\"1 c. firmly packed brown sugar\", \"1/2 c. evaporated milk\", \"1/2 tsp. vanilla\", \"1/2 c. broken nuts (pecans)\", \"2 Tbsp. butter or margarine\", \"3 1/2 c. bite size shredded rice biscuits\"]\n\nGeneric ingredients: "}, {"role": "assistant", "content": "[\"brown sugar\", \"milk\", \"vanilla\", \"nuts\", \"butter\", \"bite size shredded rice biscuits\"]"}]}
{"messages": [{"role": "system", "content": "You are a helpful recipe assistant. You are to extract the generic ingredients from each of the recipes provided."}, {"role": "user", "content": "Title: Jewell Ball'S Chicken\n\nIngredients: [\"1 small jar chipped beef, cut up\", \"4 boned chicken breasts\", \"1 can cream of mushroom soup\", \"1 carton sour cream\"]\n\nGeneric ingredients: "}, {"role": "assistant", "content": "[\"beef\", \"chicken breasts\", \"cream of mushroom soup\", \"sour cream\"]"}]}


```

Please notice that for each item (line) you provide a `messages` element containing an array of `role-content` pairs for the `system` (the behavior), `user` (the input), and `assistant` (the output).

* On the other hand, if you are fine-tuning older models (such as **Babbage** or **Davinci**), here's a sample file format that works with both of them:

```
{"prompt": "You guys are some of the best fans in the NHL", "completion": "hockey"}
{"prompt": "The Red Sox and the Yankees play tonight!", "completion": "baseball"}
{"prompt": "Pelé was one of the greatest", "completion": "soccer"}
```

You can notice that each element contains a `prompt-completion` pair, representing the input and the desired output which we'd like to be generated by the fine-tuned model.

More information about JSON Lines can be found [here](https://jsonlines.org/).

In order to generate a JSONL file, there are several approaches:

* Manual approach: Write an application that creates a text file (with `.jsonl` extension), then loop over your data collection and serialize each item into a JSON string (don't forget that you need specific properties). Write each JSON string into a new line of the recently created file.

* Library approach: Depending on the programming language you are using, it's highly probable that there exists some libraries which can export your data in JSONL format. For example, [`jsonlines`](https://jsonlines.readthedocs.io/en/latest/) for Python.

* Website approach: There are some websites which can convert your `Excel`, `SQL`, `CSV` (and others) data into JSON Lines format, for example [Table Convert](https://tableconvert.com/sql-to-jsonlines) or [Code Beautify](https://codebeautify.org/json-to-jsonl-converter).



By the way, here are some characteristics of JSONL:

- Each line is a valid JSON item
- Each line is separated by a \n character
- The file is encoded using UTF-8

Moreover, for Azure Open AI usage, the file must include a **byte-order mark (BOM).**

### Step 3. Use Azure OpenAI Studio's Create custom model wizard.

Only some models can be fine-tuned. As of today, **babbage-002**, **davinci-002**, and **gpt-35-turbo** models support this feature. 

In Azure Open AI Studio, click on **Models** and then on **Create a custom model**.


![Creating a custom model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rwm3m73r96ceve09kp5.png)


A wizard shows up. First of all, you need to select one of the [supported models for fine-tuning](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models#fine-tuning-models-preview), which will be used as the base for customization. 


![Supported base models for fine-tuning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6qv0b7xmy4bno7lcq1a.png)


For this example, I have selected `gpt-35-turbo`. The suffix is optional, although it makes it easy to identify the generated model so it is recommended to provide it. Click on Next.



![Base model step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oezpexwqvyf7ca4ct5l1.png)



Now, you need to provide a `JSONL` file, which serves as the **training dataset**. You can either select a local file or enter the URL of a public online resource (such as an Azure blob or a web location). 

For this example, I have chosen a local `JSONL` file which contains examples of a helpful virtual assistant that extracts generic ingredients from a provided recipe. Click on Upload file and once the task is completed, click on Next.



![Training data step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5ex09w88m28ynja9ol3.png)



Now, you should provide the `JSONL` file/resource for the **validation dataset**. For this example, I have chosen another local `JSONL` file with examples of a helpful virtual assistant that extracts generic ingredients from a provided recipe. Click on Upload file and then on Next once the process finishes.



![Validation data step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iixs6e9w9vep2agqft0.png)



Afterwards, you can set advanced options, such as the **number of epochs** to train the model for. Let's keep the **Default** value for this example and click on Next.  


![Advanced options step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi64ieojiln2ylv3sepl.png)


Finally, a summary with the selected choices from the previous steps is presented. Click on **Start Training job**.



![Review step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pu0jxjvgdpuzx9mwl8kp.png)




### Step 4. Wait until the model is fine-tuned.

You can check the status of the fine-tuning training job in the **Custom models** tab from **Models** menu.


![Fine-tuning job started](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8y1ibupw1842fstjsnv.png)



Click on the **Refresh** button to see the current progress of the task. 


![Training a model, in process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8df94ogp5b4ax55rsm7.png)


Training the model will take some time depending on the amount of data provided, the number of epochs, the base model, and other parameters selected for the task. Furthermore, since your job enters into a queue, the server might be handling other training tasks, causing that the process is delayed.

Once you see that the Status is `Succeeded`, **it means that your custom, fine-tuned model has been created**! Well done! 


![Training job completed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xe2zyf93zlhzv80qn8bp.png)




However, an extra step is needed before you can try using it.


### Step 5. Deploy your custom model for use.

Select the recently created model, then click on the **Deploy** button.


![Deploying a custom model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9fzlvad8ba6a92jtl4v2.png)


Afterwards, add a **deployment name** for its implementation. You can also change the model version in case the model has been fine-tuned several times. Click on **Create**.


![Set deployment options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l018d4v695m3nx8oao6k.png)


You can monitor the deployment progress under the **Deployments** menu:


![Deploying a fine-tuned model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5i3ve6bac0v06x4vod8.png)



When the job finishes (_Status = Succeeded_), you are ready to use this model.


![Fine-tuned model deployed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvq9a525hc9shh3bgphr.png)




### Step 6. Use it.

You can use the deployed fine-tuned model for inference anywhere: In an application that you develop, in the Playground, as part of an API request, etc. For example:


![Testing the fine-tuned model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lznghhcdo26tawrn8qqd.png)



As you can see, the process for fine-tuning an Open AI model using Azure is quite straightforward and it offers several benefits. However, you should also consider if this is the best solution for your needs. Join my session at the [Global AI Conference](https://globalai.community/conference/) later this month to learn more about it!


![Fine-tuning an Azure Open AI model, lessons learned](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eufi5vyosifdgie19a8f.png)

Well, this was a long post but hopefully, it was also useful for you. Remember to follow the rest of the interesting publications of the [AI Advent Calendar 2023](https://dev.to/ppiova/advent-artificial-intelligence-2881). You can also follow the conversation on Twitter with the hashtag #AIAdvent.

Thank you for reading. Until next time!

Luis