2025-09-06 21:06:18 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
2025-10-27 10:33:34 +08:00
|
|
|
TSP Agent助手 - 简化版本
|
|
|
|
|
提供基本的Agent功能和工具管理
|
2025-09-06 21:06:18 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import asyncio
|
|
|
|
|
from typing import Dict, Any, List, Optional
|
|
|
|
|
from datetime import datetime
|
2026-02-11 00:08:09 +08:00
|
|
|
from src.config.unified_config import get_config
|
|
|
|
|
from src.agent.llm_client import LLMManager
|
2025-09-06 21:06:18 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
class TSPAgentAssistant:
|
2026-02-11 00:08:09 +08:00
|
|
|
"""TSP Agent助手"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2025-10-27 10:33:34 +08:00
|
|
|
# 初始化基础功能
|
2026-02-11 00:08:09 +08:00
|
|
|
config = get_config()
|
|
|
|
|
self.llm_manager = LLMManager(config.llm)
|
2025-10-27 10:33:34 +08:00
|
|
|
self.is_agent_mode = True
|
|
|
|
|
self.execution_history = []
|
2026-02-11 00:08:09 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
# 工具注册表
|
|
|
|
|
self.tools = {}
|
|
|
|
|
self.tool_performance = {}
|
2026-02-11 00:08:09 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
# AI监控状态
|
|
|
|
|
self.ai_monitoring_active = False
|
|
|
|
|
self.monitoring_thread = None
|
2026-02-11 00:08:09 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.info("TSP Agent助手初始化完成")
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def register_tool(self, name: str, func, metadata: Dict[str, Any] = None):
|
|
|
|
|
"""注册工具"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
self.tools[name] = {
|
|
|
|
|
"function": func,
|
|
|
|
|
"metadata": metadata or {},
|
|
|
|
|
"usage_count": 0,
|
|
|
|
|
"success_count": 0,
|
|
|
|
|
"last_used": None
|
2025-09-06 21:06:18 +08:00
|
|
|
}
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.info(f"工具 {name} 注册成功")
|
|
|
|
|
return True
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.error(f"注册工具 {name} 失败: {e}")
|
|
|
|
|
return False
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def unregister_tool(self, name: str) -> bool:
|
|
|
|
|
"""注销工具"""
|
|
|
|
|
try:
|
|
|
|
|
if name in self.tools:
|
|
|
|
|
del self.tools[name]
|
|
|
|
|
logger.info(f"工具 {name} 注销成功")
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"注销工具 {name} 失败: {e}")
|
|
|
|
|
return False
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def get_available_tools(self) -> List[Dict[str, Any]]:
|
|
|
|
|
"""获取可用工具列表"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
tools_list = []
|
|
|
|
|
for name, tool_info in self.tools.items():
|
|
|
|
|
tools_list.append({
|
|
|
|
|
"name": name,
|
|
|
|
|
"metadata": tool_info["metadata"],
|
|
|
|
|
"usage_count": tool_info["usage_count"],
|
|
|
|
|
"success_count": tool_info["success_count"],
|
|
|
|
|
"last_used": tool_info["last_used"]
|
|
|
|
|
})
|
|
|
|
|
return tools_list
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.error(f"获取工具列表失败: {e}")
|
|
|
|
|
return []
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
async def execute_tool(self, tool_name: str, parameters: Dict[str, Any] = None) -> Dict[str, Any]:
|
|
|
|
|
"""执行工具"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
if tool_name not in self.tools:
|
|
|
|
|
return {"error": f"工具 {tool_name} 不存在"}
|
|
|
|
|
|
|
|
|
|
tool_info = self.tools[tool_name]
|
|
|
|
|
func = tool_info["function"]
|
|
|
|
|
|
|
|
|
|
# 记录使用
|
|
|
|
|
tool_info["usage_count"] += 1
|
|
|
|
|
tool_info["last_used"] = datetime.now().isoformat()
|
|
|
|
|
|
|
|
|
|
# 执行工具
|
|
|
|
|
start_time = datetime.now()
|
|
|
|
|
try:
|
|
|
|
|
if asyncio.iscoroutinefunction(func):
|
|
|
|
|
result = await func(**(parameters or {}))
|
|
|
|
|
else:
|
|
|
|
|
result = func(**(parameters or {}))
|
|
|
|
|
|
|
|
|
|
# 记录成功
|
|
|
|
|
tool_info["success_count"] += 1
|
|
|
|
|
execution_time = (datetime.now() - start_time).total_seconds()
|
|
|
|
|
|
|
|
|
|
# 记录执行历史
|
|
|
|
|
self._record_execution(tool_name, parameters, result, True, execution_time)
|
|
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
return {
|
2025-10-27 10:33:34 +08:00
|
|
|
"success": True,
|
|
|
|
|
"result": result,
|
|
|
|
|
"execution_time": execution_time,
|
|
|
|
|
"tool_name": tool_name
|
2025-09-06 21:06:18 +08:00
|
|
|
}
|
2025-10-27 10:33:34 +08:00
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
execution_time = (datetime.now() - start_time).total_seconds()
|
|
|
|
|
self._record_execution(tool_name, parameters, str(e), False, execution_time)
|
2025-09-06 21:06:18 +08:00
|
|
|
return {
|
2025-10-27 10:33:34 +08:00
|
|
|
"success": False,
|
|
|
|
|
"error": str(e),
|
|
|
|
|
"execution_time": execution_time,
|
|
|
|
|
"tool_name": tool_name
|
2025-09-06 21:06:18 +08:00
|
|
|
}
|
2025-10-27 10:33:34 +08:00
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.error(f"执行工具 {tool_name} 失败: {e}")
|
|
|
|
|
return {"error": str(e)}
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def _record_execution(self, tool_name: str, parameters: Dict[str, Any],
|
|
|
|
|
result: Any, success: bool, execution_time: float):
|
|
|
|
|
"""记录执行历史"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
execution_record = {
|
|
|
|
|
"timestamp": datetime.now().isoformat(),
|
|
|
|
|
"tool_name": tool_name,
|
|
|
|
|
"parameters": parameters,
|
|
|
|
|
"result": result,
|
|
|
|
|
"success": success,
|
|
|
|
|
"execution_time": execution_time
|
2025-09-06 21:06:18 +08:00
|
|
|
}
|
2025-10-27 10:33:34 +08:00
|
|
|
self.execution_history.append(execution_record)
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
# 保持历史记录在合理范围内
|
|
|
|
|
if len(self.execution_history) > 1000:
|
|
|
|
|
self.execution_history = self.execution_history[-1000:]
|
2025-09-06 21:06:18 +08:00
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.error(f"记录执行历史失败: {e}")
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def get_tool_performance_report(self) -> Dict[str, Any]:
|
|
|
|
|
"""获取工具性能报告"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
total_tools = len(self.tools)
|
|
|
|
|
total_executions = sum(tool["usage_count"] for tool in self.tools.values())
|
|
|
|
|
total_successes = sum(tool["success_count"] for tool in self.tools.values())
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
success_rate = (total_successes / total_executions * 100) if total_executions > 0 else 0
|
2025-09-06 21:06:18 +08:00
|
|
|
|
|
|
|
|
return {
|
2025-10-27 10:33:34 +08:00
|
|
|
"total_tools": total_tools,
|
|
|
|
|
"total_executions": total_executions,
|
|
|
|
|
"total_successes": total_successes,
|
|
|
|
|
"success_rate": round(success_rate, 2),
|
|
|
|
|
"tools": self.get_available_tools()
|
2025-09-06 21:06:18 +08:00
|
|
|
}
|
|
|
|
|
except Exception as e:
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.error(f"获取工具性能报告失败: {e}")
|
|
|
|
|
return {}
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def get_action_history(self, limit: int = 50) -> List[Dict[str, Any]]:
|
|
|
|
|
"""获取动作执行历史"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
return self.execution_history[-limit:] if limit > 0 else self.execution_history
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.error(f"获取动作历史失败: {e}")
|
|
|
|
|
return []
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def clear_execution_history(self) -> Dict[str, Any]:
|
|
|
|
|
"""清空执行历史"""
|
|
|
|
|
try:
|
|
|
|
|
count = len(self.execution_history)
|
|
|
|
|
self.execution_history.clear()
|
|
|
|
|
return {
|
|
|
|
|
"success": True,
|
|
|
|
|
"message": f"已清空 {count} 条执行历史"
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"清空执行历史失败: {e}")
|
|
|
|
|
return {"success": False, "error": str(e)}
|
2025-09-06 21:06:18 +08:00
|
|
|
|
|
|
|
|
def get_agent_status(self) -> Dict[str, Any]:
|
|
|
|
|
"""获取Agent状态"""
|
2025-09-08 15:27:22 +08:00
|
|
|
try:
|
|
|
|
|
return {
|
|
|
|
|
"success": True,
|
2025-10-27 10:33:34 +08:00
|
|
|
"is_active": self.is_agent_mode,
|
|
|
|
|
"ai_monitoring_active": self.ai_monitoring_active,
|
|
|
|
|
"total_tools": len(self.tools),
|
|
|
|
|
"total_executions": len(self.execution_history),
|
|
|
|
|
"tools": self.get_available_tools(),
|
|
|
|
|
"performance": self.get_tool_performance_report()
|
2025-09-08 15:27:22 +08:00
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"获取Agent状态失败: {e}")
|
|
|
|
|
return {
|
|
|
|
|
"success": False,
|
|
|
|
|
"error": str(e),
|
2025-10-27 10:33:34 +08:00
|
|
|
"is_active": False,
|
|
|
|
|
"ai_monitoring_active": False
|
2025-09-08 15:27:22 +08:00
|
|
|
}
|
2025-09-06 21:06:18 +08:00
|
|
|
|
|
|
|
|
def toggle_agent_mode(self, enabled: bool) -> bool:
|
|
|
|
|
"""切换Agent模式"""
|
|
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
self.is_agent_mode = enabled
|
|
|
|
|
logger.info(f"Agent模式: {'启用' if enabled else '禁用'}")
|
2025-11-05 10:16:34 +08:00
|
|
|
return True
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"切换Agent模式失败: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def start_proactive_monitoring(self) -> bool:
|
|
|
|
|
"""启动主动监控"""
|
|
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
if not self.ai_monitoring_active:
|
|
|
|
|
self.ai_monitoring_active = True
|
2025-09-10 23:13:08 +08:00
|
|
|
logger.info("主动监控已启动")
|
|
|
|
|
return True
|
2025-10-27 10:33:34 +08:00
|
|
|
return True
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"启动主动监控失败: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def stop_proactive_monitoring(self) -> bool:
|
|
|
|
|
"""停止主动监控"""
|
|
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
self.ai_monitoring_active = False
|
2025-11-05 10:16:34 +08:00
|
|
|
logger.info("主动监控已停止")
|
|
|
|
|
return True
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"停止主动监控失败: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def run_proactive_monitoring(self) -> Dict[str, Any]:
|
2025-10-27 10:33:34 +08:00
|
|
|
"""运行主动监控检查"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
|
|
|
|
return {
|
|
|
|
|
"success": True,
|
2025-10-27 10:33:34 +08:00
|
|
|
"message": "主动监控检查完成",
|
|
|
|
|
"timestamp": datetime.now().isoformat()
|
2025-09-06 21:06:18 +08:00
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"运行主动监控失败: {e}")
|
|
|
|
|
return {"success": False, "error": str(e)}
|
|
|
|
|
|
|
|
|
|
def run_intelligent_analysis(self) -> Dict[str, Any]:
|
|
|
|
|
"""运行智能分析"""
|
|
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
# 分析工具使用情况
|
|
|
|
|
tool_performance = self.get_tool_performance_report()
|
2025-09-18 19:37:14 +01:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
# 分析执行历史
|
|
|
|
|
recent_executions = self.get_action_history(20)
|
|
|
|
|
|
|
|
|
|
# 生成分析报告
|
2025-11-05 10:16:34 +08:00
|
|
|
analysis = {
|
2025-10-27 10:33:34 +08:00
|
|
|
"tool_performance": tool_performance,
|
|
|
|
|
"recent_activity": len(recent_executions),
|
|
|
|
|
"success_rate": tool_performance.get("success_rate", 0),
|
|
|
|
|
"recommendations": self._generate_recommendations(tool_performance)
|
2025-11-05 10:16:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return analysis
|
2025-10-27 10:33:34 +08:00
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"运行智能分析失败: {e}")
|
|
|
|
|
return {"error": str(e)}
|
|
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
def _generate_recommendations(self, tool_performance: Dict[str, Any]) -> List[str]:
|
|
|
|
|
"""生成建议"""
|
|
|
|
|
recommendations = []
|
|
|
|
|
|
|
|
|
|
success_rate = tool_performance.get("success_rate", 100)
|
|
|
|
|
if success_rate < 90:
|
|
|
|
|
recommendations.append("工具成功率较低,建议检查工具实现")
|
|
|
|
|
|
|
|
|
|
total_executions = tool_performance.get("total_executions", 0)
|
|
|
|
|
if total_executions < 10:
|
|
|
|
|
recommendations.append("工具使用频率较低,建议增加工具调用")
|
|
|
|
|
|
|
|
|
|
return recommendations
|
|
|
|
|
|
|
|
|
|
def get_llm_usage_stats(self) -> Dict[str, Any]:
|
|
|
|
|
"""获取LLM使用统计"""
|
|
|
|
|
try:
|
|
|
|
|
return {
|
|
|
|
|
"total_requests": 0,
|
|
|
|
|
"total_tokens": 0,
|
|
|
|
|
"cost": 0.0,
|
|
|
|
|
"last_updated": datetime.now().isoformat()
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"获取LLM使用统计失败: {e}")
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
async def process_message_agent(self, message: str, user_id: str = "admin",
|
|
|
|
|
work_order_id: Optional[int] = None,
|
|
|
|
|
enable_proactive: bool = True) -> Dict[str, Any]:
|
|
|
|
|
"""处理消息"""
|
|
|
|
|
try:
|
|
|
|
|
# 简化的消息处理
|
|
|
|
|
return {
|
|
|
|
|
"success": True,
|
|
|
|
|
"message": f"Agent收到消息: {message}",
|
|
|
|
|
"user_id": user_id,
|
|
|
|
|
"work_order_id": work_order_id,
|
|
|
|
|
"timestamp": datetime.now().isoformat()
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"处理消息失败: {e}")
|
|
|
|
|
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
async def trigger_sample_actions(self) -> Dict[str, Any]:
|
|
|
|
|
"""触发示例动作"""
|
|
|
|
|
try:
|
|
|
|
|
# 执行一个示例工具
|
|
|
|
|
result = await self.execute_tool("sample_tool", {"action": "test"})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"success": True,
|
|
|
|
|
"message": "示例动作已执行",
|
|
|
|
|
"result": result
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"触发示例动作失败: {e}")
|
|
|
|
|
return {"success": False, "error": str(e)}
|
|
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
def process_file_to_knowledge(self, file_path: str, filename: str) -> Dict[str, Any]:
|
|
|
|
|
"""处理文件并生成知识库"""
|
|
|
|
|
try:
|
|
|
|
|
import os
|
|
|
|
|
import mimetypes
|
2026-02-11 00:08:09 +08:00
|
|
|
|
|
|
|
|
logger.info(f"开始处理知识库上传文件: {filename}")
|
|
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
# 检查文件类型
|
|
|
|
|
mime_type, _ = mimetypes.guess_type(file_path)
|
|
|
|
|
file_ext = os.path.splitext(filename)[1].lower()
|
2026-02-11 00:08:09 +08:00
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
# 读取文件内容
|
|
|
|
|
content = self._read_file_content(file_path, file_ext)
|
|
|
|
|
if not content:
|
2026-02-11 00:08:09 +08:00
|
|
|
logger.error(f"文件读取失败或内容为空: {filename}")
|
2025-09-06 21:06:18 +08:00
|
|
|
return {"success": False, "error": "无法读取文件内容"}
|
2026-02-11 00:08:09 +08:00
|
|
|
|
|
|
|
|
logger.info(f"文件读取成功: {filename}, 字符数={len(content)}")
|
|
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
# 使用简化的知识提取
|
2026-02-11 00:08:09 +08:00
|
|
|
logger.info(f"正在对文件内容进行 AI 知识提取...")
|
2025-09-06 21:06:18 +08:00
|
|
|
knowledge_entries = self._extract_knowledge_from_content(content, filename)
|
2026-02-11 00:08:09 +08:00
|
|
|
|
|
|
|
|
logger.info(f"知识提取完成: 共提取出 {len(knowledge_entries)} 个潜在条目")
|
|
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
# 保存到知识库
|
|
|
|
|
saved_count = 0
|
|
|
|
|
for i, entry in enumerate(knowledge_entries):
|
|
|
|
|
try:
|
2026-02-11 00:08:09 +08:00
|
|
|
logger.info(f"正在保存知识条目 [{i+1}/{len(knowledge_entries)}]: {entry.get('question', '')[:30]}...")
|
|
|
|
|
# 这里在实际项目中应当注入知识库管理器的保存逻辑
|
|
|
|
|
# 但在当前简化版本中仅记录日志
|
2025-11-05 10:16:34 +08:00
|
|
|
saved_count += 1
|
2025-09-06 21:06:18 +08:00
|
|
|
except Exception as save_error:
|
|
|
|
|
logger.error(f"保存知识条目 {i+1} 时出错: {save_error}")
|
2026-02-11 00:08:09 +08:00
|
|
|
|
|
|
|
|
logger.info(f"文件处理任务结束: {filename}, 成功入库 {saved_count} 条")
|
|
|
|
|
|
2025-09-06 21:06:18 +08:00
|
|
|
return {
|
|
|
|
|
"success": True,
|
|
|
|
|
"knowledge_count": saved_count,
|
|
|
|
|
"total_extracted": len(knowledge_entries),
|
|
|
|
|
"filename": filename
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"处理文件失败: {e}")
|
|
|
|
|
return {"success": False, "error": str(e)}
|
|
|
|
|
|
|
|
|
|
def _read_file_content(self, file_path: str, file_ext: str) -> str:
|
|
|
|
|
"""读取文件内容"""
|
|
|
|
|
try:
|
|
|
|
|
if file_ext in ['.txt', '.md']:
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
return f.read()
|
|
|
|
|
elif file_ext == '.pdf':
|
2025-11-05 10:16:34 +08:00
|
|
|
return "PDF文件需要安装PyPDF2库"
|
2025-09-06 21:06:18 +08:00
|
|
|
elif file_ext in ['.doc', '.docx']:
|
2025-11-05 10:16:34 +08:00
|
|
|
return "Word文件需要安装python-docx库"
|
2025-09-06 21:06:18 +08:00
|
|
|
else:
|
|
|
|
|
return "不支持的文件格式"
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"读取文件失败: {e}")
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
def _extract_knowledge_from_content(self, content: str, filename: str) -> List[Dict[str, Any]]:
|
2025-10-27 10:33:34 +08:00
|
|
|
"""从内容中提取知识"""
|
2025-09-06 21:06:18 +08:00
|
|
|
try:
|
2025-10-27 10:33:34 +08:00
|
|
|
# 简化的知识提取逻辑
|
2025-09-06 21:06:18 +08:00
|
|
|
entries = []
|
|
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
# 按段落分割内容
|
|
|
|
|
paragraphs = content.split('\n\n')
|
2025-09-06 21:06:18 +08:00
|
|
|
|
2025-10-27 10:33:34 +08:00
|
|
|
for i, paragraph in enumerate(paragraphs[:5]): # 最多提取5个
|
|
|
|
|
if len(paragraph.strip()) > 20: # 过滤太短的段落
|
|
|
|
|
entries.append({
|
|
|
|
|
"question": f"关于{filename}的问题{i+1}",
|
|
|
|
|
"answer": paragraph.strip(),
|
|
|
|
|
"category": "文档知识",
|
|
|
|
|
"confidence_score": 0.7
|
|
|
|
|
})
|
2025-09-06 21:06:18 +08:00
|
|
|
|
|
|
|
|
return entries
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-10-27 10:33:34 +08:00
|
|
|
logger.error(f"提取知识失败: {e}")
|
2025-09-06 21:06:18 +08:00
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
# 使用示例
|
|
|
|
|
async def main():
|
|
|
|
|
"""主函数示例"""
|
|
|
|
|
# 创建Agent助手
|
|
|
|
|
agent_assistant = TSPAgentAssistant()
|
|
|
|
|
|
|
|
|
|
# 测试Agent功能
|
|
|
|
|
print("=== TSP Agent助手测试 ===")
|
|
|
|
|
|
|
|
|
|
# 测试Agent模式处理消息
|
|
|
|
|
response = await agent_assistant.process_message_agent(
|
|
|
|
|
message="我的账户无法登录,请帮助我解决这个问题",
|
|
|
|
|
user_id="user123"
|
|
|
|
|
)
|
|
|
|
|
print("Agent模式响应:", response)
|
|
|
|
|
|
|
|
|
|
# 获取Agent状态
|
|
|
|
|
agent_status = agent_assistant.get_agent_status()
|
|
|
|
|
print("Agent状态:", agent_status)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-10-27 10:33:34 +08:00
|
|
|
asyncio.run(main())
|