From 2eab89eea16ec1039f0807f2a579d4291aacdefa Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Thu, 18 Sep 2025 15:30:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=81=8A=E5=A4=A9=E5=8A=A9?= =?UTF-8?q?=E6=89=8B=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ragflow_controller.py | 3 +- .../module_admin/service/ragflow_service.py | 33 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py index cc8986a..bd21e10 100644 --- a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py +++ b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py @@ -205,7 +205,8 @@ async def converse_with_chat_assistant( 与聊天助手进行对话 """ - await RAGFlowService.converse_with_chat_assistant_services(converse_params) + result = await RAGFlowService.converse_with_chat_assistant_services(converse_params) + return parse_result(result) diff --git a/ruoyi-fastapi-backend/module_admin/service/ragflow_service.py b/ruoyi-fastapi-backend/module_admin/service/ragflow_service.py index 8e016f9..b687b13 100644 --- a/ruoyi-fastapi-backend/module_admin/service/ragflow_service.py +++ b/ruoyi-fastapi-backend/module_admin/service/ragflow_service.py @@ -179,20 +179,25 @@ class RAGFlowService: # 与助手聊天 @classmethod async def converse_with_chat_assistant_services(cls, converse_params: ConverseWithChatAssistantModel): - async with AsyncRAGFlowClient(RAGFlowConfig.RAGFLOW_BASE_URL, RAGFlowConfig.RAGFLOW_API_KEY) as client: - result = await client.converse_with_chat_assistant(**(converse_params.model_dump())) - - i = 0 - async for t in result: - try: - answer = t["data"].get("answer", "") - answer = cls.clean_text(answer) - print(repr(answer[i:])) - i = len(answer) - except Exception as e: - print(e) - print(t) - await asyncio.sleep(0.2) + if converse_params.stream: + async with AsyncRAGFlowClient(RAGFlowConfig.RAGFLOW_BASE_URL, RAGFlowConfig.RAGFLOW_API_KEY) as client: + result = await client.converse_with_chat_assistant(**(converse_params.model_dump())) + + i = 0 + async for t in result: + try: + answer = t["data"].get("answer", "") + answer = cls.clean_text(answer) + print(repr(answer[i:])) + i = len(answer) + except Exception as e: + print(e) + print(t) + await asyncio.sleep(0.2) + else: + async with AsyncRAGFlowClient(RAGFlowConfig.RAGFLOW_BASE_URL, RAGFlowConfig.RAGFLOW_API_KEY) as client: + result = await client.converse_with_chat_assistant(**(converse_params.model_dump())) + return result