-
Notifications
You must be signed in to change notification settings - Fork 1
/
kimi.test.js
177 lines (162 loc) · 5.21 KB
/
kimi.test.js
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
import assert from 'assert';
import path from 'path';
import { readAsSSE } from 'httpx';
import { fileURLToPath } from 'url';
import Kimi from '../lib/kimi.js';
const KIMI_API_KEY = process.env.KIMI_API_KEY;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
describe('kimi', () => {
it('chat should ok', async function () {
this.timeout(60000);
const client = new Kimi({
apiKey: KIMI_API_KEY
});
const response = await client.chat([
{role: 'user', content: 'Hello world'}
], {
model: 'moonshot-v1-128k'
});
let content = '';
for await (const event of readAsSSE(response)) {
if (event.data !== '[DONE]') {
const data = JSON.parse(event.data);
if (data.choices[0].finish_reason !== 'stop') {
content += data.choices[0].delta.content;
}
}
}
assert.ok(content.length > 20);
});
it('chat with tool should ok', async function () {
this.timeout(60000);
const client = new Kimi({
apiKey: KIMI_API_KEY
});
const response = await client.chat([
{role: 'user', content: '3214567是素数吗?'}
], {
model: 'moonshot-v1-128k',
'tools': [
{
'type': 'function',
'function': {
'name': 'CodeRunner',
'description': '代码执行器,支持运行 python 和 javascript 代码',
'parameters': {
'properties': {
'language': {
'type': 'string',
'description': 'python or javascript'
},
'code': {
'type': 'string',
'description': '代码写在这里'
}
},
'type': 'object'
}
}
}
]
});
let toolCall = null;
let content = '';
for await (const event of readAsSSE(response)) {
if (event.data !== '[DONE]') {
const data = JSON.parse(event.data);
const choice = data.choices[0];
if (choice.finish_reason !== 'tool_calls') {
if (choice.delta.content) {
content += choice.delta.content;
}
if (choice.delta.tool_calls) {
if (!toolCall) {
toolCall = choice.delta.tool_calls[0];
} else {
toolCall.function.arguments += choice.delta.tool_calls[0].function.arguments;
}
}
}
}
}
assert.strictEqual(toolCall.index, 0);
assert.strictEqual(toolCall.id, 'CodeRunner:0');
assert.strictEqual(toolCall.type, 'function');
assert.strictEqual(toolCall.function.name, 'CodeRunner');
assert.ok(toolCall.function.arguments.length > 0);
assert.ok(content.length > 20);
});
it('files should ok', async function() {
this.timeout(60000);
const client = new Kimi({
apiKey: KIMI_API_KEY
});
const result = await client.files();
assert.deepStrictEqual(result.object, 'list');
assert.ok(result.data.length >= 0);
result.data.forEach((d) => {
assert.strictEqual(d.status, 'ok');
});
const fileResult = await client.putFile(path.join(__dirname, './figures/1906.08237.pdf'), 'file-extract');
assert.deepStrictEqual(fileResult.bytes, 761790);
assert.strictEqual(fileResult.status, 'ok');
const id = fileResult.id;
const file = await client.getFile(id);
assert.strictEqual(file.bytes, 761790);
const content = await client.getFileContent(id);
assert.strictEqual(content.filename, '1906.08237.pdf');
assert.ok(content.content.length > 0);
const deleteResult = await client.deleteFile(id);
assert.strictEqual(deleteResult.deleted, true);
});
it('files handle error should ok', async () => {
const client = new Kimi({
apiKey: KIMI_API_KEY
});
try {
await client.putFile(path.join(__dirname, './figures/invalid_format.txt'), 'file-extract');
} catch (ex) {
assert.strictEqual(ex.message, 'invalid_request_error: File size is zero, please confirm and re-upload the file');
return;
}
assert.fail('should not to here');
});
it('models should ok', async () => {
const client = new Kimi({
apiKey: KIMI_API_KEY
});
const models = await client.models();
const names = models.data.map((d) => {
return d.id;
}).sort();
assert.deepStrictEqual(names, [
'moonshot-v1-128k',
'moonshot-v1-32k',
'moonshot-v1-8k'
]);
});
it('estimateTokenCount should ok', async () => {
const client = new Kimi({
apiKey: KIMI_API_KEY
});
const results = await client.estimateTokenCount([
{role: 'user', content: 'Hello'}
], {
model: 'moonshot-v1-128k'
});
assert.deepStrictEqual(results, { code: 0, data: { total_tokens: 8 }, scode: '0x0', status: true });
});
it('getBalance should ok', async () => {
const client = new Kimi({
apiKey: KIMI_API_KEY
});
const results = await client.getBalance();
const data = results.data;
assert.ok(data.available_balance >= 0);
assert.ok(data.cash_balance >= 0);
assert.ok(data.voucher_balance >= 0);
assert.strictEqual(results.code, 0);
assert.strictEqual(results.scode, '0x0');
assert.strictEqual(results.status, true);
});
});