From b169e9413fd624dd1597290c02b49a0e33d6d964 Mon Sep 17 00:00:00 2001 From: rainv123 <2148537152@qq.com> Date: Tue, 30 Dec 2025 11:02:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:serch=5Ffrom=5Fragflow=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E8=BF=94=E5=9B=9E=E8=AF=A6=E7=BB=86=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../functions/search_from_ragflow.py | 74 +++++++++++++++++-- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/main/xiaozhi-server/plugins_func/functions/search_from_ragflow.py b/main/xiaozhi-server/plugins_func/functions/search_from_ragflow.py index ec6ac426..7d8f13be 100644 --- a/main/xiaozhi-server/plugins_func/functions/search_from_ragflow.py +++ b/main/xiaozhi-server/plugins_func/functions/search_from_ragflow.py @@ -65,12 +65,24 @@ def search_from_ragflow(conn, question=None): result = json.loads(response_text) if result.get("code") != 0: - error_detail = response.get("error", {}).get("detail", "") + error_detail = result.get("error", {}).get("detail", "未知错误") + error_message = result.get("error", {}).get("message", "") + error_code = result.get("code", "") + # 安全地记录错误信息 logger.bind(tag=TAG).error( - "从RAGflow获取信息失败,原因:%s", str(error_detail) + f"RAGFlow API调用失败,响应码:{error_code},错误详情:{error_detail},完整响应:{result}" ) - return ActionResponse(Action.RESPONSE, None, "RAG接口返回异常") + + # 构建详细的错误响应 + error_response = f"RAG接口返回异常(错误码:{error_code})" + + if error_message: + error_response += f":{error_message}" + if error_detail: + error_response += f"\n详情:{error_detail}" + + return ActionResponse(Action.RESPONSE, None, error_response) chunks = result.get("data", {}).get("chunks", []) contents = [] @@ -94,7 +106,57 @@ def search_from_ragflow(conn, question=None): context_text = "根据知识库查询结果,没有相关信息。" return ActionResponse(Action.REQLLM, context_text, None) + except requests.exceptions.RequestException as e: + # 网络请求异常 + error_type = type(e).__name__ + logger.bind(tag=TAG).error( + f"RAGflow网络请求失败,异常类型:{error_type},详情:{str(e)}" + ) + + # 根据异常类型提供更详细的错误信息和解决方案 + if isinstance(e, requests.exceptions.ConnectTimeout): + error_response = "RAG接口连接超时(5秒)" + error_response += "\n可能原因:RAGflow服务未启动或网络连接问题" + error_response += "\n解决方案:请检查RAGflow服务状态和网络连接" + + elif isinstance(e, requests.exceptions.ConnectionError): + error_response = "无法连接到RAG接口" + error_response += "\n可能原因:RAGflow服务地址错误或服务未运行" + error_response += "\n解决方案:请检查RAGflow服务地址配置和服务状态" + + elif isinstance(e, requests.exceptions.Timeout): + error_response = "RAG接口请求超时" + error_response += "\n可能原因:RAGflow服务响应缓慢或网络延迟" + error_response += "\n解决方案:请稍后重试或检查RAGflow服务性能" + + elif isinstance(e, requests.exceptions.HTTPError): + # 处理HTTP错误状态码 + if hasattr(e.response, "status_code"): + status_code = e.response.status_code + error_response = f"RAG接口HTTP错误(状态码:{status_code})" + + # 尝试获取响应内容中的错误信息 + try: + error_detail = e.response.json().get("error", {}).get("message", "") + if error_detail: + error_response += f"\n错误详情:{error_detail}" + except: + pass + else: + error_response = f"RAG接口HTTP异常:{str(e)}" + + else: + error_response = f"RAG接口网络异常({error_type}):{str(e)}" + + return ActionResponse(Action.RESPONSE, None, error_response) + except Exception as e: - # 使用安全的方式记录异常,避免编码问题 - logger.bind(tag=TAG).error("从RAGflow获取信息失败,原因:%s", str(e)) - return ActionResponse(Action.RESPONSE, None, "RAG接口返回异常") + # 其他异常 + error_type = type(e).__name__ + logger.bind(tag=TAG).error( + f"RAGflow处理异常,异常类型:{error_type},详情:{str(e)}" + ) + + # 提供详细的错误信息 + error_response = f"RAG接口处理异常({error_type}):{str(e)}" + return ActionResponse(Action.RESPONSE, None, error_response)