P1-2: 统一Dify调用点 - 删除detect-intent死链路(approval/byod) + routing_service共享client修复连接泄漏 + 前端死代码清理
This commit is contained in:
@@ -35,6 +35,24 @@ from app.services.ws_manager import manager as ws_manager
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 共享 httpx 客户端(v4.0 P1-2:修复每次调用新建连接的泄漏问题)
|
||||
# =============================================================================
|
||||
_routing_client: Optional[httpx.AsyncClient] = None
|
||||
|
||||
|
||||
async def _get_routing_client(timeout: float) -> httpx.AsyncClient:
|
||||
"""获取共享的 httpx.AsyncClient(懒加载单例)。
|
||||
|
||||
为什么:之前每次 detect_routing_intent 调用都 `async with httpx.AsyncClient()`
|
||||
新建连接池,高并发下产生大量 TIME_WAIT 连接(与 AIService 修复前同源问题)。
|
||||
"""
|
||||
global _routing_client
|
||||
if _routing_client is None or _routing_client.is_closed:
|
||||
_routing_client = httpx.AsyncClient(timeout=httpx.Timeout(timeout))
|
||||
return _routing_client
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 路由关键词预过滤列表
|
||||
# =============================================================================
|
||||
@@ -169,24 +187,24 @@ async def detect_routing_intent(text: str, employee_id: str = "") -> dict:
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient(timeout=httpx.Timeout(timeout)) as client:
|
||||
response = await client.post(url, json=body, headers=headers)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
client = await _get_routing_client(timeout)
|
||||
response = await client.post(url, json=body, headers=headers)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
# 解析 Dify 原生响应:answer 字段包含 AI 返回的 JSON 字符串
|
||||
answer = data.get("answer", "")
|
||||
parsed = json.loads(answer)
|
||||
# 解析 Dify 原生响应:answer 字段包含 AI 返回的 JSON 字符串
|
||||
answer = data.get("answer", "")
|
||||
parsed = json.loads(answer)
|
||||
|
||||
# 解析统一意图识别的 6 个字段
|
||||
return {
|
||||
"is_approval_request": bool(parsed.get("is_approval_request", False)),
|
||||
"confidence": float(parsed.get("confidence", 0.0)),
|
||||
"approval_type": parsed.get("approval_type"),
|
||||
"intent_type": str(parsed.get("intent_type", "chitchat")),
|
||||
"business_category": parsed.get("business_category"),
|
||||
"routing_confidence": float(parsed.get("routing_confidence", 0.0)),
|
||||
}
|
||||
# 解析统一意图识别的 6 个字段
|
||||
return {
|
||||
"is_approval_request": bool(parsed.get("is_approval_request", False)),
|
||||
"confidence": float(parsed.get("confidence", 0.0)),
|
||||
"approval_type": parsed.get("approval_type"),
|
||||
"intent_type": str(parsed.get("intent_type", "chitchat")),
|
||||
"business_category": parsed.get("business_category"),
|
||||
"routing_confidence": float(parsed.get("routing_confidence", 0.0)),
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user