-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-kimi-api.js
More file actions
87 lines (74 loc) · 2.31 KB
/
test-kimi-api.js
File metadata and controls
87 lines (74 loc) · 2.31 KB
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
// 测试 Kimi API 集成
const fs = require('fs');
// 配置
const API_KEY = 'sk-Qn0gOlen3Ij0LFOf9IXh4Kq8rKzt3dHWHOajxjDRUUBEt7Ho';
const API_URL = 'https://api.moonshot.cn/v1/chat/completions';
const MODEL = 'moonshot-v1-8k'; // 使用标准模型名称
async function testKimiAPI() {
console.log('=== 测试 Kimi API 连接 ===');
console.log('API URL:', API_URL);
console.log('Model:', MODEL);
console.log('');
const testPrompt = `你是一位技术面试官。请生成一个简单的JavaScript面试题。
返回JSON格式:
{
"question": "面试题内容",
"difficulty": "EASY",
"answer": "参考答案"
}`;
try {
console.log('发送测试请求...');
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: MODEL,
messages: [
{
role: 'system',
content: '你是一位资深的技术面试官。'
},
{
role: 'user',
content: testPrompt
}
],
temperature: 0.7,
max_tokens: 500,
}),
});
console.log('响应状态码:', response.status);
console.log('响应状态文本:', response.statusText);
if (!response.ok) {
const errorText = await response.text();
console.error('API错误响应:', errorText);
return;
}
const data = await response.json();
console.log('\n=== API响应成功 ===');
console.log('使用的模型:', data.model);
console.log('Token使用情况:', data.usage);
const content = data.choices[0].message.content;
console.log('\n=== 生成的内容 ===');
console.log(content);
// 尝试解析JSON
try {
const parsed = JSON.parse(content.replace(/```json\n?|\n?```/g, '').trim());
console.log('\n=== 解析后的JSON ===');
console.log(JSON.stringify(parsed, null, 2));
} catch (e) {
console.log('(内容不是JSON格式,这是正常的)');
}
console.log('\n✅ Kimi API 测试成功!');
} catch (error) {
console.error('\n❌ 测试失败:', error.message);
if (error.response) {
console.error('错误详情:', await error.response.text());
}
}
}
// 运行测试
testKimiAPI();