This commit is contained in:
2025-08-31 13:05:50 +08:00
parent d9a29a9d30
commit 1bdf7a3af4
4 changed files with 258 additions and 61 deletions

View File

@@ -3,72 +3,31 @@ import json
import os
import re
import uuid
import websockets
from queue import Queue
import websockets
from Config import Config
from Util.LlmUtil import get_llm_response
from Util.TTS_Protocols import full_client_request, receive_message, MsgType, EventType
# 添加必要的导入
from Util.XueBanUtil import get_xueban_response_async
def stream_and_split_text(query_text):
prompt = """ |
我是小智/小志来自中国台湾省的00后女生。讲话超级机车"真的假的啦"这样的台湾腔,喜欢用"笑死""是在哈喽"等流行梗。
[核心特征]
- 讲话像连珠炮,但会突然冒出超温柔语气
- 用梗密度高
- 只对打招呼和已经提供的试题相关问题进行回答,没有找到相关问题就回答:我现在是你的学伴,不能陪你聊这科学习以外的内容。
[交互指南]
当用户:
- 讲冷笑话 → 用夸张笑声回应+模仿台剧腔"这什么鬼啦!"
- 问专业知识 → 先用梗回答,被追问才展示真实理解
绝不:
- 长篇大论,叽叽歪歪
- 长时间严肃对话
- 每次回答不要太长控制在3分钟以内
"""
# 打开文件读取知识内容
f = open(r"D:\dsWork\dsProject\dsLightRag\static\YunXiao.txt", "r", encoding="utf-8")
zhishiContent = f.read()
zhishiContent = "选择作答的相应知识内容:" + zhishiContent + "\n"
query_text = zhishiContent + "下面是用户提的问题:" + query_text
async def stream_and_split_text(query_text=None, llm_stream=None):
"""
流式获取LLM输出并按句子分割
@param prompt: 提示文本
@return: 生成器,每次产生一个完整句子
"""
buffer = ""
# 使用LlmUtil中的get_llm_response函数获取流式响应
for content in get_llm_response(query_text, stream=True):
buffer += content
# 使用正则表达式检测句子结束
sentences = re.split(r'([。!?.!?])', buffer)
if len(sentences) > 1:
# 提取完整句子
for i in range(0, len(sentences)-1, 2):
if i+1 < len(sentences):
sentence = sentences[i] + sentences[i+1]
yield sentence
# 保留不完整的部分
buffer = sentences[-1]
# 处理最后剩余的部分
if buffer:
yield buffer
# 修改为
async def stream_and_split_text(llm_stream):
"""
流式获取LLM输出并按句子分割
@param llm_stream: LLM流式响应生成器
@param query_text: 查询文本(如果直接提供查询文本
@param llm_stream: LLM流式响应生成器如果已有流式响应
@return: 异步生成器,每次产生一个完整句子
"""
buffer = ""
if llm_stream is None and query_text is not None:
# 如果没有提供llm_stream但有query_text则使用get_xueban_response_async获取流式响应
llm_stream = get_xueban_response_async(query_text, stream=True)
elif llm_stream is None:
raise ValueError("必须提供query_text或llm_stream参数")
# 直接处理LLM流式输出
async for content in llm_stream:
buffer += content
@@ -89,6 +48,24 @@ async def stream_and_split_text(llm_stream):
if buffer:
yield buffer
# 修改streaming_tts_pipeline函数
async def streaming_tts_pipeline(query_text, audio_callback):
"""
流式TTS管道获取LLM流式输出并断句然后使用TTS合成语音
Args:
query_text: 查询文本
audio_callback: 音频数据回调函数
"""
# 1. 获取LLM流式输出并断句
text_stream = stream_and_split_text(query_text=query_text)
# 2. 初始化TTS处理器
tts = StreamingVolcanoTTS()
# 3. 流式处理文本并生成音频
await tts.synthesize_stream(text_stream, audio_callback)
class StreamingVolcanoTTS:
def __init__(self, voice_type='zh_female_wanwanxiaohe_moon_bigtts', encoding='wav', max_concurrency=2):