forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fine_tuning_job_test.go
104 lines (89 loc) · 2.94 KB
/
fine_tuning_job_test.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
package openai_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/rekki/go-openai"
"github.com/rekki/go-openai/internal/test/checks"
)
const testFineTuninigJobID = "fine-tuning-job-id"
// TestFineTuningJob Tests the fine tuning job endpoint of the API using the mocked server.
func TestFineTuningJob(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler(
"/v1/fine_tuning/jobs",
func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.FineTuningJob{
Object: "fine_tuning.job",
ID: testFineTuninigJobID,
Model: "davinci-002",
CreatedAt: 1692661014,
FinishedAt: 1692661190,
FineTunedModel: "ft:davinci-002:my-org:custom_suffix:7q8mpxmy",
OrganizationID: "org-123",
ResultFiles: []string{"file-abc123"},
Status: "succeeded",
ValidationFile: "",
TrainingFile: "file-abc123",
Hyperparameters: openai.Hyperparameters{
Epochs: "auto",
},
TrainedTokens: 5768,
})
fmt.Fprintln(w, string(resBytes))
},
)
server.RegisterHandler(
"/v1/fine_tuning/jobs/"+testFineTuninigJobID+"/cancel",
func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.FineTuningJob{})
fmt.Fprintln(w, string(resBytes))
},
)
server.RegisterHandler(
"/v1/fine_tuning/jobs/"+testFineTuninigJobID,
func(w http.ResponseWriter, _ *http.Request) {
var resBytes []byte
resBytes, _ = json.Marshal(openai.FineTuningJob{})
fmt.Fprintln(w, string(resBytes))
},
)
server.RegisterHandler(
"/v1/fine_tuning/jobs/"+testFineTuninigJobID+"/events",
func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.FineTuningJobEventList{})
fmt.Fprintln(w, string(resBytes))
},
)
ctx := context.Background()
_, err := client.CreateFineTuningJob(ctx, openai.FineTuningJobRequest{})
checks.NoError(t, err, "CreateFineTuningJob error")
_, err = client.CancelFineTuningJob(ctx, testFineTuninigJobID)
checks.NoError(t, err, "CancelFineTuningJob error")
_, err = client.RetrieveFineTuningJob(ctx, testFineTuninigJobID)
checks.NoError(t, err, "RetrieveFineTuningJob error")
_, err = client.ListFineTuningJobEvents(ctx, testFineTuninigJobID)
checks.NoError(t, err, "ListFineTuningJobEvents error")
_, err = client.ListFineTuningJobEvents(
ctx,
testFineTuninigJobID,
openai.ListFineTuningJobEventsWithAfter("last-event-id"),
)
checks.NoError(t, err, "ListFineTuningJobEvents error")
_, err = client.ListFineTuningJobEvents(
ctx,
testFineTuninigJobID,
openai.ListFineTuningJobEventsWithLimit(10),
)
checks.NoError(t, err, "ListFineTuningJobEvents error")
_, err = client.ListFineTuningJobEvents(
ctx,
testFineTuninigJobID,
openai.ListFineTuningJobEventsWithAfter("last-event-id"),
openai.ListFineTuningJobEventsWithLimit(10),
)
checks.NoError(t, err, "ListFineTuningJobEvents error")
}