debug: 修复了多个bug
1.完成了端侧发送杂音识别 2.修复minimax返回帧解析错误bug 3.修复了返回过慢的bug
This commit is contained in:
1 parent
02272c8a8b
commit
dba43836b6
9 files changed
+193
-88
No files matched your search
@@ -1,20 +1,26 @@
|
||||
from fastapi import FastAPI, Depends, WebSocket, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from config import Config
|
||||
from app.concrete import Agent, AsrResultNoneError
|
||||
from app.concrete import Agent
|
||||
from app.model import Assistant, User, get_db
|
||||
from app.schemas import *
|
||||
from app.dependency import get_logger
|
||||
from app.exception import AsrResultNoneError, AbnormalLLMFrame, SideNoiseError
|
||||
import uvicorn
|
||||
import uuid
|
||||
import json
|
||||
|
||||
# 公共函数 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
def update_messages(messages, llm_text):
|
||||
def update_messages(messages, kid_text,llm_text):
|
||||
messages = json.loads(messages)
|
||||
messages.append({"role":"user","content":kid_text})
|
||||
messages.append({"role":"assistant","content":llm_text})
|
||||
return json.dumps(messages,ensure_ascii=False)
|
||||
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 引入logger对象
|
||||
logger = get_logger()
|
||||
|
||||
# 创建FastAPI实例
|
||||
app = FastAPI()
|
||||
|
||||
@@ -89,6 +95,7 @@ async def update_assistant_system_prompt(id: str,request: update_assistant_syste
|
||||
assistant = db.query(Assistant).filter(Assistant.id == id).first()
|
||||
if assistant:
|
||||
assistant.system_prompt = request.system_prompt
|
||||
assistant.messages = json.dumps([{"role":"system","content":assistant.system_prompt}],ensure_ascii=False)
|
||||
db.commit()
|
||||
return {"code":200,"msg":"success","data":{}}
|
||||
else:
|
||||
@@ -163,11 +170,13 @@ async def update_user(id: str,request: update_user_request,db=Depends(get_db)):
|
||||
@app.websocket("/api/chat/streaming/temporary")
|
||||
async def streaming_chat(ws: WebSocket,db=Depends(get_db)):
|
||||
await ws.accept()
|
||||
agent = Agent(asr_type=Config.ASR, llm_type=Config.LLM, tts_type=Config.TTS)
|
||||
assistant = None
|
||||
asr_results = []
|
||||
llm_text = ""
|
||||
logger.debug("WebSocket连接成功")
|
||||
try:
|
||||
agent = Agent(asr_type=Config.ASR, llm_type=Config.LLM, tts_type=Config.TTS)
|
||||
assistant = None
|
||||
asr_results = []
|
||||
llm_text = ""
|
||||
logger.debug("开始进行ASR识别")
|
||||
while len(asr_results)==0:
|
||||
chunk = json.loads(await ws.receive_text())
|
||||
if assistant is None:
|
||||
@@ -175,24 +184,36 @@ async def streaming_chat(ws: WebSocket,db=Depends(get_db)):
|
||||
agent.init_recorder(assistant.user_id)
|
||||
chunk["audio"] = agent.user_audio_process(chunk["audio"], db)
|
||||
asr_results = await agent.stream_recognize(chunk, db)
|
||||
kid_text = asr_results[0]['text'] #asr结果的[0]默认为孩子(主要用户)的asr结果
|
||||
logger.debug(f"ASR识别成功,识别结果为:{kid_text}")
|
||||
prompt = agent.prompt_process(asr_results, db)
|
||||
agent.recorder.input_text = prompt
|
||||
logger.debug("开始调用大模型")
|
||||
llm_frames = await agent.chat(assistant, prompt, db)
|
||||
async for llm_frame in llm_frames:
|
||||
resp_msgs = agent.llm_msg_process(llm_frame, db)
|
||||
for resp_msg in resp_msgs:
|
||||
llm_text += resp_msg
|
||||
tts_audio = agent.synthetize(assistant, resp_msg, db)
|
||||
agent.tts_audio_process(tts_audio, db)
|
||||
await ws.send_bytes(agent.encode(resp_msg, tts_audio))
|
||||
logger.debug(f'websocket返回:{resp_msg}')
|
||||
logger.debug(f"大模型返回结束,返回结果为:{llm_text}")
|
||||
await ws.send_text(json.dumps({"type": "close", "code": 200, "msg": ""}, ensure_ascii=False))
|
||||
logger.debug("结束帧发送完毕")
|
||||
assistant.messages = update_messages(assistant.messages, kid_text ,llm_text)
|
||||
db.commit()
|
||||
logger.debug("聊天更新成功")
|
||||
agent.recorder.output_text = llm_text
|
||||
agent.save()
|
||||
logger.debug("音频保存成功")
|
||||
except AsrResultNoneError:
|
||||
await ws.send_text(json.dumps({"type":"close","code":201,"msg":""}, ensure_ascii=False))
|
||||
return
|
||||
prompt = agent.prompt_process(asr_results, db)
|
||||
agent.recorder.input_text = prompt
|
||||
llm_frames = await agent.chat(assistant, prompt, db)
|
||||
async for llm_frame in llm_frames:
|
||||
resp_msgs = agent.llm_msg_process(llm_frame, db)
|
||||
for resp_msg in resp_msgs:
|
||||
llm_text += resp_msg
|
||||
tts_audio = agent.synthetize(assistant, resp_msg, db)
|
||||
agent.tts_audio_process(tts_audio, db)
|
||||
await ws.send_bytes(agent.encode(resp_msg, tts_audio))
|
||||
await ws.send_text(json.dumps({"type": "close", "code": 200, "msg": ""}, ensure_ascii=False))
|
||||
assistant.messages = update_messages(assistant.messages, llm_text)
|
||||
db.commit()
|
||||
agent.recorder.output_text = llm_text
|
||||
agent.save()
|
||||
await ws.send_text(json.dumps({"type":"close","code":201,"msg":"asr结果为空"}, ensure_ascii=False))
|
||||
except AbnormalLLMFrame as e:
|
||||
await ws.send_text(json.dumps({"type":"close","code":202,"msg":str(e)}, ensure_ascii=False))
|
||||
except SideNoiseError as e:
|
||||
await ws.send_text(json.dumps({"type":"close","code":203,"msg":str(e)}, ensure_ascii=False))
|
||||
logger.debug("WebSocket连接断开")
|
||||
await ws.close()
|
||||
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
Reference in new issue
Block a user