debug: 修复了多个bug

1.完成了端侧发送杂音识别
2.修复minimax返回帧解析错误bug
3.修复了返回过慢的bug
This commit is contained in:
killua committed 2024-06-10 02:28:16 +08:00
1 parent 02272c8a8b
commit dba43836b6
9 files changed
+193 -88

No files matched your search

+29 -15
View File
@@ -2,6 +2,8 @@ from utils.xf_asr_utils import xf_asr_websocket_factory, make_first_frame, make_
from .model import Assistant
from .abstract import *
from .public import *
from .exception import *
from .dependency import get_logger
from utils.vits_utils import TextToSpeech
from config import Config
import aiohttp
@@ -14,6 +16,9 @@ import json
vits = TextToSpeech()
# ---------------------------------- #
# ---------- 初始化logger ---------- #
logger = get_logger()
# ---------------------------------- #
#------ 具体 ASR, LLM, TTS 类 ------ #
@@ -31,6 +36,8 @@ class XF_ASR(ASR):
self.segment_start_time = None
async def stream_recognize(self, chunk):
if self.status == FIRST_FRAME and chunk['meta_info']['is_end']: #如果是第一帧,且为end,则判断为杂音
raise SideNoiseError()
if self.websocket is None: #如果websocket未建立,则建立一个新的连接
self.websocket = await xf_asr_websocket_factory()
if self.segment_start_time is None: #如果是第一段,则记录开始时间
@@ -50,8 +57,7 @@ class XF_ASR(ASR):
self.current_message += parse_xfasr_recv(json.loads(await self.websocket.recv()))
if self.current_message == "":
raise AsrResultNoneError()
await self.websocket.close()
print("语音识别结束,用户消息:", self.current_message)
asyncio.create_task(self.websocket.close())
return [{"text":self.current_message, "audio":self.audio}]
current_time = asyncio.get_event_loop().time()
@@ -71,8 +77,7 @@ class MINIMAX_LLM(LLM):
async def chat(self, assistant, prompt, db):
llm_info = json.loads(assistant.llm_info)
messages = json.loads(assistant.messages)
messages.append({"role":"user","content":prompt})
assistant.messages = json.dumps(messages)
messages.append({'role':'user','content':prompt})
payload = json.dumps({
"model": llm_info['model'],
"stream": True,
@@ -104,17 +109,26 @@ class MINIMAX_LLM(LLM):
def __parseChunk(self, llm_chunk):
result = ""
data=json.loads(llm_chunk.decode('utf-8')[6:])
if data["object"] == "chat.completion": #如果是结束帧
self.token = data['usage']['total_tokens']
raise LLMResponseEnd()
elif data['object'] == 'chat.completion.chunk':
for choice in data['choices']:
result += choice['delta']['content']
else:
raise UnkownLLMFrame()
return result
try:
result = ""
chunk_decoded = llm_chunk.decode('utf-8')
chunks = chunk_decoded.split('\n\n')
for chunk in chunks:
if not chunk:
continue
data=json.loads(chunk[6:])
if data["object"] == "chat.completion": #如果是结束帧
self.token = data['usage']['total_tokens']
raise LLMResponseEnd()
elif data['object'] == 'chat.completion.chunk':
for choice in data['choices']:
result += choice['delta']['content']
else:
raise UnkownLLMFrame()
return result
except (json.JSONDecodeError, KeyError):
logger.error(llm_chunk)
raise AbnormalLLMFrame(f"error llm_chunk:{llm_chunk}")
class VITS_TTS(TTS):
def __init__(self):