fix: switch to Gemini AI, fix free-tier output, fix spinner state, upgrade prompts, multi-domain routing

This commit is contained in:
2026-03-18 18:35:46 +08:00
parent c784d07796
commit e69ef38d44
8 changed files with 67 additions and 106 deletions
+14 -16
View File
@@ -1,33 +1,31 @@
import Anthropic from '@anthropic-ai/sdk';
import { GoogleGenerativeAI } from '@google/generative-ai';
import { getConfig } from '@/lib/db';
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || '');
export async function getModel(): Promise<string> {
return (await getConfig('openai_model')) || 'claude-3-5-sonnet-20241022';
return (await getConfig('gemini_model')) || 'gemini-1.5-pro';
}
export async function generate(
systemPrompt: string,
userPrompt: string
): Promise<{ text: string; tokens: number }> {
const model = await getModel();
const response = await anthropic.messages.create({
model,
max_tokens: 4096,
system: systemPrompt,
messages: [{ role: 'user', content: userPrompt }],
const modelName = await getModel();
const model = genAI.getGenerativeModel({
model: modelName,
systemInstruction: systemPrompt,
});
const text = response.content
.filter((b) => b.type === 'text')
.map((b) => (b as Anthropic.TextBlock).text)
.join('');
const result = await model.generateContent(userPrompt);
const response = result.response;
const text = response.text();
const tokens = (response.usage.input_tokens ?? 0) + (response.usage.output_tokens ?? 0);
const inputTokens = response.usageMetadata?.promptTokenCount ?? 0;
const outputTokens = response.usageMetadata?.candidatesTokenCount ?? 0;
const tokens = inputTokens + outputTokens;
return { text, tokens };
}
export default anthropic;
export default genAI;