From fb69e7fc01dbb3d58b86c4549525da6dba4b54a7 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Thu, 13 Feb 2025 21:22:36 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=E8=AE=BE=E7=BD=AEsentence=5Fstart?= =?UTF-8?q?=E6=88=90=E5=BB=B6=E8=BF=9F=E5=8F=91=E9=80=81=EF=BC=8C=E8=A6=81?= =?UTF-8?q?=E4=B8=8D=E7=84=B6=E8=B7=9F=E8=AF=AD=E9=9F=B3=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=AF=B9=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 1 + core/handle/audioHandle.py | 15 +++++++++------ core/utils/llm.py | 11 +++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/config.yaml b/config.yaml index 68b4b978..63baa8e5 100644 --- a/config.yaml +++ b/config.yaml @@ -53,6 +53,7 @@ LLM: url: https://api.deepseek.com api_key: 你的deepseek api key ChatGLMLLM: + # glm-4-flash 是免费的,但是还是需要注册填写api_key的 # 可在这里找到你的api key https://bigmodel.cn/usercenter/proj-mgmt/apikeys model_name: glm-4-flash url: https://open.bigmodel.cn/api/paas/v4/ diff --git a/core/handle/audioHandle.py b/core/handle/audioHandle.py index 7217178c..e3dadde0 100644 --- a/core/handle/audioHandle.py +++ b/core/handle/audioHandle.py @@ -35,11 +35,13 @@ async def handleAudioMessage(conn, audio): conn.asr_audio.clear() conn.reset_vad_states() + async def startToChat(conn, text): # 异步发送 stt 信息 - asyncio.create_task( + stt_task = asyncio.create_task( schedule_with_interrupt(0, send_stt_message(conn, text)) ) + conn.scheduled_tasks.append(stt_task) conn.executor.submit(conn.chat, text) @@ -52,7 +54,10 @@ async def sendAudioMessage(conn, audios, duration, text): conn.tts_start_speak_time = time.time() # 发送 sentence_start(每个音频文件之前发送一次) - await send_tts_message(conn, "sentence_start", text) + sentence_task = asyncio.create_task( + schedule_with_interrupt(base_delay, send_tts_message(conn, "sentence_start", text)) + ) + conn.scheduled_tasks.append(sentence_task) conn.tts_duration += duration @@ -60,10 +65,6 @@ async def sendAudioMessage(conn, audios, duration, text): for idx, opus_packet in enumerate(audios): await conn.websocket.send(opus_packet) - # 每个音频文件发送结束时,发送 sentence_end - if idx == len(audios) - 1: - await send_tts_message(conn, "sentence_end", text) - if conn.llm_finish_task and text == conn.tts_last_text: stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time) stop_task = asyncio.create_task( @@ -71,6 +72,7 @@ async def sendAudioMessage(conn, audios, duration, text): ) conn.scheduled_tasks.append(stop_task) + async def send_tts_message(conn, state, text=None): """发送 TTS 状态消息""" message = { @@ -85,6 +87,7 @@ async def send_tts_message(conn, state, text=None): if state == "stop": conn.clearSpeakStatus() + async def send_stt_message(conn, text): """发送 STT 状态消息""" stt_text = get_string_no_punctuation_or_emoji(text) diff --git a/core/utils/llm.py b/core/utils/llm.py index 0b1e8ccc..632b8874 100644 --- a/core/utils/llm.py +++ b/core/utils/llm.py @@ -12,6 +12,13 @@ logger = logging.getLogger(__name__) class LLM(ABC): + def __init__(self, config): + api_key = config.get("api_key") + # 如果api_key包含中文“你” + if "你" in api_key: + logger.error("你还没配置LLM的密钥,请在配置文件中配置密钥,否则无法正常工作") + pass + @abstractmethod def response(self, session_id, dialogue): """LLM response generator""" @@ -20,6 +27,7 @@ class LLM(ABC): class DeepSeekLLM(LLM): def __init__(self, config): + super().__init__(config) self.model_name = config.get("model_name") self.api_key = config.get("api_key") self.base_url = config.get("url") @@ -46,6 +54,7 @@ class DeepSeekLLM(LLM): class ChatGLMLLM(LLM): def __init__(self, config): + super().__init__(config) self.model_name = config.get("model_name") self.api_key = config.get("api_key") self.base_url = config.get("url") @@ -70,6 +79,7 @@ class ChatGLMLLM(LLM): class AliLLM(LLM): def __init__(self, config): + super().__init__(config) self.model_name = config.get("model_name") self.api_key = config.get("api_key") self.base_url = config.get("base_url") @@ -94,6 +104,7 @@ class AliLLM(LLM): class DifyLLM(LLM): def __init__(self, config): + super().__init__(config) self.api_key = config["api_key"] self.base_url = config.get("base_url", "https://api.dify.ai/v1").rstrip('/')