40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
import axios from 'axios';
|
|
import { getLLMConfig } from './config.js';
|
|
|
|
export async function chat(systemPrompt, userContent) {
|
|
const { provider, apiKeys, baseURL, model } = getLLMConfig();
|
|
if (!provider) throw new Error('未設定任何 LLM API Key');
|
|
|
|
console.log(` [LLM] provider=${provider} model=${model}`);
|
|
|
|
const headers = { 'Content-Type': 'application/json' };
|
|
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
|
|
|
|
const shuffled = [...apiKeys].sort(() => Math.random() - 0.5);
|
|
for (let i = 0; i < shuffled.length; i++) {
|
|
if (provider !== 'ollama') headers['Authorization'] = `Bearer ${shuffled[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 }
|
|
);
|
|
return resp.data.choices[0].message.content;
|
|
} catch (e) {
|
|
console.log(` [LLM] key[${i + 1}/${shuffled.length}] 失敗: ${e.message}`);
|
|
}
|
|
}
|
|
console.error(' [LLM] 所有 API Key 均失敗,終止流程');
|
|
process.exit(1);
|
|
}
|
|
|
|
export async function chatJSON(systemPrompt, userContent) {
|
|
const text = await chat(systemPrompt, userContent);
|
|
try {
|
|
return JSON.parse(text.trim().replace(/^```[^\n]*\n?/, '').replace(/```$/, '').trim());
|
|
} catch (e) {
|
|
console.log(` [LLM] JSON 解析失敗: ${e.message}`);
|
|
return [];
|
|
}
|
|
}
|