mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 00:23:53 +08:00
增加关闭聊天的功能
明确的退出命令可以直接关闭聊天 LLM检测到的结束聊天意图则在LLM回复后关闭聊天
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
+1
-1
@@ -90,4 +90,4 @@ def remove_punctuation_and_length(text):
|
||||
|
||||
if result == "Yeah":
|
||||
return 0
|
||||
return len(result)
|
||||
return len(result),result
|
||||
|
||||
Reference in New Issue
Block a user