-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetuning.py
56 lines (47 loc) · 1.85 KB
/
finetuning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import time
import openai
import argparse
def create_file_for_finetuning(region):
filename = f'finetuning_{region}.jsonl'
file = openai.File.create(file=open(filename, "rb"), purpose='fine-tune')
print(f"File created: {file.id}")
return file.id
def fine_tune_model(file_id):
job = openai.FineTuningJob.create(training_file=file_id, model="gpt-3.5-turbo")
print(f"Fine-tuning job started: {job.id}")
return job.id
def retrieve_and_check_status(job_id):
while True:
job = openai.FineTuningJob.retrieve(job_id)
status = job.status
print(f"Job status: {status}")
if status == 'succeeded':
print(f"Fine-tuned model: {job.fine_tuned_model}")
break
elif status in ['failed', 'cancelled']:
print("An error occurred during fine-tuning.")
if job.error:
print(f"Error message: {job.error['message']}")
else:
print("No error message provided.")
break
time.sleep(60)
def main(region):
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
raise ValueError("The OPENAI_API_KEY environment variable has not been set.")
# Start the fine-tuning process
file_id = create_file_for_finetuning(region)
job_id = fine_tune_model(file_id)
retrieve_and_check_status(job_id)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fine-tune models based on the region.")
parser.add_argument("region", type=str, choices=['usa', 'india'], help="Region to fine-tune the model for (usa/india).")
args = parser.parse_args()
main(args.region)
# USAGE
# First, set your OpenAI API key in the environment variable:
# export OPENAI_API_KEY=your_api_key
# Then run the script for the desired region:
# python finetuning.py usa # or 'india' for the other region