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