feat: refactor API key handling to shuffle keys and attempt each once
This commit is contained in:
@@ -21,7 +21,7 @@
|
|||||||
3. Comment 加上些許 emoji 讓資訊有點活力
|
3. Comment 加上些許 emoji 讓資訊有點活力
|
||||||
4. 盡量將應用程式放在 ./app,修改 entrypoint.sh 與 Dockerfile 讓程式可以正常運行
|
4. 盡量將應用程式放在 ./app,修改 entrypoint.sh 與 Dockerfile 讓程式可以正常運行
|
||||||
5. 將提示詞放到 ./app/prompts 內供程式讀取
|
5. 將提示詞放到 ./app/prompts 內供程式讀取
|
||||||
6. API Key 支援逗號分隔傳入多個,依序嘗試,失敗時自動換下一個,全部失敗則 exit 1
|
6. API Key 支援逗號分隔傳入多個,隨機順序各嘗試一次,全部失敗則 exit 1
|
||||||
|
|
||||||
# 使用說明
|
# 使用說明
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
- 完成
|
- 完成
|
||||||
|
|
||||||
## 階段八:API Key 輪替
|
## 階段八:API Key 輪替
|
||||||
- 目標:所有平台的 API Key 支援逗號分隔傳入多個,依序嘗試,單一 Key 失敗時自動換下一個,全部失敗則 exit 1。
|
- 目標:所有平台的 API Key 支援逗號分隔傳入多個,隨機順序各嘗試一次,單一 Key 失敗時自動換下一個,全部失敗則 exit 1。
|
||||||
- 驗收:log 中能看到「key[N/M] 失敗」等訊息,換 key 後繼續執行;傳入單一 Key 時行為與原本相同;全部 Key 失敗時 log「所有 API Key 均失敗,終止流程」且 workflow 狀態為失敗。
|
- 驗收:log 中能看到「key[N/M] 失敗」等訊息,換 key 後繼續執行;傳入單一 Key 時行為與原本相同;全部 Key 失敗時 log「所有 API Key 均失敗,終止流程」且 workflow 狀態為失敗。
|
||||||
- 完成
|
- 完成
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -10,19 +10,20 @@ export async function chat(systemPrompt, userContent) {
|
|||||||
const headers = { 'Content-Type': 'application/json' };
|
const headers = { 'Content-Type': 'application/json' };
|
||||||
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
|
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
|
||||||
|
|
||||||
|
const shuffled = [...apiKeys].sort(() => Math.random() - 0.5);
|
||||||
let lastError;
|
let lastError;
|
||||||
for (let i = 0; i < apiKeys.length; i++) {
|
for (let i = 0; i < shuffled.length; i++) {
|
||||||
if (provider !== 'ollama') headers['Authorization'] = `Bearer ${apiKeys[i]}`;
|
if (provider !== 'ollama') headers['Authorization'] = `Bearer ${shuffled[i]}`;
|
||||||
try {
|
try {
|
||||||
const resp = await axios.post(
|
const resp = await axios.post(
|
||||||
`${baseURL.replace(/\/$/, '')}/chat/completions`,
|
`${baseURL.replace(/\/$/, '')}/chat/completions`,
|
||||||
{ model, messages: [{ role: 'system', content: systemPrompt }, { role: 'user', content: userContent }], temperature: 0.2 },
|
{ model, messages: [{ role: 'system', content: systemPrompt }, { role: 'user', content: userContent }], temperature: 0.2 },
|
||||||
{ headers, timeout: 30000 }
|
{ headers }
|
||||||
);
|
);
|
||||||
return resp.data.choices[0].message.content;
|
return resp.data.choices[0].message.content;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
lastError = 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 均失敗,終止流程');
|
console.error(' [LLM] 所有 API Key 均失敗,終止流程');
|
||||||
|
|||||||
+13
-13
@@ -48,18 +48,18 @@ describe('chat - key rotation', async () => {
|
|||||||
assert.equal(result, 'hello');
|
assert.equal(result, 'hello');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rotates to second key when first fails', async () => {
|
it('shuffles keys and tries each exactly once', async () => {
|
||||||
process.env.OPENAI_API_KEY = 'key1,key2';
|
process.env.OPENAI_API_KEY = 'key1,key2,key3';
|
||||||
mockAxiosPost([new Error('rate limit'), makeOkResponse('from key2')]);
|
const usedKeys = [];
|
||||||
const result = await chat('sys', 'user');
|
mock.method(axios, 'post', async (_url, _body, opts) => {
|
||||||
assert.equal(result, 'from key2');
|
usedKeys.push(opts.headers['Authorization'].replace('Bearer ', ''));
|
||||||
|
throw new Error('fail');
|
||||||
});
|
});
|
||||||
|
const exitMock = mock.method(process, 'exit', () => { throw new Error('exit:1'); });
|
||||||
it('rotates through all keys and succeeds on last', async () => {
|
await assert.rejects(() => chat('sys', 'user'), /exit:1/);
|
||||||
process.env.OPENAI_API_KEY = 'k1,k2,k3';
|
assert.equal(exitMock.mock.calls[0].arguments[0], 1);
|
||||||
mockAxiosPost([new Error('fail'), new Error('fail'), makeOkResponse('from k3')]);
|
assert.equal(usedKeys.length, 3);
|
||||||
const result = await chat('sys', 'user');
|
assert.deepEqual([...usedKeys].sort(), ['key1', 'key2', 'key3']);
|
||||||
assert.equal(result, 'from k3');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls process.exit(1) when all keys fail', async () => {
|
it('calls process.exit(1) when all keys fail', async () => {
|
||||||
@@ -93,7 +93,7 @@ describe('chat - key rotation', async () => {
|
|||||||
assert.equal(capturedHeaders['Authorization'], 'Bearer sk-test');
|
assert.equal(capturedHeaders['Authorization'], 'Bearer sk-test');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uses 30s timeout', async () => {
|
it('does not set timeout', async () => {
|
||||||
process.env.OPENAI_API_KEY = 'sk-test';
|
process.env.OPENAI_API_KEY = 'sk-test';
|
||||||
let capturedOpts;
|
let capturedOpts;
|
||||||
mock.method(axios, 'post', async (_url, _body, opts) => {
|
mock.method(axios, 'post', async (_url, _body, opts) => {
|
||||||
@@ -101,7 +101,7 @@ describe('chat - key rotation', async () => {
|
|||||||
return makeOkResponse();
|
return makeOkResponse();
|
||||||
});
|
});
|
||||||
await chat('sys', 'user');
|
await chat('sys', 'user');
|
||||||
assert.equal(capturedOpts.timeout, 30000);
|
assert.equal(capturedOpts.timeout, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not pass httpsAgent to axios', async () => {
|
it('does not pass httpsAgent to axios', async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user