allow proxy auto model selection
CI / 1. BUILD (pull_request) Successful in 2s
CI / 2. TEST (pull_request) Successful in 2s
CI / 3. RESULT (pull_request) Successful in 0s

This commit is contained in:
2026-08-07 06:16:45 +00:00
parent 0eb30cf9d4
commit 651e221e90
10 changed files with 96 additions and 41 deletions
+14 -13
View File
@@ -147,8 +147,8 @@ function summarizeApiError(e) {
* 由 axios 直接拋出例外,交由呼叫端(chat())攔截並摘要。僅使用
* `apiKeys` 陣列的第一個元素,不會輪替其他金鑰。
*
* @param {{provider: string, baseURL: string, apiKeys: string[], model: string}} cfg - 連線設定;
* 僅使用 `apiKeys[0]`。
* @param {{provider: string, baseURL: string, apiKeys: string[], model?: string|null}} cfg - 連線設定;
* 僅使用 `apiKeys[0]`;`model` 可省略,省略時交由 CLIProxyAPI 自動選擇。
* @param {string} prompt - 送給 API 的完整 prompt 內容,會作為 user 訊息內容;
* HTTP 層的 system 訊息為固定的通用指示,與 prompt 內可能內嵌的 `<system>` 內容無關。
* @returns {Promise<any>} API 回應的原始資料物件(`resp.data`),並非純文字;
@@ -164,17 +164,18 @@ async function runProxyAPI({ provider, baseURL, apiKeys, model }, prompt) {
const maxBuffer = Number(process.env.AI_ASSISTANT_MAX_BUFFER || 20 * 1024 * 1024);
const root = String(baseURL || '').trim().replace(/\/$/, '');
const apiKey = Array.isArray(apiKeys) ? apiKeys[0] : '';
const body = {
messages: [
{ role: 'system', content: '請依照以下系統指示處理使用者內容,並只輸出要求的最終結果。' },
{ role: 'user', content: prompt },
],
temperature: 0,
stream: false,
};
if (model) body.model = model;
const resp = await axios.post(
`${root}/v1/chat/completions`,
{
model,
messages: [
{ role: 'system', content: '請依照以下系統指示處理使用者內容,並只輸出要求的最終結果。' },
{ role: 'user', content: prompt },
],
temperature: 0,
stream: false,
},
body,
{
timeout,
maxBodyLength: maxBuffer,
@@ -209,9 +210,9 @@ async function runProxyAPI({ provider, baseURL, apiKeys, model }, prompt) {
export async function chat(systemPrompt, userContent) {
const cfg = getLLMConfig();
const { provider, baseURL, model } = cfg;
if (!provider || !baseURL || !model) throw new Error('未偵測到可用的 CLIProxyAPI 設定,請確認 CLI_PROXY_API 與 MODEL');
if (!provider || !baseURL) throw new Error('未偵測到可用的 CLIProxyAPI 設定,請確認 CLI_PROXY_API');
line(`[LLM] provider=${provider} baseURL=${baseURL} model=${model}`);
line(`[LLM] provider=${provider} baseURL=${baseURL} model=${model || 'auto'}`);
try {
const data = await runProxyAPI(cfg, buildPrompt(systemPrompt, userContent));