Fine-Tuning chatGPT model
My thoughts on fine-tuning chatGPT model.
I just tried to fine-tune a model on my own dataset. Compared to traditional fine-tuning, this process is very simple. You basically just need to specify your dataset in a specific format, following this structure:
{
"messages": [
{"role": "system", "content": "Your system message here."},
{"role": "user", "content": "Your user message here."},
{"role": "assistant", "content": "Your assistant response here."}
]
}
Each datapoint represents a conversation. The first message is from the system, the second is from the user, and the third is from the assistant. You can add as many messages as you like, but the structure must follow this format.
Uploading the dataset to OpenAI
Once you have the dataset in place, you will need to upload the dataset to OpenAI. You can do this by running the following code:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.File.create(
file=open("mydata.jsonl", "rb"),
purpose='fine-tune'
)
Starting the fine-tuning process
Once you've prepared your dataset, you'll need to upload it to OpenAI. You can accomplish this by executing the following code:
openai.FineTuningJob.create(training_file="file-abc123", model="gpt-3.5-turbo")
Here is a sample script for uploading the dataset and starting the fine-tuning process once the dataset is uploaded:
# Upload the JSON file to OpenAI
response = openai.File.create(
file=open("formatted_data.json", "rb"),
purpose='fine-tune'
)
file_id = response.id
print(f"File uploaded to OpenAI with ID: {file_id}")
# Poll the file status until it's ready
while True:
file_status = openai.File.retrieve(file_id).status
if file_status == "processed":
print(f"File {file_id} is ready for fine-tuning!")
break
elif file_status == "error":
print(f"An error occurred while processing file {file_id}. Exiting.")
exit(1) # Exit the script with an error code
else:
print(f"File {file_id} is still being processed. Waiting for 30 seconds before checking again.")
time.sleep(30) # Wait for 30 seconds before checking again
# Start a fine-tuning job
fine_tuning_response = openai.FineTuningJob.create(
training_file=file_id,
model="gpt-3.5-turbo"
)
print(f"Fine-tuning job started with ID: {fine_tuning_response.id}")
Conclusion
Overall the process is very simple and straightforward.
There are more information on the OpenAI API page.
© Magnus Friberg.RSS