mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 10:03:54 +08:00
fix: intent阻塞主线程
This commit is contained in:
@@ -118,8 +118,17 @@ async def process_intent_result(
|
|||||||
|
|
||||||
请根据以上信息回答用户的问题:{original_text}"""
|
请根据以上信息回答用户的问题:{original_text}"""
|
||||||
|
|
||||||
response = conn.intent.replyResult(context_prompt, original_text)
|
# 使用异步调用避免阻塞事件循环,影响其他设备的音频播放
|
||||||
speak_txt(conn, response)
|
try:
|
||||||
|
response = asyncio.run_coroutine_threadsafe(
|
||||||
|
conn.intent.replyResult(context_prompt, original_text),
|
||||||
|
conn.loop,
|
||||||
|
).result()
|
||||||
|
except Exception as e:
|
||||||
|
conn.logger.bind(tag=TAG).error(f"LLM生成回复失败: {e}")
|
||||||
|
response = None
|
||||||
|
if response:
|
||||||
|
speak_txt(conn, response)
|
||||||
|
|
||||||
conn.executor.submit(process_context_result)
|
conn.executor.submit(process_context_result)
|
||||||
return True
|
return True
|
||||||
@@ -184,7 +193,15 @@ async def process_intent_result(
|
|||||||
elif result.action == Action.REQLLM: # 调用函数后再请求llm生成回复
|
elif result.action == Action.REQLLM: # 调用函数后再请求llm生成回复
|
||||||
text = result.result
|
text = result.result
|
||||||
conn.dialogue.put(Message(role="tool", content=text))
|
conn.dialogue.put(Message(role="tool", content=text))
|
||||||
llm_result = conn.intent.replyResult(text, original_text)
|
# 使用异步调用避免阻塞事件循环,影响其他设备的音频播放
|
||||||
|
try:
|
||||||
|
llm_result = asyncio.run_coroutine_threadsafe(
|
||||||
|
conn.intent.replyResult(text, original_text),
|
||||||
|
conn.loop,
|
||||||
|
).result()
|
||||||
|
except Exception as e:
|
||||||
|
conn.logger.bind(tag=TAG).error(f"LLM生成回复失败: {e}")
|
||||||
|
llm_result = text
|
||||||
if llm_result is None:
|
if llm_result is None:
|
||||||
llm_result = text
|
llm_result = text
|
||||||
speak_txt(conn, llm_result)
|
speak_txt(conn, llm_result)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
from typing import List, Dict, TYPE_CHECKING
|
from typing import List, Dict, TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -120,12 +121,18 @@ class IntentProvider(IntentProviderBase):
|
|||||||
)
|
)
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
def replyResult(self, text: str, original_text: str):
|
async def replyResult(self, text: str, original_text: str):
|
||||||
|
"""使用 asyncio.to_thread 避免阻塞事件循环"""
|
||||||
try:
|
try:
|
||||||
llm_result = self.llm.response_no_stream(
|
user_prompt = (
|
||||||
|
"请根据以上内容,像人类一样说话的口吻回复用户,要求简洁,请直接返回结果。用户现在说:"
|
||||||
|
+ original_text
|
||||||
|
)
|
||||||
|
# 使用 to_thread 将同步阻塞调用放到线程池中执行,不阻塞事件循环
|
||||||
|
llm_result = await asyncio.to_thread(
|
||||||
|
self.llm.response_no_stream,
|
||||||
system_prompt=text,
|
system_prompt=text,
|
||||||
user_prompt="请根据以上内容,像人类一样说话的口吻回复用户,要求简洁,请直接返回结果。用户现在说:"
|
user_prompt=user_prompt,
|
||||||
+ original_text,
|
|
||||||
)
|
)
|
||||||
return llm_result
|
return llm_result
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -207,8 +214,11 @@ class IntentProvider(IntentProviderBase):
|
|||||||
logger.bind(tag=TAG).debug(f"开始LLM意图识别调用, 模型: {model_info}")
|
logger.bind(tag=TAG).debug(f"开始LLM意图识别调用, 模型: {model_info}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
intent = self.llm.response_no_stream(
|
# 使用 to_thread 将同步阻塞调用放到线程池中,避免阻塞事件循环
|
||||||
system_prompt=prompt_music, user_prompt=user_prompt
|
intent = await asyncio.to_thread(
|
||||||
|
self.llm.response_no_stream,
|
||||||
|
system_prompt=prompt_music,
|
||||||
|
user_prompt=user_prompt,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"Error in intent detection LLM call: {e}")
|
logger.bind(tag=TAG).error(f"Error in intent detection LLM call: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user