forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
65 lines (56 loc) · 1.37 KB
/
api_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
package gogpt
import (
"context"
"io/ioutil"
"testing"
)
func TestAPI(t *testing.T) {
tokenBytes, err := ioutil.ReadFile(".openai-token")
if err != nil {
t.Fatalf("Could not load auth token from .openai-token file")
}
c := NewClient(string(tokenBytes))
ctx := context.Background()
_, 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)
}
fileRes, err := c.ListFiles(ctx)
if err != nil {
t.Fatalf("ListFiles error: %v", err)
}
if len(fileRes.Files) > 0 {
_, err = c.GetFile(ctx, fileRes.Files[0].ID)
if err != nil {
t.Fatalf("GetFile error: %v", err)
}
} // else skip
req := CompletionRequest{MaxTokens: 5}
req.Prompt = "Lorem ipsum"
_, err = c.CreateCompletion(ctx, "ada", req)
if err != nil {
t.Fatalf("CreateCompletion error: %v", err)
}
searchReq := SearchRequest{
Documents: []string{"White House", "hospital", "school"},
Query: "the president",
}
_, err = c.Search(ctx, "ada", searchReq)
if err != nil {
t.Fatalf("Search error: %v", err)
}
embeddingReq := EmbeddingRequest{
Input: []string{
"The food was delicious and the waiter",
"Other examples of embedding request",
},
}
_, err = c.CreateEmbeddings(ctx, embeddingReq, AdaSearchQuery)
if err != nil {
t.Fatalf("Embedding error: %v", err)
}
}