From 19e08d99b3797cbcd25a0b18b98055c7f7955b0b Mon Sep 17 00:00:00 2001 From: xiamuceer Date: Sun, 30 Nov 2025 11:46:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:1.=E4=BF=AE=E5=A4=8Dmcp=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=AF=8D=E6=B2=A1=E6=9C=89=E5=8A=A0=E8=BD=BD=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E8=AF=8D=E6=A8=A1=E6=9D=BF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/wizard_stream.py | 76 ++++++++---------------- backend/app/services/mcp_test_service.py | 8 ++- backend/app/services/prompt_service.py | 45 ++++++++++++-- 3 files changed, 70 insertions(+), 59 deletions(-) diff --git a/backend/app/api/wizard_stream.py b/backend/app/api/wizard_stream.py index ef1776d..9bc6473 100644 --- a/backend/app/api/wizard_stream.py +++ b/backend/app/api/wizard_stream.py @@ -83,23 +83,14 @@ async def world_building_generator( if available_tools: yield await SSEResponse.send_progress("🔍 尝试使用MCP工具收集参考资料...", 18) - # 构建资料收集提示词 - planning_prompt = f"""你正在为小说《{title}》设计世界观。 - -【小说信息】 -- 题材:{genre} -- 主题:{theme} -- 简介:{description} - -【任务】 -请使用可用工具搜索相关背景资料,帮助构建更真实、更有深度的世界观设定。 -你可以查询: -1. 历史背景(如果是历史题材) -2. 地理环境和文化特征 -3. 相关领域的专业知识 -4. 类似作品的设定参考 - -请查询最关键的1个问题(不要超过1个)。""" + mcp_template = await PromptService.get_template("MCP_WORLD_BUILDING_PLANNING", user_id, db) + planning_prompt = PromptService.format_prompt( + mcp_template, + title=title, + genre=genre, + theme=theme, + description=description + ) # 调用MCP增强的AI(非流式,最多1轮工具调用,避免超时) planning_result = await user_ai_service.generate_text_with_mcp( @@ -357,24 +348,15 @@ async def characters_generator( if available_tools: yield await SSEResponse.send_progress("🔍 尝试使用MCP工具收集角色参考资料...", 8) - # 构建角色资料收集提示词 - planning_prompt = f"""你正在为小说《{project.title}》设计角色。 - -【小说信息】 -- 题材:{genre or project.genre} -- 主题:{theme or project.theme} -- 时代背景:{world_context.get('time_period', '未设定')} -- 地理位置:{world_context.get('location', '未设定')} - -【任务】 -请使用可用工具搜索相关参考资料,帮助设计更真实、更有深度的角色。 -你可以查询: -1. 该时代/地域的真实历史人物特征 -2. 文化背景和社会习俗 -3. 职业特点和生活方式 -4. 相关领域的人物原型 - -请查询最关键的1个问题(不要超过1个)。""" + mcp_template = await PromptService.get_template("MCP_CHARACTER_PLANNING", user_id, db) + planning_prompt = PromptService.format_prompt( + mcp_template, + title=project.title, + genre=genre or project.genre, + theme=theme or project.theme, + time_period=world_context.get('time_period', '未设定'), + location=world_context.get('location', '未设定') + ) # 调用MCP增强的AI(非流式,最多1轮工具调用,避免超时) planning_result = await user_ai_service.generate_text_with_mcp( @@ -1185,22 +1167,14 @@ async def world_building_regenerate_generator( if available_tools: yield await SSEResponse.send_progress("🔍 尝试使用MCP工具收集参考资料...", 18) - planning_prompt = f"""你正在为小说《{project.title}》重新设计世界观。 - -【小说信息】 -- 题材:{project.genre} -- 主题:{project.theme} -- 简介:{project.description or '未设定'} - -【任务】 -请使用可用工具搜索相关背景资料,帮助构建更真实、更有深度的世界观设定。 -你可以查询: -1. 历史背景(如果是历史题材) -2. 地理环境和文化特征 -3. 相关领域的专业知识 -4. 类似作品的设定参考 - -请查询最关键的1个问题(不要超过1个)。""" + mcp_template = await PromptService.get_template("MCP_WORLD_BUILDING_PLANNING", user_id, db) + planning_prompt = PromptService.format_prompt( + mcp_template, + title=project.title, + genre=project.genre, + theme=project.theme, + description=project.description or '未设定' + ) planning_result = await user_ai_service.generate_text_with_mcp( prompt=planning_prompt, diff --git a/backend/app/services/mcp_test_service.py b/backend/app/services/mcp_test_service.py index e8235f2..098c64b 100644 --- a/backend/app/services/mcp_test_service.py +++ b/backend/app/services/mcp_test_service.py @@ -168,8 +168,12 @@ class MCPTestService: logger.info(f"📋 转换后的OpenAI工具数量: {len(openai_tools)}") logger.debug(f"📋 OpenAI工具列表: {[t['function']['name'] for t in openai_tools]}") - # 调用AI选择工具 - prompts = prompt_service.get_mcp_tool_test_prompts(plugin.plugin_name) + # 调用AI选择工具(使用自定义模板系统) + prompts = await prompt_service.get_mcp_tool_test_prompts( + plugin_name=plugin.plugin_name, + user_id=user.user_id, + db=db_session + ) ai_response = await ai_service.generate_text( prompt=prompts["user"], diff --git a/backend/app/services/prompt_service.py b/backend/app/services/prompt_service.py index db9a6c4..581a9df 100644 --- a/backend/app/services/prompt_service.py +++ b/backend/app/services/prompt_service.py @@ -2018,11 +2018,38 @@ class PromptService: } @classmethod - def get_mcp_tool_test_prompts(cls, plugin_name: str) -> Dict[str, str]: - """获取MCP工具测试的提示词""" + async def get_mcp_tool_test_prompts( + cls, + plugin_name: str, + user_id: str = None, + db = None + ) -> Dict[str, str]: + """ + 获取MCP工具测试的提示词(支持自定义) + + Args: + plugin_name: 插件名称 + user_id: 用户ID(可选) + db: 数据库会话(可选) + + Returns: + 包含user和system提示词的字典 + """ + # 获取用户自定义或系统默认的user提示词 + if user_id and db: + user_template = await cls.get_template("MCP_TOOL_TEST", user_id, db) + else: + user_template = cls.MCP_TOOL_TEST + + # 获取用户自定义或系统默认的system提示词 + if user_id and db: + system_template = await cls.get_template("MCP_TOOL_TEST_SYSTEM", user_id, db) + else: + system_template = cls.MCP_TOOL_TEST_SYSTEM + return { - "user": cls.format_prompt(cls.MCP_TOOL_TEST, plugin_name=plugin_name), - "system": cls.MCP_TOOL_TEST_SYSTEM + "user": cls.format_prompt(user_template, plugin_name=plugin_name), + "system": system_template } # 创建全局提示词服务实例 @@ -2233,11 +2260,17 @@ class PromptService: "end_index", "target_chapter_count", "scene_instruction", "scene_field"] }, "MCP_TOOL_TEST": { - "name": "MCP工具测试", + "name": "MCP工具测试(用户提示词)", "category": "MCP测试", - "description": "用于测试MCP插件功能", + "description": "用于测试MCP插件功能的用户提示词", "parameters": ["plugin_name"] }, + "MCP_TOOL_TEST_SYSTEM": { + "name": "MCP工具测试(系统提示词)", + "category": "MCP测试", + "description": "用于测试MCP插件功能的系统提示词", + "parameters": [] + }, "MCP_WORLD_BUILDING_PLANNING": { "name": "MCP世界观规划", "category": "MCP增强",