原有的 ask_react_agent_stream
API 在处理复杂数据库查询时,会出现 Vector 搜索异步冲突错误:
RuntimeError: Task <Task pending...> got Future <Future pending> attached to a different loop
根本原因:LangGraph异步流式处理与Vanna Vector搜索的同步操作冲突。
创建了完全独立的同步流式API,彻底避免异步冲突问题。
GET /api/v0/ask_react_agent_stream_sync
SyncCustomReactAgent
graph.invoke()
而不是 ainvoke()
streaming=False
enable_thinking=False
(千问模型非流式调用要求)# PowerShell
Invoke-WebRequest -Uri 'http://localhost:8084/api/v0/ask_react_agent_stream_sync?question=请问当前系统中哪个服务区档口最多?&user_id=test_user' -Headers @{'Accept'='text/event-stream'} -Method GET
# curl (在支持的终端中)
curl -X GET "http://localhost:8084/api/v0/ask_react_agent_stream_sync?question=请查询所有服务区的名称和档口数量&user_id=test_user" -H "Accept: text/event-stream"
与原API完全兼容:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
question |
string | ✅ | 用户问题 |
user_id |
string | ✅ | 用户ID |
thread_id |
string | ❌ | 对话线程ID |
routing_mode |
string | ❌ | 路由模式,默认'agent' |
continue_conversation |
boolean | ❌ | 是否继续对话,默认false |
返回SSE流式响应:
data: {"type": "start", "message": "开始处理请求...", "timestamp": 1754412317.7335036}
data: {"type": "thinking", "content": "正在分析问题并准备查询数据库...", "timestamp": 1754412317.7335036}
data: {"type": "response", "content": "查询结果...", "metadata": {...}, "timestamp": 1754412317.7335036}
data: {"type": "completed", "message": "请求处理完成", "user_id": "test_user", "thread_id": "...", "timestamp": 1754412317.7335036}
测试查询:
请查询所有服务区的名称和档口数量,按档口数量降序排列
执行结果:
react_agent/
├── sync_agent.py # 新增:同步Agent类
├── agent.py # 原有:异步Agent类
└── ...
unified_api.py # 新增:同步流式API端点
LLM配置 (sync_agent.py
):
ChatOpenAI(
model=config.QWEN_MODEL,
temperature=0.1,
streaming=False, # 关键:禁用流式
extra_body={
"enable_thinking": False, # 关键:非流式调用必须为false
"misc": {"ensure_ascii": False}
}
)
LangGraph配置:
# 使用同步invoke而不是异步ainvoke
final_state = self.agent_executor.invoke(inputs, run_config)
react_agent/sync_agent.py
unified_api.py
(新增API端点)通过创建同步版本的React Agent,彻底解决了Vector搜索异步冲突问题,为复杂数据库查询提供了稳定可靠的解决方案。
创建时间:2025-08-06
解决的核心问题:React Agent流式API的Vector搜索异步冲突
技术方案:同步LangGraph + 同步LLM配置