|
@@ -446,37 +446,37 @@ class ToTextBotAgent:
|
|
|
# self.call_agent.logger.info(f"请求数据:{request_data},url:{url}")
|
|
|
with requests.Session() as session:
|
|
|
massage = None
|
|
|
- try:
|
|
|
- session.headers.update({'Content-Type': 'application/json'})
|
|
|
- while try_count > 0:
|
|
|
- try:
|
|
|
- try_count = try_count - 1
|
|
|
- response = session.post(url=url, json=json.loads(request_data), timeout=.5)
|
|
|
- # response = requests.post(url=url, json=json.loads(request_data), headers=headers, timeout=10) # 使用占位URL
|
|
|
- if response.status_code == 200:
|
|
|
- response_data = response.json()
|
|
|
- if "data" in response_data and response_data["code"]==0:
|
|
|
- data = response_data["data"]
|
|
|
- massage = ChatMessage.from_json(data)
|
|
|
- self.call_agent.message_queue.put(massage)
|
|
|
- else:
|
|
|
- self.call_agent.logger.info("响应中没有 'data' 字段")
|
|
|
+ # try:
|
|
|
+ session.headers.update({'Content-Type': 'application/json'})
|
|
|
+ while try_count > 0:
|
|
|
+ try:
|
|
|
+ try_count = try_count - 1
|
|
|
+ response = session.post(url=url, json=json.loads(request_data), timeout=.5)
|
|
|
+ # response = requests.post(url=url, json=json.loads(request_data), headers=headers, timeout=10) # 使用占位URL
|
|
|
+ if response.status_code == 200:
|
|
|
+ response_data = response.json()
|
|
|
+ if "data" in response_data and response_data["code"]==0:
|
|
|
+ data = response_data["data"]
|
|
|
+ massage = ChatMessage.from_json(data)
|
|
|
+ self.call_agent.message_queue.put(massage)
|
|
|
else:
|
|
|
- self.call_agent.logger.info(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
|
|
|
- break
|
|
|
- except requests.RequestException as e:
|
|
|
- traceback.print_exc()
|
|
|
- self.call_agent.logger.error(f"TaskId={request.taskId}, 请求发生异常: {e}, URL: {url}")
|
|
|
- if not response or response.status_code != 200:
|
|
|
- massage = self.get_default_response()
|
|
|
- self.call_agent.message_queue.put(massage)
|
|
|
- finally:
|
|
|
- session.close()
|
|
|
+ self.call_agent.logger.info("响应中没有 'data' 字段")
|
|
|
+ else:
|
|
|
+ self.call_agent.logger.info(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
|
|
|
+ break
|
|
|
+ except requests.RequestException as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ self.call_agent.logger.error(f"TaskId={request.taskId}, 请求发生异常: {e}, URL: {url}")
|
|
|
+ if not response or response.status_code != 200:
|
|
|
+ massage = self.get_default_response()
|
|
|
+ self.call_agent.message_queue.put(massage)
|
|
|
+ # finally:
|
|
|
+ # session.close()
|
|
|
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"sessionId={ self.call_agent.session_id}, timeCost={latency}, chat::request:{request_data}, response:{response.text}")
|
|
|
+ self.call_agent.logger.info(f"sessionId={ self.call_agent.session_id}, timeCost={latency}, chat::request:{request_data}, response:{response.text if response else None}")
|
|
|
|
|
|
def get_default_response(self):
|
|
|
response = {
|