This commit is contained in:
runny committed 2026-05-08 12:50:03 +08:00
1 parent fec2fc096e
commit 283ec9e7b3
11 files changed
+1379 -19

No files matched your search

+13 -2
View File
@@ -435,17 +435,20 @@ def chat_completions():
query = data.get('query')
# 提取会话 ID(可选)
conversation_id = data.get('conversation_id')
# 知识库类型
kb_type = data.get('kb_type', '企业知识库')
# 从全局 g 中获取装饰器注入的用户和 Agent 对象
user = getattr(g, 'current_user', None)
agent = getattr(g, 'current_agent', None)
current_app.logger.info(
"[chat-api] /completions request user_id=%s agent_id=%s query_len=%s conversation_id=%s",
"[chat-api] /completions request user_id=%s agent_id=%s query_len=%s conversation_id=%s kb_type=%s",
getattr(user, 'id', None),
getattr(agent, 'id', None),
len(query or ''),
conversation_id,
kb_type
)
# 安全兜底:如果装饰器未正确注入,返回错误
@@ -453,10 +456,18 @@ def chat_completions():
current_app.logger.error("[chat-api] missing user/agent context user=%s agent=%s", bool(user), bool(agent))
return jsonify({'error': 'Missing user or agent context'}), 500
from app.services.dify import DifyService
if kb_type == 'ERP知识库':
from app.services.text2sql import Text2SQLService
generator = Text2SQLService.stream_chat(query, user.id, agent, conversation_id)
else:
generator = DifyService.stream_chat(query, user.id, agent, conversation_id)
# 返回 SSE 响应,mimetype 设为 text/event-stream
return Response(
stream_with_context(
DifyService.stream_chat(query, user.id, agent, conversation_id)
generator
),
mimetype='text/event-stream',
headers={