feat: 导图生成支持携带同源报告作为参考
This commit is contained in:
@@ -43,6 +43,36 @@ describe("generateMindMapPrompt", () => {
|
|||||||
expect(messages[1].content).toContain("corePoolSize");
|
expect(messages[1].content).toContain("corePoolSize");
|
||||||
expect(messages[1].content).not.toContain("学习报告");
|
expect(messages[1].content).not.toContain("学习报告");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("没有报告时不带报告块与报告规则", () => {
|
||||||
|
const messages = generateMindMapPrompt("t", "", ["x"]);
|
||||||
|
expect(messages[0].content).not.toContain("参考报告规则");
|
||||||
|
expect(messages[1].content).not.toContain("参考报告");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("携带报告时:碎片标注同源报告,报告三段进入上下文,并给出相关性约束", () => {
|
||||||
|
const messages = generateMindMapPrompt(
|
||||||
|
"Java 并发",
|
||||||
|
"",
|
||||||
|
[{ content: "corePoolSize 是核心线程数", reportRef: "S1" }],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
ref: "S1",
|
||||||
|
taskName: "Java 并发",
|
||||||
|
sessionSummary: "线程池参数总结",
|
||||||
|
freeSummary: "还需补队列的取舍",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
// 系统规则里说明「实质讨论/领域一致才用,否则完全不用」
|
||||||
|
expect(messages[0].content).toContain("域一致");
|
||||||
|
expect(messages[0].content).toContain("完全不要使用该报告");
|
||||||
|
// 用户上下文里碎片标注来源报告,并带上报告三段
|
||||||
|
expect(messages[1].content).toContain("来源报告:S1");
|
||||||
|
expect(messages[1].content).toContain("报告 S1");
|
||||||
|
expect(messages[1].content).toContain("会话摘要:线程池参数总结");
|
||||||
|
expect(messages[1].content).toContain("自由总结:还需补队列的取舍");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("guidancePrompt", () => {
|
describe("guidancePrompt", () => {
|
||||||
|
|||||||
+75
-27
@@ -10,6 +10,23 @@ export interface FragmentInput {
|
|||||||
createdTime?: string;
|
createdTime?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 导图生成的一条碎片素材:正文 + 可空的同源报告标识(报告-02) */
|
||||||
|
export interface MindMapFragmentInput {
|
||||||
|
content: string;
|
||||||
|
/** 同源报告标识(会话编号):该碎片与这份报告来自同一次学习会话 */
|
||||||
|
reportRef?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 导图生成的一份报告参考上下文(报告-02):同源会话的总结三段 */
|
||||||
|
export interface MindMapReportInput {
|
||||||
|
ref: string;
|
||||||
|
/** 产生该报告的会话所属任务名,用于判断领域是否一致 */
|
||||||
|
taskName?: string;
|
||||||
|
sessionSummary?: string;
|
||||||
|
expectationVerdict?: string;
|
||||||
|
freeSummary?: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 残片聚合为结构化的学习报告草稿。
|
* 残片聚合为结构化的学习报告草稿。
|
||||||
*
|
*
|
||||||
@@ -58,44 +75,75 @@ export function aggregateReportPrompt(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从知识碎片生成标准思维导图(预留,用于替代 Java 端 BuiltinMindMapGenerator)。
|
* 从知识碎片生成标准思维导图(报告-02 起可携带报告作为参考上下文;预留,用于替代 Java 端 BuiltinMindMapGenerator)。
|
||||||
*
|
*
|
||||||
* <p>素材按**内容归属**给(一个任务名下的碎片),而不是按产生它的会话给:
|
* <p>素材按**内容归属**给(一个任务名下的碎片),而不是按产生它的会话给:
|
||||||
* 学 A 时顺手记下的旁支知识若归属到 B,就应该出现在 B 的导图里。</p>
|
* 学 A 时顺手记下的旁支知识若归属到 B,就应该出现在 B 的导图里。</p>
|
||||||
*
|
*
|
||||||
|
* <p>报告是同源会话的总结:一次总结可能把若干碎片拼接出新的定义与观点,
|
||||||
|
* 这些内容只存在于报告里。因此把与碎片同源的报告作为参考带上,但**是否采用由模型按相关性判断**:
|
||||||
|
* 报告必须实质讨论相关碎片的同一主题、且与本批碎片领域一致,才可提取;只是顺带提及或没提及则完全不用。</p>
|
||||||
|
*
|
||||||
* 输出为缩进大纲文本,与后端 MindMapTreeTool.parseOutline 格式一致。
|
* 输出为缩进大纲文本,与后端 MindMapTreeTool.parseOutline 格式一致。
|
||||||
*/
|
*/
|
||||||
export function generateMindMapPrompt(
|
export function generateMindMapPrompt(
|
||||||
taskName: string,
|
taskName: string,
|
||||||
taskDescription: string,
|
taskDescription: string,
|
||||||
fragments: string[],
|
fragments: Array<string | MindMapFragmentInput>,
|
||||||
|
reports: MindMapReportInput[] = [],
|
||||||
): ChatMessage[] {
|
): ChatMessage[] {
|
||||||
|
const fragmentItems: MindMapFragmentInput[] = fragments.map((f) =>
|
||||||
|
typeof f === "string" ? { content: f } : f,
|
||||||
|
);
|
||||||
|
const hasReports = reports.length > 0;
|
||||||
|
|
||||||
|
const systemRules = [
|
||||||
|
"你是一名知识结构整理助手。请阅读某个学习任务名下的全部知识碎片,",
|
||||||
|
"将这些碎片整理成一份思维导图大纲。要求:",
|
||||||
|
"1. 第一行是导图根标题(任务主题);",
|
||||||
|
"2. 之后每行一个节点,用两个空格的缩进表示层级;",
|
||||||
|
"3. 按知识的内在结构组织(概念→细分→要点),不按时间顺序;",
|
||||||
|
"4. 合并重复的知识点;",
|
||||||
|
"5. 只基于给定内容,不补充外部知识;",
|
||||||
|
"6. 直接输出大纲,不要任何解释。",
|
||||||
|
];
|
||||||
|
if (hasReports) {
|
||||||
|
systemRules.push(
|
||||||
|
"参考报告规则(这些报告是同源学习会话的总结,仅作参考):",
|
||||||
|
"7. 只有当某份报告实质地讨论了相关碎片的同一主题、且与本批碎片的领域一致时,",
|
||||||
|
" 才可提取报告中由碎片拼接出的新定义、新观点进入导图;",
|
||||||
|
"8. 报告只是顺带提及、或者根本没有提到该主题时,完全不要使用该报告的内容;",
|
||||||
|
"9. 报告与该批碎片的主干强相关时,可以参考它的组织结构,但不要照搬无关内容;",
|
||||||
|
"10. 不要引入碎片与报告都未涉及的知识。",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const fragmentLines = fragmentItems.map((fragment, i) => {
|
||||||
|
const source = fragment.reportRef ? `(来源报告:${fragment.reportRef})` : "";
|
||||||
|
return `${i + 1}. ${fragment.content}${source}`;
|
||||||
|
});
|
||||||
|
|
||||||
|
const userLines = [
|
||||||
|
`学习任务:${taskName}`,
|
||||||
|
taskDescription ? `任务描述:${taskDescription}` : "",
|
||||||
|
"",
|
||||||
|
"知识碎片:",
|
||||||
|
...fragmentLines,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (hasReports) {
|
||||||
|
userLines.push("", "参考报告(是否采用见规则 7~10):");
|
||||||
|
for (const report of reports) {
|
||||||
|
userLines.push(`[报告 ${report.ref}]${report.taskName ? `(来自学习任务「${report.taskName}」)` : ""}`);
|
||||||
|
if (report.sessionSummary) userLines.push(`会话摘要:${report.sessionSummary}`);
|
||||||
|
if (report.expectationVerdict) userLines.push(`预期达成判断:${report.expectationVerdict}`);
|
||||||
|
if (report.freeSummary) userLines.push(`自由总结:${report.freeSummary}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{
|
{ role: "system", content: systemRules.join("") },
|
||||||
role: "system",
|
{ role: "user", content: userLines.filter(Boolean).join("\n") },
|
||||||
content: [
|
|
||||||
"你是一名知识结构整理助手。请阅读某个学习任务名下的全部知识碎片,",
|
|
||||||
"将这些碎片整理成一份思维导图大纲。要求:",
|
|
||||||
"1. 第一行是导图根标题(任务主题);",
|
|
||||||
"2. 之后每行一个节点,用两个空格的缩进表示层级;",
|
|
||||||
"3. 按知识的内在结构组织(概念→细分→要点),不按时间顺序;",
|
|
||||||
"4. 合并重复的知识点;",
|
|
||||||
"5. 只基于给定内容,不补充外部知识;",
|
|
||||||
"6. 直接输出大纲,不要任何解释。",
|
|
||||||
].join(""),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: [
|
|
||||||
`学习任务:${taskName}`,
|
|
||||||
taskDescription ? `任务描述:${taskDescription}` : "",
|
|
||||||
"",
|
|
||||||
"知识碎片:",
|
|
||||||
...fragments.map((r, i) => `${i + 1}. ${r}`),
|
|
||||||
]
|
|
||||||
.filter(Boolean)
|
|
||||||
.join("\n"),
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-4
@@ -15,7 +15,14 @@ import { randomUUID, createHash } from "node:crypto";
|
|||||||
import { chat, isAvailable, loadConfig, LlmError, type ChatMessage, type ChatOptions } from "./llm/client.js";
|
import { chat, isAvailable, loadConfig, LlmError, type ChatMessage, type ChatOptions } from "./llm/client.js";
|
||||||
import { accumulateUsage, combineSource, emptyUsage, type TokenUsage, type UsageSource } from "./llm/usage.js";
|
import { accumulateUsage, combineSource, emptyUsage, type TokenUsage, type UsageSource } from "./llm/usage.js";
|
||||||
import { withRetry, isRetryableLlmError, type RetryOptions } from "./llm/retry.js";
|
import { withRetry, isRetryableLlmError, type RetryOptions } from "./llm/retry.js";
|
||||||
import { aggregateReportPrompt, compareRecallPrompt, generateMindMapPrompt, guidancePrompt } from "./llm/prompts.js";
|
import {
|
||||||
|
aggregateReportPrompt,
|
||||||
|
compareRecallPrompt,
|
||||||
|
generateMindMapPrompt,
|
||||||
|
guidancePrompt,
|
||||||
|
type MindMapFragmentInput,
|
||||||
|
type MindMapReportInput,
|
||||||
|
} from "./llm/prompts.js";
|
||||||
import { loadThinkingConfig, promptLength, reasoningEffortFor, type ThinkingConfig } from "./llm/thinking.js";
|
import { loadThinkingConfig, promptLength, reasoningEffortFor, type ThinkingConfig } from "./llm/thinking.js";
|
||||||
import { logStore } from "./admin/store.js";
|
import { logStore } from "./admin/store.js";
|
||||||
import { loadPricingConfig, priceUsage, type PricingSnapshot } from "./admin/pricing.js";
|
import { loadPricingConfig, priceUsage, type PricingSnapshot } from "./admin/pricing.js";
|
||||||
@@ -565,12 +572,14 @@ export function buildMessages(task: TaskRecord): ChatMessage[] {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
case "generate-mind-map": {
|
case "generate-mind-map": {
|
||||||
// 素材为「该任务名下的知识碎片」(按内容归属取),不再用按会话产生的报告
|
// 素材为「该任务名下的知识碎片」(按内容归属取),并携带同源会话的报告作为参考(报告-02)
|
||||||
const fragments = p.fragments as string[] | undefined;
|
const fragments = (p.fragments as Array<string | MindMapFragmentInput> | undefined) ?? [];
|
||||||
|
const reports = (p.reports as MindMapReportInput[] | undefined) ?? [];
|
||||||
return generateMindMapPrompt(
|
return generateMindMapPrompt(
|
||||||
p.taskName as string,
|
p.taskName as string,
|
||||||
(p.taskDescription as string) ?? "",
|
(p.taskDescription as string) ?? "",
|
||||||
fragments ?? [],
|
fragments,
|
||||||
|
reports,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
case "guidance": {
|
case "guidance": {
|
||||||
|
|||||||
Reference in New Issue
Block a user