fix(story-summary): unify vector api test actions

This commit is contained in:
2026-04-04 01:06:18 +08:00
parent 485016abdd
commit 37b2f15d05
5 changed files with 105 additions and 30 deletions

View File

@@ -39,6 +39,17 @@ function getL0ApiConfig() {
};
}
function normalizeL0ApiConfig(apiConfig = null) {
const fallback = getL0ApiConfig();
const next = apiConfig || {};
return {
provider: String(next.provider || fallback.provider || 'siliconflow').trim(),
url: String(next.url || fallback.url || DEFAULT_L0_API_URL).trim(),
key: String(next.key || fallback.key || '').trim(),
model: String(next.model || fallback.model || DEFAULT_L0_MODEL).trim(),
};
}
function getNextKey(rawKey) {
const keys = String(rawKey || '')
.split(/[,;|\n]+/)
@@ -60,12 +71,13 @@ export async function callLLM(messages, options = {}) {
temperature = 0.2,
max_tokens = 500,
timeout = 40000,
apiConfig = null,
} = options;
const mod = getStreamingModule();
if (!mod) throw new Error('Streaming module not ready');
const apiCfg = getL0ApiConfig();
const apiCfg = normalizeL0ApiConfig(apiConfig);
const apiKey = getNextKey(apiCfg.key);
if (!apiKey) {
throw new Error('L0 requires siliconflow API key');
@@ -111,6 +123,24 @@ export async function callLLM(messages, options = {}) {
}
}
export async function testL0Service(apiConfig = {}) {
if (!apiConfig?.key) {
throw new Error('请配置 L0 API Key');
}
const result = await callLLM([
{ role: 'system', content: '你是一个测试助手。请只输出 OK。' },
{ role: 'user', content: '只输出 OK' },
], {
apiConfig,
temperature: 0,
max_tokens: 16,
timeout: 15000,
});
const text = String(result || '').trim();
if (!text) throw new Error('返回为空');
return { success: true, message: `连接成功:${text.slice(0, 60)}` };
}
export function cancelAllL0Requests() {
const mod = getStreamingModule();
if (!mod?.cancel) return;

View File

@@ -273,19 +273,51 @@ export async function rerankChunks(query, chunks, options = {}) {
/**
* 测试 Rerank 服务连接
*/
export async function testRerankService() {
const key = getApiKey();
if (!key) {
throw new Error('请配置硅基 API Key');
export async function testRerankService(apiConfig = {}) {
const next = {
provider: String(apiConfig.provider || 'siliconflow').trim(),
url: String(apiConfig.url || DEFAULT_RERANK_URL).trim(),
key: String(apiConfig.key || '').trim(),
model: String(apiConfig.model || RERANK_MODEL).trim(),
};
if (!next.key) {
throw new Error('请配置 Rerank API Key');
}
const key = getNextRerankKey(next.key);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 15000);
try {
const { results } = await rerank('测试查询', ['测试文档1', '测试文档2'], { topN: 2 });
return {
success: true,
message: `连接成功,返回 ${results.length} 个结果`,
const baseUrl = String(next.url || DEFAULT_RERANK_URL).replace(/\/+$/, '');
const response = await fetch(`${baseUrl}/rerank`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: next.model,
query: '测试查询',
documents: ['测试文档1', '测试文档2'],
top_n: 2,
return_documents: false,
}),
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
const errorText = await response.text().catch(() => '');
throw new Error(`Rerank API ${response.status}: ${errorText.slice(0, 200)}`);
}
const data = await response.json();
const results = Array.isArray(data.results) ? data.results : [];
return {
success: true,
message: `连接成功:返回 ${results.length} 个结果`,
};
} catch (e) {
throw new Error(`连接失败: ${e.message}`);
} finally {
clearTimeout(timeoutId);
}
}