update:优化对话数据上传

This commit is contained in:
hrz
2025-05-02 00:07:36 +08:00
parent 456b1eddcc
commit f0e353cc1a
22 changed files with 295 additions and 340 deletions
+35 -31
View File
@@ -1,6 +1,6 @@
import base64
import os
import time
import base64
from typing import Optional, Dict
import httpx
@@ -54,7 +54,7 @@ class ManageApiClient:
headers={
"User-Agent": f"PythonClient/2.0 (PID:{os.getpid()})",
"Accept": "application/json",
"Authorization": "Bearer " + cls._secret
"Authorization": "Bearer " + cls._secret,
},
timeout=cls.config.get("timeout", 30), # 默认超时时间30秒
)
@@ -127,9 +127,7 @@ class ManageApiClient:
def get_server_config() -> Optional[Dict]:
"""获取服务器基础配置"""
return ManageApiClient._instance._execute_request(
"POST", "/config/server-base"
)
return ManageApiClient._instance._execute_request("POST", "/config/server-base")
def get_agent_models(
@@ -146,35 +144,41 @@ def get_agent_models(
},
)
async def report(mac_address: str,
session_id: str,
sort: int,
chat_type: int,
content: str,
audio,
file_extension: str = "wav",
need_report: bool = None,
report_type: int = None,
reported: bool = None) -> Optional[Dict]:
def report(
mac_address: str, session_id: str, chat_type: int, content: str, opus_data
) -> Optional[Dict]:
"""带熔断的业务方法示例"""
if not content or not ManageApiClient._instance:
return None
return await ManageApiClient._instance._execute_request(
"POST",
f"/agent/chat-history/report",
json = {
"macAddress": mac_address,
"sessionId": session_id,
"sort": sort,
"chatType": chat_type,
"content": content,
"fileBase64": base64.b64encode(audio).decode('utf-8'),
"fileExtension": file_extension,
"needReport": need_report,
"reportType": report_type,
"reported": reported
}
)
try:
# 处理opus_data为列表的情况
if isinstance(opus_data, list):
# 将列表中的所有bytes数据合并
combined_data = b"".join(opus_data)
else:
combined_data = opus_data
# 将二进制数据转换为Base64编码的字符串
opus_data_base64 = (
base64.b64encode(combined_data).decode("utf-8") if combined_data else None
)
return ManageApiClient._instance._execute_request(
"POST",
f"/agent/chat-history/report",
json={
"macAddress": mac_address,
"sessionId": session_id,
"chatType": chat_type,
"content": content,
"opusDataBase64": opus_data_base64,
},
)
except Exception as e:
print(f"TTS上报失败: {e}")
return None
def init_service(config):
ManageApiClient(config)