-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_test.go
377 lines (338 loc) · 10.1 KB
/
client_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
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
package openai
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/fabiustech/openai/images"
"github.com/fabiustech/openai/models"
"github.com/fabiustech/openai/objects"
"github.com/fabiustech/openai/params"
)
/*
This test suite has been ported from the original repo: https://github.com/sashabaranov/go-gpt3.
It is incomplete, and it's usefulness is questionable.
TODO: Cover all endpoints.
*/
const (
testToken = "this-is-my-secure-token-do-not-steal!!"
)
func TestAPI(t *testing.T) {
var token, ok = os.LookupEnv("OPENAI_TOKEN")
if !ok {
t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.")
}
var c = NewClient(token)
var ctx = context.Background()
var _, err = c.ListEngines(ctx)
if err != nil {
t.Fatalf("ListEngines error: %v", err)
}
_, err = c.GetEngine(ctx, "davinci")
if err != nil {
t.Fatalf("GetEngine error: %v", err)
}
var fl *List[*File]
fl, err = c.ListFiles(ctx)
if err != nil {
t.Fatalf("ListFiles error: %v", err)
}
if len(fl.Data) > 0 {
_, err = c.RetrieveFile(ctx, fl.Data[0].ID)
if err != nil {
t.Fatalf("RetrieveFile error: %v", err)
}
}
_, err = c.CreateEmbeddings(ctx, &EmbeddingRequest{
Input: []string{
"The food was delicious and the waiter",
"Other examples of embedding request",
},
Model: models.AdaEmbeddingV2,
})
if err != nil {
t.Fatalf("Embedding error: %v", err)
}
}
func newTestClient(u string) (*Client, error) {
var client = NewClient(testToken)
if err := client.SetBaseURL(u + "/v1"); err != nil {
return nil, err
}
return client, nil
}
// TestCompletions Tests the completions endpoint of the API using the mocked server.
func TestCompletions(t *testing.T) {
var ts = OpenAITestServer()
ts.Start()
defer ts.Close()
var client, _ = newTestClient(ts.URL)
var _, err = client.CreateCompletion(context.Background(), &CompletionRequest[models.Completion]{
Prompt: "Lorem ipsum",
Model: models.TextDavinci003,
MaxTokens: 5,
})
if err != nil {
t.Fatalf("CreateCompletion error: %v", err)
}
}
// TestEdits Tests the edits endpoint of the API using the mocked server.
func TestEdits(t *testing.T) {
var ts = OpenAITestServer()
ts.Start()
defer ts.Close()
var client, _ = newTestClient(ts.URL)
var n = 3
var resp, err = client.CreateEdit(context.Background(), &EditsRequest{
Model: models.TextDavinciEdit001,
Input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim" +
" ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip" +
" ex ea commodo consequat. Duis aute irure dolor in reprehe",
Instruction: "test instruction",
N: n,
})
if err != nil {
t.Fatalf("Edits error: %v", err)
}
if len(resp.Choices) != n {
t.Fatalf("edits does not properly return the correct number of choices")
}
}
func TestEmbedding(t *testing.T) {
embeddedModels := []models.Embedding{
models.AdaEmbeddingV2,
}
for _, model := range embeddedModels {
embeddingReq := &EmbeddingRequest{
Input: []string{
"The food was delicious and the waiter",
"Other examples of embedding request",
},
Model: model,
}
// marshal embeddingReq to JSON and confirm that the model field equals
// the AdaSearchQuery type
marshaled, err := json.Marshal(embeddingReq)
if err != nil {
t.Fatalf("Could not marshal embedding request: %v", err)
}
if !bytes.Contains(marshaled, []byte(`"model":"`+model.String()+`"`)) {
t.Fatalf("Expected embedding request to contain model field")
}
}
}
// getEditBody Returns the body of the request to create an edit.
func getEditBody(r *http.Request) (*EditsRequest, error) {
edit := &EditsRequest{}
// read the request body
reqBody, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(reqBody, &edit)
if err != nil {
return nil, err
}
return edit, nil
}
// handleEditEndpoint Handles the edit endpoint by the test server.
func handleEditEndpoint(w http.ResponseWriter, r *http.Request) {
var err error
var resBytes []byte
// edits only accepts POST requests
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
var editReq *EditsRequest
editReq, err = getEditBody(r)
if err != nil {
http.Error(w, "could not read request", http.StatusInternalServerError)
return
}
// create a response
res := &EditsResponse{
Object: objects.Edit,
Created: uint64(time.Now().Unix()),
}
// edit and calculate token usage
editString := "edited by mocked OpenAI server :)"
inputTokens := numTokens(editReq.Input+editReq.Instruction) * editReq.N
completionTokens := int(float32(len(editString))/4) * editReq.N
for i := 0; i < editReq.N; i++ {
// instruction will be hidden and only seen by OpenAI
res.Choices = append(res.Choices, &EditsChoice{
Text: editReq.Input + editString,
Index: i,
})
}
res.Usage = &Usage{
PromptTokens: inputTokens,
CompletionTokens: completionTokens,
TotalTokens: inputTokens + completionTokens,
}
resBytes, _ = json.Marshal(res)
fmt.Fprint(w, string(resBytes))
}
// handleCompletionEndpoint Handles the completion endpoint by the test server.
func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
var err error
var resBytes []byte
// completions only accepts POST requests
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
var completionReq *CompletionRequest[models.Completion]
if completionReq, err = getCompletionBody(r); err != nil {
http.Error(w, "could not read request", http.StatusInternalServerError)
return
}
res := &CompletionResponse[models.Completion]{
ID: strconv.Itoa(int(time.Now().Unix())),
Object: objects.TextCompletion,
Created: uint64(time.Now().Unix()),
// would be nice to validate Model during testing, but
// this may not be possible with how much upkeep
// would be required / wouldn't make much sense
Model: completionReq.Model,
}
// create completions
for i := 0; i < completionReq.N; i++ {
// generate a random string of length completionReq.Length
completionStr := strings.Repeat("a", completionReq.MaxTokens)
if completionReq.Echo {
completionStr = completionReq.Prompt + completionStr
}
res.Choices = append(res.Choices, &CompletionChoice{
Text: completionStr,
Index: i,
})
}
inputTokens := numTokens(completionReq.Prompt) * completionReq.N
completionTokens := completionReq.MaxTokens * completionReq.N
res.Usage = &Usage{
PromptTokens: inputTokens,
CompletionTokens: completionTokens,
TotalTokens: inputTokens + completionTokens,
}
resBytes, _ = json.Marshal(res)
fmt.Fprintln(w, string(resBytes))
}
// handleImageEndpoint Handles the images endpoint by the test server.
func handleImageEndpoint(w http.ResponseWriter, r *http.Request) {
// Images only accepts POST requests.
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
var ir, err = getImageBody(r)
if err != nil {
http.Error(w, "could not read request", http.StatusInternalServerError)
return
}
var resp = &ImageResponse{
Created: uint64(time.Now().Unix()),
}
// Handle default values.
if ir.N == 0 {
ir.N = 1
}
for i := 0; i < ir.N; i++ {
var imageData = &ImageData{}
switch ir.ResponseFormat {
// Invalid is the go default value, and URL is the default API behavior.
case images.FormatURL, images.FormatInvalid:
imageData.URL = params.Optional("https://example.com/image.png")
case images.FormatB64JSON:
// This decodes to "{}" in base64.
imageData.B64JSON = params.Optional("e30K")
default:
http.Error(w, "invalid response format", http.StatusBadRequest)
return
}
resp.Data = append(resp.Data, imageData)
}
var b, _ = json.Marshal(resp)
_, _ = w.Write(b)
}
// getCompletionBody Returns the body of the request to create a completion.
func getCompletionBody(r *http.Request) (*CompletionRequest[models.Completion], error) {
var completion = &CompletionRequest[models.Completion]{}
// read the request body
reqBody, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(reqBody, &completion)
if err != nil {
return nil, err
}
return completion, nil
}
// getImageBody Returns the body of the request to create a image.
func getImageBody(r *http.Request) (*CreateImageRequest, error) {
var image = &CreateImageRequest{}
// read the request body
var reqBody, err = io.ReadAll(r.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(reqBody, &image)
if err != nil {
return nil, err
}
return image, nil
}
// numTokens Returns the number of GPT-3 encoded tokens in the given text.
// This function approximates based on the rule of thumb stated by OpenAI:
// https://beta.com/tokenizer
//
// TODO: implement an actual tokenizer for GPT-3 and Codex (once available)
func numTokens(s string) int {
return int(float32(len(s)) / 4)
}
func TestImages(t *testing.T) {
var ts = OpenAITestServer()
ts.Start()
defer ts.Close()
var client, _ = newTestClient(ts.URL)
var _, err = client.CreateImage(context.Background(), &CreateImageRequest{Prompt: "Lorem ipsum"})
if err != nil {
t.Fatalf("CreateImage error: %v", err)
}
}
// OpenAITestServer Creates a mocked OpenAI server which can pretend to handle requests during testing.
func OpenAITestServer() *httptest.Server {
return httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("received request at path %q\n", r.URL.Path)
// check auth
if r.Header.Get("Authorization") != "Bearer "+testToken {
w.WriteHeader(http.StatusUnauthorized)
return
}
// OPTIMIZE: create separate handler functions for these
switch r.URL.Path {
case "/v1/edits":
handleEditEndpoint(w, r)
return
case "/v1/completions":
handleCompletionEndpoint(w, r)
return
case "/v1/images/generations":
handleImageEndpoint(w, r)
// TODO: Implement the other endpoints.
default:
// the endpoint doesn't exist
http.Error(w, "the resource path doesn't exist", http.StatusNotFound)
return
}
}))
}