feat: refactor LLM API handling, add tests for key rotation and update package files
This commit is contained in:
+2
-5
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user