Browse Source

clean_agent

丁云鹏 1 week ago
parent
commit
c602a3baa7
1 changed files with 40 additions and 13 deletions
  1. 40 13
      agent.py

+ 40 - 13
agent.py

@@ -12,6 +12,7 @@ import time
 from typing import Any, Dict, List, Optional, TypedDict, Annotated
 from contextlib import asynccontextmanager
 import asyncio
+from utils.mysql_db import MysqlHelper
 
 # 保证可以导入本项目模块
 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
@@ -571,23 +572,33 @@ async def process_request_background(request_id: str):
         # 处理失败,更新状态为3
         update_request_status(request_id, 3)
 
+
+extraction_requests: set = set()
+
 @app.post("/extract")
-async def extract(input: str):
-    """
-    执行Agent处理用户指令
-    
-    Args:
-        input: 包含用户指令的对象
-        
-    Returns:
-        dict: 包含执行结果的字典
-    """
+async def extract(request_id: str, query_word: str):
     try:
-        result = execute_agent_with_api(input)
-        return {"status": "success", "result": result}
+        # 检查请求是否已经在处理中
+        async with RUNNING_LOCK:
+            if request_id in extraction_requests:
+                return {"status": 1, "request_id": request_id, "message": "请求已在处理中"}
+            extraction_requests.add(request_id)
+        
+        try:
+            # 更新状态为处理中
+            update_extract_status(request_id, 1)
+            # 执行Agent
+            result = execute_agent_with_api(json.dumps({"query_word":query_word, "request_id": request_id}
+        finally:
+            # 无论成功失败,都从运行集合中移除
+            async with RUNNING_LOCK:
+                extraction_requests.discard(request_id)
     except Exception as e:
+        # 发生异常,更新状态为处理失败
+        update_request_status(request_id, 3)
         raise HTTPException(status_code=500, detail=f"执行Agent时出错: {str(e)}")
 
+<<<<<<< HEAD
 @app.post("/expand")
 async def expand(requestId: str):
     """
@@ -604,6 +615,22 @@ async def expand(requestId: str):
         return {"status": 1, "result": result}
     except Exception as e:
         raise HTTPException(status_code=500, detail=f"执行Agent时出错: {str(e)}")
+=======
+def update_extract_status(request_id: str, status: int):
+    try:
+        from utils.mysql_db import MysqlHelper
+        
+        sql = "UPDATE knowledge_request SET extraction_status = %s WHERE request_id = %s"
+        result = MysqlHelper.update_values(sql, (status, request_id))
+        
+        if result is not None:
+            logger.info(f"更新请求状态成功: requestId={request_id}, status={status}")
+        else:
+            logger.error(f"更新请求状态失败: requestId={request_id}, status={status}")
+            
+    except Exception as e:
+        logger.error(f"更新请求状态异常: requestId={request_id}, status={status}, error={e}")
+>>>>>>> 0b19ef7 (clean_agent)
 
 if __name__ == "__main__":
     # 启动服务
@@ -613,4 +640,4 @@ if __name__ == "__main__":
         port=8080,
         reload=True,  # 开发模式,自动重载
         log_level="info"
-    ) 
+    )