diff --git a/main/xiaozhi-server/core/handle/intentHandler.py b/main/xiaozhi-server/core/handle/intentHandler.py index cae22b2ce..2dde62558 100644 --- a/main/xiaozhi-server/core/handle/intentHandler.py +++ b/main/xiaozhi-server/core/handle/intentHandler.py @@ -122,12 +122,10 @@ async def analyze_intent_with_llm(conn, text): logger.bind(tag=TAG).warning("意图识别服务未初始化") return None - # 创建对话历史记录 + # 对话历史记录 dialogue = conn.dialogue - dialogue.put(Message(role="user", content=text)) - try: - intent_result = await conn.intent.detect_intent(dialogue.dialogue) + intent_result = await conn.intent.detect_intent(dialogue.dialogue, text) logger.bind(tag=TAG).info(f"意图识别结果: {intent_result}") # 尝试解析JSON结果 diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index 97624898c..bf6d4c36c 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -67,8 +67,9 @@ async def no_voice_close_connect(conn): else: no_voice_time = time.time() * 1000 - conn.client_no_voice_last_time close_connection_no_voice_time = conn.config.get("close_connection_no_voice_time", 120) - if no_voice_time > 1000 * close_connection_no_voice_time: + if not conn.close_after_chat and no_voice_time > 1000 * close_connection_no_voice_time: + conn.close_after_chat = True conn.client_abort = False conn.asr_server_receive = False - prompt = "时间过得真快,我都好久没说话了。请你用十个字左右话跟我告别,以“再见”或“拜拜”为结尾" + prompt = "请你以“时间过得真快”未来头,用富有感情、依依不舍的话来结束这场对话吧。" await startToChat(conn, prompt) diff --git a/main/xiaozhi-server/core/providers/intent/base.py b/main/xiaozhi-server/core/providers/intent/base.py index 1333f620b..9726f1be3 100644 --- a/main/xiaozhi-server/core/providers/intent/base.py +++ b/main/xiaozhi-server/core/providers/intent/base.py @@ -5,6 +5,7 @@ from config.logger import setup_logging TAG = __name__ logger = setup_logging() + class IntentProviderBase(ABC): def __init__(self, config): self.config = config @@ -17,9 +18,9 @@ class IntentProviderBase(ABC): def set_llm(self, llm): self.llm = llm logger.bind(tag=TAG).debug("Set LLM for intent provider") - + @abstractmethod - async def detect_intent(self, dialogue_history: List[Dict]) -> str: + async def detect_intent(self, dialogue_history: List[Dict], text: str) -> str: """ 检测用户最后一句话的意图 Args: diff --git a/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py b/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py index c3930296a..191f73dfd 100644 --- a/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py +++ b/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py @@ -37,7 +37,7 @@ class IntentProvider(IntentProviderBase): ) return prompt - async def detect_intent(self, dialogue_history: List[Dict]) -> str: + async def detect_intent(self, dialogue_history: List[Dict], text:str) -> str: if not self.llm: raise ValueError("LLM provider not set") @@ -48,9 +48,8 @@ class IntentProvider(IntentProviderBase): msgStr += f"User: {msg.content}\n" elif msg.role== "assistant": msgStr += f"Assistant: {msg.content}\n" - + msgStr += f"User: {text}\n" user_prompt = f"请分析用户的意图:\n{msgStr}" - # 使用LLM进行意图识别 intent = self.llm.response_no_stream( system_prompt=self.promot, diff --git a/main/xiaozhi-server/core/providers/intent/nointent/nointent.py b/main/xiaozhi-server/core/providers/intent/nointent/nointent.py index 9feadaad2..70ea96fb9 100644 --- a/main/xiaozhi-server/core/providers/intent/nointent/nointent.py +++ b/main/xiaozhi-server/core/providers/intent/nointent/nointent.py @@ -5,12 +5,14 @@ from config.logger import setup_logging TAG = __name__ logger = setup_logging() + class IntentProvider(IntentProviderBase): - async def detect_intent(self, dialogue_history: List[Dict]) -> str: + async def detect_intent(self, dialogue_history: List[Dict], text: str) -> str: """ 默认的意图识别实现,始终返回继续聊天 Args: dialogue_history: 对话历史记录列表 + text: 本次对话记录 Returns: 固定返回"继续聊天" """