feat: 优化提示词
knowledge-base / deploy (push) Successful in 15s

This commit is contained in:
2026-06-23 19:50:39 +08:00
parent 0061e1a58d
commit 901f9948c7
+42 -30
View File
@@ -1,6 +1,7 @@
import type { KnowledgeItem } from "@/lib/types" import type { KnowledgeItem } from "@/lib/types";
import { CATEGORIES, DIFFICULTIES, MASTERY_LEVELS, TAGS } from "@/lib/types" import { CATEGORIES, DIFFICULTIES, MASTERY_LEVELS, TAGS } from "@/lib/types";
// 知识库条目字段列表
const ITEM_FIELDS = [ const ITEM_FIELDS = [
"title", "title",
"summary", "summary",
@@ -11,33 +12,35 @@ const ITEM_FIELDS = [
"difficulty", "difficulty",
"mastery", "mastery",
"source_url", "source_url",
"notes", "notes"
] as const ] as const;
export interface BatchImportPromptOptions { export interface BatchImportPromptOptions {
/** tags 可选枚举,默认取 lib/types 中的 TAGS */ /** tags 可选枚举,默认取 lib/types 中的 TAGS */
tags?: readonly string[] tags?: readonly string[];
/** category 可选枚举,默认取 lib/types 中的 CATEGORIES */ /** category 可选枚举,默认取 lib/types 中的 CATEGORIES */
categories?: readonly string[] categories?: readonly string[];
/** difficulty 可选枚举,默认取 lib/types 中的 DIFFICULTIES */ /** difficulty 可选枚举,默认取 lib/types 中的 DIFFICULTIES */
difficulties?: readonly string[] difficulties?: readonly string[];
/** mastery 默认值,默认取 MASTERY_LEVELS 的第一项 */ /** mastery 默认值,默认取 MASTERY_LEVELS 的第一项 */
defaultMastery?: string defaultMastery?: string;
} }
/** /**
* 根据类型定义生成批量导入提示词。 * 根据类型定义生成批量导入提示词。
* 枚举字段全部从 lib/types 的常量派生,修改类型后提示词会自动同步。 * 枚举字段全部从 lib/types 的常量派生,修改类型后提示词会自动同步。
*/ */
export function buildBatchImportPrompt(options: BatchImportPromptOptions = {}): string { export function buildBatchImportPrompt(
options: BatchImportPromptOptions = {}
): string {
const { const {
tags = TAGS, tags = TAGS,
categories = CATEGORIES, categories = CATEGORIES,
difficulties = DIFFICULTIES, difficulties = DIFFICULTIES,
defaultMastery = MASTERY_LEVELS[0], defaultMastery = MASTERY_LEVELS[0]
} = options } = options;
const fieldsList = ITEM_FIELDS.map((f) => ` - ${f}`).join("\n") const fieldsList = ITEM_FIELDS.map((f) => ` - ${f}`).join("\n");
return `请把用户输入的多个前端题目整理成知识库 JSON。 return `请把用户输入的多个前端题目整理成知识库 JSON。
要求: 要求:
@@ -52,13 +55,14 @@ ${categories.join("、")}
6. difficulty 只能是: 6. difficulty 只能是:
${difficulties.join("、")} ${difficulties.join("、")}
7. mastery 默认是 ${defaultMastery} 7. mastery 默认是 ${defaultMastery}
8. 如果题目适合写代码示例,请放入 code_snippet 8. summary 为一句话精炼摘要
9. content 要适合后续复习,结构清晰 9. content 结构清晰、分点说明,突出重点与易错点,通俗易懂,适合反复复习记忆
10. 不要返回 Markdown,不要返回解释` 10. 如果题目适合写代码示例,请放入 code_snippet,代码简洁、可运行、有代表性,加上注释
11. 不要返回 Markdown,不要返回解释`;
} }
/** 默认批量导入提示词(使用 lib/types 中的枚举常量) */ /** 默认批量导入提示词(使用 lib/types 中的枚举常量) */
export const BATCH_IMPORT_PROMPT = buildBatchImportPrompt() export const BATCH_IMPORT_PROMPT = buildBatchImportPrompt();
export function buildItemContext(item: KnowledgeItem): string { export function buildItemContext(item: KnowledgeItem): string {
return `当前知识点: return `当前知识点:
@@ -67,7 +71,7 @@ export function buildItemContext(item: KnowledgeItem): string {
正文:${item.content ?? ""} 正文:${item.content ?? ""}
代码:${item.code_snippet ?? ""} 代码:${item.code_snippet ?? ""}
标签:${item.tags.join("、")} 标签:${item.tags.join("、")}
难度:${item.difficulty}` 难度:${item.difficulty}`;
} }
export function buildChatSystemPrompt(item: KnowledgeItem): string { export function buildChatSystemPrompt(item: KnowledgeItem): string {
@@ -77,14 +81,18 @@ ${buildItemContext(item)}
请基于这个知识点回答用户问题。 请基于这个知识点回答用户问题。
回答要求: 回答要求:
- 解释清晰 - 结构清晰、分点说明,通俗易懂,突出重点与易错点
- 尽量结合面试场景 - 尽量结合面试场景
- 必要时给代码示例 - 必要时给代码示例,代码简洁且带注释
- 如果用户回答错误,要指出问题并给出正确理解 - 如果用户回答错误,要指出问题并给出正确理解
- 不要编造不存在的上下文` - 不要编造不存在的上下文`;
} }
export function buildOptimizePrompt(item: { title: string; summary: string; content: string }): string { export function buildOptimizePrompt(item: {
title: string;
summary: string;
content: string;
}): string {
return `请优化下面这个前端知识点,使其更适合复习记忆。 return `请优化下面这个前端知识点,使其更适合复习记忆。
只返回严格 JSON,格式为:{ "summary": "...", "content": "..." } 只返回严格 JSON,格式为:{ "summary": "...", "content": "..." }
不要返回 Markdown,不要返回解释。 不要返回 Markdown,不要返回解释。
@@ -95,25 +103,29 @@ export function buildOptimizePrompt(item: { title: string; summary: string; cont
要求: 要求:
- summary 为一句话精炼摘要 - summary 为一句话精炼摘要
- content 结构清晰、分点突出重点与易错点,适合反复复习` - content 结构清晰、分点说明,突出重点与易错点,通俗易懂,适合反复复习记忆`;
} }
export function buildCodeGenPrompt(item: { title: string; summary: string; content: string }): string { export function buildCodeGenPrompt(item: {
return `请为下面这个前端知识点生成一段简洁、可运行、有代表性的示例代码。 title: string;
summary: string;
content: string;
}): string {
return `请为下面这个前端知识点生成一段简洁、可运行、有代表性的示例代码,加上代码注释。
只返回代码本身,不要返回 Markdown 代码块标记,不要返回解释。 只返回代码本身,不要返回 Markdown 代码块标记,不要返回解释。
标题:${item.title} 标题:${item.title}
摘要:${item.summary} 摘要:${item.summary}
正文:${item.content}` 正文:${item.content}`;
} }
export function buildInterviewPrompt(item: KnowledgeItem): string { export function buildInterviewPrompt(item: KnowledgeItem): string {
return `请把下面这个前端知识点整理成"面试回答版" return `我在准备面试
要求: 要求:
- 模拟面试场景下口语化但专业的回答 - 先简单概述这个知识点,再给出面试简答版本,让我快速复习和记忆
- 先给结论,再展开原理,最后补充延伸/注意 - 简答版本结构清晰、分点说明,突出重点与易错
- 必要时给简短代码示例 - 必要时给简短代码示例,代码简洁、可运行,加上注释
- 不要返回 Markdown 代码块以外的多余解释 - 不要返回 Markdown 代码块以外的多余解释
${buildItemContext(item)}` ${buildItemContext(item)}`;
} }