-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatglm_test.go
167 lines (147 loc) · 5.12 KB
/
chatglm_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
package chatglm
import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
var (
chatglm *Chatglm
modelType string
)
func setup() {
testModelPath, exist := os.LookupEnv("TEST_MODEL")
if !exist {
testModelPath = "chatglm3-ggml-q4_0.bin"
}
var err error
chatglm, err = New(testModelPath)
if err != nil {
panic("load model failed.")
}
modelType = chatglm.ModelType()
}
func TestMain(m *testing.M) {
setup()
code := m.Run()
defer chatglm.Free()
os.Exit(code)
}
func TestGenerate(t *testing.T) {
ret, err := chatglm.Generate("2+2等于多少")
if err != nil {
assert.Fail(t, "generate failed.")
}
assert.Contains(t, ret, "4")
}
func TestStreamGenerate(t *testing.T) {
prompt := "2+2等于多少"
ret, err := chatglm.StreamGenerate(prompt)
if err != nil {
assert.Fail(t, "stream generate failed.")
}
streamOut := chatglm.stream.String()
defer chatglm.stream.Reset()
assert.Contains(t, streamOut, "4")
assert.Contains(t, ret, "4")
}
func TestChat(t *testing.T) {
var messages []*ChatMessage
messages = append(messages, NewUserMsg("2+2等于多少"))
ret, err := chatglm.Chat(messages)
if err != nil {
assert.Fail(t, "first chat failed")
}
assert.Contains(t, ret, "4")
messages = append(messages, NewAssistantMsg(ret, modelType))
messages = append(messages, NewUserMsg("再加4等于多少"))
ret, err = chatglm.Chat(messages)
if err != nil {
assert.Fail(t, "second chat failed")
}
assert.Contains(t, ret, "8")
messages = append(messages, NewAssistantMsg(ret, modelType))
assert.Len(t, messages, 4)
}
func TestChatStream(t *testing.T) {
var messages []*ChatMessage
messages = append(messages, NewUserMsg("2+2等于多少"))
out1 := strings.Builder{}
ret, err := chatglm.StreamChat(messages, SetStreamCallback(func(s string) bool {
out1.WriteString(s)
return true
}))
if err != nil {
assert.Fail(t, "first chat failed")
}
outStr1 := out1.String()
outStr1 = strings.TrimPrefix(outStr1, " ")
outStr1 = strings.TrimPrefix(outStr1, "\n")
assert.Contains(t, ret, "4")
assert.Contains(t, outStr1, "4")
messages = append(messages, NewAssistantMsg(ret, modelType))
messages = append(messages, NewUserMsg("再加4等于多少"))
ret, err = chatglm.StreamChat(messages)
if err != nil {
assert.Fail(t, "second chat failed")
}
out2 := chatglm.stream.String()
out2 = strings.TrimPrefix(out2, " ")
out2 = strings.TrimPrefix(out2, "\n")
assert.Contains(t, ret, "8")
assert.Contains(t, out2, "8")
messages = append(messages, NewAssistantMsg(ret, modelType))
assert.Len(t, messages, 4)
}
func TestEmbedding(t *testing.T) {
maxLength := 1024
embeddings, err := chatglm.Embeddings("你好", SetMaxLength(maxLength))
if err != nil {
assert.Fail(t, "embedding failed.")
}
assert.Len(t, embeddings, maxLength)
}
func TestSystemToolCall(t *testing.T) {
file, err := os.ReadFile("examples/system/function_call.txt")
if err != nil {
return
}
var messages []*ChatMessage
messages = append(messages, NewSystemMsg(string(file)))
messages = append(messages, NewUserMsg("生成一个随机数"))
ret, err := chatglm.Chat(messages, SetDoSample(false))
if err != nil {
assert.Fail(t, "call system tool failed.")
}
assert.Contains(t, ret, "```python\ntool_call(seed=42, range=(0, 100))\n```")
messages = append(messages, NewAssistantMsg(ret, modelType))
messages = append(messages, NewObservationMsg("22"))
ret, err = chatglm.Chat(messages, SetDoSample(false))
if err != nil {
assert.Fail(t, "call system tool failed.")
}
assert.Contains(t, ret, "22")
}
func TestCodeInterpreter(t *testing.T) {
file, err := os.ReadFile("examples/system/code_interpreter.txt")
if err != nil {
return
}
var messages []*ChatMessage
messages = append(messages, NewSystemMsg(string(file)))
messages = append(messages, NewUserMsg("列出100以内的所有质数"))
ret, err := chatglm.Chat(messages, SetDoSample(false))
if err != nil {
assert.Fail(t, "call code interpreter failed.")
}
msg := NewAssistantMsg(ret, modelType)
msg.ToolCalls = append(msg.ToolCalls, &ToolCallMessage{Type: TypeCode, Code: &CodeMessage{Input: "```python\ndef is_prime(n):\n \"\"\"Check if a number is prime.\"\"\"\n if n <= 1:\n return False\n if n <= 3:\n return True\n if n % 2 == 0 or n % 3 == 0:\n return False\n i = 5\n while i * i <= n:\n if n % i == 0 or n % (i + 2) == 0:\n return False\n i += 6\n return True\n\n# Get all prime numbers up to 100\nprimes_upto_100 = [i for i in range(2, 101) if is_prime(i)]\nprimes_upto_100\n```"}})
messages = append(messages, msg)
assert.Contains(t, ret, "好的,我会为您列出100以内的所有质数。\n\n质数是指只能被1和它本身整除的大于1的整数。例如,2、3、5、7等都是质数。\n\n让我们开始吧!")
messages = append(messages, NewObservationMsg("[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]"))
ret, err = chatglm.Chat(messages, SetDoSample(false))
if err != nil {
assert.Fail(t, "call code interpreter failed.")
}
assert.Contains(t, ret, "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97")
}