Selaa lähdekoodia

nlp-api-improve

luojunhui 4 kuukautta sitten
vanhempi
commit
e03a16cf66
1 muutettua tiedostoa jossa 45 lisäystä ja 3 poistoa
  1. 45 3
      applications/api/nlp_api.py

+ 45 - 3
applications/api/nlp_api.py

@@ -2,6 +2,9 @@
 @author: luojunhui
 """
 import requests
+from requests.exceptions import RequestException, JSONDecodeError
+
+from applications import bot
 
 
 def similarity_between_title_list(target_title_list: list[str], base_title_list: list[str]) -> list[list[float]]:
@@ -11,7 +14,9 @@ def similarity_between_title_list(target_title_list: list[str], base_title_list:
     :param base_title_list: base title_list
     :return: list of similarity
     """
-    url = 'http://61.48.133.26:6061/nlp'
+
+    url = 'http://ip:6060/nlp'
+    url_backup = 'http://ip:6061/nlp'
     body = {
         "data": {
             "text_list_a": target_title_list,
@@ -20,7 +25,44 @@ def similarity_between_title_list(target_title_list: list[str], base_title_list:
         "function": "similarities_cross",
         "use_cache": False
     }
-    response_json = requests.post(url, json=body, timeout=120).json()
-    score_array = response_json['score_list_list']
+
+    response = None
+    try:
+        response = requests.post(url, json=body, timeout=120)
+        if response.status_code != 200:
+            response = requests.post(url_backup, json=body, timeout=120)
+    except RequestException as e:
+        bot(
+            title='NLP API 网络异常',
+            detail={
+                "error_type": type(e).__name__,
+                "error_msg": str(e)
+            }
+        )
+        return []
+
+    if response.status_code != 200:
+        bot(
+            title='NLP API 业务异常',
+            detail={
+                "status_code": response.status_code,
+                "response_text": response.text[:200]  # 截取部分内容避免过大
+            }
+        )
+        return []
+
+    try:
+        response_json = response.json()
+        score_array = response_json['score_list_list']
+    except (JSONDecodeError, KeyError) as e:
+        bot(
+            title='NLP响应数据异常',
+            detail={
+                "error_type": type(e).__name__,
+                "raw_response": response.text[:200]
+            }
+        )
+        return []
+
     return score_array