File size: 2,289 Bytes
3b45075
 
 
 
 
 
 
 
 
 
6d5f959
7b903ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# !/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