-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fine_tune.go
456 lines (392 loc) · 11.1 KB
/
fine_tune.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package zhipu
import (
"context"
"strconv"
"github.com/go-resty/resty/v2"
)
const (
HyperParameterAuto = "auto"
FineTuneStatusCreate = "create"
FineTuneStatusValidatingFiles = "validating_files"
FineTuneStatusQueued = "queued"
FineTuneStatusRunning = "running"
FineTuneStatusSucceeded = "succeeded"
FineTuneStatusFailed = "failed"
FineTuneStatusCancelled = "cancelled"
)
// FineTuneItem is the item of the FineTune
type FineTuneItem struct {
ID string `json:"id"`
RequestID string `json:"request_id"`
FineTunedModel string `json:"fine_tuned_model"`
Status string `json:"status"`
Object string `json:"object"`
TrainingFile string `json:"training_file"`
ValidationFile string `json:"validation_file"`
Error APIError `json:"error"`
}
// FineTuneCreateService creates a new fine tune
type FineTuneCreateService struct {
client *Client
model string
trainingFile string
validationFile *string
learningRateMultiplier *StringOr[float64]
batchSize *StringOr[int]
nEpochs *StringOr[int]
suffix *string
requestID *string
}
// FineTuneCreateResponse is the response of the FineTuneCreateService
type FineTuneCreateResponse = FineTuneItem
// NewFineTuneCreateService creates a new FineTuneCreateService
func NewFineTuneCreateService(client *Client) *FineTuneCreateService {
return &FineTuneCreateService{
client: client,
}
}
// SetModel sets the model parameter
func (s *FineTuneCreateService) SetModel(model string) *FineTuneCreateService {
s.model = model
return s
}
// SetTrainingFile sets the trainingFile parameter
func (s *FineTuneCreateService) SetTrainingFile(trainingFile string) *FineTuneCreateService {
s.trainingFile = trainingFile
return s
}
// SetValidationFile sets the validationFile parameter
func (s *FineTuneCreateService) SetValidationFile(validationFile string) *FineTuneCreateService {
s.validationFile = &validationFile
return s
}
// SetLearningRateMultiplier sets the learningRateMultiplier parameter
func (s *FineTuneCreateService) SetLearningRateMultiplier(learningRateMultiplier float64) *FineTuneCreateService {
s.learningRateMultiplier = &StringOr[float64]{}
s.learningRateMultiplier.SetValue(learningRateMultiplier)
return s
}
// SetLearningRateMultiplierAuto sets the learningRateMultiplier parameter to auto
func (s *FineTuneCreateService) SetLearningRateMultiplierAuto() *FineTuneCreateService {
s.learningRateMultiplier = &StringOr[float64]{}
s.learningRateMultiplier.SetString(HyperParameterAuto)
return s
}
// SetBatchSize sets the batchSize parameter
func (s *FineTuneCreateService) SetBatchSize(batchSize int) *FineTuneCreateService {
s.batchSize = &StringOr[int]{}
s.batchSize.SetValue(batchSize)
return s
}
// SetBatchSizeAuto sets the batchSize parameter to auto
func (s *FineTuneCreateService) SetBatchSizeAuto() *FineTuneCreateService {
s.batchSize = &StringOr[int]{}
s.batchSize.SetString(HyperParameterAuto)
return s
}
// SetNEpochs sets the nEpochs parameter
func (s *FineTuneCreateService) SetNEpochs(nEpochs int) *FineTuneCreateService {
s.nEpochs = &StringOr[int]{}
s.nEpochs.SetValue(nEpochs)
return s
}
// SetNEpochsAuto sets the nEpochs parameter to auto
func (s *FineTuneCreateService) SetNEpochsAuto() *FineTuneCreateService {
s.nEpochs = &StringOr[int]{}
s.nEpochs.SetString(HyperParameterAuto)
return s
}
// SetSuffix sets the suffix parameter
func (s *FineTuneCreateService) SetSuffix(suffix string) *FineTuneCreateService {
s.suffix = &suffix
return s
}
// SetRequestID sets the requestID parameter
func (s *FineTuneCreateService) SetRequestID(requestID string) *FineTuneCreateService {
s.requestID = &requestID
return s
}
// Do makes the request
func (s *FineTuneCreateService) Do(ctx context.Context) (res FineTuneCreateResponse, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
body := M{
"model": s.model,
"training_file": s.trainingFile,
}
if s.validationFile != nil {
body["validation_file"] = *s.validationFile
}
if s.suffix != nil {
body["suffix"] = *s.suffix
}
if s.requestID != nil {
body["request_id"] = *s.requestID
}
if s.learningRateMultiplier != nil || s.batchSize != nil || s.nEpochs != nil {
hp := M{}
if s.learningRateMultiplier != nil {
hp["learning_rate_multiplier"] = s.learningRateMultiplier
}
if s.batchSize != nil {
hp["batch_size"] = s.batchSize
}
if s.nEpochs != nil {
hp["n_epochs"] = s.nEpochs
}
body["hyperparameters"] = hp
}
if resp, err = s.client.request(ctx).
SetBody(body).
SetResult(&res).
SetError(&apiError).
Post("fine_tuning/jobs"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FineTuneEventListService creates a new fine tune event list
type FineTuneEventListService struct {
client *Client
jobID string
limit *int
after *string
}
// FineTuneEventData is the data of the FineTuneEventItem
type FineTuneEventData struct {
Acc float64 `json:"acc"`
Loss float64 `json:"loss"`
CurrentSteps int64 `json:"current_steps"`
RemainingTime string `json:"remaining_time"`
ElapsedTime string `json:"elapsed_time"`
TotalSteps int64 `json:"total_steps"`
Epoch int64 `json:"epoch"`
TrainedTokens int64 `json:"trained_tokens"`
LearningRate float64 `json:"learning_rate"`
}
// FineTuneEventItem is the item of the FineTuneEventListResponse
type FineTuneEventItem struct {
ID string `json:"id"`
Type string `json:"type"`
Level string `json:"level"`
Message string `json:"message"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
Data FineTuneEventData `json:"data"`
}
// FineTuneEventListResponse is the response of the FineTuneEventListService
type FineTuneEventListResponse struct {
Data []FineTuneEventItem `json:"data"`
HasMore bool `json:"has_more"`
Object string `json:"object"`
}
// NewFineTuneEventListService creates a new FineTuneEventListService
func NewFineTuneEventListService(client *Client) *FineTuneEventListService {
return &FineTuneEventListService{
client: client,
}
}
// SetJobID sets the jobID parameter
func (s *FineTuneEventListService) SetJobID(jobID string) *FineTuneEventListService {
s.jobID = jobID
return s
}
// SetLimit sets the limit parameter
func (s *FineTuneEventListService) SetLimit(limit int) *FineTuneEventListService {
s.limit = &limit
return s
}
// SetAfter sets the after parameter
func (s *FineTuneEventListService) SetAfter(after string) *FineTuneEventListService {
s.after = &after
return s
}
// Do makes the request
func (s *FineTuneEventListService) Do(ctx context.Context) (res FineTuneEventListResponse, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
req := s.client.request(ctx)
if s.limit != nil {
req.SetQueryParam("limit", strconv.Itoa(*s.limit))
}
if s.after != nil {
req.SetQueryParam("after", *s.after)
}
if resp, err = req.
SetPathParam("job_id", s.jobID).
SetResult(&res).
SetError(&apiError).
Get("fine_tuning/jobs/{job_id}/events"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FineTuneGetService creates a new fine tune get
type FineTuneGetService struct {
client *Client
jobID string
}
// NewFineTuneGetService creates a new FineTuneGetService
func NewFineTuneGetService(client *Client) *FineTuneGetService {
return &FineTuneGetService{
client: client,
}
}
// SetJobID sets the jobID parameter
func (s *FineTuneGetService) SetJobID(jobID string) *FineTuneGetService {
s.jobID = jobID
return s
}
// Do makes the request
func (s *FineTuneGetService) Do(ctx context.Context) (res FineTuneItem, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
if resp, err = s.client.request(ctx).
SetPathParam("job_id", s.jobID).
SetResult(&res).
SetError(&apiError).
Get("fine_tuning/jobs/{job_id}"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FineTuneListService creates a new fine tune list
type FineTuneListService struct {
client *Client
limit *int
after *string
}
// FineTuneListResponse is the response of the FineTuneListService
type FineTuneListResponse struct {
Data []FineTuneItem `json:"data"`
Object string `json:"object"`
}
// NewFineTuneListService creates a new FineTuneListService
func NewFineTuneListService(client *Client) *FineTuneListService {
return &FineTuneListService{
client: client,
}
}
// SetLimit sets the limit parameter
func (s *FineTuneListService) SetLimit(limit int) *FineTuneListService {
s.limit = &limit
return s
}
// SetAfter sets the after parameter
func (s *FineTuneListService) SetAfter(after string) *FineTuneListService {
s.after = &after
return s
}
// Do makes the request
func (s *FineTuneListService) Do(ctx context.Context) (res FineTuneListResponse, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
req := s.client.request(ctx)
if s.limit != nil {
req.SetQueryParam("limit", strconv.Itoa(*s.limit))
}
if s.after != nil {
req.SetQueryParam("after", *s.after)
}
if resp, err = req.
SetResult(&res).
SetError(&apiError).
Get("fine_tuning/jobs"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FineTuneDeleteService creates a new fine tune delete
type FineTuneDeleteService struct {
client *Client
jobID string
}
// NewFineTuneDeleteService creates a new FineTuneDeleteService
func NewFineTuneDeleteService(client *Client) *FineTuneDeleteService {
return &FineTuneDeleteService{
client: client,
}
}
// SetJobID sets the jobID parameter
func (s *FineTuneDeleteService) SetJobID(jobID string) *FineTuneDeleteService {
s.jobID = jobID
return s
}
// Do makes the request
func (s *FineTuneDeleteService) Do(ctx context.Context) (res FineTuneItem, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
if resp, err = s.client.request(ctx).
SetPathParam("job_id", s.jobID).
SetResult(&res).
SetError(&apiError).
Delete("fine_tuning/jobs/{job_id}"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FineTuneCancelService creates a new fine tune cancel
type FineTuneCancelService struct {
client *Client
jobID string
}
// NewFineTuneCancelService creates a new FineTuneCancelService
func NewFineTuneCancelService(client *Client) *FineTuneCancelService {
return &FineTuneCancelService{
client: client,
}
}
// SetJobID sets the jobID parameter
func (s *FineTuneCancelService) SetJobID(jobID string) *FineTuneCancelService {
s.jobID = jobID
return s
}
// Do makes the request
func (s *FineTuneCancelService) Do(ctx context.Context) (res FineTuneItem, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
if resp, err = s.client.request(ctx).
SetPathParam("job_id", s.jobID).
SetResult(&res).
SetError(&apiError).
Post("fine_tuning/jobs/{job_id}/cancel"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}