130 lines
4.9 KiB
JavaScript
130 lines
4.9 KiB
JavaScript
/**
|
|
* @file test_volc_embedding.js
|
|
* @description 测试火山方舟 embedding API 连通性(仿写第五步测试脚本)
|
|
* @author 姜维 - 平台总督
|
|
* @date 2026-03-31
|
|
*/
|
|
|
|
const https = require('https');
|
|
|
|
// 配置信息(使用你提供的 API Key)
|
|
const VOLC_CONFIG = {
|
|
baseUrl: "https://ark.cn-beijing.volces.com/api/v3",
|
|
apiKey: "d9aaff82-7fe3-4c8b-a44b-3b4c83c48965",
|
|
model: "doubao-seed-2-0-lite-260215"
|
|
};
|
|
|
|
function testEmbeddingApi() {
|
|
console.log('='.repeat(70));
|
|
console.log('🧪 开始测试火山方舟 Embedding API 连通性');
|
|
console.log('📌 模型: ' + VOLC_CONFIG.model);
|
|
console.log('📌 端点: ' + VOLC_CONFIG.baseUrl);
|
|
console.log('📌 API Key: ' + VOLC_CONFIG.apiKey.slice(0, 8) + '...' + VOLC_CONFIG.apiKey.slice(-4));
|
|
console.log('='.repeat(70));
|
|
console.log('');
|
|
|
|
const url = new URL(VOLC_CONFIG.baseUrl + '/embeddings');
|
|
|
|
const requestBody = {
|
|
model: VOLC_CONFIG.model,
|
|
input: ["Hello world, this is a test sentence for embedding."]
|
|
};
|
|
|
|
const options = {
|
|
hostname: url.hostname,
|
|
port: url.port || 443,
|
|
path: url.pathname,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${VOLC_CONFIG.apiKey}`,
|
|
'Content-Length': Buffer.byteLength(JSON.stringify(requestBody))
|
|
}
|
|
};
|
|
|
|
console.log('🔗 正在建立 HTTPS 连接并发送请求...');
|
|
console.log(`📍 Host: ${options.hostname}`);
|
|
console.log(`📍 Port: ${options.port}`);
|
|
console.log(`📍 Path: ${options.path}`);
|
|
console.log('');
|
|
|
|
const req = https.request(options, (res) => {
|
|
console.log(`✅ 连接已建立,状态码: ${res.statusCode}`);
|
|
console.log(`📋 响应头:`);
|
|
Object.entries(res.headers).forEach(([key, value]) => {
|
|
console.log(` ${key}: ${value}`);
|
|
});
|
|
console.log('');
|
|
|
|
let data = '';
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
console.log('📄 完整响应:');
|
|
console.log('-' .repeat(70));
|
|
try {
|
|
const parsed = JSON.parse(data);
|
|
console.log(JSON.stringify(parsed, null, 2));
|
|
console.log('-' .repeat(70));
|
|
console.log('');
|
|
|
|
if (res.statusCode === 200 && parsed.data && parsed.data.length > 0) {
|
|
console.log('🎉 测试成功!');
|
|
console.log('🔍 结果统计:');
|
|
console.log(` 模型: ${parsed.model}`);
|
|
console.log(` 生成 embedding 数量: ${parsed.data.length}`);
|
|
console.log(` embedding 维度: ${parsed.data[0].embedding.length}`);
|
|
console.log(' 使用 token: ' + parsed.usage.total_tokens);
|
|
console.log('');
|
|
console.log('✅ 总结: API Key 有效,模型已激活,SSL 连接正常,服务可用');
|
|
} else if (res.statusCode === 401) {
|
|
console.log('❌ 认证失败');
|
|
console.log(' API Key 可能无效或者过期');
|
|
} else if (res.statusCode === 404) {
|
|
console.log('❌ 模型不存在 (404)');
|
|
console.log(' 请检查模型 ID 是否正确,以及是否在方舟控制台激活了该模型');
|
|
if (parsed.error && parsed.error.message) {
|
|
console.log(' 错误信息: ' + parsed.error.message);
|
|
}
|
|
} else {
|
|
console.log('⚠️ 收到响应,但状态码不是预期的 200');
|
|
}
|
|
} catch (e) {
|
|
console.log(data);
|
|
console.log('❌ JSON 解析失败: ' + e.message);
|
|
}
|
|
console.log('');
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
console.log('❌ 连接失败:');
|
|
console.log(` 错误: ${e.message}`);
|
|
console.log('');
|
|
console.log('🔍 可能原因分析:');
|
|
if (e.message.includes('SSL')) {
|
|
console.log(' 📛 SSL 证书验证失败 → 这就是 CSDN 文章说的问题');
|
|
console.log(' 💡 解决方案: 设置 NODE_TLS_REJECT_UNAUTHORIZED=0');
|
|
} else if (e.message.includes('ECONNREFUSED')) {
|
|
console.log(' 📛 连接被拒绝 → 服务没启动或者端口错了');
|
|
} else if (e.message.includes('ETIMEDOUT')) {
|
|
console.log(' 📛 连接超时 → 网络不通或者防火墙拦截');
|
|
} else if (e.message.includes('getaddrinfo')) {
|
|
console.log(' 📛 DNS 解析失败 → 域名错了');
|
|
}
|
|
console.log('');
|
|
});
|
|
|
|
req.write(JSON.stringify(requestBody));
|
|
req.end();
|
|
}
|
|
|
|
// 如果直接运行,则执行测试
|
|
if (require.main === module) {
|
|
testEmbeddingApi();
|
|
}
|
|
|
|
module.exports = { testEmbeddingApi };
|