Merge pull request #1340 from xinnan-tech/py_symbol_fix

解决符号误识别问题,添加与退出相关的提示词
This commit is contained in:
hrz
2025-05-21 22:12:52 +08:00
committed by GitHub
5 changed files with 18 additions and 15 deletions
@@ -40,8 +40,8 @@ async def checkWakeupWords(conn, text):
if not enable_wakeup_words_response_cache: if not enable_wakeup_words_response_cache:
return False return False
"""检查是否是唤醒词""" """检查是否是唤醒词"""
_, text = remove_punctuation_and_length(text) _, filtered_text = remove_punctuation_and_length(text)
if text in conn.config.get("wakeup_words"): if filtered_text in conn.config.get("wakeup_words"):
await send_stt_message(conn, text) await send_stt_message(conn, text)
conn.tts_first_text_index = 0 conn.tts_first_text_index = 0
conn.tts_last_text_index = 0 conn.tts_last_text_index = 0
@@ -13,10 +13,11 @@ TAG = __name__
async def handle_user_intent(conn, text): async def handle_user_intent(conn, text):
# 检查是否有明确的退出命令 # 检查是否有明确的退出命令
if await check_direct_exit(conn, text): filtered_text = remove_punctuation_and_length(text)[1]
if await check_direct_exit(conn, filtered_text):
return True return True
# 检查是否是唤醒词 # 检查是否是唤醒词
if await checkWakeupWords(conn, text): if await checkWakeupWords(conn, filtered_text):
return True return True
if conn.intent_type == "function_call": if conn.intent_type == "function_call":
@@ -39,14 +39,14 @@ async def handleAudioMessage(conn, audio):
if len(conn.asr_audio) < 15: if len(conn.asr_audio) < 15:
conn.asr_server_receive = True conn.asr_server_receive = True
else: else:
text, _ = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id) raw_text, _ = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id) # 确保ASR模块返回原始文本
conn.logger.bind(tag=TAG).info(f"识别文本: {text}") conn.logger.bind(tag=TAG).info(f"识别文本: {raw_text}")
text_len, _ = remove_punctuation_and_length(text) text_len, _ = remove_punctuation_and_length(raw_text)
if text_len > 0: if text_len > 0:
# 使用自定义模块进行上报 # 使用自定义模块进行上报
enqueue_asr_report(conn, text, copy.deepcopy(conn.asr_audio)) enqueue_asr_report(conn, raw_text, copy.deepcopy(conn.asr_audio))
await startToChat(conn, text) await startToChat(conn, raw_text)
else: else:
conn.asr_server_receive = True conn.asr_server_receive = True
conn.asr_audio.clear() conn.asr_audio.clear()
@@ -45,17 +45,17 @@ async def handleTextMessage(conn, message):
conn.client_have_voice = False conn.client_have_voice = False
conn.asr_audio.clear() conn.asr_audio.clear()
if "text" in msg_json: if "text" in msg_json:
text = msg_json["text"] original_text = msg_json["text"] # 保留原始文本
_, text = remove_punctuation_and_length(text) filtered_len, filtered_text = remove_punctuation_and_length(original_text)
# 识别是否是唤醒词 # 识别是否是唤醒词
is_wakeup_words = text in conn.config.get("wakeup_words") is_wakeup_words = filtered_text in conn.config.get("wakeup_words")
# 是否开启唤醒词回复 # 是否开启唤醒词回复
enable_greeting = conn.config.get("enable_greeting", True) enable_greeting = conn.config.get("enable_greeting", True)
if is_wakeup_words and not enable_greeting: if is_wakeup_words and not enable_greeting:
# 如果是唤醒词,且关闭了唤醒词回复,就不用回答 # 如果是唤醒词,且关闭了唤醒词回复,就不用回答
await send_stt_message(conn, text) await send_stt_message(conn, original_text)
await send_tts_message(conn, "stop", None) await send_tts_message(conn, "stop", None)
elif is_wakeup_words: elif is_wakeup_words:
# 上报纯文字数据(复用ASR上报功能,但不提供音频数据) # 上报纯文字数据(复用ASR上报功能,但不提供音频数据)
@@ -63,9 +63,9 @@ async def handleTextMessage(conn, message):
await startToChat(conn, "嘿,你好呀") await startToChat(conn, "嘿,你好呀")
else: else:
# 上报纯文字数据(复用ASR上报功能,但不提供音频数据) # 上报纯文字数据(复用ASR上报功能,但不提供音频数据)
enqueue_asr_report(conn, text, []) enqueue_asr_report(conn, original_text, [])
# 否则需要LLM对文字内容进行答复 # 否则需要LLM对文字内容进行答复
await startToChat(conn, text) await startToChat(conn, original_text)
elif msg_json["type"] == "iot": elif msg_json["type"] == "iot":
conn.logger.bind(tag=TAG).info(f"收到iot消息:{message}") conn.logger.bind(tag=TAG).info(f"收到iot消息:{message}")
if "descriptors" in msg_json: if "descriptors" in msg_json:
@@ -53,6 +53,8 @@ class IntentProvider(IntentProviderBase):
prompt = ( prompt = (
"你是一个意图识别助手。请分析用户的最后一句话,判断用户意图并调用相应的函数。\n\n" "你是一个意图识别助手。请分析用户的最后一句话,判断用户意图并调用相应的函数。\n\n"
"- 如果用户使用疑问词(如'怎么''为什么''如何')询问退出相关的问题(例如'怎么退出了?'),注意这不是让你退出,请返回 {'function_call': {'name': 'continue_chat'}\n"
"- 仅当用户明确使用'退出系统''结束对话''我不想和你说话了'等指令时,才触发 handle_exit_intent\n\n"
f"{functions_desc}\n" f"{functions_desc}\n"
"处理步骤:\n" "处理步骤:\n"
"1. 分析用户输入,确定用户意图\n" "1. 分析用户输入,确定用户意图\n"