|
|
@@ -0,0 +1,206 @@
|
|
|
+/**
|
|
|
+ * 提示词组装器
|
|
|
+ * 根据需求分析动态组装提示词
|
|
|
+ */
|
|
|
+
|
|
|
+import { ChatMessage } from '../../../services/llm';
|
|
|
+import {
|
|
|
+ OUTLINE_SYSTEM_PROMPT,
|
|
|
+ SECTION_SYSTEM_PROMPT,
|
|
|
+ SUBSECTION_SYSTEM_PROMPT,
|
|
|
+} from './templates';
|
|
|
+import {
|
|
|
+ BOOK_TYPE_PROMPTS,
|
|
|
+ AUDIENCE_PROMPTS,
|
|
|
+ DEPTH_PROMPTS,
|
|
|
+ INDUSTRY_PROMPTS,
|
|
|
+} from './dynamic.config';
|
|
|
+
|
|
|
+// ============ 规模配置 ============
|
|
|
+
|
|
|
+export const SCALE_CHAPTER_RANGE: Record<string, { min: number; max: number }> = {
|
|
|
+ '800': { min: 1, max: 1 },
|
|
|
+ '2000': { min: 1, max: 1 },
|
|
|
+ '5000': { min: 1, max: 1 },
|
|
|
+ '小册子': { min: 3, max: 8 },
|
|
|
+ '标准教程': { min: 5, max: 15 },
|
|
|
+ '系统教材': { min: 10, max: 20 },
|
|
|
+};
|
|
|
+
|
|
|
+export const SCALE_DESC: Record<string, string> = {
|
|
|
+ '800': '短文,约800字',
|
|
|
+ '2000': '短文,约2000字',
|
|
|
+ '5000': '短文,约5000字',
|
|
|
+ '小册子': '小册子,1-5万字,3-8章',
|
|
|
+ '标准教程': '标准教程,5-15万字,5-15章',
|
|
|
+ '系统教材': '系统教材,15-30万字,10-20章',
|
|
|
+};
|
|
|
+
|
|
|
+// ============ 需求分析 ============
|
|
|
+
|
|
|
+/**
|
|
|
+ * 需求分析: 从用户描述中提取关键信息
|
|
|
+ */
|
|
|
+interface BookRequirements {
|
|
|
+ bookType?: string; // 教材/技术教程/小说/商业/科普
|
|
|
+ audience?: string; // 初学者/专业人士/大众读者
|
|
|
+ depth?: string; // 入门/进阶/专家
|
|
|
+ industry?: string; // IT/金融/医学/教育
|
|
|
+ specialNeeds?: string[]; // 特殊需求: 有案例/有代码/有习题等
|
|
|
+}
|
|
|
+
|
|
|
+export function analyzeRequirements(topic: string, description?: string): BookRequirements {
|
|
|
+ const text = `${topic} ${description || ''}`.toLowerCase();
|
|
|
+ const req: BookRequirements = {
|
|
|
+ specialNeeds: [],
|
|
|
+ };
|
|
|
+
|
|
|
+ // 分析书籍类型
|
|
|
+ if (/教材|课程|教学|大学|学校/i.test(text)) {
|
|
|
+ req.bookType = '教材';
|
|
|
+ } else if (/教程|实战|开发|编程|代码/i.test(text)) {
|
|
|
+ req.bookType = '技术教程';
|
|
|
+ } else if (/小说|故事|文学|传记|散文/i.test(text)) {
|
|
|
+ req.bookType = '小说';
|
|
|
+ } else if (/商业|管理|营销|创业|投资|经济/i.test(text)) {
|
|
|
+ req.bookType = '商业';
|
|
|
+ } else if (/科普|百科|科学|探索|揭秘/i.test(text)) {
|
|
|
+ req.bookType = '科普';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分析目标读者
|
|
|
+ if (/儿童|小学生/i.test(text)) {
|
|
|
+ req.audience = '儿童';
|
|
|
+ } else if (/青少年|中学生|高中生/i.test(text)) {
|
|
|
+ req.audience = '青少年';
|
|
|
+ } else if (/初学者|入门|新手|零基础|小白/i.test(text)) {
|
|
|
+ req.audience = '初学者';
|
|
|
+ } else if (/大学生|本科生/i.test(text)) {
|
|
|
+ req.audience = '大学生';
|
|
|
+ } else if (/研究生|硕士|博士/i.test(text)) {
|
|
|
+ req.audience = '研究生';
|
|
|
+ } else if (/专业|高级|深入|专家|研究/i.test(text)) {
|
|
|
+ req.audience = '专业人士';
|
|
|
+ } else if (/大众|通俗|普及|广泛/i.test(text)) {
|
|
|
+ req.audience = '大众读者';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分析知识难度
|
|
|
+ if (/入门|零基础|简单|易懂/i.test(text)) {
|
|
|
+ req.depth = '入门';
|
|
|
+ } else if (/基础|基本概念|循序渐进/i.test(text)) {
|
|
|
+ req.depth = '基础';
|
|
|
+ } else if (/进阶|深入|系统全面/i.test(text)) {
|
|
|
+ req.depth = '进阶';
|
|
|
+ } else if (/高级|专业深入|前沿/i.test(text)) {
|
|
|
+ req.depth = '高级';
|
|
|
+ } else if (/专家|研究|底层原理/i.test(text)) {
|
|
|
+ req.depth = '专家';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分析行业领域
|
|
|
+ if (/计算机|IT|编程|软件|互联网|AI|人工智能/i.test(text)) {
|
|
|
+ req.industry = 'IT/计算机';
|
|
|
+ } else if (/金融|股票|投资|银行|证券/i.test(text)) {
|
|
|
+ req.industry = '金融';
|
|
|
+ } else if (/医学|医疗|健康|临床|疾病/i.test(text)) {
|
|
|
+ req.industry = '医学';
|
|
|
+ } else if (/教育|教学|学习|学校|课程/i.test(text)) {
|
|
|
+ req.industry = '教育';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分析特殊需求
|
|
|
+ if (/案例|实例/i.test(text)) req.specialNeeds.push('有案例');
|
|
|
+ if (/代码|示例/i.test(text)) req.specialNeeds.push('有代码');
|
|
|
+ if (/习题|练习|作业/i.test(text)) req.specialNeeds.push('有习题');
|
|
|
+ if (/图表|插图|配图/i.test(text)) req.specialNeeds.push('有图表');
|
|
|
+ if (/项目|实战|实践/i.test(text)) req.specialNeeds.push('有项目实战');
|
|
|
+
|
|
|
+ return req;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 动态组装提示词
|
|
|
+ */
|
|
|
+export function buildDynamicPrompt(topic: string, bookScale: string, description?: string): string {
|
|
|
+ const req = analyzeRequirements(topic, description);
|
|
|
+
|
|
|
+ // 基础提示词
|
|
|
+ let prompt = OUTLINE_SYSTEM_PROMPT;
|
|
|
+
|
|
|
+ // 根据书籍类型添加
|
|
|
+ if (req.bookType && BOOK_TYPE_PROMPTS[req.bookType]) {
|
|
|
+ prompt += BOOK_TYPE_PROMPTS[req.bookType];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据目标读者添加
|
|
|
+ if (req.audience && AUDIENCE_PROMPTS[req.audience]) {
|
|
|
+ prompt += AUDIENCE_PROMPTS[req.audience];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据内容深度添加
|
|
|
+ if (req.depth && DEPTH_PROMPTS[req.depth]) {
|
|
|
+ prompt += DEPTH_PROMPTS[req.depth];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据行业领域添加
|
|
|
+ if (req.industry && INDUSTRY_PROMPTS[req.industry]) {
|
|
|
+ prompt += INDUSTRY_PROMPTS[req.industry];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 特殊需求
|
|
|
+ if (req.specialNeeds.length > 0) {
|
|
|
+ prompt += `\n## 特殊要求\n`;
|
|
|
+ prompt += `根据用户需求,本书应包含: ${req.specialNeeds.join('、')}\n`;
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(`[DynamicPrompt] 检测到需求:`, req);
|
|
|
+
|
|
|
+ return prompt;
|
|
|
+}
|
|
|
+
|
|
|
+// ============ 消息构建函数 ============
|
|
|
+
|
|
|
+/**
|
|
|
+ * 构建大纲生成消息(动态提示词)
|
|
|
+ */
|
|
|
+export function buildOutlineMessages(topic: string, bookScale: string, description?: string): ChatMessage[] {
|
|
|
+ const range = SCALE_CHAPTER_RANGE[bookScale as keyof typeof SCALE_CHAPTER_RANGE] || { min: 5, max: 10 };
|
|
|
+
|
|
|
+ // 使用动态提示词
|
|
|
+ const systemPrompt = buildDynamicPrompt(topic, bookScale, description);
|
|
|
+
|
|
|
+ return [
|
|
|
+ { role: 'system', content: systemPrompt },
|
|
|
+ {
|
|
|
+ role: 'user',
|
|
|
+ content: `书名:《${topic}》\n规模:${SCALE_DESC[bookScale] || '中篇'}\n章节范围:${range.min}-${range.max}章\n${description ? `需求描述:${description}\n` : ''}\n请生成书籍大纲。`,
|
|
|
+ },
|
|
|
+ ];
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 构建节大纲生成消息
|
|
|
+ */
|
|
|
+export function buildSectionMessages(chapterTitle: string, chapterSummary: string, keyPoints: string[]): ChatMessage[] {
|
|
|
+ return [
|
|
|
+ { role: 'system', content: SECTION_SYSTEM_PROMPT },
|
|
|
+ {
|
|
|
+ role: 'user',
|
|
|
+ content: `章标题:${chapterTitle}\n章概述:${chapterSummary}\n核心知识点:${keyPoints.join('、')}\n\n请为该章设计节的大纲(通常每章 2-4 节)。`,
|
|
|
+ },
|
|
|
+ ];
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 构建小节大纲生成消息
|
|
|
+ */
|
|
|
+export function buildSubsectionMessages(sectionTitle: string, sectionSummary: string, keyPoints: string[]): ChatMessage[] {
|
|
|
+ return [
|
|
|
+ { role: 'system', content: SUBSECTION_SYSTEM_PROMPT },
|
|
|
+ {
|
|
|
+ role: 'user',
|
|
|
+ content: `节标题:${sectionTitle}\n节概述:${sectionSummary}\n核心知识点:${keyPoints.join('、')}\n\n请为该节设计小节的大纲(通常每节 2-4 小节)。`,
|
|
|
+ },
|
|
|
+ ];
|
|
|
+}
|