Files
learn-languages/src/lib/bigmodel/llm.ts
goddonebianu de7c1321c2 refactor: remove Anki import/export and simplify card system
- Remove Anki apkg import/export functionality
- Remove OCR feature module
- Remove note and note-type modules
- Simplify card/deck modules (remove spaced repetition complexity)
- Update translator and dictionary features
- Clean up unused translations and update i18n files
- Simplify prisma schema
2026-03-17 20:24:42 +08:00

47 lines
1.2 KiB
TypeScript

"use server";
type Messages = Array<
| { role: "system"; content: string }
| { role: "user"; content: string }
| { role: "assistant"; content: string }
>;
async function getAnswer(prompt: string): Promise<string>;
async function getAnswer(prompt: Messages): Promise<string>;
async function getAnswer(prompt: string | Messages): Promise<string> {
const messages: Messages = typeof prompt === "string"
? [{ role: "user", content: prompt }]
: prompt;
const response = await fetch("https://open.bigmodel.cn/api/paas/v4/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.ZHIPU_API_KEY}`,
},
body: JSON.stringify({
model: process.env.ZHIPU_MODEL_NAME || "glm-4.6",
messages,
temperature: 0.2,
thinking: {
type: "disabled"
}
}),
});
if (!response.ok) {
throw new Error(`AI API 请求失败: ${response.status}`);
}
const data = await response.json() as { choices?: Array<{ message?: { content?: string } }> };
const content = data.choices?.[0]?.message?.content;
if (!content) {
throw new Error("AI API 返回空响应");
}
return content.trim();
}
export { getAnswer };