Files
Next_Knowledge_Base/lib/ai/prompts.ts
T
jiawei 901f9948c7
knowledge-base / deploy (push) Successful in 15s
feat: 优化提示词
2026-06-23 19:50:39 +08:00

132 lines
4.2 KiB
TypeScript

import type { KnowledgeItem } from "@/lib/types";
import { CATEGORIES, DIFFICULTIES, MASTERY_LEVELS, TAGS } from "@/lib/types";
// 知识库条目字段列表
const ITEM_FIELDS = [
"title",
"summary",
"content",
"code_snippet",
"tags",
"category",
"difficulty",
"mastery",
"source_url",
"notes"
] as const;
export interface BatchImportPromptOptions {
/** tags 可选枚举,默认取 lib/types 中的 TAGS */
tags?: readonly string[];
/** category 可选枚举,默认取 lib/types 中的 CATEGORIES */
categories?: readonly string[];
/** difficulty 可选枚举,默认取 lib/types 中的 DIFFICULTIES */
difficulties?: readonly string[];
/** mastery 默认值,默认取 MASTERY_LEVELS 的第一项 */
defaultMastery?: string;
}
/**
* 根据类型定义生成批量导入提示词。
* 枚举字段全部从 lib/types 的常量派生,修改类型后提示词会自动同步。
*/
export function buildBatchImportPrompt(
options: BatchImportPromptOptions = {}
): string {
const {
tags = TAGS,
categories = CATEGORIES,
difficulties = DIFFICULTIES,
defaultMastery = MASTERY_LEVELS[0]
} = options;
const fieldsList = ITEM_FIELDS.map((f) => ` - ${f}`).join("\n");
return `请把用户输入的多个前端题目整理成知识库 JSON。
要求:
1. 只返回严格 JSON。
2. JSON 顶层格式必须是:{ "items": [] }
3. 每个 item 必须包含:
${fieldsList}
4. tags 只能从这些枚举中选择:
${tags.join("、")}
5. category 只能从这些枚举中选择:
${categories.join("、")}
6. difficulty 只能是:
${difficulties.join("、")}
7. mastery 默认是 ${defaultMastery}
8. summary 为一句话精炼摘要
9. content 结构清晰、分点说明,突出重点与易错点,通俗易懂,适合反复复习记忆
10. 如果题目适合写代码示例,请放入 code_snippet,代码简洁、可运行、有代表性,加上注释
11. 不要返回 Markdown,不要返回解释`;
}
/** 默认批量导入提示词(使用 lib/types 中的枚举常量) */
export const BATCH_IMPORT_PROMPT = buildBatchImportPrompt();
export function buildItemContext(item: KnowledgeItem): string {
return `当前知识点:
标题:${item.title}
摘要:${item.summary ?? ""}
正文:${item.content ?? ""}
代码:${item.code_snippet ?? ""}
标签:${item.tags.join("、")}
难度:${item.difficulty}`;
}
export function buildChatSystemPrompt(item: KnowledgeItem): string {
return `你正在帮助用户复习一个前端知识点。
${buildItemContext(item)}
请基于这个知识点回答用户问题。
回答要求:
- 结构清晰、分点说明,通俗易懂,突出重点与易错点
- 尽量结合面试场景
- 必要时给代码示例,代码简洁且带注释
- 如果用户回答错误,要指出问题并给出正确理解
- 不要编造不存在的上下文`;
}
export function buildOptimizePrompt(item: {
title: string;
summary: string;
content: string;
}): string {
return `请优化下面这个前端知识点,使其更适合复习记忆。
只返回严格 JSON,格式为:{ "summary": "...", "content": "..." }
不要返回 Markdown,不要返回解释。
标题:${item.title}
当前摘要:${item.summary}
当前正文:${item.content}
要求:
- summary 为一句话精炼摘要
- content 结构清晰、分点说明,突出重点与易错点,通俗易懂,适合反复复习记忆`;
}
export function buildCodeGenPrompt(item: {
title: string;
summary: string;
content: string;
}): string {
return `请为下面这个前端知识点生成一段简洁、可运行、有代表性的示例代码,加上代码注释。
只返回代码本身,不要返回 Markdown 代码块标记,不要返回解释。
标题:${item.title}
摘要:${item.summary}
正文:${item.content}`;
}
export function buildInterviewPrompt(item: KnowledgeItem): string {
return `我在准备面试。
要求:
- 先简单概述这个知识点,再给出面试简答版本,让我快速复习和记忆
- 简答版本结构清晰、分点说明,突出重点与易错点
- 必要时给简短代码示例,代码简洁、可运行,加上注释
- 不要返回 Markdown 代码块以外的多余解释
${buildItemContext(item)}`;
}