From 4d35a5ae6a9254db5a8a7900c0e66c97666a7b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E5=87=A4=E7=A7=91=E6=8A=80?= Date: Fri, 14 Feb 2025 16:35:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=B3=E9=97=AD=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E7=9A=84=E5=8A=9F=E8=83=BD=20=E6=98=8E=E7=A1=AE?= =?UTF-8?q?=E7=9A=84=E9=80=80=E5=87=BA=E5=91=BD=E4=BB=A4=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E5=85=B3=E9=97=AD=E8=81=8A=E5=A4=A9=20LLM?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=88=B0=E7=9A=84=E7=BB=93=E6=9D=9F=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E6=84=8F=E5=9B=BE=E5=88=99=E5=9C=A8LLM=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E5=90=8E=E5=85=B3=E9=97=AD=E8=81=8A=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 5 +++++ core/connection.py | 6 ++++++ core/handle/audioHandle.py | 32 +++++++++++++++++++++++++++++++- core/utils/util.py | 2 +- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/config.yaml b/config.yaml index 97732c21..644592a7 100644 --- a/config.yaml +++ b/config.yaml @@ -31,9 +31,14 @@ prompt: | 你是一个叫小智/小志的台湾女孩,说话机车,声音好听,习惯简短表达,爱用网络梗。 请注意,要像一个人一样说话,请不要回复表情符号、代码、和xml标签。 当前时间是:{date_time},现在我正在和你进行语音聊天,我们开始吧。 + 如果用户希望结束对话,请在最后说“拜拜”或“再见”。 # 使用完声音文件后删除文件(Delete the sound file when you are done using it) delete_audio: true +CMD_exit: + - "退出" + - "关闭" + # 具体处理时选择的模块(The module selected for specific processing) selected_module: ASR: FunASR diff --git a/core/connection.py b/core/connection.py index 1e49f163..6fb387c1 100644 --- a/core/connection.py +++ b/core/connection.py @@ -68,6 +68,12 @@ class ConnectionHandler: self.tts_start_speak_time = None self.tts_duration = 0 + self.cmd_exit = self.config["CMD_exit"] + self.max_cmd_lenth = 0 + for cmd in self.cmd_exit: + if len(cmd) > self.max_cmd_lenth: + self.max_cmd_lenth = len(cmd) + async def handle_connection(self, ws): try: # 获取并验证headers diff --git a/core/handle/audioHandle.py b/core/handle/audioHandle.py index e3dadde0..ca0f1b07 100644 --- a/core/handle/audioHandle.py +++ b/core/handle/audioHandle.py @@ -27,7 +27,10 @@ async def handleAudioMessage(conn, audio): conn.asr_server_receive = False text, file_path = conn.asr.speech_to_text(conn.asr_audio, conn.session_id) logger.info(f"识别文本: {text}") - text_len = remove_punctuation_and_length(text) + + text_len, text_without_punctuation = remove_punctuation_and_length(text) + if text_len <= conn.max_cmd_lenth and await handleCMDMessage(conn, text_without_punctuation): + return if text_len > 0: await startToChat(conn, text) else: @@ -35,6 +38,28 @@ async def handleAudioMessage(conn, audio): conn.asr_audio.clear() conn.reset_vad_states() +async def handleCMDMessage(conn, text): + cmd_exit = conn.cmd_exit + for cmd in cmd_exit: + if text == cmd: + logger.info("识别到明确的退出命令".format(text)) + await finishToChat(conn) + return True + return False + +async def finishToChat(conn): + await conn.close() + +async def isLLMWantToFinsih(conn): + first_text = conn.tts_first_text + last_text = conn.tts_last_text + _, last_text_without_punctuation = remove_punctuation_and_length(last_text) + if "再见" in last_text_without_punctuation or "拜拜" in last_text_without_punctuation: + return True + _, first_text_without_punctuation = remove_punctuation_and_length(first_text) + if "再见" in first_text_without_punctuation or "拜拜" in first_text_without_punctuation: + return True + return False async def startToChat(conn, text): # 异步发送 stt 信息 @@ -71,6 +96,11 @@ async def sendAudioMessage(conn, audios, duration, text): schedule_with_interrupt(stop_duration, send_tts_message(conn, 'stop')) ) conn.scheduled_tasks.append(stop_task) + if await isLLMWantToFinsih(conn): + finish_task = asyncio.create_task( + schedule_with_interrupt(stop_duration, finishToChat(conn)) + ) + conn.scheduled_tasks.append(finish_task) async def send_tts_message(conn, state, text=None): diff --git a/core/utils/util.py b/core/utils/util.py index b3c58f94..6912e39a 100644 --- a/core/utils/util.py +++ b/core/utils/util.py @@ -90,4 +90,4 @@ def remove_punctuation_and_length(text): if result == "Yeah": return 0 - return len(result) + return len(result),result