feat(AI服务):lpt-ai 独立服务初始版本

- Fastify + TypeScript,OpenAI 兼容接口对接 SiliconFlow
- POST /ai/aggregate-report:残片聚合为学习报告(支持预期对比)
- POST /ai/generate-mind-map:生成思维导图缩进大纲(与后端解析格式一致)
- Key 从环境变量读取且日志脱敏,未配置时返回 503 由调用方降级
- prompt 模板集中管理,vitest 单元测试
This commit is contained in:
2026-07-03 23:48:51 +08:00
commit 44c07d28bd
11 changed files with 4187 additions and 0 deletions
+98
View File
@@ -0,0 +1,98 @@
/**
* LLM 客户端 — SiliconFlow OpenAI 兼容接口
*
* 设计要点:
* - Key 从环境变量读取,永不写入代码或日志
* - 未配置 Key 时 isAvailable() 为 false,调用方返回 503,由 Java 端降级
* - 超时保护,避免长时间挂起
*/
export interface ChatMessage {
role: "system" | "user" | "assistant";
content: string;
}
export interface LlmConfig {
apiUrl: string;
apiKey: string;
model: string;
timeoutMs: number;
}
export function loadConfig(): LlmConfig {
return {
apiUrl: process.env.LLM_API_URL ?? "https://api.siliconflow.cn/v1/chat/completions",
apiKey: process.env.LLM_API_KEY ?? "",
model: process.env.LLM_MODEL ?? "Qwen/Qwen2.5-32B-Instruct",
timeoutMs: Number(process.env.LLM_TIMEOUT_MS ?? 60000),
};
}
export function isAvailable(config: LlmConfig): boolean {
return config.apiKey.length > 0;
}
export class LlmError extends Error {
constructor(
message: string,
public readonly statusCode: number,
) {
super(message);
}
}
/**
* 非流式调用,返回完整文本
*/
export async function chat(
config: LlmConfig,
messages: ChatMessage[],
options?: { temperature?: number; maxTokens?: number },
): Promise<string> {
if (!isAvailable(config)) {
throw new LlmError("LLM API Key 未配置", 503);
}
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), config.timeoutMs);
try {
const response = await fetch(config.apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config.apiKey}`,
},
body: JSON.stringify({
model: config.model,
messages,
temperature: options?.temperature ?? 0.3,
max_tokens: options?.maxTokens ?? 2048,
stream: false,
}),
signal: controller.signal,
});
if (!response.ok) {
const body = await response.text().catch(() => "");
throw new LlmError(`LLM 请求失败 (${response.status}): ${body.slice(0, 200)}`, 502);
}
const data = (await response.json()) as {
choices?: { message?: { content?: string } }[];
};
const content = data.choices?.[0]?.message?.content;
if (!content) {
throw new LlmError("LLM 返回内容为空", 502);
}
return content;
} catch (err) {
if (err instanceof LlmError) throw err;
if (err instanceof Error && err.name === "AbortError") {
throw new LlmError("LLM 请求超时", 504);
}
throw new LlmError(`LLM 请求异常: ${err instanceof Error ? err.message : String(err)}`, 502);
} finally {
clearTimeout(timer);
}
}