feat: 新增学习引导提示词与任务类型

This commit is contained in:
2026-10-09 19:26:50 +08:00
parent e359e58ff2
commit fb3c6f9933
4 changed files with 67 additions and 5 deletions
+23 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { aggregateReportPrompt, generateMindMapPrompt } from "./prompts.js";
import { aggregateReportPrompt, generateMindMapPrompt, guidancePrompt } from "./prompts.js";
describe("aggregateReportPrompt", () => {
it("包含全部残片和任务名", () => {
@@ -44,3 +44,25 @@ describe("generateMindMapPrompt", () => {
expect(messages[1].content).not.toContain("学习报告");
});
});
describe("guidancePrompt", () => {
it("把任务名与概念写进 system,并带上历史轮次", () => {
const messages = guidancePrompt("AI 基础", "参数量与显存", [
{ role: "user", content: "我觉得参数量决定显存占用" },
{ role: "assistant", content: "那 1B 模型一定占 1GB 吗?" },
]);
expect(messages).toHaveLength(3);
expect(messages[0].role).toBe("system");
expect(messages[0].content).toContain("AI 基础");
expect(messages[0].content).toContain("参数量与显存");
// 不判分、不设轮数上限
expect(messages[0].content).toContain("不给分数");
// 历史轮次原样带上,供多轮对话使用
expect(messages[1].content).toContain("参数量决定显存占用");
expect(messages[2].content).toContain("1B 模型");
});
it("没有历史时只有 system", () => {
expect(guidancePrompt("t", "c", [])).toHaveLength(1);
});
});
+32
View File
@@ -164,3 +164,35 @@ export function compareRecallPrompt(
},
];
}
/**
* 学习引导:用一个开放式问题引出用户的回答,并针对回答里隐含的概念边界问题纠正。
*
* 与其它能力不同,学习引导**允许完全依赖 AI**:它不属于核心闭环,AI 不可用时整体不可用,
* 因此这里不做降级拼接。
*
* 要求:不判分、不评价对错、不设轮数上限;只在回答里确实存在边界问题时纠正,
* 没有问题时继续往下追问一层,不做无意义的夸奖。
*/
export function guidancePrompt(
taskName: string,
concept: string,
history: ChatMessage[],
): ChatMessage[] {
return [
{
role: "system",
content: [
"你是一名学习引导助手。用户在复习某个知识概念,你要用开放式问题引导他主动表达,",
"并从他的回答里发现隐含的概念边界问题并纠正。要求:",
"1. 不给分数、不评价好坏、不用「很好」「不错」这类空话;",
"2. 只有当回答里真的存在概念混淆、边界模糊或错误前提时才纠正,纠正要给出具体边界;",
"3. 纠正之后继续追问一层更深的问题,帮助用户把边界说清楚;",
"4. 如果回答没有问题,直接追问更深或相邻的问题;",
"5. 一次只问一个问题,语言简短、口语化,不超过 80 字;",
`6. 用户正在复习的学习任务是「${taskName}」,被引导的概念是「${concept}」。`,
].join(""),
},
...history,
];
}
+2 -2
View File
@@ -65,8 +65,8 @@ export async function aiRoutes(app: FastifyInstance): Promise<void> {
.send({ error: "提交太频繁了,请稍后再试" });
}
if (!type || !["aggregate-report", "generate-mind-map", "compare-recall"].includes(type)) {
return reply.status(400).send({ error: "无效的 type,允许: aggregate-report, generate-mind-map, compare-recall" });
if (!type || !["aggregate-report", "generate-mind-map", "compare-recall", "guidance"].includes(type)) {
return reply.status(400).send({ error: "无效的 type,允许: aggregate-report, generate-mind-map, compare-recall, guidance" });
}
if (!params || typeof params !== "object") {
return reply.status(400).send({ error: "params 必须为非空对象" });
+10 -2
View File
@@ -14,12 +14,12 @@
import { randomUUID, createHash } from "node:crypto";
import { chat, isAvailable, loadConfig, LlmError, type ChatMessage } from "./llm/client.js";
import { withRetry, isRetryableLlmError, type RetryOptions } from "./llm/retry.js";
import { aggregateReportPrompt, compareRecallPrompt, generateMindMapPrompt } from "./llm/prompts.js";
import { aggregateReportPrompt, compareRecallPrompt, generateMindMapPrompt, guidancePrompt } from "./llm/prompts.js";
import { logStore } from "./admin/store.js";
// ---- 类型 ----
export type TaskType = "aggregate-report" | "generate-mind-map" | "compare-recall";
export type TaskType = "aggregate-report" | "generate-mind-map" | "compare-recall" | "guidance";
export type TaskStatus = "pending" | "running" | "done" | "failed";
@@ -381,6 +381,7 @@ export class TaskQueue {
function callOptionsFor(type: TaskType): { temperature: number; maxTokens: number } {
if (type === "compare-recall") return { temperature: 0.2, maxTokens: 4096 };
if (type === "generate-mind-map") return { temperature: 0.3, maxTokens: 4096 };
if (type === "guidance") return { temperature: 0.7, maxTokens: 1024 };
return { temperature: 0.3, maxTokens: 2048 };
}
@@ -429,6 +430,9 @@ export function parseResult(type: TaskType, raw: string, model = ""): unknown {
if (type === "generate-mind-map") {
return { outline: text, model };
}
if (type === "guidance") {
return { reply: text, model };
}
const clean = text.replace(/^```(?:json)?\s*/, "").replace(/\s*```$/, "");
return JSON.parse(clean);
}
@@ -453,6 +457,10 @@ export function buildMessages(task: TaskRecord): ChatMessage[] {
fragments ?? [],
);
}
case "guidance": {
const history = (p.history as ChatMessage[] | undefined) ?? [];
return guidancePrompt(p.taskName as string, p.concept as string, history);
}
case "compare-recall": {
return compareRecallPrompt(
p.taskName as string,