Fine-Tuning

Open AI allows developers to fine-tune their models for specific purposes using their fine-tunes API endpoint. Below listed are the models you can fine-tune using this API:

  • davinci
  • curie
  • babbage
  • ada

For fine-tuning a model, you first need to make sure that your dataset is in JSONL format and if you are working on free trial, your training bill should not exceed 5 dollars. To prepare your dataset, you can execute the below command in your jupyter notebook:

!openai tools fine_tunes.prepare_data -f <dataset location>

With the exception of a prompt and a completion column/key, this tool accepts a variety of forms. You can instruct it to store the output into a JSONL file suitable for fine-tuning by passing it a CSV, TSV, XLSX, JSON, or JSONL file. It will walk you through the process of proposed adjustments.

Now, you can fine-tune a model by executing the below code in python environment:

Python3




# FineTune Endpoint of OpenAI is used to fine-tune models
# for a specific task
res = openai.FineTune.create("training_file"="<training-file-id>",
    "validation_file"="<validation-file-id>",
    "model"="<model-name>",
    "n_epochs"="<number of epochs/iterations>[integer]",
    "batch_size"="<batch size for model to train on>[integer]",
    "learning_rate_multiplier"="""<The pretraining learning rate multiplied by this 
    number yields the fine-tuning learning rate.>[integer]""")
# storing the fine-tuning Job ID 
jobID = res["id"]
# storing the status of fine-tuning
# initially it will show pending that means your task is in the queue
status = res["status"]
  
print(f'Fine-tunning model with jobID: {jobID}.')
print(f"Training Response: {res}")
print(f"Training Status: {status}")


Example of Fine-tuning

We will be taking the first 100 rows of Spam Classification dataset here.

Note: We are using only 100 rows here because the training cost for further rows was not supported in the free trial.

Step 1: First prepare the dataset using OpenAI’s in-built function.

!openai tools fine_tunes.prepare_data -f "spam.csv"

Output:

Analyzing...
- Based on your file extension, your file is formatted as a CSV file
- Your file contains 100 prompt-completion pairs
- The `prompt` column/key should be lowercase
- The `completion` column/key should be lowercase
- The input file should contain exactly two columns/keys per row. Additional columns/keys present are: ['Unnamed: 0']
  WARNING: Some of the additional columns/keys contain `Unnamed: 0` in their name. These will be ignored, and the column/key `Unnamed: 0` will be used instead. This could also result from a duplicate column/key in the provided file.
- Based on your data it seems like you're trying to fine-tune a model for classification
- For classification, we recommend you try one of the faster and cheaper models, such as `ada`
- For classification, you can estimate the expected model performance by keeping a held out dataset, which is not used for training
- Your data does not contain a common separator at the end of your prompts. Having a separator string appended to the end of the prompt makes it clearer to the fine-tuned model where the completion should begin. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more detail and examples. If you intend to do open-ended generation, then you should leave the prompts empty
- The completion should start with a whitespace character (` `). This tends to produce better results due to the tokenization we use. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details
Based on the analysis we will perform the following actions:
- [Necessary] Your format `CSV` will be converted to `JSONL`
- [Necessary] Lower case column name to `prompt`
- [Necessary] Lower case column name to `completion`
- [Necessary] Remove additional columns/keys: ['Unnamed: 0']
- [Recommended] Add a suffix separator ` ->` to all prompts [Y/n]: n
- [Recommended] Add a whitespace character to the beginning of the completion [Y/n]: n
- [Recommended] Would you like to split into training and validation set? [Y/n]: y
Your data will be written to a new JSONL file. Proceed [Y/n]: y
Wrote modified files to `/content/clean_spam_prepared_train (1).jsonl` and `/content/clean_spam_prepared_valid (1).jsonl`
Feel free to take a look!
Now use that file when fine-tuning:
> openai api fine_tunes.create -t "/content/clean_spam_prepared_train (1).jsonl" -v "/content/clean_spam_prepared_valid (1).jsonl" --compute_classification_metrics --classification_positive_class "ham"
 Make sure to include `stop=["am"]` so that the generated texts ends at the expected place.
Once your model starts training, it'll approximately take 4.73 minutes to train a `curie` model, and less for `ada` and `babbage`. Queue will approximately take half an hour per job ahead of you.

Step 2: Upload your training and validation dataset to OpenAI using the following lines of code in python

Python3




# uploading the training dataset
openai.File.create(
  file=open("spam_prepared_train.jsonl", "rb"),
  purpose='fine-tune'
)


Output:

<File file id=file-jTiNyDdGWLAvjP3RPN6RqumP at 0x7fefd161f150> JSON: {
  "bytes": 9822,
  "created_at": 1685693212,
  "filename": "file",
  "id": "file-jTiNyDdGWLAvjP3RPN6RqumP",
  "object": "file",
  "purpose": "fine-tune",
  "status": "uploaded",
  "status_details": null
}

Python3




# uploading the validation dataset
openai.File.create(
  file=open("spam_prepared_valid.jsonl", "rb"),
  purpose='fine-tune'
)


Output:

<File file id=file-u6qLHlRivL52CuEj7qnnREpS at 0x7fefd0d6ba10> JSON: {
  "bytes": 2211,
  "created_at": 1685693224,
  "filename": "file",
  "id": "file-u6qLHlRivL52CuEj7qnnREpS",
  "object": "file",
  "purpose": "fine-tune",
  "status": "uploaded",
  "status_details": null
}

Note: The Id in the outputs are to be passed in the next step of fine-tuning, so do save it.

Step 3: Now we will fine-tune Davinci model.

Python3




# call the FineTune API endpoint
res = openai.FineTune.create(
  # training file ID
  "training_file" ="file-jTiNyDdGWLAvjP3RPN6RqumP",
    # validation file ID
  "validation_file"="file-u6qLHlRivL52CuEj7qnnREpS",
    # model ID
  "model"= "davinci",
    # number of epochs
  "n_epochs" =10,
  # batch size to process
    "batch_size"= 4,
  # learning rate multiplier
    "learning_rate_multiplier"= 0.133)
  
# storing the job_id of the process
jobID = res["id"]
# storing the status of the process
status = res["status"]
  
print(f'Fine-tunning model with jobID: {jobID}.')
print(f"Training Response: {res}")
print(f"Training Status: {status}")


Output:

Fine-tunning model with jobID: ft-nPNCod70qnSWBXjnQ5kbZXK2.
Training Response: {
  "created_at": 1685693249,
  "events": [
    {
      "created_at": 1685693249,
      "level": "info",
      "message": "Created fine-tune: ft-nPNCod70qnSWBXjnQ5kbZXK2",
      "object": "fine-tune-event"
    }
  ],
  "fine_tuned_model": null,
  "hyperparams": {
    "batch_size": 4,
    "learning_rate_multiplier": 0.133,
    "n_epochs": 10,
    "prompt_loss_weight": 0.01
  },
  "id": "ft-nPNCod70qnSWBXjnQ5kbZXK2",
  "model": "davinci",
  "object": "fine-tune",
  "organization_id": "org-Sb3Fliwt5e4Qvq5kjbBtmpd8",
  "result_files": [],
  "status": "pending",
  "training_files": [
    {
      "bytes": 9822,
      "created_at": 1685693212,
      "filename": "file",
      "id": "file-jTiNyDdGWLAvjP3RPN6RqumP",
      "object": "file",
      "purpose": "fine-tune",
      "status": "processed",
      "status_details": null
    }
  ],
  "updated_at": 1685693249,
  "validation_files": [
    {
      "bytes": 2211,
      "created_at": 1685693224,
      "filename": "file",
      "id": "file-u6qLHlRivL52CuEj7qnnREpS",
      "object": "file",
      "purpose": "fine-tune",
      "status": "processed",
      "status_details": null
    }
  ]
}
Training Status: pending

As you can see the training status is pending, to know the status of your fine-tuning task you can execute the below code in your jupyter notebook.

Python3




import time
# retrieve the status of fine-tuning from OpenAI
status = openai.FineTune.retrieve(id=jobID)["status"]
# check if status is not "succeeded" or "failed" 
if status not in ["succeeded", "failed"]:
  print(f'Job not in terminal status: {status}. Waiting.')
  # loop keeps checking for the status
  while status not in ["succeeded", "failed"]:
    time.sleep(2)
    status = openai.FineTune.retrieve(id=jobID)["status"]
    print(f'Status: {status}')
# if status is "succeeded" or "failed" then we print the status
else:
  print(f'Finetune job {jobID} finished with status: {status}')
# also print all the fine-tuning jobs
print('Checking other finetune jobs in the subscription.')
result = openai.FineTune.list()
print(f'Found {len(result.data)} finetune jobs.')


Output:

Finetune job ft-nPNCod70qnSWBXjnQ5kbZXK2 finished with status: succeeded
Checking other finetune jobs in the subscription.
Found 2 finetune jobs.

Once the fine-tuning process is finished, move to the next step.

Step 4: Retrieve the model

By executing the below code, you can retrieve the model and save it in a variable.

Python3




result = openai.FineTune.retrieve(id=jobID)
fine_tuned_model  = result.fine_tuned_model
print(fine_tuned_model)


Output:

davinci:ft-personal-2023-06-02-08-17-42

Step 5: Use your custom model with any API endpoint.

We are using the Completion API endpoint of OpenAI here to check whether a message is spam or not.

Python3




# text prompt
new_prompt = f"""Classify the following message as spam or not spam:\n\n###Congratulations! 
You've Won a $1000 gift card from walmart. 
Go to https://bit.ly to claim your reward.###\n\nAnswer:"""
# calling the Completion API
answer = openai.Completion.create(
  # model ID collected from previous step is supposed to be passed here
  model=fine_tuned_model,
  # text input is passed here
  prompt=new_prompt
)
# displaying the result
print(answer['choices'][0]['text'])


Output:

Spam

OpenAI Python API – Complete Guide

OpenAI is the leading company in the field of AI. With the public release of software like ChatGPT, DALL-E, GPT-3, and Whisper, the company has taken the entire AI industry by storm. Everyone has incorporated ChatGPT to do their work more efficiently and those who failed to do so have lost their jobs. The age of AI has started and people not adapting to AI could introduce some difficulties for them. 

In this article, we will be discussing how you can leverage the power of AI and make your day-to-day tasks a lot easier by using the OpenAI APIs (Application Programming Interface) that allow developers to easily access their AI models and Integrate them into their own applications using Python.

Table of Content

  • What is OpenAI?
  • What is OpenAI API?
  • Generate OpenAI API key
  • Installation of OpenAI package
  • Prompt Engineering
  • Text
  • Chat
  • Image
  • Audio
  • Embeddings
  • Fine-Tuning
  • API Error Codes
  • Conclusion
  • FAQs on OpenAI Python API

Similar Reads

What is OpenAI?

...

What is OpenAI API?

OpenAI is a Leading Company in the field of Artificial Intelligence(AI). It was originally founded in 2015 by Sam Altman and Elon Musk as a Non-profit Organization. They primarily focus on AI-based Software products Such as ChatGPT 3, ChatGPT 4 and DALL-E etc. They develop next-generation AI products holding incredible capabilities, for example, OpenAIs GPT-3 which is a Content filtering model that allows you to implement advanced text classification, outline, question-answering, and other chatbot applications....

Generate OpenAI API key

OpenAI API is a powerful cloud-based platform, hosted on Microsoft’s Azure, designed to provide developers with seamless access to state-of-the-art, pre-trained artificial intelligence models. This API empowers developers to effortlessly integrate cutting-edge AI capabilities into their applications, regardless of the programming language they choose to work with. By leveraging the OpenAI Python API, developers can unlock advanced AI functionalities and enhance the intelligence and performance of their software solutions....

Installation of OpenAI package

For you to use OpenAI’s models in your Python environment, you must first generate an API key. You can follow the below steps to generate the API key:...

Prompt Engineering

Step 1: Now open a text editor of your choosing or an online notebook like Google Colab or Jupyter Notebook. Here, we’re using a Google Colab notebook to run the command indicated below in order to install the Open AI library in Python....

Text

...

Chat

Giving the AI brain a unique set of instructions to increase its intelligence and responsiveness is what AI prompt engineering entails. To comprehend what we want from AI models like ChatGPT or GPT-4, they need to be gently nudged in the right direction. Prompt engineering can help with it. The finest answers from the AI may be ensured by carefully structuring the prompts. Now, prompt engineering doesn’t only happen once. The process of adjusting and experimenting is continuing. When we ask the AI a question, we experiment with varied wording and the addition of unique rules. We seem to be concocting a miraculous concoction of instructions! Let’s take a look at some rules to construct good prompts to generate accurate results for AI....

Image

For performing any text-specific tasks you can define the following function and execute it with your desired prompts....

Audio

...

Embeddings

...

Fine-Tuning

...

API Error Codes

...

Conclusion

...

OpenAI Python API – FAQs

...

Contact Us