From a142039b98a335b5f76e90a7eddeb8799b9083e7 Mon Sep 17 00:00:00 2001 From: Zach Koch Date: Thu, 11 Jan 2024 11:32:42 -0500 Subject: [PATCH] match new openai sdk changs --- openai_finetune.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/openai_finetune.py b/openai_finetune.py index 58751bd..f746ed6 100644 --- a/openai_finetune.py +++ b/openai_finetune.py @@ -2,43 +2,48 @@ import time import openai +from openai import OpenAI -openai.api_key = os.getenv("OPENAI_API_KEY") -training_file = "/Users/juberti/Downloads/pirate_tune.jsonl" -training_file_response = openai.File.create( +client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) + +training_file = "/Users/zachkoch/Downloads/GPT3_5_complete_fine_tuning_examples.jsonl" +training_file_response = client.files.create( file=open(training_file, "rb"), purpose="fine-tune" ) -training_file_id = training_file_response["id"] +training_file_id = training_file_response.id print(f"Training file uploaded with ID: {training_file_id}") -fine_tuning_job = openai.FineTuningJob.create( - training_file=training_file_id, model="gpt-3.5-turbo" +fine_tuning_job = client.fine_tuning.jobs.create( + training_file=training_file_id, model="gpt-3.5-turbo-1106" ) -job_id = fine_tuning_job["id"] +job_id = fine_tuning_job.id print(f"Fine-tuning job created with ID: {job_id}") while True: try: - fine_tuning_status = openai.FineTune.retrieve(job_id) - except openai.error.InvalidRequestError as e: + fine_tuning_status = client.fine_tuning.jobs.retrieve(job_id) + except openai.InvalidRequestError as e: print(e) time.sleep(1) continue - status = fine_tuning_status["status"] + status = fine_tuning_status.status print(f"Fine-tuning job status: {status}") - if status in ["completed", "failed"]: + if status in ["succeeded", "failed"]: break time.sleep(60) -fine_tuned_model_id = fine_tuning_status["fine_tuned_model_id"] +fine_tuned_model_id = fine_tuning_status.fine_tuned_model -completion = openai.ChatCompletion.create( +completion = client.chat.completions.create( model=fine_tuned_model_id, messages=[ - {"role": "system", "content": "You are a helpful assistant."}, - {"role": "user", "content": "Hello!"}, + { + "role": "system", + "content": "You are a helpful assistant.", + }, + {"role": "user", "content": "What is 1/2 + 1/4?"}, ], ) print(completion.choices[0].message)