-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.js
More file actions
57 lines (49 loc) · 2.09 KB
/
Copy pathtest-api.js
File metadata and controls
57 lines (49 loc) · 2.09 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
// 测试 DeepSeek API 连接的脚本
require('dotenv').config({ path: '.env.local' });
const apiKey = process.env.DEEPSEEK_API_KEY;
console.log('API Key 状态:', apiKey ? '已配置' : '未配置');
console.log('API Key 长度:', apiKey ? apiKey.length : 0);
console.log('API Key 前缀:', apiKey ? apiKey.substring(0, 10) + '...' : '无');
if (apiKey === 'your_deepseek_api_key_here') {
console.log('\n❌ 错误: API Key 仍然是占位符,请替换为真实的 DeepSeek API Key');
console.log('请编辑 .env.local 文件,将 DEEPSEEK_API_KEY 的值替换为您的真实 API key');
} else if (!apiKey) {
console.log('\n❌ 错误: 未找到 DEEPSEEK_API_KEY 环境变量');
} else if (apiKey.length < 20) {
console.log('\n⚠️ 警告: API Key 长度似乎不正确,请检查是否完整');
} else {
console.log('\n✅ API Key 配置看起来正确');
// 尝试测试 API 连接
testApiConnection();
}
async function testApiConnection() {
try {
console.log('\n正在测试 DeepSeek API 连接...');
const response = await fetch('https://api.deepseek.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [
{ role: 'user', content: '你好' }
],
max_tokens: 10
})
});
console.log('API 响应状态:', response.status);
if (response.ok) {
const data = await response.json();
console.log('✅ API 连接成功!');
console.log('响应内容:', data.choices?.[0]?.message?.content || '无内容');
} else {
const errorData = await response.json();
console.log('❌ API 连接失败');
console.log('错误详情:', errorData);
}
} catch (error) {
console.log('❌ API 连接测试出错:', error.message);
}
}