-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
54 lines (40 loc) · 1.54 KB
/
train.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
import openai
import time
import logging
import config
openai.api_key = config.DevelopmentConfig.OPENAI_KEY
def configure_logging():
"""
Configures logging settings.
"""
logging.basicConfig(filename='output.log', level=logging.INFO,
format='%(asctime)s [%(levelname)s]: %(message)s')
return logging.getLogger()
def upload_file(file_name):
"""
Uploads a file to OpenAI for fine-tuning.
:param file_name: Path to the file to be uploaded.
:return: Uploaded file object.
"""
# Note: For a 400KB train_file, it takes about 1 minute to upload.
file_upload = openai.File.create(file=open(file_name, "rb"), purpose="fine-tune")
logger.info(f"Uploaded file with id: {file_upload.id}")
while True:
logger.info("Waiting for file to process...")
file_handle = openai.File.retrieve(id=file_upload.id)
if len(file_handle) and file_handle.status == "processed":
logger.info("File processed")
break
time.sleep(60)
return file_upload
if __name__ == '__main__':
# Configure logger
logger = configure_logging()
file_name = "dataset.jsonl"
uploaded_file = upload_file(file_name)
logger.info(uploaded_file)
job = openai.FineTuningJob.create(training_file=uploaded_file.id, model="gpt-3.5-turbo")
logger.info(f"Job created with id: {job.id}")
# Note: If you forget the job id, you can use the following code to list all the models fine-tuned.
# result = openai.FineTuningJob.list(limit=10)
# print(result)