Train ChatGPT with your own data.

https://platform.openai.com/docs/guides/fine-tuning

OpenAI’s text generation models have been pre-trained on a vast amount of text. To use the models effectively, we include instructions and sometimes several examples in a prompt. Using demonstrations to show how to perform a task is often called “few-shot learning.”

Fine-tuning improves on few-shot learning by training on many more examples than can fit in the prompt, letting you achieve better results on a wide number of tasks. Once a model has been fine-tuned, you won’t need to provide as many examples in the prompt. This saves costs and enables lower-latency requests.

Fine-tuning is currently available for the following models: gpt-3.5-turbo-0125 (recommended), gpt-3.5-turbo-1106, gpt-3.5-turbo-0613, babbage-002, davinci-002, and gpt-4-0613 (experimental).

Preparing your dataset

In this example, our goal is to create a chatbot that occasionally gives sarcastic responses, these are three training examples (conversations) we could create for a dataset:

json
{"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."},
{"role": "user", "content": "What's the capital of France?"}, {"role": "assistant", "content":
"Paris, as if everyone doesn't know that already."}]}{"messages": [{"role": "system", "content":
"Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content":
"Who wrote 'Romeo and Juliet'?"}, {"role": "assistant", "content":
"Oh, just some guy named William Shakespeare. Ever heard of him?"}]}{"messages": [{"role": "system",
"content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content":
"How far is the Moon from Earth?"}, {"role": "assistant", "content":
"Around 384,400 kilometers. Give or take a few, like that really matters."}]}

Convert CSV to json

python
json_string = ""
for index, row in data.iterrows():    message = str(row['Subject']) + "   " +
str(row['Description'])    answer = str(row['Answer'])    prompt =
"You are a formal chat bot assistant."    json_text =
'{"messages": [{"role": "system", "content":"'+prompt
+'"}, {"role": "user", "content":"'+message+'" }, {"role": "assistant", "content":"'+answer+'"} ]}'
json_string = json_string + json_text  + "\n"
python
with open("out.json", "w") as text_file:    text_file.write(json_string)

ImportError: cannot import name ‘OpenAI’ from ‘openai’

Run:
pip install openai — upgrade

This is available only in version openai==1.2.0

Upload a training file

python
from openai
import OpenAIclient = OpenAI()client.files.create(  file=open("mydata.jsonl", "rb"),
purpose="fine-tune")
python
FileObject(id='file-KAdfdfgefd*****', bytes=54530, created_at=1710157205, filename='out.json',
object='file', purpose='fine-tune', status='processed', status_details=None)

Waiting for Queue

python
client.fine_tuning.jobs.list(limit=1)
python
SyncCursorPage[FineTuningJob](data=[FineTuningJob(id='ftjob-zde************', created_at=1710169666,
error=Error(code=None, message=None, param=None, error=None), fine_tuned_model=None,
finished_at=None, hyperparameters=Hyperparameters(n_epochs=3, batch_size=1,
learning_rate_multiplier=2), model='gpt-3.5-turbo-0125', object='fine_tuning.job',
organization_id='org-nUFI**************', result_files=[], status='queued', trained_tokens=None,
training_file='file-TlDij************', validation_file=None, user_provided_suffix=None)],
object='list', has_more=True)

Status Of traning

python
client.fine_tuning.jobs.retrieve("ftjob-zde*********")
python
FineTuningJob(id='ftjob-zde***********', created_at=1710169666, error=Error(code=None, message=None,
param=None, error=None), fine_tuned_model=None, finished_at=None,
hyperparameters=Hyperparameters(n_epochs=3, batch_size=1, learning_rate_multiplier=2),
model='gpt-3.5-turbo-0125', object='fine_tuning.job', organization_id='org-nUFIO0I7*********',
result_files=[], status='queued', trained_tokens=None, training_file='file-TlDi****',
validation_file=None, user_provided_suffix=None)

Check the status

python
res = client.fine_tuning.jobs.retrieve("ftjob-zde*************")
res.status
python
status='queued'
status='running'
status='succeeded'

Use a fine-tuned model

name of model

text
res.fine_tuned_model
python
completion = client.chat.completions.create(  model=res.fine_tuned_model,  messages=[    {"role":
"system", "content": "You are a formal chat bot."},    {"role": "user", "content": "Hello"}
])
print(completion.choices[0].message.content)

Stackademic 🎓

Thank you for reading until the end. Before you go: