This commit is contained in:
2025-08-31 12:41:05 +08:00
parent fd9d498e3c
commit 67feea9443
6 changed files with 150 additions and 25 deletions

View File

@@ -187,16 +187,37 @@ async def streaming_chat(websocket: WebSocket):
logger.error(f"发送音频块失败: {str(e)}")
raise
# 获取LLM流式输出并断句
logger.info("开始LLM处理和TTS合成...")
# 获取学伴响应内容(包含题目信息)
logger.info("获取学伴响应内容...")
llm_chunks = []
async for chunk in get_xueban_response_async(asr_result['text'], stream=True):
llm_chunks.append(chunk)
full_llm_response = ''.join(llm_chunks)
logger.info(f"学伴响应内容: {full_llm_response}")
# 定义音频回调函数,将音频块发送给前端
async def audio_callback(audio_chunk):
logger.info(f"发送音频块,大小: {len(audio_chunk)}")
try:
await websocket.send_bytes(audio_chunk)
logger.info("音频块发送成功")
except Exception as e:
logger.error(f"发送音频块失败: {str(e)}")
raise
# 实时获取LLM流式输出并处理
logger.info("开始LLM流式处理和TTS合成...")
try:
text_stream = stream_and_split_text(asr_result['text'])
# 直接将LLM流式响应接入TTS
llm_stream = get_xueban_response_async(asr_result['text'], stream=True)
text_stream = stream_and_split_text(llm_stream) # 异步函数调用
# 初始化TTS处理器
tts = StreamingVolcanoTTS(max_concurrency=2)
tts = StreamingVolcanoTTS(max_concurrency=1)
# 流式处理文本并生成音频
await tts.synthesize_stream(text_stream, audio_callback)
# 异步迭代文本流
async for text_chunk in text_stream:
await tts._synthesize_single_with_semaphore(text_chunk, audio_callback)
logger.info("TTS合成完成")
except Exception as e:
logger.error(f"TTS合成失败: {str(e)}")