Selaa lähdekoodia

Merge branch 'dev_20241205' of ssh://gitlab.fuxicarbon.com:1111/client_service/voice-gateway-service into dev_20241205

DavidLiu 3 kuukautta sitten
vanhempi
commit
75abcbc5aa
1 muutettua tiedostoa jossa 23 lisäystä ja 19 poistoa
  1. 23 19
      src/core/voip/bot.py

+ 23 - 19
src/core/voip/bot.py

@@ -415,26 +415,30 @@ class ToTextBotAgent:
         url = f"http://{SERVE_HOST}:40072/botservice"
         start_time = time.time()
         self.call_agent.logger.info(f"请求数据:{request_data},url:{url}")
-        try:
-            response = requests.post(url=url,  json=json.loads(request_data), headers=headers, timeout=10)  # 使用占位URL
-            self.call_agent.logger.info(f"sessionId={ self.call_agent.session_id}, chat::request:{request_data}, response:{response.text}")
-            if response.status_code == 200:
-                response_data = response.json()
-                if "data" in response_data and response_data["code"]==0:
-                    data = response_data["data"]
-                    parsed_response = ChatMessage.from_json(data)
-                    self.call_agent.message_queue.put(parsed_response)
+        with requests.Session() as session:
+            session.headers.update({'Content-Type': 'application/json'})
+            try:
+                response = session.post(url=url, json=json.loads(request_data), timeout=10)
+                # response = requests.post(url=url,  json=json.loads(request_data), headers=headers, timeout=10)  # 使用占位URL
+                self.call_agent.logger.info(f"sessionId={ self.call_agent.session_id}, chat::request:{request_data}, response:{response.text}")
+                if response.status_code == 200:
+                    response_data = response.json()
+                    if "data" in response_data and response_data["code"]==0:
+                        data = response_data["data"]
+                        parsed_response = ChatMessage.from_json(data)
+                        self.call_agent.message_queue.put(parsed_response)
+                    else:
+                        self.call_agent.logger.info("响应中没有 'data' 字段")
                 else:
-                    self.call_agent.logger.info("响应中没有 'data' 字段")
-            else:
-                # 错误处理
-                self.call_agent.logger.info(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
-        except requests.RequestException as e:
-            self.call_agent.logger.info(f"请求发生异常: {e}")
-        finally:
-            latency = (time.time() - start_time)
-            registry.BOT_REQUEST_COUNT.inc()
-            registry.BOT_REQUEST_LATENCY.labels(request.taskId).observe(latency)
+                    # 错误处理
+                    self.call_agent.logger.info(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
+            except requests.RequestException as e:
+                traceback.print_exc()
+                self.call_agent.logger.error(f"TaskId={request.taskId}, 请求发生异常: {e}, URL: {url}")
+            finally:
+                latency = (time.time() - start_time)
+                registry.BOT_REQUEST_COUNT.inc()
+                registry.BOT_REQUEST_LATENCY.labels(request.taskId).observe(latency)
 
 
 # 模拟接口请求返回