From 1bb3be3942f9db0c341ca7821c021b9eceb5b4f0 Mon Sep 17 00:00:00 2001 From: xiamuceer Date: Sun, 30 Nov 2025 12:25:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:1.=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=B3=BB=E7=BB=9F=E6=A8=A1=E6=9D=BF=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=AF=8D=E7=9A=84=E5=A4=9A=E5=87=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/chapters.py | 4 ++- backend/app/services/chapter_regenerator.py | 14 ++++----- backend/app/services/plot_analyzer.py | 7 ++++- backend/app/services/prompt_service.py | 32 ++++++++++++++++++--- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/backend/app/api/chapters.py b/backend/app/api/chapters.py index 48ae04a..a1fe916 100644 --- a/backend/app/api/chapters.py +++ b/backend/app/api/chapters.py @@ -2709,7 +2709,9 @@ async def regenerate_chapter_stream( analysis=analysis, regenerate_request=regenerate_request, project_context=project_context, - style_content=style_content + style_content=style_content, + user_id=user_id, + db=db_session ): # 处理不同类型的事件 if event['type'] == 'chunk': diff --git a/backend/app/services/chapter_regenerator.py b/backend/app/services/chapter_regenerator.py index 59a7714..6bdd20f 100644 --- a/backend/app/services/chapter_regenerator.py +++ b/backend/app/services/chapter_regenerator.py @@ -67,7 +67,7 @@ class ChapterRegenerator: user_id=user_id, db=db ) - + logger.info(f"🎯 提示词构建完成,开始AI生成") yield {'type': 'progress', 'progress': 15, 'message': '开始AI生成内容...'} @@ -177,11 +177,9 @@ class ChapterRegenerator: db: AsyncSession = None ) -> str: """构建完整的重新生成提示词""" - # 获取自定义提示词模板 - template = await PromptService.get_template("CHAPTER_REGENERATION_SYSTEM", user_id, db) - # 格式化提示词 - return PromptService.format_prompt( - template, + # 使用PromptService的get_chapter_regeneration_prompt方法 + # 该方法会处理自定义模板加载和完整提示词构建 + return await PromptService.get_chapter_regeneration_prompt( chapter_number=chapter.chapter_number, title=chapter.title, word_count=chapter.word_count, @@ -189,7 +187,9 @@ class ChapterRegenerator: modification_instructions=modification_instructions, project_context=project_context, style_content=style_content, - target_word_count=regenerate_request.target_word_count + target_word_count=regenerate_request.target_word_count, + user_id=user_id, + db=db ) def calculate_content_diff( diff --git a/backend/app/services/plot_analyzer.py b/backend/app/services/plot_analyzer.py index 7c2e437..6178de0 100644 --- a/backend/app/services/plot_analyzer.py +++ b/backend/app/services/plot_analyzer.py @@ -53,7 +53,12 @@ class PlotAnalyzer: analysis_content = content[:8000] if len(content) > 8000 else content # 获取自定义提示词模板 - template = await PromptService.get_template("PLOT_ANALYSIS", user_id, db) + if user_id and db: + template = await PromptService.get_template("PLOT_ANALYSIS", user_id, db) + else: + # 降级到系统默认模板 + template = PromptService.PLOT_ANALYSIS + # 格式化提示词 prompt = PromptService.format_prompt( template, diff --git a/backend/app/services/prompt_service.py b/backend/app/services/prompt_service.py index 581a9df..9e12738 100644 --- a/backend/app/services/prompt_service.py +++ b/backend/app/services/prompt_service.py @@ -1901,11 +1901,35 @@ class PromptService: ) @classmethod - def get_chapter_regeneration_prompt(cls, chapter_number: int, title: str, word_count: int, content: str, + async def get_chapter_regeneration_prompt(cls, chapter_number: int, title: str, word_count: int, content: str, modification_instructions: str, project_context: Dict[str, Any], - style_content: str, target_word_count: int) -> str: - """获取章节重写提示词""" - prompt_parts = [cls.CHAPTER_REGENERATION_SYSTEM] + style_content: str, target_word_count: int, + user_id: str = None, db = None) -> str: + """ + 获取章节重写提示词(支持用户自定义) + + Args: + chapter_number: 章节序号 + title: 章节标题 + word_count: 原始字数 + content: 原始内容 + modification_instructions: 修改指令 + project_context: 项目上下文 + style_content: 写作风格 + target_word_count: 目标字数 + user_id: 用户ID(可选,用于获取自定义模板) + db: 数据库会话(可选,用于查询自定义模板) + + Returns: + 完整的章节重写提示词 + """ + # 获取系统提示词模板(支持用户自定义) + if user_id and db: + system_template = await cls.get_template("CHAPTER_REGENERATION_SYSTEM", user_id, db) + else: + system_template = cls.CHAPTER_REGENERATION_SYSTEM + + prompt_parts = [system_template] # 原始章节信息 prompt_parts.append(f"""## 📖 原始章节信息