feat: refactor LLM API handling, add tests for key rotation and update package files

This commit is contained in:
2026-05-12 07:38:38 +00:00
parent 328d6b2100
commit 2ced37f54f
5 changed files with 600 additions and 6 deletions
+2 -5
View File
@@ -1,9 +1,6 @@
import axios from 'axios';
import https from 'https';
import { getLLMConfig } from './config.js';
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
export async function chat(systemPrompt, userContent) {
const { provider, apiKeys, baseURL, model } = getLLMConfig();
if (!provider) throw new Error('未設定任何 LLM API Key');
@@ -15,12 +12,12 @@ export async function chat(systemPrompt, userContent) {
let lastError;
for (let i = 0; i < apiKeys.length; i++) {
headers['Authorization'] = `Bearer ${apiKeys[i]}`;
if (provider !== 'ollama') headers['Authorization'] = `Bearer ${apiKeys[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: 120000, httpsAgent }
{ headers, timeout: 30000 }
);
return resp.data.choices[0].message.content;
} catch (e) {