-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat_completion_test.go
100 lines (89 loc) · 2.66 KB
/
chat_completion_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
package zhipu
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestChatCompletionService(t *testing.T) {
client, err := NewClient()
require.NoError(t, err)
s := client.ChatCompletionService("glm-4-flash")
s.AddMessage(ChatCompletionMessage{
Role: RoleUser,
Content: "你好呀",
})
res, err := s.Do(context.Background())
require.NoError(t, err)
require.NotEmpty(t, res.Choices)
choice := res.Choices[0]
require.Equal(t, FinishReasonStop, choice.FinishReason)
require.NotEmpty(t, choice.Message.Content)
}
func TestChatCompletionServiceCharGLM(t *testing.T) {
client, err := NewClient()
require.NoError(t, err)
s := client.ChatCompletionService("charglm-3")
s.SetMeta(
ChatCompletionMeta{
UserName: "啵酱",
UserInfo: "啵酱是小少爷",
BotName: "塞巴斯酱",
BotInfo: "塞巴斯酱是一个冷酷的恶魔管家",
},
).AddMessage(ChatCompletionMessage{
Role: RoleUser,
Content: "早上好",
})
res, err := s.Do(context.Background())
require.NoError(t, err)
require.NotEmpty(t, res.Choices)
choice := res.Choices[0]
require.Equal(t, FinishReasonStop, choice.FinishReason)
require.NotEmpty(t, choice.Message.Content)
}
func TestChatCompletionServiceStream(t *testing.T) {
client, err := NewClient()
require.NoError(t, err)
var content string
s := client.ChatCompletionService("glm-4-flash").AddMessage(ChatCompletionMessage{
Role: RoleUser,
Content: "你好呀",
}).SetStreamHandler(func(chunk ChatCompletionResponse) error {
content += chunk.Choices[0].Delta.Content
return nil
})
res, err := s.Do(context.Background())
require.NoError(t, err)
require.NotEmpty(t, res.Choices)
choice := res.Choices[0]
require.Equal(t, FinishReasonStop, choice.FinishReason)
require.NotEmpty(t, choice.Message.Content)
require.Equal(t, content, choice.Message.Content)
}
func TestChatCompletionServiceVision(t *testing.T) {
client, err := NewClient()
require.NoError(t, err)
s := client.ChatCompletionService("glm-4v")
s.AddMessage(ChatCompletionMultiMessage{
Role: RoleUser,
Content: []ChatCompletionMultiContent{
{
Type: MultiContentTypeText,
Text: "图里有什么",
},
{
Type: MultiContentTypeImageURL,
ImageURL: &ImageURL{
URL: "https://img1.baidu.com/it/u=1369931113,3388870256&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1703696400&t=f3028c7a1dca43a080aeb8239f09cc2f",
},
},
},
})
res, err := s.Do(context.Background())
require.NoError(t, err)
require.NotEmpty(t, res.Choices)
require.NotZero(t, res.Usage.CompletionTokens)
choice := res.Choices[0]
require.Equal(t, FinishReasonStop, choice.FinishReason)
require.NotEmpty(t, choice.Message.Content)
}