update:1.更新mcp插件功能,目前只支持remote调用
This commit is contained in:
@@ -5,6 +5,7 @@ from anthropic import AsyncAnthropic
|
||||
from app.config import settings as app_settings
|
||||
from app.logger import get_logger
|
||||
import httpx
|
||||
import json
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -126,10 +127,12 @@ class AIService:
|
||||
model: Optional[str] = None,
|
||||
temperature: Optional[float] = None,
|
||||
max_tokens: Optional[int] = None,
|
||||
system_prompt: Optional[str] = None
|
||||
) -> str:
|
||||
system_prompt: Optional[str] = None,
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
tool_choice: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
生成文本
|
||||
生成文本(支持工具调用)
|
||||
|
||||
Args:
|
||||
prompt: 用户提示词
|
||||
@@ -138,9 +141,14 @@ class AIService:
|
||||
temperature: 温度参数
|
||||
max_tokens: 最大token数
|
||||
system_prompt: 系统提示词
|
||||
tools: 可用工具列表(MCP工具格式)
|
||||
tool_choice: 工具选择策略 (auto/required/none)
|
||||
|
||||
Returns:
|
||||
生成的文本
|
||||
Dict包含:
|
||||
- content: 文本内容(如果没有工具调用)
|
||||
- tool_calls: 工具调用列表(如果AI决定调用工具)
|
||||
- finish_reason: 完成原因
|
||||
"""
|
||||
provider = provider or self.api_provider
|
||||
model = model or self.default_model
|
||||
@@ -148,12 +156,12 @@ class AIService:
|
||||
max_tokens = max_tokens or self.default_max_tokens
|
||||
|
||||
if provider == "openai":
|
||||
return await self._generate_openai(
|
||||
prompt, model, temperature, max_tokens, system_prompt
|
||||
return await self._generate_openai_with_tools(
|
||||
prompt, model, temperature, max_tokens, system_prompt, tools, tool_choice
|
||||
)
|
||||
elif provider == "anthropic":
|
||||
return await self._generate_anthropic(
|
||||
prompt, model, temperature, max_tokens, system_prompt
|
||||
return await self._generate_anthropic_with_tools(
|
||||
prompt, model, temperature, max_tokens, system_prompt, tools, tool_choice
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"不支持的AI提供商: {provider}")
|
||||
@@ -247,6 +255,7 @@ class AIService:
|
||||
logger.info(f"✅ OpenAI API调用成功")
|
||||
logger.info(f" - 响应ID: {data.get('id', 'N/A')}")
|
||||
logger.info(f" - 选项数量: {len(data.get('choices', []))}")
|
||||
logger.debug(f" - 完整API响应: {data}")
|
||||
|
||||
if not data.get('choices'):
|
||||
logger.error("❌ OpenAI返回的choices为空")
|
||||
@@ -294,6 +303,173 @@ class AIService:
|
||||
logger.error(f" - 模型: {model}")
|
||||
raise
|
||||
|
||||
|
||||
async def _generate_openai_with_tools(
|
||||
self,
|
||||
prompt: str,
|
||||
model: str,
|
||||
temperature: float,
|
||||
max_tokens: int,
|
||||
system_prompt: Optional[str],
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
tool_choice: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""使用OpenAI生成文本(支持工具调用)"""
|
||||
if not self.openai_http_client:
|
||||
raise ValueError("OpenAI客户端未初始化,请检查API key配置")
|
||||
|
||||
messages = []
|
||||
if system_prompt:
|
||||
messages.append({"role": "system", "content": system_prompt})
|
||||
messages.append({"role": "user", "content": prompt})
|
||||
|
||||
try:
|
||||
logger.info(f"🔵 开始调用OpenAI API(支持工具调用)")
|
||||
logger.info(f" - 模型: {model}")
|
||||
logger.info(f" - 工具数量: {len(tools) if tools else 0}")
|
||||
|
||||
url = f"{self.openai_base_url}/chat/completions"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.openai_api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"model": model,
|
||||
"messages": messages,
|
||||
"temperature": temperature,
|
||||
"max_tokens": max_tokens
|
||||
}
|
||||
|
||||
# 添加工具参数
|
||||
if tools:
|
||||
payload["tools"] = tools
|
||||
if tool_choice:
|
||||
if tool_choice == "required":
|
||||
payload["tool_choice"] = "required"
|
||||
elif tool_choice == "auto":
|
||||
payload["tool_choice"] = "auto"
|
||||
elif tool_choice == "none":
|
||||
payload["tool_choice"] = "none"
|
||||
|
||||
response = await self.openai_http_client.post(url, headers=headers, json=payload)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
logger.info(f"✅ OpenAI API调用成功")
|
||||
logger.debug(f" - 完整API响应: {data}")
|
||||
|
||||
if not data.get('choices'):
|
||||
logger.error(f"❌ API返回的choices为空")
|
||||
logger.error(f" - 完整响应: {data}")
|
||||
logger.error(f" - 响应键: {list(data.keys())}")
|
||||
raise ValueError(f"API返回的响应格式错误:choices字段为空。完整响应: {data}")
|
||||
|
||||
choice = data['choices'][0]
|
||||
message = choice.get('message', {})
|
||||
finish_reason = choice.get('finish_reason')
|
||||
|
||||
# 检查是否有工具调用
|
||||
tool_calls = message.get('tool_calls')
|
||||
if tool_calls:
|
||||
logger.info(f"🔧 AI请求调用 {len(tool_calls)} 个工具")
|
||||
return {
|
||||
"tool_calls": tool_calls,
|
||||
"content": message.get('content', ''),
|
||||
"finish_reason": finish_reason
|
||||
}
|
||||
|
||||
# 没有工具调用,返回普通内容
|
||||
content = message.get('content', '')
|
||||
if content:
|
||||
return {
|
||||
"content": content,
|
||||
"finish_reason": finish_reason
|
||||
}
|
||||
else:
|
||||
raise ValueError(f"AI返回了空内容(finish_reason: {finish_reason})")
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
logger.error(f"❌ OpenAI API调用失败 (HTTP {e.response.status_code})")
|
||||
logger.error(f" - 错误信息: {e.response.text}")
|
||||
raise Exception(f"API返回错误 ({e.response.status_code}): {e.response.text}")
|
||||
except Exception as e:
|
||||
logger.error(f"❌ OpenAI API调用失败: {str(e)}")
|
||||
raise
|
||||
|
||||
async def _generate_anthropic_with_tools(
|
||||
self,
|
||||
prompt: str,
|
||||
model: str,
|
||||
temperature: float,
|
||||
max_tokens: int,
|
||||
system_prompt: Optional[str],
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
tool_choice: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""使用Anthropic生成文本(支持工具调用)"""
|
||||
if not self.anthropic_client:
|
||||
raise ValueError("Anthropic客户端未初始化,请检查API key配置")
|
||||
|
||||
try:
|
||||
logger.info(f"🔵 开始调用Anthropic API(支持工具调用)")
|
||||
logger.info(f" - 模型: {model}")
|
||||
logger.info(f" - 工具数量: {len(tools) if tools else 0}")
|
||||
|
||||
kwargs = {
|
||||
"model": model,
|
||||
"max_tokens": max_tokens,
|
||||
"temperature": temperature,
|
||||
"messages": [{"role": "user", "content": prompt}]
|
||||
}
|
||||
|
||||
if system_prompt:
|
||||
kwargs["system"] = system_prompt
|
||||
|
||||
# 添加工具参数
|
||||
if tools:
|
||||
kwargs["tools"] = tools
|
||||
if tool_choice == "required":
|
||||
kwargs["tool_choice"] = {"type": "any"}
|
||||
elif tool_choice == "auto":
|
||||
kwargs["tool_choice"] = {"type": "auto"}
|
||||
|
||||
response = await self.anthropic_client.messages.create(**kwargs)
|
||||
|
||||
# 检查是否有工具调用
|
||||
tool_calls = []
|
||||
content_text = ""
|
||||
|
||||
for block in response.content:
|
||||
if block.type == "tool_use":
|
||||
tool_calls.append({
|
||||
"id": block.id,
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": block.name,
|
||||
"arguments": block.input
|
||||
}
|
||||
})
|
||||
elif block.type == "text":
|
||||
content_text += block.text
|
||||
|
||||
if tool_calls:
|
||||
logger.info(f"🔧 AI请求调用 {len(tool_calls)} 个工具")
|
||||
return {
|
||||
"tool_calls": tool_calls,
|
||||
"content": content_text,
|
||||
"finish_reason": response.stop_reason
|
||||
}
|
||||
|
||||
return {
|
||||
"content": content_text,
|
||||
"finish_reason": response.stop_reason
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Anthropic API调用失败: {str(e)}")
|
||||
raise
|
||||
|
||||
async def _generate_openai_stream(
|
||||
self,
|
||||
prompt: str,
|
||||
@@ -456,6 +632,232 @@ class AIService:
|
||||
logger.error(f"❌ Anthropic流式API调用失败: {str(e)}")
|
||||
logger.error(f" - 错误类型: {type(e).__name__}")
|
||||
raise
|
||||
|
||||
async def generate_text_with_mcp(
|
||||
self,
|
||||
prompt: str,
|
||||
user_id: str,
|
||||
db_session,
|
||||
enable_mcp: bool = True,
|
||||
max_tool_rounds: int = 3,
|
||||
tool_choice: str = "auto",
|
||||
**kwargs
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
支持MCP工具的AI文本生成(非流式)
|
||||
|
||||
Args:
|
||||
prompt: 用户提示词
|
||||
user_id: 用户ID,用于获取MCP工具
|
||||
db_session: 数据库会话
|
||||
enable_mcp: 是否启用MCP增强
|
||||
max_tool_rounds: 最大工具调用轮次
|
||||
tool_choice: 工具选择策略(auto/required/none)
|
||||
**kwargs: 其他AI参数(provider, model, temperature等)
|
||||
|
||||
Returns:
|
||||
{
|
||||
"content": "AI生成的最终文本",
|
||||
"tool_calls_made": 2, # 实际调用的工具次数
|
||||
"tools_used": ["exa_search", "filesystem_read"],
|
||||
"finish_reason": "stop",
|
||||
"mcp_enhanced": True
|
||||
}
|
||||
"""
|
||||
from app.services.mcp_tool_service import mcp_tool_service, MCPToolServiceError
|
||||
|
||||
# 初始化返回结果
|
||||
result = {
|
||||
"content": "",
|
||||
"tool_calls_made": 0,
|
||||
"tools_used": [],
|
||||
"finish_reason": "",
|
||||
"mcp_enhanced": False
|
||||
}
|
||||
|
||||
# 1. 获取MCP工具(如果启用)
|
||||
tools = None
|
||||
if enable_mcp:
|
||||
try:
|
||||
tools = await mcp_tool_service.get_user_enabled_tools(
|
||||
user_id=user_id,
|
||||
db_session=db_session
|
||||
)
|
||||
if tools:
|
||||
logger.info(f"MCP增强: 加载了 {len(tools)} 个工具")
|
||||
result["mcp_enhanced"] = True
|
||||
except MCPToolServiceError as e:
|
||||
logger.error(f"获取MCP工具失败,降级为普通生成: {e}")
|
||||
tools = None
|
||||
|
||||
# 2. 工具调用循环
|
||||
conversation_history = [
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
|
||||
for round_num in range(max_tool_rounds):
|
||||
logger.info(f"MCP工具调用轮次: {round_num + 1}/{max_tool_rounds}")
|
||||
|
||||
# 调用AI
|
||||
ai_response = await self.generate_text(
|
||||
prompt=conversation_history[-1]["content"],
|
||||
tools=tools if round_num == 0 else None, # 只在第一轮传递工具
|
||||
tool_choice=tool_choice if round_num == 0 else None,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
# 检查是否有工具调用
|
||||
tool_calls = ai_response.get("tool_calls", [])
|
||||
|
||||
if not tool_calls:
|
||||
# AI返回最终内容
|
||||
result["content"] = ai_response.get("content", "")
|
||||
result["finish_reason"] = ai_response.get("finish_reason", "stop")
|
||||
break
|
||||
|
||||
# 3. 执行工具调用
|
||||
logger.info(f"AI请求调用 {len(tool_calls)} 个工具")
|
||||
|
||||
try:
|
||||
tool_results = await mcp_tool_service.execute_tool_calls(
|
||||
user_id=user_id,
|
||||
tool_calls=tool_calls,
|
||||
db_session=db_session
|
||||
)
|
||||
|
||||
# 记录使用的工具
|
||||
for tool_call in tool_calls:
|
||||
tool_name = tool_call["function"]["name"]
|
||||
if tool_name not in result["tools_used"]:
|
||||
result["tools_used"].append(tool_name)
|
||||
|
||||
result["tool_calls_made"] += len(tool_calls)
|
||||
|
||||
# 4. 构建工具上下文
|
||||
tool_context = await mcp_tool_service.build_tool_context(
|
||||
tool_results,
|
||||
format="markdown"
|
||||
)
|
||||
|
||||
# 5. 更新对话历史
|
||||
conversation_history.append({
|
||||
"role": "assistant",
|
||||
"content": ai_response.get("content", ""),
|
||||
"tool_calls": tool_calls
|
||||
})
|
||||
|
||||
for tool_result in tool_results:
|
||||
conversation_history.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": tool_result["tool_call_id"],
|
||||
"content": tool_result["content"]
|
||||
})
|
||||
|
||||
# 6. 构建下一轮提示
|
||||
next_prompt = (
|
||||
f"{prompt}\n\n"
|
||||
f"{tool_context}\n\n"
|
||||
f"请基于以上工具查询结果,继续完成任务。"
|
||||
)
|
||||
conversation_history.append({
|
||||
"role": "user",
|
||||
"content": next_prompt
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"执行MCP工具失败: {e}", exc_info=True)
|
||||
# 降级:返回当前AI响应
|
||||
result["content"] = ai_response.get("content", "")
|
||||
result["finish_reason"] = "tool_error"
|
||||
break
|
||||
|
||||
else:
|
||||
# 达到最大轮次
|
||||
logger.warning(f"达到MCP最大调用轮次 {max_tool_rounds}")
|
||||
result["content"] = conversation_history[-1].get("content", "")
|
||||
result["finish_reason"] = "max_rounds"
|
||||
|
||||
return result
|
||||
|
||||
async def generate_text_stream_with_mcp(
|
||||
self,
|
||||
prompt: str,
|
||||
user_id: str,
|
||||
db_session,
|
||||
enable_mcp: bool = True,
|
||||
mcp_planning_prompt: Optional[str] = None,
|
||||
**kwargs
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
支持MCP工具的AI流式文本生成(两阶段模式)
|
||||
|
||||
Args:
|
||||
prompt: 用户提示词
|
||||
user_id: 用户ID
|
||||
db_session: 数据库会话
|
||||
enable_mcp: 是否启用MCP增强
|
||||
mcp_planning_prompt: MCP规划阶段的提示词(可选)
|
||||
**kwargs: 其他AI参数
|
||||
|
||||
Yields:
|
||||
流式文本chunk
|
||||
"""
|
||||
from app.services.mcp_tool_service import mcp_tool_service
|
||||
|
||||
# 阶段1: 工具调用阶段(非流式)
|
||||
enhanced_prompt = prompt
|
||||
|
||||
if enable_mcp:
|
||||
try:
|
||||
# 获取MCP工具
|
||||
tools = await mcp_tool_service.get_user_enabled_tools(
|
||||
user_id=user_id,
|
||||
db_session=db_session
|
||||
)
|
||||
|
||||
if tools:
|
||||
logger.info(f"MCP增强(流式): 加载了 {len(tools)} 个工具")
|
||||
|
||||
# 使用规划提示让AI决定需要查询什么
|
||||
if not mcp_planning_prompt:
|
||||
mcp_planning_prompt = (
|
||||
f"任务: {prompt}\n\n"
|
||||
f"请分析这个任务,决定是否需要查询外部信息。"
|
||||
f"如果需要,请调用相应的工具获取信息。"
|
||||
)
|
||||
|
||||
# 非流式调用获取工具结果
|
||||
planning_result = await self.generate_text_with_mcp(
|
||||
prompt=mcp_planning_prompt,
|
||||
user_id=user_id,
|
||||
db_session=db_session,
|
||||
enable_mcp=True,
|
||||
max_tool_rounds=2,
|
||||
tool_choice="auto",
|
||||
**kwargs
|
||||
)
|
||||
|
||||
# 如果有工具调用,将结果融入提示
|
||||
if planning_result["tool_calls_made"] > 0:
|
||||
enhanced_prompt = (
|
||||
f"{prompt}\n\n"
|
||||
f"【参考资料】\n"
|
||||
f"{planning_result.get('content', '')}"
|
||||
)
|
||||
logger.info(
|
||||
f"MCP工具规划完成,调用了 "
|
||||
f"{planning_result['tool_calls_made']} 次工具"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"MCP工具规划失败,使用原始提示: {e}")
|
||||
|
||||
# 阶段2: 内容生成阶段(流式)
|
||||
async for chunk in self.generate_text_stream(
|
||||
prompt=enhanced_prompt,
|
||||
**kwargs
|
||||
):
|
||||
yield chunk
|
||||
|
||||
|
||||
# 创建全局AI服务实例
|
||||
|
||||
@@ -0,0 +1,355 @@
|
||||
"""MCP工具服务 - 统一管理MCP工具的注入和执行"""
|
||||
|
||||
from typing import List, Dict, Any, Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
import asyncio
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from app.models.mcp_plugin import MCPPlugin
|
||||
from app.mcp.registry import mcp_registry
|
||||
from app.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class MCPToolServiceError(Exception):
|
||||
"""MCP工具服务异常"""
|
||||
pass
|
||||
|
||||
|
||||
class MCPToolService:
|
||||
"""MCP工具服务 - 统一管理MCP工具的注入和执行"""
|
||||
|
||||
def __init__(self):
|
||||
self._tool_cache = {} # 工具定义缓存
|
||||
self._result_cache = {} # 工具结果缓存(可选)
|
||||
|
||||
async def get_user_enabled_tools(
|
||||
self,
|
||||
user_id: str,
|
||||
db_session: AsyncSession,
|
||||
category: Optional[str] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
获取用户启用的MCP工具列表
|
||||
|
||||
Args:
|
||||
user_id: 用户ID
|
||||
db_session: 数据库会话
|
||||
category: 工具类别筛选(search/analysis/filesystem等)
|
||||
|
||||
Returns:
|
||||
工具定义列表,格式符合OpenAI Function Calling规范
|
||||
"""
|
||||
try:
|
||||
# 1. 查询用户启用的插件(enabled=True即可,不强制要求status=active)
|
||||
# 因为新启用的插件status可能还是inactive,需要给它机会被调用
|
||||
query = select(MCPPlugin).where(
|
||||
MCPPlugin.user_id == user_id,
|
||||
MCPPlugin.enabled == True
|
||||
)
|
||||
|
||||
if category:
|
||||
query = query.where(MCPPlugin.category == category)
|
||||
|
||||
result = await db_session.execute(query)
|
||||
plugins = result.scalars().all()
|
||||
|
||||
if not plugins:
|
||||
logger.info(f"用户 {user_id} 没有启用的MCP插件")
|
||||
return []
|
||||
|
||||
# 2. 获取所有工具定义
|
||||
all_tools = []
|
||||
for plugin in plugins:
|
||||
try:
|
||||
# 确保插件已加载到注册表
|
||||
if not mcp_registry.get_client(user_id, plugin.plugin_name):
|
||||
logger.info(f"插件 {plugin.plugin_name} 未加载,尝试加载...")
|
||||
success = await mcp_registry.load_plugin(plugin)
|
||||
if not success:
|
||||
logger.warning(f"插件 {plugin.plugin_name} 加载失败,跳过")
|
||||
continue
|
||||
|
||||
# 从registry获取该插件的工具列表
|
||||
plugin_tools = await mcp_registry.get_plugin_tools(
|
||||
user_id=user_id,
|
||||
plugin_name=plugin.plugin_name
|
||||
)
|
||||
|
||||
# 格式化为Function Calling格式
|
||||
formatted_tools = self._format_tools_for_ai(
|
||||
plugin_tools,
|
||||
plugin.plugin_name # ✅ 修复:使用正确的属性名plugin_name
|
||||
)
|
||||
all_tools.extend(formatted_tools)
|
||||
|
||||
logger.info(
|
||||
f"从插件 {plugin.plugin_name} 加载了 "
|
||||
f"{len(formatted_tools)} 个工具"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"获取插件 {plugin.plugin_name} 的工具失败: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
continue
|
||||
|
||||
logger.info(f"用户 {user_id} 共加载 {len(all_tools)} 个MCP工具")
|
||||
return all_tools
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取用户MCP工具失败: {e}", exc_info=True)
|
||||
raise MCPToolServiceError(f"获取MCP工具失败: {str(e)}")
|
||||
|
||||
def _format_tools_for_ai(
|
||||
self,
|
||||
plugin_tools: List[Dict[str, Any]],
|
||||
plugin_name: str
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
将MCP工具定义格式化为AI Function Calling格式
|
||||
|
||||
Args:
|
||||
plugin_tools: MCP插件的工具列表
|
||||
plugin_name: 插件名称
|
||||
|
||||
Returns:
|
||||
格式化后的工具列表
|
||||
"""
|
||||
formatted_tools = []
|
||||
|
||||
for tool in plugin_tools:
|
||||
formatted_tool = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": f"{plugin_name}_{tool['name']}", # 加插件前缀避免冲突
|
||||
"description": tool.get("description", ""),
|
||||
"parameters": tool.get("inputSchema", {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
})
|
||||
}
|
||||
}
|
||||
formatted_tools.append(formatted_tool)
|
||||
|
||||
return formatted_tools
|
||||
|
||||
async def execute_tool_calls(
|
||||
self,
|
||||
user_id: str,
|
||||
tool_calls: List[Dict[str, Any]],
|
||||
db_session: AsyncSession,
|
||||
timeout: float = 60.0
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
批量执行AI请求的工具调用(并行执行)
|
||||
|
||||
Args:
|
||||
user_id: 用户ID
|
||||
tool_calls: AI返回的工具调用列表
|
||||
db_session: 数据库会话
|
||||
timeout: 单个工具调用的超时时间(秒,默认30秒)
|
||||
|
||||
Returns:
|
||||
工具调用结果列表
|
||||
"""
|
||||
if not tool_calls:
|
||||
return []
|
||||
|
||||
logger.info(f"开始执行 {len(tool_calls)} 个工具调用")
|
||||
|
||||
# 创建异步任务列表
|
||||
tasks = [
|
||||
self._execute_single_tool(
|
||||
user_id=user_id,
|
||||
tool_call=tool_call,
|
||||
db_session=db_session,
|
||||
timeout=timeout
|
||||
)
|
||||
for tool_call in tool_calls
|
||||
]
|
||||
|
||||
# 并行执行所有工具调用
|
||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||
|
||||
# 处理结果
|
||||
formatted_results = []
|
||||
for i, result in enumerate(results):
|
||||
tool_call = tool_calls[i]
|
||||
|
||||
if isinstance(result, Exception):
|
||||
# 工具调用异常
|
||||
formatted_results.append({
|
||||
"tool_call_id": tool_call.get("id", f"call_{i}"),
|
||||
"role": "tool",
|
||||
"name": tool_call["function"]["name"],
|
||||
"content": f"工具调用失败: {str(result)}",
|
||||
"success": False,
|
||||
"error": str(result)
|
||||
})
|
||||
else:
|
||||
formatted_results.append(result)
|
||||
|
||||
return formatted_results
|
||||
|
||||
async def _execute_single_tool(
|
||||
self,
|
||||
user_id: str,
|
||||
tool_call: Dict[str, Any],
|
||||
db_session: AsyncSession,
|
||||
timeout: float
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
执行单个工具调用
|
||||
|
||||
Args:
|
||||
user_id: 用户ID
|
||||
tool_call: 工具调用信息
|
||||
db_session: 数据库会话
|
||||
timeout: 超时时间
|
||||
|
||||
Returns:
|
||||
工具调用结果
|
||||
"""
|
||||
tool_call_id = tool_call.get("id", "unknown")
|
||||
function_name = tool_call["function"]["name"]
|
||||
|
||||
try:
|
||||
# 解析插件名和工具名
|
||||
if "_" in function_name:
|
||||
plugin_name, tool_name = function_name.split("_", 1)
|
||||
else:
|
||||
raise ValueError(f"无效的工具名称格式: {function_name}")
|
||||
|
||||
# 解析参数
|
||||
arguments_str = tool_call["function"]["arguments"]
|
||||
if isinstance(arguments_str, str):
|
||||
arguments = json.loads(arguments_str)
|
||||
else:
|
||||
arguments = arguments_str
|
||||
|
||||
logger.info(
|
||||
f"执行工具: {plugin_name}.{tool_name}, "
|
||||
f"参数: {arguments}"
|
||||
)
|
||||
|
||||
# 设置超时
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
mcp_registry.call_tool(
|
||||
user_id=user_id,
|
||||
plugin_name=plugin_name,
|
||||
tool_name=tool_name,
|
||||
arguments=arguments
|
||||
),
|
||||
timeout=timeout
|
||||
)
|
||||
|
||||
# 成功返回
|
||||
return {
|
||||
"tool_call_id": tool_call_id,
|
||||
"role": "tool",
|
||||
"name": function_name,
|
||||
"content": json.dumps(result, ensure_ascii=False),
|
||||
"success": True,
|
||||
"error": None
|
||||
}
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
raise MCPToolServiceError(
|
||||
f"工具调用超时(>{timeout}秒)"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"工具 {function_name} 调用失败: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
return {
|
||||
"tool_call_id": tool_call_id,
|
||||
"role": "tool",
|
||||
"name": function_name,
|
||||
"content": f"工具调用失败: {str(e)}",
|
||||
"success": False,
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
async def build_tool_context(
|
||||
self,
|
||||
tool_results: List[Dict[str, Any]],
|
||||
format: str = "markdown"
|
||||
) -> str:
|
||||
"""
|
||||
将工具调用结果格式化为上下文文本
|
||||
|
||||
Args:
|
||||
tool_results: 工具调用结果列表
|
||||
format: 输出格式(markdown/json/plain)
|
||||
|
||||
Returns:
|
||||
格式化的上下文字符串
|
||||
"""
|
||||
if not tool_results:
|
||||
return ""
|
||||
|
||||
if format == "markdown":
|
||||
return self._build_markdown_context(tool_results)
|
||||
elif format == "json":
|
||||
return json.dumps(tool_results, ensure_ascii=False, indent=2)
|
||||
else: # plain
|
||||
return self._build_plain_context(tool_results)
|
||||
|
||||
def _build_markdown_context(
|
||||
self,
|
||||
tool_results: List[Dict[str, Any]]
|
||||
) -> str:
|
||||
"""构建Markdown格式的工具上下文"""
|
||||
lines = ["## 🔧 工具调用结果\n"]
|
||||
|
||||
for i, result in enumerate(tool_results, 1):
|
||||
tool_name = result.get("name", "unknown")
|
||||
success = result.get("success", False)
|
||||
content = result.get("content", "")
|
||||
|
||||
status_emoji = "✅" if success else "❌"
|
||||
lines.append(f"### {status_emoji} {i}. {tool_name}\n")
|
||||
|
||||
if success:
|
||||
# 尝试美化JSON内容
|
||||
try:
|
||||
content_obj = json.loads(content)
|
||||
content = json.dumps(content_obj, ensure_ascii=False, indent=2)
|
||||
except:
|
||||
pass
|
||||
lines.append(f"```json\n{content}\n```\n")
|
||||
else:
|
||||
lines.append(f"**错误**: {content}\n")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _build_plain_context(
|
||||
self,
|
||||
tool_results: List[Dict[str, Any]]
|
||||
) -> str:
|
||||
"""构建纯文本格式的工具上下文"""
|
||||
lines = ["=== 工具调用结果 ===\n"]
|
||||
|
||||
for i, result in enumerate(tool_results, 1):
|
||||
tool_name = result.get("name", "unknown")
|
||||
success = result.get("success", False)
|
||||
content = result.get("content", "")
|
||||
|
||||
status = "成功" if success else "失败"
|
||||
lines.append(f"{i}. {tool_name} - {status}")
|
||||
lines.append(f" 结果: {content}\n")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# 全局单例
|
||||
mcp_tool_service = MCPToolService()
|
||||
@@ -225,8 +225,22 @@ class PlotAnalyzer:
|
||||
temperature=0.3 # 降低温度以获得更稳定的JSON输出
|
||||
)
|
||||
|
||||
# 🔍 添加调试日志:查看AI返回的原始内容
|
||||
logger.info(f"🔍 AI返回类型: {type(response)}")
|
||||
logger.info(f"🔍 AI返回内容(前500字符): {str(response)}")
|
||||
|
||||
# 从返回的字典中提取content字段
|
||||
if isinstance(response, dict):
|
||||
response_text = response.get('content', '')
|
||||
if not response_text:
|
||||
logger.error("❌ AI返回的字典中没有content字段或content为空")
|
||||
return None
|
||||
else:
|
||||
# 兼容旧的字符串返回格式
|
||||
response_text = response
|
||||
|
||||
# 解析JSON结果
|
||||
analysis_result = self._parse_analysis_response(response)
|
||||
analysis_result = self._parse_analysis_response(response_text)
|
||||
|
||||
if analysis_result:
|
||||
logger.info(f"✅ 第{chapter_number}章分析完成")
|
||||
|
||||
@@ -282,6 +282,8 @@ class PromptService:
|
||||
角色信息:
|
||||
{characters_info}
|
||||
|
||||
{mcp_references}
|
||||
|
||||
其他要求:{requirements}
|
||||
|
||||
整体要求:
|
||||
@@ -356,6 +358,8 @@ class PromptService:
|
||||
|
||||
{memory_context}
|
||||
|
||||
{mcp_references}
|
||||
|
||||
【续写指导】
|
||||
- 当前情节阶段:{plot_stage_instruction}
|
||||
- 起始章节编号:第{start_chapter}章
|
||||
@@ -836,8 +840,17 @@ class PromptService:
|
||||
chapter_count: int, narrative_perspective: str,
|
||||
target_words: int, time_period: str, location: str,
|
||||
atmosphere: str, rules: str, characters_info: str,
|
||||
requirements: str = "") -> str:
|
||||
"""获取向导大纲生成提示词"""
|
||||
requirements: str = "",
|
||||
mcp_references: str = "") -> str:
|
||||
"""获取向导大纲生成提示词(支持MCP增强)"""
|
||||
# 格式化MCP参考资料
|
||||
mcp_text = ""
|
||||
if mcp_references:
|
||||
mcp_text = "【📚 MCP工具搜索 - 情节设计参考】\n"
|
||||
mcp_text += "以下是通过MCP工具搜索到的情节设计参考资料,可用于设计大纲结构和情节发展:\n\n"
|
||||
mcp_text += mcp_references
|
||||
mcp_text += "\n"
|
||||
|
||||
return cls.format_prompt(
|
||||
cls.COMPLETE_OUTLINE_GENERATION,
|
||||
title=title,
|
||||
@@ -851,6 +864,7 @@ class PromptService:
|
||||
atmosphere=atmosphere,
|
||||
rules=rules,
|
||||
characters_info=characters_info,
|
||||
mcp_references=mcp_text,
|
||||
requirements=requirements or "无特殊要求"
|
||||
)
|
||||
|
||||
@@ -862,7 +876,8 @@ class PromptService:
|
||||
chapter_number: int, chapter_title: str,
|
||||
chapter_outline: str, style_content: str = "",
|
||||
target_word_count: int = 3000,
|
||||
memory_context: dict = None) -> str:
|
||||
memory_context: dict = None,
|
||||
mcp_references: str = "") -> str:
|
||||
"""
|
||||
获取章节完整创作提示词
|
||||
|
||||
@@ -870,6 +885,7 @@ class PromptService:
|
||||
style_content: 写作风格要求内容,如果提供则会追加到提示词中
|
||||
target_word_count: 目标字数,默认3000字
|
||||
memory_context: 记忆上下文(可选)
|
||||
mcp_references: MCP工具搜索的参考资料(可选)
|
||||
"""
|
||||
# 计算最大字数(目标字数+1000)
|
||||
max_word_count = target_word_count + 1000
|
||||
@@ -884,6 +900,14 @@ class PromptService:
|
||||
memory_text += "\n" + memory_context.get('character_states', '')
|
||||
memory_text += "\n" + memory_context.get('plot_points', '')
|
||||
|
||||
# 格式化MCP参考资料
|
||||
mcp_text = ""
|
||||
if mcp_references:
|
||||
mcp_text = "\n【📚 MCP工具搜索 - 参考资料】\n"
|
||||
mcp_text += "以下是通过MCP工具搜索到的相关参考资料,可用于丰富情节和细节:\n\n"
|
||||
mcp_text += mcp_references
|
||||
mcp_text += "\n"
|
||||
|
||||
base_prompt = cls.format_prompt(
|
||||
cls.CHAPTER_GENERATION,
|
||||
title=title,
|
||||
@@ -903,11 +927,17 @@ class PromptService:
|
||||
max_word_count=max_word_count
|
||||
)
|
||||
|
||||
# 插入记忆上下文
|
||||
# 插入记忆上下文和MCP参考资料
|
||||
insert_text = ""
|
||||
if memory_text:
|
||||
insert_text += memory_text
|
||||
if mcp_text:
|
||||
insert_text += mcp_text
|
||||
|
||||
if insert_text:
|
||||
base_prompt = base_prompt.replace(
|
||||
"本章信息:",
|
||||
memory_text + "\n\n本章信息:"
|
||||
insert_text + "\n\n本章信息:"
|
||||
)
|
||||
|
||||
# 如果有风格要求,应用到提示词中
|
||||
@@ -925,7 +955,8 @@ class PromptService:
|
||||
chapter_title: str, chapter_outline: str,
|
||||
style_content: str = "",
|
||||
target_word_count: int = 3000,
|
||||
memory_context: dict = None) -> str:
|
||||
memory_context: dict = None,
|
||||
mcp_references: str = "") -> str:
|
||||
"""
|
||||
获取章节完整创作提示词(带前置章节上下文和记忆增强)
|
||||
|
||||
@@ -933,6 +964,7 @@ class PromptService:
|
||||
style_content: 写作风格要求内容,如果提供则会追加到提示词中
|
||||
target_word_count: 目标字数,默认3000字
|
||||
memory_context: 记忆上下文(可选)
|
||||
mcp_references: MCP工具搜索的参考资料(可选)
|
||||
"""
|
||||
# 计算最大字数(目标字数+1000)
|
||||
max_word_count = target_word_count + 1000
|
||||
@@ -948,6 +980,12 @@ class PromptService:
|
||||
else:
|
||||
memory_text = "暂无相关记忆"
|
||||
|
||||
# 格式化MCP参考资料
|
||||
if mcp_references:
|
||||
memory_text += "\n\n【📚 MCP工具搜索 - 参考资料】\n"
|
||||
memory_text += "以下是通过MCP工具搜索到的相关参考资料,可用于丰富情节和细节:\n\n"
|
||||
memory_text += mcp_references
|
||||
|
||||
base_prompt = cls.format_prompt(
|
||||
cls.CHAPTER_GENERATION_WITH_CONTEXT,
|
||||
title=title,
|
||||
@@ -996,8 +1034,9 @@ class PromptService:
|
||||
recent_plot: str, plot_stage_instruction: str,
|
||||
start_chapter: int, story_direction: str,
|
||||
requirements: str = "",
|
||||
memory_context: dict = None) -> str:
|
||||
"""获取大纲续写提示词(支持记忆增强)"""
|
||||
memory_context: dict = None,
|
||||
mcp_references: str = "") -> str:
|
||||
"""获取大纲续写提示词(支持记忆+MCP增强)"""
|
||||
end_chapter = start_chapter + chapter_count - 1
|
||||
|
||||
# 格式化记忆上下文
|
||||
@@ -1011,6 +1050,14 @@ class PromptService:
|
||||
else:
|
||||
memory_text = "暂无相关记忆(可能是首次续写或记忆库为空)"
|
||||
|
||||
# 格式化MCP参考资料
|
||||
mcp_text = ""
|
||||
if mcp_references:
|
||||
mcp_text = "\n\n【📚 MCP工具搜索 - 续写参考资料】\n"
|
||||
mcp_text += "以下是通过MCP工具搜索到的续写参考资料,可用于丰富情节发展和冲突设计:\n\n"
|
||||
mcp_text += mcp_references
|
||||
mcp_text += "\n"
|
||||
|
||||
return cls.format_prompt(
|
||||
cls.OUTLINE_CONTINUE_GENERATION,
|
||||
title=title,
|
||||
@@ -1031,7 +1078,8 @@ class PromptService:
|
||||
end_chapter=end_chapter,
|
||||
story_direction=story_direction,
|
||||
requirements=requirements or "无特殊要求",
|
||||
memory_context=memory_text
|
||||
memory_context=memory_text,
|
||||
mcp_references=mcp_text
|
||||
)
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user