Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a6df5c4f43 | |||
| e28c6ea5a3 | |||
| 4a631bc62a |
@@ -33,9 +33,9 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@v${{ needs.version.outputs.version }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@v${{ needs.version.outputs.version }}
|
||||||
with:
|
with:
|
||||||
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
|
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }},${{ secrets.GEMINI_API_KEY_1 }},${{ secrets.GEMINI_API_KEY_2 }},${{ secrets.GEMINI_API_KEY_3 }},${{ secrets.GEMINI_API_KEY_4 }},${{ secrets.GEMINI_API_KEY_5 }},${{ secrets.GEMINI_API_KEY_6 }},${{ secrets.GEMINI_API_KEY_7 }},${{ secrets.GEMINI_API_KEY_8 }},${{ secrets.GEMINI_API_KEY_9 }}
|
||||||
OPENAI_BASE_URL: https://openrouter.ai/api/v1
|
GEMINI_BASE_URL: https://generativelanguage.googleapis.com/v1beta
|
||||||
OPENAI_MODEL: ${{ vars.OPENROUTER_MODEL }}
|
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
# 使用說明
|
# 使用說明
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
||||||
with:
|
with:
|
||||||
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} # OpenRouter 使用 OpenAI 相容介面,以 OPENAI_API_KEY 傳入,支援逗號分隔多個 Key
|
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }},${{ secrets.OPENROUTER_API_KEY_1 }}
|
||||||
OPENAI_BASE_URL: https://openrouter.ai/api/v1
|
OPENAI_BASE_URL: https://openrouter.ai/api/v1
|
||||||
OPENAI_MODEL: ${{ vars.OPENROUTER_MODEL }}
|
OPENAI_MODEL: ${{ vars.OPENROUTER_MODEL }}
|
||||||
permissions:
|
permissions:
|
||||||
|
|||||||
@@ -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 均失敗,終止流程');
|
||||||
|
|||||||
+14
-14
@@ -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');
|
||||||
|
});
|
||||||
it('rotates through all keys and succeeds on last', async () => {
|
const exitMock = mock.method(process, 'exit', () => { throw new Error('exit:1'); });
|
||||||
process.env.OPENAI_API_KEY = 'k1,k2,k3';
|
await assert.rejects(() => chat('sys', 'user'), /exit:1/);
|
||||||
mockAxiosPost([new Error('fail'), new Error('fail'), makeOkResponse('from k3')]);
|
assert.equal(exitMock.mock.calls[0].arguments[0], 1);
|
||||||
const result = await chat('sys', 'user');
|
assert.equal(usedKeys.length, 3);
|
||||||
assert.equal(result, 'from k3');
|
assert.deepEqual([...usedKeys].sort(), ['key1', 'key2', 'key3']);
|
||||||
});
|
});
|
||||||
|
|
||||||
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