|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | +) |
| 9 | + |
| 10 | +type FineTuningJob struct { |
| 11 | + ID string `json:"id"` |
| 12 | + Object string `json:"object"` |
| 13 | + CreatedAt int64 `json:"created_at"` |
| 14 | + FinishedAt int64 `json:"finished_at"` |
| 15 | + Model string `json:"model"` |
| 16 | + FineTunedModel string `json:"fine_tuned_model,omitempty"` |
| 17 | + OrganizationID string `json:"organization_id"` |
| 18 | + Status string `json:"status"` |
| 19 | + Hyperparameters Hyperparameters `json:"hyperparameters"` |
| 20 | + TrainingFile string `json:"training_file"` |
| 21 | + ValidationFile string `json:"validation_file,omitempty"` |
| 22 | + ResultFiles []string `json:"result_files"` |
| 23 | + TrainedTokens int `json:"trained_tokens"` |
| 24 | +} |
| 25 | + |
| 26 | +type Hyperparameters struct { |
| 27 | + Epochs int `json:"n_epochs"` |
| 28 | +} |
| 29 | + |
| 30 | +type FineTuningJobRequest struct { |
| 31 | + TrainingFile string `json:"training_file"` |
| 32 | + ValidationFile string `json:"validation_file,omitempty"` |
| 33 | + Model string `json:"model,omitempty"` |
| 34 | + Hyperparameters *Hyperparameters `json:"hyperparameters,omitempty"` |
| 35 | + Suffix string `json:"suffix,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +type FineTuningJobEventList struct { |
| 39 | + Object string `json:"object"` |
| 40 | + Data []FineTuneEvent `json:"data"` |
| 41 | + HasMore bool `json:"has_more"` |
| 42 | +} |
| 43 | + |
| 44 | +type FineTuningJobEvent struct { |
| 45 | + Object string `json:"object"` |
| 46 | + ID string `json:"id"` |
| 47 | + CreatedAt int `json:"created_at"` |
| 48 | + Level string `json:"level"` |
| 49 | + Message string `json:"message"` |
| 50 | + Data any `json:"data"` |
| 51 | + Type string `json:"type"` |
| 52 | +} |
| 53 | + |
| 54 | +// CreateFineTuningJob create a fine tuning job. |
| 55 | +func (c *Client) CreateFineTuningJob( |
| 56 | + ctx context.Context, |
| 57 | + request FineTuningJobRequest, |
| 58 | +) (response FineTuningJob, err error) { |
| 59 | + urlSuffix := "/fine_tuning/jobs" |
| 60 | + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request)) |
| 61 | + if err != nil { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + err = c.sendRequest(req, &response) |
| 66 | + return |
| 67 | +} |
| 68 | + |
| 69 | +// CancelFineTuningJob cancel a fine tuning job. |
| 70 | +func (c *Client) CancelFineTuningJob(ctx context.Context, fineTuningJobID string) (response FineTuningJob, err error) { |
| 71 | + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine_tuning/jobs/"+fineTuningJobID+"/cancel")) |
| 72 | + if err != nil { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + err = c.sendRequest(req, &response) |
| 77 | + return |
| 78 | +} |
| 79 | + |
| 80 | +// RetrieveFineTuningJob retrieve a fine tuning job. |
| 81 | +func (c *Client) RetrieveFineTuningJob( |
| 82 | + ctx context.Context, |
| 83 | + fineTuningJobID string, |
| 84 | +) (response FineTuningJob, err error) { |
| 85 | + urlSuffix := fmt.Sprintf("/fine_tuning/jobs/%s", fineTuningJobID) |
| 86 | + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) |
| 87 | + if err != nil { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + err = c.sendRequest(req, &response) |
| 92 | + return |
| 93 | +} |
| 94 | + |
| 95 | +type listFineTuningJobEventsParameters struct { |
| 96 | + after *string |
| 97 | + limit *int |
| 98 | +} |
| 99 | + |
| 100 | +type ListFineTuningJobEventsParameter func(*listFineTuningJobEventsParameters) |
| 101 | + |
| 102 | +func ListFineTuningJobEventsWithAfter(after string) ListFineTuningJobEventsParameter { |
| 103 | + return func(args *listFineTuningJobEventsParameters) { |
| 104 | + args.after = &after |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func ListFineTuningJobEventsWithLimit(limit int) ListFineTuningJobEventsParameter { |
| 109 | + return func(args *listFineTuningJobEventsParameters) { |
| 110 | + args.limit = &limit |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// ListFineTuningJobs list fine tuning jobs events. |
| 115 | +func (c *Client) ListFineTuningJobEvents( |
| 116 | + ctx context.Context, |
| 117 | + fineTuningJobID string, |
| 118 | + setters ...ListFineTuningJobEventsParameter, |
| 119 | +) (response FineTuningJobEventList, err error) { |
| 120 | + parameters := &listFineTuningJobEventsParameters{ |
| 121 | + after: nil, |
| 122 | + limit: nil, |
| 123 | + } |
| 124 | + |
| 125 | + for _, setter := range setters { |
| 126 | + setter(parameters) |
| 127 | + } |
| 128 | + |
| 129 | + urlValues := url.Values{} |
| 130 | + if parameters.after != nil { |
| 131 | + urlValues.Add("after", *parameters.after) |
| 132 | + } |
| 133 | + if parameters.limit != nil { |
| 134 | + urlValues.Add("limit", fmt.Sprintf("%d", *parameters.limit)) |
| 135 | + } |
| 136 | + |
| 137 | + encodedValues := "" |
| 138 | + if len(urlValues) > 0 { |
| 139 | + encodedValues = "?" + urlValues.Encode() |
| 140 | + } |
| 141 | + |
| 142 | + req, err := c.newRequest( |
| 143 | + ctx, |
| 144 | + http.MethodGet, |
| 145 | + c.fullURL("/fine_tuning/jobs/"+fineTuningJobID+"/events"+encodedValues), |
| 146 | + ) |
| 147 | + if err != nil { |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + err = c.sendRequest(req, &response) |
| 152 | + return |
| 153 | +} |
0 commit comments