-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest-modelscope-api.js
More file actions
166 lines (139 loc) · 5.4 KB
/
Copy pathtest-modelscope-api.js
File metadata and controls
166 lines (139 loc) · 5.4 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
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
#!/usr/bin/env node
// ModelScope API 测试脚本
// 使用环境变量提供 API Key
const API_KEY = process.env.MODELSCOPE_API_KEY || '';
if (!API_KEY) {
console.error('❌ 错误: 未设置 MODELSCOPE_API_KEY 环境变量');
console.error('');
console.error('使用方法:');
console.error(' MODELSCOPE_API_KEY=your-api-key node test-modelscope-api.js');
console.error('');
console.error('或者:');
console.error(' export MODELSCOPE_API_KEY=your-api-key');
console.error(' node test-modelscope-api.js');
process.exit(1);
}
// 模拟一些 Git 提交记录
const testCommits = [
{ hash: '3c305a8', subject: '添加 amd64-v3 优化版本构建支持', author: 'doumao', date: '2 hours ago' },
{ hash: '83c0819', subject: '添加代理检测提示和sudo -E使用说明', author: 'doumao', date: '3 hours ago' },
{ hash: '59d5b01', subject: '优先使用用户设置的代理,而不是加速镜像', author: 'doumao', date: '5 hours ago' },
{ hash: '43af80e', subject: '添加下载前提示:告知用户可使用代理或加速镜像', author: 'doumao', date: '6 hours ago' },
{ hash: 'f9b0ed1', subject: '修复:移除stderr重定向,正确显示下载进度条', author: 'doumao', date: '8 hours ago' }
];
async function testModelScopeAPI() {
console.log('🧪 开始测试 ModelScope API...\n');
console.log(`📝 测试提交记录数量: ${testCommits.length}`);
console.log(`🔑 API Key: ${API_KEY.substring(0, 10)}...${API_KEY.substring(API_KEY.length - 10)}\n`);
try {
console.log('📡 发送 API 请求...');
const response = await fetch('https://api-inference.modelscope.cn/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: 'Qwen/Qwen2.5-Coder-32B-Instruct',
messages: [
{
role: 'system',
content: '你是一个专业的软件版本发布助手,擅长分析 Git 提交记录并生成结构化的版本发布文档。'
},
{
role: 'user',
content: `请分析以下 Git 提交记录,生成结构化的版本发布文档。
提交记录(共 ${testCommits.length} 个):
${testCommits.map(c => `- ${c.subject} (${c.author}, ${c.date})`).join('\n')}
要求:
1. 用中文输出
2. 按照以下格式分类输出:
### ✨ 新增(Added)
- 新增的功能或特性
### 🔧 变更(Changed)
- 行为调整、重构、配置变更等
### 🐛 修复(Fixed)
- Bug 修复、问题解决等
### ⚠️ 废弃(Deprecated)
- 即将废弃的功能(如果有)
### 📝 备注(Notes)
- 重要的使用注意事项或兼容性说明(如果有)
3. **重要**:如果某个分类没有内容,完全省略该分类的标题和内容,不要输出"(无)"
4. 每个要点简洁明了,不超过 30 字
5. 合并相似的提交
6. 突出重要变更
7. 只输出有内容的分类和要点,不要其他内容`
}
],
temperature: 0.7,
max_tokens: 1024,
stream: false
})
});
console.log(`📊 响应状态: ${response.status} ${response.statusText}\n`);
if (!response.ok) {
const errorText = await response.text();
console.error('❌ API 请求失败:');
console.error(` 状态码: ${response.status}`);
console.error(` 错误信息: ${errorText}`);
return false;
}
const data = await response.json();
// 检查响应格式
if (!data.choices || !data.choices[0] || !data.choices[0].message) {
console.error('❌ API 响应格式异常:');
console.error(JSON.stringify(data, null, 2));
return false;
}
const summary = data.choices[0].message.content.trim();
// 清理可能的重复 "- " 前缀
const cleanedSummary = summary.replace(/^- - /gm, '- ');
console.log('✅ API 调用成功!\n');
console.log('📋 AI 生成的总结(原始):');
console.log('─'.repeat(60));
console.log(summary);
console.log('─'.repeat(60));
console.log('');
console.log('📋 AI 生成的总结(清理后):');
console.log('─'.repeat(60));
console.log(cleanedSummary);
console.log('─'.repeat(60));
console.log('');
// 显示 token 使用情况
if (data.usage) {
console.log('📊 Token 使用情况:');
console.log(` 输入 tokens: ${data.usage.prompt_tokens}`);
console.log(` 输出 tokens: ${data.usage.completion_tokens}`);
console.log(` 总计 tokens: ${data.usage.total_tokens}`);
console.log('');
}
// 显示完整响应(用于调试)
console.log('🔍 完整响应数据:');
console.log(JSON.stringify(data, null, 2));
return true;
} catch (error) {
console.error('❌ 测试失败:');
console.error(` 错误类型: ${error.name}`);
console.error(` 错误信息: ${error.message}`);
if (error.stack) {
console.error(` 堆栈跟踪:\n${error.stack}`);
}
return false;
}
}
// 运行测试
testModelScopeAPI()
.then(success => {
console.log('');
if (success) {
console.log('✅ 测试通过!ModelScope API 工作正常。');
process.exit(0);
} else {
console.log('❌ 测试失败!请检查错误信息。');
process.exit(1);
}
})
.catch(error => {
console.error('💥 未捕获的错误:', error);
process.exit(1);
});