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:
{"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
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"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
from openai
import OpenAIclient = OpenAI()client.files.create( file=open("mydata.jsonl", "rb"),
purpose="fine-tune")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
client.fine_tuning.jobs.list(limit=1)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
client.fine_tuning.jobs.retrieve("ftjob-zde*********")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
res = client.fine_tuning.jobs.retrieve("ftjob-zde*************")
res.statusstatus='queued'
status='running'
status='succeeded'Use a fine-tuned model
name of model
res.fine_tuned_modelcompletion = 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:
- Please consider clapping and following the writer! 👏
- Follow us X | LinkedIn | YouTube | Discord
- Visit our other platforms: In Plain English | CoFeed | Venture | Cubed
- More content at Stackademic.com
