forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fine_tunes_test.go
101 lines (86 loc) · 2.33 KB
/
fine_tunes_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
package openai_test
import (
. "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test"
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
)
const testFineTuneID = "fine-tune-id"
// TestFineTunes Tests the fine tunes endpoint of the API using the mocked server.
func TestFineTunes(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler(
"/v1/fine-tunes",
func(w http.ResponseWriter, r *http.Request) {
var resBytes []byte
if r.Method == http.MethodGet {
resBytes, _ = json.Marshal(FineTuneList{})
} else {
resBytes, _ = json.Marshal(FineTune{})
}
fmt.Fprintln(w, string(resBytes))
},
)
server.RegisterHandler(
"/v1/fine-tunes/"+testFineTuneID+"/cancel",
func(w http.ResponseWriter, r *http.Request) {
resBytes, _ := json.Marshal(FineTune{})
fmt.Fprintln(w, string(resBytes))
},
)
server.RegisterHandler(
"/v1/fine-tunes/"+testFineTuneID,
func(w http.ResponseWriter, r *http.Request) {
var resBytes []byte
if r.Method == http.MethodDelete {
resBytes, _ = json.Marshal(FineTuneDeleteResponse{})
} else {
resBytes, _ = json.Marshal(FineTune{})
}
fmt.Fprintln(w, string(resBytes))
},
)
server.RegisterHandler(
"/v1/fine-tunes/"+testFineTuneID+"/events",
func(w http.ResponseWriter, r *http.Request) {
resBytes, _ := json.Marshal(FineTuneEventList{})
fmt.Fprintln(w, string(resBytes))
},
)
// create the test server
var err error
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()
config := DefaultConfig(test.GetTestToken())
config.BaseURL = ts.URL + "/v1"
client := NewClientWithConfig(config)
ctx := context.Background()
_, err = client.ListFineTunes(ctx)
if err != nil {
t.Fatalf("ListFineTunes error: %v", err)
}
_, err = client.CreateFineTune(ctx, FineTuneRequest{})
if err != nil {
t.Fatalf("CreateFineTune error: %v", err)
}
_, err = client.CancelFineTune(ctx, testFineTuneID)
if err != nil {
t.Fatalf("CancelFineTune error: %v", err)
}
_, err = client.GetFineTune(ctx, testFineTuneID)
if err != nil {
t.Fatalf("GetFineTune error: %v", err)
}
_, err = client.DeleteFineTune(ctx, testFineTuneID)
if err != nil {
t.Fatalf("DeleteFineTune error: %v", err)
}
_, err = client.ListFineTuneEvents(ctx, testFineTuneID)
if err != nil {
t.Fatalf("ListFineTuneEvents error: %v", err)
}
}