From 4e301c1fcf4a0b6e2e797acaa3169d5ff4fe92f5 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期三, 08 一月 2025 13:50:52 +0800
Subject: [PATCH] sse
---
app/service/v2/chat.py | 77 ++++++++++++++++++++++++++++++++------
1 files changed, 64 insertions(+), 13 deletions(-)
diff --git a/app/service/v2/chat.py b/app/service/v2/chat.py
index df8cb0f..982ed43 100644
--- a/app/service/v2/chat.py
+++ b/app/service/v2/chat.py
@@ -1,19 +1,70 @@
+import json
+
+from Log import logger
+from app.config.agent_base_url import RG_CHAT_DIALOG
+from app.config.config import settings
+from app.config.const import max_chunk_size
+from app.models.v2.session_model import ChatSessionDao
+from app.service.v2.app_driver.chat_dialog import ChatDialog
-async def service_chat_dialog(question: str, session_id: str):
-
- if session_id:
- ...
-
-
-
+async def service_chat_dialog(db, chat_id:str, question: str, session_id: str, user_id):
+ token = "ragflow-YzMzE1NDRjYzMyZjExZWY5ZjkxMDI0Mm"
+ url = settings.fwr_base_url+RG_CHAT_DIALOG.format(chat_id)
+ chat = ChatDialog(token)
+ request_data = {
+ "question": question,
+ "stream": True,
+ "session_id": session_id
+ }
+ headers = {
+ 'Content-Type': 'application/json',
+ 'Authorization': f"Bearer {token}"
+ }
try:
- for ans in chat(dia, msg, True, **req):
-
- yield "data:" + json.dumps({"code": 0, "message": "", "data": ans}, ensure_ascii=False) + "\n\n"
- ConversationService.update_by_id(conv.id, conv.to_dict())
+ await ChatSessionDao(db).update_or_insert_by_id(
+ session_id=session_id,
+ name=question[:255],
+ agent_id=chat_id,
+ agent_type=1,
+ tenant_id=user_id,
+ message={"role": "user", "content": question},
+ conversation_id=session_id,
+ event_type="message"
+ )
except Exception as e:
- yield "data:" + json.dumps({"code": 500, "message": str(e),
+ logger.error(e)
+ try:
+ message = {"role": "assistant","answer":"", "reference": {}}
+ async for ans in chat.chat_completions(url, request_data, headers):
+ if ans.get("code", None) == 102:
+ error = ans.get("message", "璇疯緭鍏ヤ綘鐨勯棶棰橈紒")
+ data = {"answer":error}
+ event = "message"
+ else:
+ if isinstance(ans.get("data"), bool) and ans.get("data") is True:
+ data = {}
+ event = "message_end"
+ else:
+ data = ans.get("data", {})
+ message = ans.get("data", {})
+ event = "message"
+ message_str = "data: " + json.dumps({"event": event, "data": data}, ensure_ascii=False) + "\n\n"
+ for i in range(0, len(message_str), max_chunk_size):
+ chunk = message_str[i:i + max_chunk_size]
+ # print(chunk)
+ yield chunk # 鍙戦�佸垎鍧楁秷鎭�
+ await ChatSessionDao(db).update_session_by_id(
+ session_id=session_id,
+ session=None,
+ message=message
+ )
+ except Exception as e:
+ logger.error(e)
+ yield "data: " + json.dumps({"message": "message",
"data": {"answer": "**ERROR**: " + str(e), "reference": []}},
ensure_ascii=False) + "\n\n"
- yield "data:" + json.dumps({"code": 0, "message": "", "data": True}, ensure_ascii=False) + "\n\n"
\ No newline at end of file
+
+ yield "data: " + json.dumps({"message": "message_end",
+ "data": {}},
+ ensure_ascii=False) + "\n\n"
\ No newline at end of file
--
Gitblit v1.8.0