Spaces:
Runtime error
Runtime error
# !/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Copyright reserved by Yang Hao | |
GPT fine-tuning data upload, fine-tuning task creation, and task status query | |
Authors: yanghao([email protected]) | |
Date: 2023/09/12 19:23:06 | |
""" | |
import os | |
import openai | |
def upData_OpenAI(dataPath,API_Key): | |
try: | |
dataPath=dataPath.name | |
except: | |
pass | |
openai.api_key = API_Key | |
if isinstance(dataPath,list): | |
jsonl=[] | |
for dataFilePath in dataPath: | |
try: | |
dataFilePath = dataFilePath.name | |
except: | |
pass | |
res=openai.File.create( | |
file=open(dataFilePath, "rb"), | |
purpose='fine-tune' | |
) | |
jsonl.append(res) | |
nesJson={"TotalFileState":jsonl} | |
return nesJson | |
else: | |
res = openai.File.create( | |
file=open(dataPath, "rb"), | |
purpose='fine-tune' | |
) | |
return res | |
def createTask(training_file,apikey): | |
openai.apikey=apikey | |
res=openai.FineTuningJob.create(training_file=training_file, model="gpt-3.5-turbo") | |
print(res) | |
return res | |
#print(openai.FineTuningJob.retrieve(taskId)) | |
# List 10 fine-tuning jobs | |
#print(openai.FineTuningJob.list(limit=10)) | |
def GetFineTuningJobState(apiKey): | |
openai.api_key=apiKey | |
res=openai.FineTuningJob.list(limit=20) | |
return res | |
def getStatus(taskId): | |
# Retrieve the state of a fine-tune | |
print(openai.FineTuningJob.retrieve(taskId)) | |
# List 10 fine-tuning jobs | |
print(openai.FineTuningJob.list(limit=20)) | |
# Cancel a job | |
# openai.FineTuningJob.cancel("ft-abc123") | |
# List up to 10 events from a fine-tuning job | |
openai.FineTuningJob.list_events(id=taskId, limit=10) | |
# Delete a fine-tuned model (must be an owner of the org the model was created in) | |
def userFineTuneLLM(systemText,ques,model,openaiapiKey): | |
openai.api_key = openaiapiKey | |
completion = openai.ChatCompletion.create( | |
model=model, | |
messages=[ | |
{"role": "system", "content": systemText}, | |
{"role": "user", "content": ques} | |
] | |
) | |
print(completion['choices'][0]['message']['content']) | |
return completion['choices'][0]['message']['content'] | |
if __name__=="__main__": | |
pass | |