...

How to Fine-Tune ChatGPT (gpt-3.5-turbo) Using the OpenAI API in Python

A Symphony of Possibilities: Fine-Tuning Unleashed

Since the launch of GPT-3.5 Turbo, the AI community has been drumming up a storm, craving the ability to mold and sculpt models for a truly unique user experience. Brace yourselves, because that dream has come true. Developers now have the reins to conduct supervised fine-tuning, refining the model to absolute precision for their individual use cases.

How to Fine-Tune gpt-3.5-turbo in Python

Step 1: Prepare your data

{
  "messages": [
    {
      "role": "system",
      "content": "You are an assistant that occasionally misspells words"
    },
    {
      "role": "user",
      "content": "Tell me a story."
    },
    {
      "role": "assistant",
      "content": "One day a student went to schoool."
    }
  ]
}

Note: This should be one line in your JSONL file.

The system message provides the system prompt. This tells the model how to respond. For example, the web version of ChatGPT’s system prompt is: “You are a helpful assistant”.

!pip install -U openai
import openai
openai.api_key = "YOUR_OPENAI_API_KEY"
openai.File.create(
  file=open('/path/to/your/data.jsonl'),
  purpose='fine-tune',
)
openai.File.retrieve('your_file_id')
openai.FineTuningJob.create(
  training_file='your_file_id',
  model='gpt-3.5-turbo',
)
openai.FineTuningJob.retrieve('ftjob-xxxxxxxx')
openai.FineTuningJob.list_events(id='ftjob-xxxxx', limit=10)

Step 4: Use your fine-tuned model

You can now test your model. A good idea would be to compare it to the non-fine-tuned GPT-3.5 Turbo, which can be done as follows:

completion = openai.ChatCompletion.create(
  model='gpt-3.5-turbo',
  messages=[
    {"role": "system", "content": "You are Zordon, leader of the Power Rangers."},
    {"role" "user", "content": "Zordon, the Red Ranger has been captured! What do we do?"}
  ]
)
print(completion.choices[0].message)
completion = openai.ChatCompletion.create(
  model='ft:gpt-3.5-turbo-xxxx:<your_username>::<some_id>', # your model id
  messages=[
    {"role": "system", "content": "You are Zordon, leader of the Power Rangers."},
    {"role" "user", "content": "Zordon, the Red Ranger has been captured! What do we do?"}
  ]
)
print(completion.choices[0].message)