generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_test.go
70 lines (54 loc) · 1.55 KB
/
openai_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
package openai_test
import (
"strings"
"testing"
"maragu.dev/env"
"maragu.dev/gai"
"maragu.dev/is"
openai "maragu.dev/gai-openai"
)
func TestNewClient(t *testing.T) {
t.Run("can create a new client with a key", func(t *testing.T) {
client := openai.NewClient(openai.NewClientOptions{Key: "123"})
is.NotNil(t, client)
})
}
func TestClient_ChatComplete(t *testing.T) {
t.Run("can send a streaming chat completion request", func(t *testing.T) {
c := newClient()
cc := c.NewChatCompleter(openai.NewChatCompleterOptions{Model: openai.ChatCompleteModelGPT4oMini})
req := gai.ChatCompleteRequest{
Messages: []gai.Message{
gai.NewUserTextMessage("Hi!"),
},
Temperature: gai.Ptr(gai.Temperature(0)),
}
res, err := cc.ChatComplete(t.Context(), req)
is.NotError(t, err)
var output string
for part, err := range res.Parts() {
is.NotError(t, err)
output += part.Text()
}
is.Equal(t, "Hello! How can I assist you today?", output)
})
}
func TestClient_Embed(t *testing.T) {
t.Run("can create a text embedding", func(t *testing.T) {
c := newClient()
e := c.NewEmbedder(openai.NewEmbedderOptions{
Model: openai.EmbedModelTextEmbedding3Small,
Dimensions: 1536,
})
req := gai.EmbedRequest{
Input: strings.NewReader("Embed this, please."),
}
res, err := e.Embed(t.Context(), req)
is.NotError(t, err)
is.Equal(t, 1536, len(res.Embedding))
})
}
func newClient() *openai.Client {
_ = env.Load(".env.test.local")
return openai.NewClient(openai.NewClientOptions{Key: env.GetStringOrDefault("OPENAI_KEY", "")})
}