|
@@ -8,6 +8,28 @@
|
|
|
- `migration_and_integration_plan.md` - 总体方案概述
|
|
|
- `complete_migration_implementation_guide.md` (本文档) - 详细实施指南
|
|
|
|
|
|
+### 🔧 用户需求澄清 (2025-01-15更新)
|
|
|
+
|
|
|
+根据用户反馈,明确以下关键要点:
|
|
|
+
|
|
|
+#### 1. API整合方式澄清
|
|
|
+- ✅ 在项目根目录创建新的 `unified_api.py`(推荐命名)
|
|
|
+- ✅ 从 `citu_app.py` **复制**所需API到新文件(保留原文件)
|
|
|
+- ✅ 包含 `custom_react_agent/api.py` 的**全部内容**
|
|
|
+- ✅ **保留**原有的 `citu_app.py` 和 `test/custom_react_agent/api.py` 不变
|
|
|
+
|
|
|
+#### 2. 配置文件策略调整
|
|
|
+- ✅ `agent/` 目录**保持独立**的 `config.py` 文件
|
|
|
+- ✅ `react_agent/` 目录**也保持独立**的 `config.py` 文件
|
|
|
+- ❌ **不需要**创建统一的 `config/agent_config.py`
|
|
|
+- 📝 **理由**:每个模块保持独立配置更清晰,维护性更好
|
|
|
+
|
|
|
+#### 3. 日志管理策略确认
|
|
|
+- ✅ 使用项目**统一的日志管理服务**(`core.logging`)
|
|
|
+- ✅ 为 `react_agent` 设置**独立的日志文件**(仿照 `data_pipeline` 模式)
|
|
|
+- ✅ **经验证**:`agent/` 使用 `get_agent_logger("CituAgent")`,`data_pipeline` 有独立日志文件
|
|
|
+- 📁 **日志文件位置**:`logs/react_agent_YYYYMMDD.log`
|
|
|
+
|
|
|
---
|
|
|
|
|
|
## 📋 一、API兼容性分析详细报告
|
|
@@ -141,30 +163,35 @@ API_COMPATIBILITY_MAP = {
|
|
|
|
|
|
### 🏗️ Step-by-Step 迁移操作
|
|
|
|
|
|
-#### Step 1: 创建新目录结构
|
|
|
+#### Step 1: 创建新目录结构(已调整)
|
|
|
|
|
|
```bash
|
|
|
-# 1. 创建react_agent目录
|
|
|
+# 1. 创建react_agent目录(不创建config目录)
|
|
|
mkdir -p react_agent
|
|
|
-mkdir -p config
|
|
|
+mkdir -p logs # 确保日志目录存在
|
|
|
|
|
|
-# 2. 复制核心文件 (保留原文件)
|
|
|
+# 2. 复制核心文件 (保留原文件,保持配置文件独立)
|
|
|
cp test/custom_react_agent/agent.py react_agent/
|
|
|
cp test/custom_react_agent/state.py react_agent/
|
|
|
cp test/custom_react_agent/sql_tools.py react_agent/
|
|
|
cp test/custom_react_agent/shell.py react_agent/
|
|
|
cp test/custom_react_agent/enhanced_redis_api.py react_agent/
|
|
|
-cp test/custom_react_agent/config.py react_agent/config_react.py # 重命名避免冲突
|
|
|
+cp test/custom_react_agent/config.py react_agent/ # 保持原名,不重命名
|
|
|
|
|
|
-# 3. 复制API文件到根目录 (重命名)
|
|
|
-cp test/custom_react_agent/api.py ./api_unified.py # 重命名,后续将整合所有API
|
|
|
-cp test/custom_react_agent/asgi_app.py ./asgi_app_new.py # 重命名,避免冲突
|
|
|
+# 3. 复制API文件到根目录(使用推荐命名)
|
|
|
+cp test/custom_react_agent/api.py ./unified_api.py # 使用推荐的文件名
|
|
|
+cp test/custom_react_agent/asgi_app.py ./ # 保持原名,后续会修改导入
|
|
|
|
|
|
# 4. 创建初始化文件
|
|
|
echo "# React Agent Module" > react_agent/__init__.py
|
|
|
|
|
|
# 5. 复制依赖文件
|
|
|
cp test/custom_react_agent/requirements.txt react_agent/
|
|
|
+
|
|
|
+echo "✅ 目录结构创建完成"
|
|
|
+echo "📁 react_agent/ - React Agent模块"
|
|
|
+echo "📄 unified_api.py - 统一API入口"
|
|
|
+echo "📄 asgi_app.py - ASGI启动器"
|
|
|
```
|
|
|
|
|
|
#### Step 2: 路径修正脚本
|
|
@@ -206,8 +233,8 @@ def fix_all_imports():
|
|
|
'react_agent/sql_tools.py',
|
|
|
'react_agent/shell.py',
|
|
|
'react_agent/enhanced_redis_api.py',
|
|
|
- 'api_unified.py',
|
|
|
- 'asgi_app_new.py'
|
|
|
+ 'unified_api.py', # 使用正确的文件名
|
|
|
+ 'asgi_app.py' # 使用正确的文件名
|
|
|
]
|
|
|
|
|
|
for file_path in react_agent_files:
|
|
@@ -241,17 +268,237 @@ echo "✅ 目录迁移完成,语法检查通过"
|
|
|
|
|
|
## 📋 四、日志服务统一详细方案
|
|
|
|
|
|
-### 🔧 统一日志配置
|
|
|
+### 🔧 React Agent独立日志配置
|
|
|
+
|
|
|
+基于用户需求和现有实践,为React Agent设置独立日志文件,仿照`data_pipeline`模式。
|
|
|
+
|
|
|
+#### 📊 现有日志系统分析
|
|
|
|
|
|
-#### Step 1: 修改React Agent配置
|
|
|
+经验证,项目中的日志使用情况:
|
|
|
+- **`agent/`**: 使用 `get_agent_logger("CituAgent")` 统一日志系统
|
|
|
+- **`data_pipeline/`**: 使用独立日志文件 `./data_pipeline/training_data/{task_id}/data_pipeline.log`
|
|
|
+- **方案**: React Agent使用统一日志系统但输出到独立文件
|
|
|
+
|
|
|
+#### Step 1: 创建React Agent日志管理器
|
|
|
|
|
|
```python
|
|
|
-# react_agent/config_react.py (修改后)
|
|
|
+# react_agent/logger.py (新建)
|
|
|
"""
|
|
|
-React Agent 统一配置
|
|
|
-与项目主配置保持一致
|
|
|
+React Agent 独立日志管理器
|
|
|
+仿照data_pipeline模式,使用统一日志系统但输出到独立文件
|
|
|
"""
|
|
|
import os
|
|
|
+from pathlib import Path
|
|
|
+from datetime import datetime
|
|
|
+from core.logging import get_agent_logger
|
|
|
+
|
|
|
+class ReactAgentLogManager:
|
|
|
+ """React Agent 日志管理器"""
|
|
|
+
|
|
|
+ _logger_instance = None
|
|
|
+ _file_handler = None
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_logger(cls, name: str = "ReactAgent"):
|
|
|
+ """
|
|
|
+ 获取React Agent专用logger
|
|
|
+ 使用统一日志系统但输出到独立文件
|
|
|
+ """
|
|
|
+ if cls._logger_instance is None:
|
|
|
+ cls._logger_instance = cls._create_logger(name)
|
|
|
+ return cls._logger_instance
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def _create_logger(cls, name: str):
|
|
|
+ """创建独立文件的logger"""
|
|
|
+ # 使用统一日志系统获取logger
|
|
|
+ logger = get_agent_logger(name)
|
|
|
+
|
|
|
+ # 添加独立的文件处理器
|
|
|
+ cls._add_file_handler(logger)
|
|
|
+
|
|
|
+ return logger
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def _add_file_handler(cls, logger):
|
|
|
+ """添加独立的文件处理器"""
|
|
|
+ try:
|
|
|
+ # 确保日志目录存在
|
|
|
+ project_root = Path(__file__).parent.parent
|
|
|
+ log_dir = project_root / "logs"
|
|
|
+ log_dir.mkdir(exist_ok=True)
|
|
|
+
|
|
|
+ # 按日期创建日志文件
|
|
|
+ today = datetime.now().strftime("%Y%m%d")
|
|
|
+ log_file = log_dir / f"react_agent_{today}.log"
|
|
|
+
|
|
|
+ # 创建文件处理器
|
|
|
+ import logging
|
|
|
+ file_handler = logging.FileHandler(log_file, encoding='utf-8')
|
|
|
+ file_handler.setLevel(logging.DEBUG)
|
|
|
+
|
|
|
+ # 设置格式
|
|
|
+ formatter = logging.Formatter(
|
|
|
+ '%(asctime)s [%(levelname)s] [%(name)s] %(filename)s:%(lineno)d - %(message)s',
|
|
|
+ datefmt='%Y-%m-%d %H:%M:%S'
|
|
|
+ )
|
|
|
+ file_handler.setFormatter(formatter)
|
|
|
+
|
|
|
+ # 添加到logger(不影响原有的控制台输出)
|
|
|
+ logger.addHandler(file_handler)
|
|
|
+ cls._file_handler = file_handler
|
|
|
+
|
|
|
+ logger.info(f"✅ React Agent独立日志文件已创建: {log_file}")
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ logger.warning(f"⚠️ 创建React Agent独立日志文件失败: {e}")
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def cleanup(cls):
|
|
|
+ """清理资源"""
|
|
|
+ if cls._file_handler:
|
|
|
+ cls._file_handler.close()
|
|
|
+ cls._file_handler = None
|
|
|
+
|
|
|
+# 对外接口
|
|
|
+def get_react_agent_logger(name: str = "ReactAgent"):
|
|
|
+ """获取React Agent专用logger"""
|
|
|
+ return ReactAgentLogManager.get_logger(name)
|
|
|
+```
|
|
|
+
|
|
|
+#### Step 2: 修改React Agent配置
|
|
|
+
|
|
|
+```python
|
|
|
+# react_agent/config.py (修改后)
|
|
|
+"""
|
|
|
+React Agent 独立配置
|
|
|
+保持独立配置,但使用统一日志系统和独立日志文件
|
|
|
+"""
|
|
|
+import os
|
|
|
+from .logger import get_react_agent_logger
|
|
|
+
|
|
|
+# 使用React Agent专用logger
|
|
|
+logger = get_react_agent_logger("ReactAgentConfig")
|
|
|
+
|
|
|
+# 继承主配置
|
|
|
+try:
|
|
|
+ from app_config import (
|
|
|
+ LLM_MODEL_TYPE, API_LLM_MODEL, API_QIANWEN_CONFIG,
|
|
|
+ REDIS_URL, VECTOR_DB_TYPE
|
|
|
+ )
|
|
|
+ logger.info("✅ 成功加载主配置文件")
|
|
|
+except ImportError as e:
|
|
|
+ logger.warning(f"⚠️ 主配置加载失败,使用默认配置: {e}")
|
|
|
+ # 默认配置
|
|
|
+ REDIS_URL = "redis://localhost:6379"
|
|
|
+ LLM_MODEL_TYPE = "api"
|
|
|
+
|
|
|
+# React Agent 特定配置
|
|
|
+REACT_AGENT_CONFIG = {
|
|
|
+ "default_user_id": "guest",
|
|
|
+ "max_retries": 3,
|
|
|
+ "retry_base_delay": 3,
|
|
|
+ "network_timeout": 60,
|
|
|
+ "debug_mode": True,
|
|
|
+ "max_log_length": 1000
|
|
|
+}
|
|
|
+
|
|
|
+# HTTP连接配置
|
|
|
+HTTP_CONFIG = {
|
|
|
+ "max_connections": 10,
|
|
|
+ "max_keepalive_connections": 5,
|
|
|
+ "keepalive_expiry": 30.0,
|
|
|
+ "connect_timeout": 10.0,
|
|
|
+ "pool_timeout": 5.0
|
|
|
+}
|
|
|
+
|
|
|
+logger.info("✅ React Agent配置初始化完成")
|
|
|
+```
|
|
|
+
|
|
|
+#### Step 3: 更新Agent实现类
|
|
|
+
|
|
|
+```python
|
|
|
+# react_agent/agent.py (关键修改部分)
|
|
|
+"""
|
|
|
+Custom React Agent 实现
|
|
|
+使用统一日志系统和独立日志文件
|
|
|
+"""
|
|
|
+from .logger import get_react_agent_logger
|
|
|
+from .config import REACT_AGENT_CONFIG
|
|
|
+
|
|
|
+class CustomReactAgent:
|
|
|
+ def __init__(self):
|
|
|
+ # 使用React Agent专用logger
|
|
|
+ self.logger = get_react_agent_logger("ReactAgent.Core")
|
|
|
+ self.config = REACT_AGENT_CONFIG
|
|
|
+
|
|
|
+ self.logger.info("🚀 CustomReactAgent 初始化开始")
|
|
|
+
|
|
|
+ # 其他初始化逻辑...
|
|
|
+
|
|
|
+ self.logger.info("✅ CustomReactAgent 初始化完成")
|
|
|
+
|
|
|
+ async def process_question(self, question: str, **kwargs):
|
|
|
+ """处理问题的主要方法"""
|
|
|
+ self.logger.info(f"📝 开始处理问题: {question[:100]}...")
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 处理逻辑...
|
|
|
+ result = await self._internal_process(question, **kwargs)
|
|
|
+
|
|
|
+ self.logger.info("✅ 问题处理完成")
|
|
|
+ return result
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ self.logger.error(f"❌ 问题处理失败: {str(e)}")
|
|
|
+ raise
|
|
|
+
|
|
|
+ def cleanup(self):
|
|
|
+ """清理资源"""
|
|
|
+ self.logger.info("🧹 开始清理React Agent资源")
|
|
|
+ # 清理逻辑...
|
|
|
+
|
|
|
+ # 清理日志资源
|
|
|
+ from .logger import ReactAgentLogManager
|
|
|
+ ReactAgentLogManager.cleanup()
|
|
|
+```
|
|
|
+
|
|
|
+#### Step 4: 日志文件组织结构
|
|
|
+
|
|
|
+```
|
|
|
+logs/
|
|
|
+├── app.log # 主应用日志(原有)
|
|
|
+├── react_agent_20250115.log # React Agent独立日志(新增)
|
|
|
+├── react_agent_20250116.log # 按日期轮换
|
|
|
+└── data_pipeline/ # Data Pipeline日志目录(原有)
|
|
|
+ └── task_20250115_143052/
|
|
|
+ └── data_pipeline.log
|
|
|
+```
|
|
|
+
|
|
|
+#### Step 5: 验证日志配置
|
|
|
+
|
|
|
+```python
|
|
|
+# scripts/test_react_agent_logging.py
|
|
|
+"""
|
|
|
+验证React Agent日志配置
|
|
|
+"""
|
|
|
+def test_react_agent_logging():
|
|
|
+ """测试React Agent日志功能"""
|
|
|
+
|
|
|
+ # 测试日志系统
|
|
|
+ from react_agent.logger import get_react_agent_logger
|
|
|
+
|
|
|
+ logger = get_react_agent_logger("TestLogger")
|
|
|
+
|
|
|
+ logger.info("测试 React Agent 日志系统")
|
|
|
+ logger.warning("测试警告日志")
|
|
|
+ logger.error("测试错误日志")
|
|
|
+
|
|
|
+ print("✅ React Agent日志系统测试完成")
|
|
|
+ print("📁 请检查 logs/react_agent_YYYYMMDD.log 文件")
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ test_react_agent_logging()
|
|
|
from core.logging import get_agent_logger, initialize_logging
|
|
|
|
|
|
# 使用项目统一日志系统
|
|
@@ -364,15 +611,28 @@ if __name__ == "__main__":
|
|
|
|
|
|
---
|
|
|
|
|
|
-## 📋 五、API整合详细实施方案
|
|
|
+## 📋 五、API整合详细实施方案(已调整)
|
|
|
+
|
|
|
+### 📋 API整合策略说明
|
|
|
+
|
|
|
+基于用户澄清,API整合采用**复制策略**而非合并策略:
|
|
|
+
|
|
|
+1. **保留原文件**:`citu_app.py` 和 `test/custom_react_agent/api.py` 保持不变
|
|
|
+2. **创建新文件**:在根目录创建 `unified_api.py`
|
|
|
+3. **复制内容**:
|
|
|
+ - 从 `citu_app.py` **复制**需要的API到 `unified_api.py`
|
|
|
+ - 包含 `custom_react_agent/api.py` 的**全部内容**
|
|
|
+4. **独立运行**:新的 `unified_api.py` 可以独立提供所有服务
|
|
|
|
|
|
### 🔗 统一API文件结构
|
|
|
|
|
|
```python
|
|
|
-# api_unified.py (完整结构)
|
|
|
+# unified_api.py (完整结构)
|
|
|
"""
|
|
|
统一API服务入口
|
|
|
-整合原有agent API、React Agent API和所有管理API
|
|
|
+复制原有agent API、包含React Agent API的全部内容和所有管理API
|
|
|
+
|
|
|
+注意:这是一个独立的API文件,不影响原有的citu_app.py和test/custom_react_agent/api.py
|
|
|
"""
|
|
|
import asyncio
|
|
|
import logging
|