diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index e1e6cd8de..2ff7fe8bc 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -73,8 +73,9 @@ selected_module: # TTS将根据配置名称对应的type调用实际的TTS适配器 TTS: EdgeTTS # 记忆模块,默认使用本地压缩记忆,如果想使用超长记忆,推荐使用mem0ai - Memory: mem_local_short + Memory: nomem # 意图识别模块,默认不开启。开启后,可以播放音乐、控制音量、识别退出指令 + # 如果意图识别设置成 function_call,建议使用:DoubaoLLM,推荐把的model_name设置为:doubao-pro-32k-functioncall-241028 Intent: nointent # 意图识别,是用于理解用户意图的模块,例如:播放音乐 @@ -86,7 +87,6 @@ Intent: intent_llm: type: intent_llm # 意图识别使用function_call,缺点:需要所选择的LLM支持function_call,优点:按需调用工具、速度快 - # 新手使用function_call,建议使用:AliLLM,并把的model_name设置为:qwen-max function_call: type: function_call @@ -129,6 +129,13 @@ LLM: base_url: https://dashscope.aliyuncs.com/compatible-mode/v1 model_name: qwen-turbo api_key: 你的deepseek web key + DoubaoLLM: + # 定义LLM API类型 + type: openai + # 可在这里开通 https://console.volcengine.com/ark/region:ark+cn-beijing/model/detail?Id=doubao-pro-32k&projectName=undefined + base_url: https://ark.cn-beijing.volces.com/api/v3 + model_name: doubao-pro-32k-functioncall-241028 + api_key: 你的doubao web key DeepSeekLLM: # 定义LLM API类型 type: openai diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 114c553bb..570c31055 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -90,14 +90,13 @@ class ConnectionHandler: self.auth_code_gen = AuthCodeGenerator.get_instance() self.is_device_verified = False # 添加设备验证状态标志 self.music_handler = _music - self.close_after_chat = False # 是否在聊天结束后关闭连接 + self.close_after_chat = False # 是否在聊天结束后关闭连接 self.use_function_call_mode = False if self.config["selected_module"]["Intent"] == 'function_call': self.use_function_call_mode = True self.logger.bind(tag=TAG).info(f"use_function_call_mode:{self.use_function_call_mode}") - async def handle_connection(self, ws): try: # 获取并验证headers @@ -214,7 +213,6 @@ class ConnectionHandler: return False return not self.is_device_verified - def chat(self, query): if self.isNeedAuth(): self.llm_finish_task = True @@ -231,10 +229,10 @@ class ConnectionHandler: # 使用带记忆的对话 future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop) memory_str = future.result() - + self.logger.bind(tag=TAG).debug(f"记忆内容: {memory_str}") llm_responses = self.llm.response( - self.session_id, + self.session_id, self.dialogue.get_llm_dialogue_with_memory(memory_str) ) except Exception as e: @@ -256,7 +254,7 @@ class ConnectionHandler: current_text = full_text[processed_chars:] # 从未处理的位置开始 # 查找最后一个有效标点 - punctuations = ("。", "?", "!", "?", "!", ";", ";", ":", ":") + punctuations = ("。", "?", "!", ";", ":") last_punct_pos = -1 for punct in punctuations: pos = current_text.rfind(punct) @@ -303,26 +301,26 @@ class ConnectionHandler: return True self.dialogue.put(Message(role="user", content=query)) - + # Define intent functions functions = get_functions() - + response_message = [] processed_chars = 0 # 跟踪已处理的字符位置 function_call_data = None # 存储function call数据 - + try: start_time = time.time() - + # 使用带记忆的对话 future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop) memory_str = future.result() - - #self.logger.bind(tag=TAG).info(f"记忆内容: {memory_str}") - + + # self.logger.bind(tag=TAG).info(f"记忆内容: {memory_str}") + # 使用支持functions的streaming接口 llm_responses = self.llm.response_with_functions( - self.session_id, + self.session_id, self.dialogue.get_llm_dialogue_with_memory(memory_str), functions=functions ) @@ -332,13 +330,13 @@ class ConnectionHandler: self.llm_finish_task = False text_index = 0 - + # 处理流式响应 for response in llm_responses: if response["type"] == "content": content = response["content"] response_message.append(content) - + if self.client_abort: break @@ -351,7 +349,7 @@ class ConnectionHandler: current_text = full_text[processed_chars:] # 从未处理的位置开始 # 查找最后一个有效标点 - punctuations = ("。", "?", "!", "?", "!", ";", ";", ":", ":") + punctuations = ("。", "?", "!", ";", ":") last_punct_pos = -1 for punct in punctuations: pos = current_text.rfind(punct) @@ -368,7 +366,7 @@ class ConnectionHandler: future = self.executor.submit(self.speak_and_play, segment_text, text_index) self.tts_queue.put(future) processed_chars += len(segment_text_raw) # 更新已处理字符位置 - + elif response["type"] == "function_call": # Extract function call data function_call_data = { @@ -376,7 +374,7 @@ class ConnectionHandler: "arguments": response["function_call"]["function"]["arguments"] } self.logger.bind(tag=TAG).info(f"Function call detected: {function_call_data}") - + # 处理最后剩余的文本 full_text = "".join(response_message) remaining_text = full_text[processed_chars:] @@ -390,7 +388,7 @@ class ConnectionHandler: # 存储对话内容 self.dialogue.put(Message(role="assistant", content="".join(response_message))) - + # 处理function call if function_call_data: result = handle_llm_function_call(self, function_call_data) @@ -400,10 +398,10 @@ class ConnectionHandler: self.recode_first_last_text(text, text_index) future = self.executor.submit(self.speak_and_play, text, text_index) self.tts_queue.put(future) - + self.llm_finish_task = True self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False)) - + return True def _tts_priority_thread(self): @@ -502,7 +500,7 @@ class ConnectionHandler: try: # Use the existing chat method self.chat(text) - + # After chat is complete, close the connection self.close_after_chat = True except Exception as e: diff --git a/main/xiaozhi-server/core/providers/llm/openai/openai.py b/main/xiaozhi-server/core/providers/llm/openai/openai.py index 79f0aacc7..b73f0bedf 100644 --- a/main/xiaozhi-server/core/providers/llm/openai/openai.py +++ b/main/xiaozhi-server/core/providers/llm/openai/openai.py @@ -79,7 +79,7 @@ class LLMProvider(LLMProviderBase): } if current_function_call: - logger.bind(tag=TAG).debug(f"ollama Function call detected: {current_function_call}") + logger.bind(tag=TAG).debug(f"openai Function call detected: {current_function_call}") yield {"type": "function_call", "function_call": current_function_call} except Exception as e: