Browse Source

feat: 报错返回&格式化返回信息

huangzhichao 1 month ago
parent
commit
2c4fd56ea9
1 changed files with 4 additions and 2 deletions
  1. 4 2
      app/providers/understand_image_provider.py

+ 4 - 2
app/providers/understand_image_provider.py

@@ -66,13 +66,14 @@ class UnderstandImageProvider:
     print("UnderstandImageProvider called")
     def understand_image(self, image_url: str, *, model: str) -> DataResponse:
 
-
         client = OpenAI(
             api_key = settings.dashscope_api_key or "",
             base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
         )
+
         if not client:
             logger.error("OpenAI client is not initialized.")
+            return DataResponse(code=1, data=None, msg=f"OpenAI client is not initialized")
 
         completion = client.chat.completions.create(
             model=model,
@@ -105,6 +106,7 @@ class UnderstandImageProvider:
                         ocr_text = str(args.get("ocr_text", "")).strip()
         except Exception as e:
             logger.error("parse tool call failed: %s", e, exc_info=True)
+            return DataResponse(code=1, data=None, msg=f"parse tool call failed: {e}")
 
         # Fallback: if no tool-calls returned, try to read text content
         content = getattr(msg, "content", None)
@@ -113,5 +115,5 @@ class UnderstandImageProvider:
 
         print("✅ OCR_TEXT:\n", ocr_text)
 
-        return DataResponse(code=200, data=ocr_text, msg="Image understood successfully")
+        return DataResponse(code=0, data=ocr_text, msg="success")