feat: refactor API key handling to shuffle keys and attempt each once

This commit is contained in:
2026-05-12 08:17:49 +00:00
parent e28c6ea5a3
commit a6df5c4f43
4 changed files with 21 additions and 20 deletions
+5 -4
View File
@@ -10,19 +10,20 @@ export async function chat(systemPrompt, userContent) {
const headers = { 'Content-Type': 'application/json' };
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
const shuffled = [...apiKeys].sort(() => Math.random() - 0.5);
let lastError;
for (let i = 0; i < apiKeys.length; i++) {
if (provider !== 'ollama') headers['Authorization'] = `Bearer ${apiKeys[i]}`;
for (let i = 0; i < shuffled.length; i++) {
if (provider !== 'ollama') headers['Authorization'] = `Bearer ${shuffled[i]}`;
try {
const resp = await axios.post(
`${baseURL.replace(/\/$/, '')}/chat/completions`,
{ model, messages: [{ role: 'system', content: systemPrompt }, { role: 'user', content: userContent }], temperature: 0.2 },
{ headers, timeout: 30000 }
{ headers }
);
return resp.data.choices[0].message.content;
} catch (e) {
lastError = e;
console.log(` [LLM] key[${i + 1}/${apiKeys.length}] 失敗: ${e.message}`);
console.log(` [LLM] key[${i + 1}/${shuffled.length}] 失敗: ${e.message}`);
}
}
console.error(' [LLM] 所有 API Key 均失敗,終止流程');