From ee65032f7d6a05090a4b1e7f48393d89d10223d1 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 2 Jun 2025 17:03:53 +0800 Subject: [PATCH 01/12] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E7=81=AB?= =?UTF-8?q?=E5=B1=B1=E5=BC=95=E6=93=8E=E5=8F=8C=E6=B5=81TTS=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/handle/receiveAudioHandle.py | 2 - .../xiaozhi-server/core/providers/tts/base.py | 3 + .../providers/tts/huoshan_double_stream.py | 537 +++++++----------- 3 files changed, 203 insertions(+), 339 deletions(-) diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index b829afb52..a5947e5a1 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -11,10 +11,8 @@ TAG = __name__ async def handleAudioMessage(conn, audio): if conn.vad is None: - conn.logger.bind(tag=TAG).warning("VAD模块未初始化,继续等待") return if conn.asr is None or not hasattr(conn.asr, "conn") or conn.asr.conn is None: - conn.logger.bind(tag=TAG).warning("ASR模块未初始化或通道未就绪,继续等待") return # 当前片段是否有人说话 have_voice = conn.vad.is_vad(conn, audio) diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index 36b6ecbfc..9c094f24d 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -178,6 +178,9 @@ class TTSProviderBase(ABC): while not self.conn.stop_event.is_set(): try: message = self.tts_text_queue.get(timeout=1) + if self.conn.client_abort: + logger.bind(tag=TAG).info("收到打断信息,终止TTS文本处理线程") + continue if message.sentence_type == SentenceType.FIRST: # 初始化参数 self.tts_stop_request = False diff --git a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py index de50c18c3..4d15204ec 100644 --- a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py +++ b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py @@ -3,14 +3,13 @@ import uuid import json import queue import asyncio -import threading import traceback import websockets -import time from config.logger import setup_logging from core.utils import opus_encoder_utils from core.utils.util import check_model_key from core.providers.tts.base import TTSProviderBase +from core.handle.abortHandle import handleAbortMessage from core.providers.tts.dto.dto import SentenceType, ContentType, InterfaceType TAG = __name__ @@ -139,7 +138,7 @@ class Response: class TTSProvider(TTSProviderBase): def __init__(self, config, delete_audio_file): super().__init__(config, delete_audio_file) - self.ws = None # 初始化ws属性 + self.ws = None self.interface_type = InterfaceType.DUAL_STREAM self.appId = config.get("appid") self.access_token = config.get("access_token") @@ -154,83 +153,43 @@ class TTSProvider(TTSProviderBase): self.authorization = config.get("authorization") self.header = {"Authorization": f"{self.authorization}{self.access_token}"} self.enable_two_way = True - self.start_connection_flag = False self.tts_text = "" - # 合成文字语音后,播放的音频文件列表 self.before_stop_play_files = [] self.opus_encoder = opus_encoder_utils.OpusEncoderUtils( sample_rate=16000, channels=1, frame_size_ms=60 ) check_model_key("TTS", self.access_token) - # 添加会话状态控制 - self._session_lock = asyncio.Lock() # 会话操作的并发锁 - self._current_session_id = None # 当前会话ID - self._session_started = False # 会话是否已开始 - self._session_finished = False # 会话是否已结束 - self._connection_ready = False # 连接是否就绪 - self._reconnect_attempts = 0 # 重连尝试次数 - self._max_reconnect_attempts = 3 # 最大重连次数 - - ################################################################################### - # 火山双流式TTS重写父类的方法--开始 - ################################################################################### - async def open_audio_channels(self, conn): try: await super().open_audio_channels(conn) - await self._ensure_connection() - tts_priority = threading.Thread( - target=self._start_monitor_tts_response_thread, daemon=True - ) - tts_priority.start() except Exception as e: logger.bind(tag=TAG).error(f"Failed to open audio channels: {str(e)}") self.ws = None raise async def _ensure_connection(self): - """确保WebSocket连接可用""" + """建立新的WebSocket连接""" try: - if self.ws is None: - logger.bind(tag=TAG).info("WebSocket连接不存在,开始建立新连接...") - ws_header = { - "X-Api-App-Key": self.appId, - "X-Api-Access-Key": self.access_token, - "X-Api-Resource-Id": self.resource_id, - "X-Api-Connect-Id": uuid.uuid4(), - } - self.ws = await websockets.connect( - self.ws_url, additional_headers=ws_header, max_size=1000000000 - ) - self._connection_ready = True - self._reconnect_attempts = 0 - logger.bind(tag=TAG).info("WebSocket连接建立成功") - else: - # 尝试发送ping来检查连接是否还活着 - try: - logger.bind(tag=TAG).debug("检查WebSocket连接状态...") - pong_waiter = await self.ws.ping() - await asyncio.wait_for(pong_waiter, timeout=1.0) - logger.bind(tag=TAG).debug("WebSocket连接状态正常") - except (asyncio.TimeoutError, websockets.ConnectionClosed): - # 如果ping失败,重新建立连接 - logger.bind(tag=TAG).warning("WebSocket连接已断开,准备重新连接...") - try: - await self.ws.close() - except: - pass - self.ws = None - self._connection_ready = False - # 重新建立连接 - await self._ensure_connection() + logger.bind(tag=TAG).info("开始建立新连接...") + ws_header = { + "X-Api-App-Key": self.appId, + "X-Api-Access-Key": self.access_token, + "X-Api-Resource-Id": self.resource_id, + "X-Api-Connect-Id": uuid.uuid4(), + } + self.ws = await websockets.connect( + self.ws_url, additional_headers=ws_header, max_size=1000000000 + ) + logger.bind(tag=TAG).info("WebSocket连接建立成功") + return self.ws except Exception as e: - logger.bind(tag=TAG).error(f"确保连接失败: {str(e)}") - self._connection_ready = False + logger.bind(tag=TAG).error(f"建立连接失败: {str(e)}") self.ws = None raise def tts_text_priority_thread(self): + """火山引擎双流式TTS的文本处理线程""" logger.bind(tag=TAG).info("TTS文本处理线程启动") while not self.conn.stop_event.is_set(): try: @@ -239,6 +198,10 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).info( f"收到TTS任务|{message.sentence_type.name} | {message.content_type.name} | 会话ID: {self.conn.sentence_id}" ) + if self.conn.client_abort: + logger.bind(tag=TAG).info("收到打断信息,终止TTS文本处理线程") + continue + if message.sentence_type == SentenceType.FIRST: # 初始化参数 try: @@ -253,9 +216,8 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).info("TTS会话启动成功") except Exception as e: logger.bind(tag=TAG).error(f"启动TTS会话失败: {str(e)}") - # 直接跳过当前消息,不重新入队 - time.sleep(1) continue + elif ContentType.TEXT == message.content_type: if message.content_detail: try: @@ -270,9 +232,8 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).info("TTS文本发送成功") except Exception as e: logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}") - # 直接跳过当前消息,不重新入队 - time.sleep(1) continue + elif ContentType.FILE == message.content_type: logger.bind(tag=TAG).info( f"添加音频文件到待播放列表: {message.content_file}" @@ -292,8 +253,6 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).info("TTS会话结束成功") except Exception as e: logger.bind(tag=TAG).error(f"结束TTS会话失败: {str(e)}") - # 直接跳过当前消息,不重新入队 - time.sleep(1) continue except queue.Empty: @@ -302,103 +261,204 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).error( f"处理TTS文本失败: {str(e)}, 类型: {type(e).__name__}, 堆栈: {traceback.format_exc()}" ) - # 如果是WebSocket连接关闭错误,等待一段时间后继续 - if "non-exist session" in str(e): - time.sleep(1) continue async def text_to_speak(self, text, _): """发送文本到TTS服务""" try: - # 确保WebSocket连接可用 - if not self._connection_ready or self.ws is None: - logger.bind(tag=TAG).warning("WebSocket连接不可用,尝试重新连接...") - await self._ensure_connection() - + # 建立新连接 + if self.ws is None: + await handleAbortMessage(self.conn) + logger.bind(tag=TAG).error(f"WebSocket连接不存在,终止发送文本") + return # 发送文本 await self.send_text(self.speaker, text, self.conn.sentence_id) return except Exception as e: logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}") - # 如果是连接问题,尝试重新连接 - if isinstance(e, websockets.ConnectionClosed): - self._connection_ready = False + if self.ws: + try: + await self.ws.close() + except: + pass self.ws = None - await self._handle_connection_error() raise - ################################################################################### - # 火山双流式TTS重写父类的方法--结束 - ################################################################################### - def _start_monitor_tts_response_thread(self): - # 初始化链接 - asyncio.run_coroutine_threadsafe( - self._start_monitor_tts_response(), loop=self.conn.loop - ) + async def start_session(self, session_id): + logger.bind(tag=TAG).info(f"开始会话~~{session_id}") + try: + # 建立新连接 + await self._ensure_connection() + + # 启动监听任务 + self._monitor_task = asyncio.create_task(self._start_monitor_tts_response()) + + header = Header( + message_type=FULL_CLIENT_REQUEST, + message_type_specific_flags=MsgTypeFlagWithEvent, + serial_method=JSON, + ).as_bytes() + optional = Optional( + event=EVENT_StartSession, sessionId=session_id + ).as_bytes() + payload = self.get_payload_bytes( + event=EVENT_StartSession, speaker=self.speaker + ) + await self.send_event(header, optional, payload) + logger.bind(tag=TAG).info("会话启动请求已发送") + except Exception as e: + logger.bind(tag=TAG).error(f"启动会话失败: {str(e)}") + # 确保清理资源 + if hasattr(self, "_monitor_task"): + try: + self._monitor_task.cancel() + await self._monitor_task + except: + pass + self._monitor_task = None + if self.ws: + try: + await self.ws.close() + except: + pass + self.ws = None + raise + + async def finish_session(self, session_id): + logger.bind(tag=TAG).info(f"关闭会话~~{session_id}") + try: + if self.ws: + header = Header( + message_type=FULL_CLIENT_REQUEST, + message_type_specific_flags=MsgTypeFlagWithEvent, + serial_method=JSON, + ).as_bytes() + optional = Optional( + event=EVENT_FinishSession, sessionId=session_id + ).as_bytes() + payload = str.encode("{}") + await self.send_event(header, optional, payload) + logger.bind(tag=TAG).info("会话结束请求已发送") + + # 等待监听任务完成 + if hasattr(self, "_monitor_task"): + try: + await self._monitor_task + except Exception as e: + logger.bind(tag=TAG).error( + f"等待监听任务完成时发生错误: {str(e)}" + ) + finally: + self._monitor_task = None + + # 关闭连接 + await self.close() + except Exception as e: + logger.bind(tag=TAG).error(f"关闭会话失败: {str(e)}") + # 确保清理资源 + if hasattr(self, "_monitor_task"): + try: + self._monitor_task.cancel() + await self._monitor_task + except: + pass + self._monitor_task = None + if self.ws: + try: + await self.ws.close() + except: + pass + self.ws = None + raise + + async def close(self): + """资源清理方法""" + if self.ws: + try: + await self.ws.close() + except: + pass + self.ws = None async def _start_monitor_tts_response(self): + """监听TTS响应""" opus_datas_cache = [] - # 添加标志来区分是否是第一句话 is_first_sentence = True - while not self.conn.stop_event.is_set(): - try: - # 确保 `recv()` 运行在同一个 event loop - msg = await self.ws.recv() - res = self.parser_response(msg) - self.print_response(res, "send_text res:") + try: + while not self.conn.stop_event.is_set(): + try: + # 确保 `recv()` 运行在同一个 event loop + msg = await self.ws.recv() + res = self.parser_response(msg) + self.print_response(res, "send_text res:") - if res.optional.event == EVENT_TTSSentenceStart: - json_data = json.loads(res.payload.decode("utf-8")) - self.tts_text = json_data.get("text", "") - logger.bind(tag=TAG).debug(f"句子语音生成开始: {self.tts_text}") - self.tts_audio_queue.put((SentenceType.FIRST, [], self.tts_text)) - opus_datas_cache = [] - elif ( - res.optional.event == EVENT_TTSResponse - and res.header.message_type == AUDIO_ONLY_RESPONSE - ): - logger.bind(tag=TAG).debug(f"推送数据到队列里面~~") - opus_datas = self.wav_to_opus_data_audio_raw(res.payload) - logger.bind(tag=TAG).debug( - f"推送数据到队列里面帧数~~{len(opus_datas)}" - ) - if is_first_sentence: - # 第一句话直接发送 + # 检查客户端是否中止 + if self.conn.client_abort: + logger.bind(tag=TAG).info("收到打断信息,终止监听TTS响应") + break + + if res.optional.event == EVENT_TTSSentenceStart: + json_data = json.loads(res.payload.decode("utf-8")) + self.tts_text = json_data.get("text", "") + logger.bind(tag=TAG).debug(f"句子语音生成开始: {self.tts_text}") self.tts_audio_queue.put( - (SentenceType.MIDDLE, opus_datas, self.tts_text) + (SentenceType.FIRST, [], self.tts_text) ) - else: - # 后续句子缓存 - opus_datas_cache = opus_datas_cache + opus_datas - elif res.optional.event == EVENT_TTSSentenceEnd: - logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}") - if not is_first_sentence: - # 只有非第一句话才发送缓存的数据 - self.tts_audio_queue.put( - (SentenceType.MIDDLE, opus_datas_cache, self.tts_text) + opus_datas_cache = [] + elif ( + res.optional.event == EVENT_TTSResponse + and res.header.message_type == AUDIO_ONLY_RESPONSE + ): + logger.bind(tag=TAG).debug(f"推送数据到队列里面~~") + opus_datas = self.wav_to_opus_data_audio_raw(res.payload) + logger.bind(tag=TAG).debug( + f"推送数据到队列里面帧数~~{len(opus_datas)}" ) - # 第一句话结束后,将标志设置为False - is_first_sentence = False - elif res.optional.event == EVENT_SessionFinished: - logger.bind(tag=TAG).debug(f"会话结束~~") - for tts_file, text in self.before_stop_play_files: - if tts_file and os.path.exists(tts_file): - audio_datas = self._process_audio_file(tts_file) + if is_first_sentence: + # 第一句话直接发送 self.tts_audio_queue.put( - (SentenceType.MIDDLE, audio_datas, text) + (SentenceType.MIDDLE, opus_datas, self.tts_text) ) - self.before_stop_play_files.clear() - self.tts_audio_queue.put((SentenceType.LAST, [], None)) - - opus_datas_cache = [] - is_first_sentence = True - continue - except websockets.ConnectionClosed: - break # 连接关闭时退出监听 - except Exception as e: - logger.bind(tag=TAG).error(f"Error in _start_monitor_tts_response: {e}") - traceback.print_exc() - continue + else: + # 后续句子缓存 + opus_datas_cache = opus_datas_cache + opus_datas + elif res.optional.event == EVENT_TTSSentenceEnd: + logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}") + if not is_first_sentence: + # 只有非第一句话才发送缓存的数据 + self.tts_audio_queue.put( + (SentenceType.MIDDLE, opus_datas_cache, self.tts_text) + ) + # 第一句话结束后,将标志设置为False + is_first_sentence = False + elif res.optional.event == EVENT_SessionFinished: + logger.bind(tag=TAG).debug(f"会话结束~~") + for tts_file, text in self.before_stop_play_files: + if tts_file and os.path.exists(tts_file): + audio_datas = self._process_audio_file(tts_file) + self.tts_audio_queue.put( + (SentenceType.MIDDLE, audio_datas, text) + ) + self.before_stop_play_files.clear() + self.tts_audio_queue.put((SentenceType.LAST, [], None)) + break + except websockets.ConnectionClosed: + logger.bind(tag=TAG).warning("WebSocket连接已关闭") + break + except Exception as e: + logger.bind(tag=TAG).error( + f"Error in _start_monitor_tts_response: {e}" + ) + traceback.print_exc() + break + finally: + # 确保清理资源 + if self.ws: + try: + await self.ws.close() + except: + pass + self.ws = None async def send_event( self, header: bytes, optional: bytes | None = None, payload: bytes = None @@ -413,11 +473,8 @@ class TTSProvider(TTSProviderBase): full_client_request.extend(payload) await self.ws.send(full_client_request) except websockets.ConnectionClosed: - if await self._handle_connection_error(): - # 重连成功后重试发送 - await self.ws.send(full_client_request) - else: - raise + logger.bind(tag=TAG).error(f"ConnectionClosed") + raise async def send_text(self, speaker: str, text: str, session_id): header = Header( @@ -540,200 +597,6 @@ class TTSProvider(TTSProviderBase): ) ) - async def finish_connection(self): - header = Header( - message_type=FULL_CLIENT_REQUEST, - message_type_specific_flags=MsgTypeFlagWithEvent, - serial_method=JSON, - ).as_bytes() - optional = Optional(event=EVENT_FinishConnection).as_bytes() - payload = str.encode("{}") - await self.send_event(header, optional, payload) - return - - async def start_session(self, session_id): - logger.bind(tag=TAG).info(f"开始会话~~{session_id}") - try: - async with self._session_lock: - try: - # 确保连接可用 - logger.bind(tag=TAG).info("检查WebSocket连接状态...") - await asyncio.wait_for(self._ensure_connection(), timeout=5) - - # 如果已有会话未结束,先关闭它 - if self._session_started and not self._session_finished: - logger.bind(tag=TAG).warning( - f"发现未关闭的会话 {self._current_session_id},正在关闭..." - ) - try: - await asyncio.wait_for( - self.finish_session(self._current_session_id), timeout=5 - ) - except Exception as e: - logger.bind(tag=TAG).error(f"关闭旧会话失败: {str(e)}") - # 强制重置会话状态 - self._session_started = False - self._session_finished = True - self._current_session_id = None - - # 重置会话状态 - self._current_session_id = session_id - self._session_started = True - self._session_finished = False - logger.bind(tag=TAG).info( - f"会话状态已更新 - 开始: {self._session_started}, 结束: {self._session_finished}" - ) - - header = Header( - message_type=FULL_CLIENT_REQUEST, - message_type_specific_flags=MsgTypeFlagWithEvent, - serial_method=JSON, - ).as_bytes() - optional = Optional( - event=EVENT_StartSession, sessionId=session_id - ).as_bytes() - payload = self.get_payload_bytes( - event=EVENT_StartSession, speaker=self.speaker - ) - await asyncio.wait_for( - self.send_event(header, optional, payload), timeout=5 - ) - logger.bind(tag=TAG).info("会话启动请求已发送") - except Exception as e: - logger.bind(tag=TAG).error(f"启动会话失败: {str(e)}") - self._session_started = False - self._session_finished = True - self._current_session_id = None - raise - except asyncio.TimeoutError: - logger.bind(tag=TAG).error(f"启动会话超时: {session_id}") - # 超时后强制重置会话状态 - self._session_started = False - self._session_finished = True - self._current_session_id = None - # 尝试关闭WebSocket连接 - if self.ws: - try: - await self.ws.close() - except: - pass - self.ws = None - except Exception as e: - logger.bind(tag=TAG).error(f"启动会话时发生未知错误: {str(e)}") - # 发生未知错误时也重置会话状态 - self._session_started = False - self._session_finished = True - self._current_session_id = None - - async def finish_session(self, session_id): - logger.bind(tag=TAG).info(f"关闭会话~~{session_id}") - try: - async with self._session_lock: - try: - # 检查会话状态 - if not self._session_started: - logger.bind(tag=TAG).warning( - f"尝试关闭未开始的会话 {session_id}" - ) - return - - if self._session_finished: - logger.bind(tag=TAG).warning(f"会话 {session_id} 已经关闭") - return - - if self._current_session_id != session_id: - logger.bind(tag=TAG).warning( - f"尝试关闭错误的会话 {session_id},当前会话为 {self._current_session_id}" - ) - # 即使会话ID不匹配,也尝试关闭当前会话 - if self._current_session_id: - session_id = self._current_session_id - - # 确保WebSocket连接可用 - if self.ws is None: - logger.bind(tag=TAG).warning( - "WebSocket连接不存在,尝试重新连接..." - ) - await asyncio.wait_for(self._ensure_connection(), timeout=5) - - header = Header( - message_type=FULL_CLIENT_REQUEST, - message_type_specific_flags=MsgTypeFlagWithEvent, - serial_method=JSON, - ).as_bytes() - optional = Optional( - event=EVENT_FinishSession, sessionId=session_id - ).as_bytes() - payload = str.encode("{}") - await asyncio.wait_for( - self.send_event(header, optional, payload), timeout=5 - ) - logger.bind(tag=TAG).info("会话结束请求已发送") - - # 更新会话状态 - self._session_finished = True - self._session_started = False - self._current_session_id = None - logger.bind(tag=TAG).info( - "会话状态已更新 - 开始: False, 结束: True" - ) - except Exception as e: - logger.bind(tag=TAG).error(f"关闭会话失败: {str(e)}") - # 即使发生错误,也要重置会话状态 - self._session_finished = True - self._session_started = False - self._current_session_id = None - raise - except asyncio.TimeoutError: - logger.bind(tag=TAG).error(f"关闭会话超时: {session_id}") - # 超时后强制重置会话状态 - self._session_finished = True - self._session_started = False - self._current_session_id = None - # 尝试关闭WebSocket连接 - if self.ws: - try: - await self.ws.close() - except: - pass - self.ws = None - except Exception as e: - logger.bind(tag=TAG).error(f"关闭会话时发生未知错误: {str(e)}") - # 发生未知错误时也重置会话状态 - self._session_finished = True - self._session_started = False - self._current_session_id = None - - async def reset(self): - # 关闭之前的对话 - if self.start_connection_flag: - await self.finish_connection() - self.start_connection_flag = False - await self.start_connection() - self.start_connection_flag = True - - async def close(self): - """资源清理方法""" - await self.finish_connection() - await self.ws.close() - def wav_to_opus_data_audio_raw(self, raw_data_var, is_end=False): opus_datas = self.opus_encoder.encode_pcm_to_opus(raw_data_var, is_end) return opus_datas - - async def _handle_connection_error(self): - """处理连接错误""" - if self._reconnect_attempts < self._max_reconnect_attempts: - self._reconnect_attempts += 1 - logger.bind(tag=TAG).warning( - f"尝试重新连接 (第{self._reconnect_attempts}次)" - ) - try: - await self._ensure_connection() - return True - except Exception as e: - logger.bind(tag=TAG).error(f"重新连接失败: {str(e)}") - return False - else: - logger.bind(tag=TAG).error("达到最大重连次数,放弃重连") - return False From fd4193daab533a2c69da56b69398c8019169cd03 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 2 Jun 2025 21:06:51 +0800 Subject: [PATCH 02/12] fix:local variable 'response' referenced before assignment --- main/xiaozhi-server/core/api/vision_handler.py | 11 +++++++---- main/xiaozhi-server/core/handle/helloHandle.py | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/main/xiaozhi-server/core/api/vision_handler.py b/main/xiaozhi-server/core/api/vision_handler.py index e0f7096a9..168440e4d 100644 --- a/main/xiaozhi-server/core/api/vision_handler.py +++ b/main/xiaozhi-server/core/api/vision_handler.py @@ -37,17 +37,19 @@ class VisionHandler: async def handle_post(self, request): """处理 MCP Vision POST 请求""" + response = None # 初始化response变量 try: # 验证token is_valid, token_device_id = self._verify_auth_token(request) if not is_valid: - return web.Response( + response = web.Response( text=json.dumps( self._create_error_response("无效的认证token或token已过期") ), content_type="application/json", status=401, ) + return response # 获取请求头信息 device_id = request.headers.get("Device-Id", "") @@ -120,11 +122,11 @@ class VisionHandler: vllm_type, current_config["VLLM"][select_vllm_module] ) - response = vllm.response(question, image_base64) + result = vllm.response(question, image_base64) return_json = { "success": True, - "result": response, + "result": result, } response = web.Response( @@ -146,7 +148,8 @@ class VisionHandler: content_type="application/json", ) finally: - self._add_cors_headers(response) + if response: + self._add_cors_headers(response) return response async def handle_get(self, request): diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index adf835ca3..6b8cb4fd4 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -74,6 +74,8 @@ async def checkWakeupWords(conn, text): text_hello = WAKEUP_CONFIG["text"] if not text_hello: text_hello = text + if conn.tts is None: + return False conn.tts.tts_one_sentence( conn, ContentType.FILE, content_file=file, content_detail=text_hello ) From 6bf6159e6cbfb3484de3b6c0a851d532fb7d7c0f Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 2 Jun 2025 21:15:07 +0800 Subject: [PATCH 03/12] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0hamcp=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/homeassistant-integration.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/homeassistant-integration.md b/docs/homeassistant-integration.md index 3142244ef..474049c1e 100644 --- a/docs/homeassistant-integration.md +++ b/docs/homeassistant-integration.md @@ -202,7 +202,15 @@ change_role;get_weather;get_news;play_music;hass_get_state;hass_set_state #### 2. 配置小智开源服务端MCP配置信息 -切换到小智开源服务端`xiaozhi-esp32-server`的`mcp_server_settings.json`文件内,在`"mcpServers"`的括号内添加以下内容: + +进入`data`目录,找到`.mcp_server_settings.json`文件。 + +如果你的`data`目录下没有`.mcp_server_settings.json`文件, +- 请把在`xiaozhi-server`文件夹根目录的`mcp_server_settings.json`文件复制到`data`目录下,并重命名为`.mcp_server_settings.json` +- 或[下载这个文件](https://github.com/xinnan-tech/xiaozhi-esp32-server/blob/main/main/xiaozhi-server/mcp_server_settings.json),下载到`data`目录下,并重命名为`.mcp_server_settings.json` + + +修改`"mcpServers"`里的这部分的内容: ```json "Home Assistant": { From c0d4bbcecffda6a23cc14270521d86a9693c3a24 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 2 Jun 2025 21:24:26 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E5=B8=83?= =?UTF-8?q?=E5=BC=8F=E9=83=A8=E7=BD=B2=E6=97=B6jwt=E5=AF=86=E9=92=A5?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/app.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py index 8c8a728bd..2088e1838 100644 --- a/main/xiaozhi-server/app.py +++ b/main/xiaozhi-server/app.py @@ -46,8 +46,13 @@ async def main(): check_ffmpeg_installed() config = load_config() - # 生成随机密钥并添加到配置中 - config["server"]["auth_key"] = str(uuid.uuid4().hex) + # 默认使用manager-api的secret作为auth_key + # 如果secret为空,则生成随机密钥 + # auth_key用于jwt认证,比如视觉分析接口的jwt认证 + auth_key = config.get("manager-api", {}).get("secret", "") + if not auth_key or len(auth_key) == 0 or "你" in auth_key: + auth_key = str(uuid.uuid4().hex) + config["server"]["auth_key"] = auth_key # 添加 stdin 监控任务 stdin_task = asyncio.create_task(monitor_stdin()) From a6bc910b0f9daef50e3b752fee7e7a150f82e647 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 3 Jun 2025 10:27:13 +0800 Subject: [PATCH 05/12] =?UTF-8?q?udpate:=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +- README_en.md | 180 +++++++++++++++--------------------- docs/dev-ops-integration.md | 163 ++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+), 104 deletions(-) create mode 100644 docs/dev-ops-integration.md diff --git a/README.md b/README.md index 2f99f957f..43a8acd44 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ | 部署方式 | 特点 | 适用场景 | 部署文档 | 配置要求 | 视频教程 | |---------|------|---------|---------|---------|---------| | **最简化安装** | 智能对话、IOT功能,数据存储在配置文件 | 低配置环境,无需数据库 | [Docker版](./docs/Deployment.md#%E6%96%B9%E5%BC%8F%E4%B8%80docker%E5%8F%AA%E8%BF%90%E8%A1%8Cserver) / [源码部署](./docs/Deployment.md#%E6%96%B9%E5%BC%8F%E4%BA%8C%E6%9C%AC%E5%9C%B0%E6%BA%90%E7%A0%81%E5%8F%AA%E8%BF%90%E8%A1%8Cserver)| 如果使用`FunASR`要2核4G,如果全API,要2核2G | - | -| **全模块安装** | 智能对话、IOT、OTA、智控台,数据存储在数据库 | 完整功能体验 |[Docker版](./docs/Deployment_all.md#%E6%96%B9%E5%BC%8F%E4%B8%80docker%E8%BF%90%E8%A1%8C%E5%85%A8%E6%A8%A1%E5%9D%97) / [源码部署](./docs/Deployment_all.md#%E6%96%B9%E5%BC%8F%E4%BA%8C%E6%9C%AC%E5%9C%B0%E6%BA%90%E7%A0%81%E8%BF%90%E8%A1%8C%E5%85%A8%E6%A8%A1%E5%9D%97) | 如果使用`FunASR`要4核8G,如果全API,要2核4G| [本地源码视频教程](https://www.bilibili.com/video/BV1wBJhz4Ewe) | +| **全模块安装** | 智能对话、IOT、OTA、智控台,数据存储在数据库 | 完整功能体验 |[Docker版](./docs/Deployment_all.md#%E6%96%B9%E5%BC%8F%E4%B8%80docker%E8%BF%90%E8%A1%8C%E5%85%A8%E6%A8%A1%E5%9D%97) / [源码部署](./docs/Deployment_all.md#%E6%96%B9%E5%BC%8F%E4%BA%8C%E6%9C%AC%E5%9C%B0%E6%BA%90%E7%A0%81%E8%BF%90%E8%A1%8C%E5%85%A8%E6%A8%A1%E5%9D%97) | 如果使用`FunASR`要4核8G,如果全API,要2核4G| [本地源码启动视频教程](https://www.bilibili.com/video/BV1wBJhz4Ewe) / [本地源码自动更新教程](./docs/dev-ops-integration.md) | > 💡 提示:以下是按最新代码部署后的测试平台,有需要可烧录测试,并发为6个,每天会清空数据 @@ -195,6 +195,7 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ |---------|---------|------| | ASR(语音识别) | FunASR(本地) | ✅DoubaoASR(火山流式语音识别) | | LLM(大模型) | ChatGLMLLM(智谱glm-4-flash) | ✅DoubaoLLM(火山doubao-1-5-pro-32k-250115) | +| VLLM(视觉大模型) | ChatGLMVLLM(智谱glm-4v-flash) | ✅ChatGLMVLLM(智谱glm-4v-flash) | | TTS(语音合成) | EdgeTTS(微软语音) | ✅HuoshanDoubleStreamTTS(火山双流式语音合成) | | Intent(意图识别) | function_call(函数调用) | ✅function_call(函数调用) | | Memory(记忆功能) | mem_local_short(本地短期记忆) | ✅mem_local_short(本地短期记忆) | diff --git a/README_en.md b/README_en.md index 9b4ed8ce7..6b4d2c1f3 100644 --- a/README_en.md +++ b/README_en.md @@ -6,14 +6,14 @@ This project provides backend services for the open-source smart hardware project xiaozhi-esp32
Implemented using Python, Java, and Vue according to the Xiaozhi Communication Protocol
-Helping you quickly set up your Xiaozhi server +Helps you quickly set up your Xiaozhi server

中文 · FAQ · Report Issues -· Deployment Guide +· Deployment Guide · Release Notes

@@ -50,7 +50,7 @@ Want to see it in action? Check out these videos 🎥 - Xiaozhi esp32 connecting to custom backend model + Xiaozhi esp32 connecting to own backend model @@ -64,14 +64,14 @@ Want to see it in action? Check out these videos 🎥 - Cantonese communication + Using Cantonese - Home appliance control + Control home appliances @@ -94,7 +94,7 @@ Want to see it in action? Check out these videos 🎥 - Music playback + Play music @@ -108,7 +108,7 @@ Want to see it in action? Check out these videos 🎥 - IOT device control + IOT command control @@ -120,6 +120,33 @@ Want to see it in action? Check out these videos 🎥 + + + + + Real-time interruption + + + + + + + Photo recognition + + + + + + + Multi-command tasks + + + + + + + + --- @@ -137,137 +164,84 @@ It is recommended that users prioritize service providers with relevant business ![Banners](docs/images/banner2.png) -This project offers two deployment methods. Please choose based on your specific needs: +This project provides two deployment methods. Please choose according to your specific needs: #### 🚀 Deployment Method Selection +| Deployment Method | Features | Suitable Scenarios | Deployment Guide | Requirements | Video Tutorial | +|---------|------|---------|---------|---------|---------| +| **Simplified Installation** | Smart dialogue, IOT functionality, data stored in configuration files | Low-configuration environment, no database needed | [Docker Version](./docs/Deployment.md#method-1-docker-server-only) / [Source Code Deployment](./docs/Deployment.md#method-2-local-source-code-server-only) | 2 cores 4G if using `FunASR`, 2 cores 2G if using all APIs | - | +| **Full Module Installation** | Smart dialogue, IOT, OTA, Control Panel, data stored in database | Complete functionality experience | [Docker Version](./docs/Deployment_all.md#method-1-docker-full-modules) / [Source Code Deployment](./docs/Deployment_all.md#method-2-local-source-code-full-modules) | 4 cores 8G if using `FunASR`, 2 cores 4G if using all APIs | [Local Source Code Startup Video Tutorial](https://www.bilibili.com/video/BV1wBJhz4Ewe) / [Local Source Code Auto-Update Tutorial](./docs/dev-ops-integration.md) | -| Deployment Method | Features | Use Case | Docker Deployment Guide | Source Code Deployment Guide | -|---------|------|---------|---------|---------| -| **Simplified Installation** | Smart dialogue, IOT functionality, data stored in configuration files | Low-configuration environment, no database required | [Docker Server Only](./docs/Deployment.md#%E6%96%B9%E5%BC%8F%E4%B8%80docker%E5%8F%AA%E8%BF%90%E8%A1%8Cserver) | [Local Source Code Server Only](./docs/Deployment.md#%E6%96%B9%E5%BC%8F%E4%BA%8C%E6%9C%AC%E5%9C%B0%E6%BA%90%E7%A0%81%E5%8F%AA%E8%BF%90%E8%A1%8Cserver)| -| **Full Module Installation** | Smart dialogue, IOT, OTA, Control Panel, data stored in database | Complete functionality experience |[Docker Full Module](./docs/Deployment_all.md#%E6%96%B9%E5%BC%8F%E4%B8%80docker%E8%BF%90%E8%A1%8C%E5%85%A8%E6%A8%A1%E5%9D%97) | [Local Source Code Full Module](./docs/Deployment_all.md#%E6%96%B9%E5%BC%8F%E4%BA%8C%E6%9C%AC%E5%9C%B0%E6%BA%90%E7%A0%81%E8%BF%90%E8%A1%8C%E5%85%A8%E6%A8%A1%E5%9D%97) | - -> 💡 Note: Below is the test platform deployed with the latest code. You can flash and test if needed. Concurrent users: 6, data cleared daily +> 💡 Note: Below are the test platforms deployed with the latest code. You can flash and test if needed. Concurrent users: 6, data will be cleared daily ``` -Control Panel: https://2662r3426b.vicp.fun +Control Panel Address: https://2662r3426b.vicp.fun Service Test Tool: https://2662r3426b.vicp.fun/test/ -OTA Interface: https://2662r3426b.vicp.fun/xiaozhi/ota/ -Websocket Interface: wss://2662r3426b.vicp.fun/xiaozhi/v1/ +OTA Interface Address: https://2662r3426b.vicp.fun/xiaozhi/ota/ +Websocket Interface Address: wss://2662r3426b.vicp.fun/xiaozhi/v1/ ``` +#### 🚩 Configuration Description and Recommendations +> [!Note] +> The default configuration of this project is `Entry Level Free` settings. For better results, we recommend using `Full Streaming Configuration`. +> +> Since version `0.5.2`, this project supports full streaming throughout the entire lifecycle. Compared to versions before `0.5`, response speed has improved by approximately `2.5 seconds` + +| Module Name | Entry Level Free Settings | Full Streaming Configuration | +|---------|---------|------| +| ASR(Speech Recognition) | FunASR(Local) | ✅DoubaoASR(Volcano Streaming Speech Recognition) | +| LLM(Large Language Model) | ChatGLMLLM(Zhipu glm-4-flash) | ✅DoubaoLLM(Volcano doubao-1-5-pro-32k-250115) | +| VLLM(Vision Large Model) | ChatGLMVLLM(Zhipu glm-4v-flash) | ✅ChatGLMVLLM(Zhipu glm-4v-flash) | +| TTS(Speech Synthesis) | EdgeTTS(Microsoft Speech) | ✅HuoshanDoubleStreamTTS(Volcano Double Streaming Speech Synthesis) | +| Intent(Intent Recognition) | function_call(Function Call) | ✅function_call(Function Call) | +| Memory(Memory Function) | mem_local_short(Local Short-term Memory) | ✅mem_local_short(Local Short-term Memory) | + --- ## Feature List ✨ - ### Implemented ✅ | Feature Module | Description | |---------|------| | Communication Protocol | Based on `xiaozhi-esp32` protocol, implements data interaction through WebSocket | -| Dialogue Interaction | Supports wake-up dialogue, manual dialogue, and real-time interruption. Auto-sleep after long periods of inactivity | +| Dialogue Interaction | Supports wake-up dialogue, manual dialogue, and real-time interruption. Auto-sleep after long periods of no dialogue | | Intent Recognition | Supports LLM intent recognition, function call, reducing hard-coded intent judgment | | Multi-language Recognition | Supports Mandarin, Cantonese, English, Japanese, Korean (default using FunASR) | -| LLM Module | Supports flexible LLM module switching, default using ChatGLMLLM, also supports Ali Bailing, DeepSeek, Ollama, etc. | -| TTS Module | Supports EdgeTTS (default), Volcano Engine Doubao TTS, and other TTS interfaces for speech synthesis | -| Memory Function | Supports ultra-long memory, local summary memory, and no memory modes for different scenarios | -| IOT Function | Supports managing registered device IOT functionality, intelligent IoT control based on dialogue context | -| Control Panel | Provides web management interface, supports agent management, user management, system configuration, etc. | +| LLM Module | Supports flexible LLM module switching, default using ChatGLMLLM, can also use Ali Bailian, DeepSeek, Ollama, etc. | +| TTS Module | Supports EdgeTTS (default), Volcano Engine Doubao TTS, and other TTS interfaces | +| Memory Function | Supports ultra-long memory, local summary memory, and no memory modes | +| IOT Function | Supports managing registered device IOT functionality, supports smart IoT control based on dialogue context | +| Control Panel | Provides Web management interface, supports agent management, user management, system configuration, etc. | ### In Development 🚧 To learn about specific development progress, [click here](https://github.com/users/xinnan-tech/projects/3) -If you're a software developer, here's an [Open Letter to Developers](docs/contributor_open_letter.md). Welcome to join! +If you are a software developer, here is an [Open Letter to Developers](docs/contributor_open_letter.md). Welcome to join! --- ## Product Ecosystem 👬 -Xiaozhi is an ecosystem. When using this product, you might want to check out other excellent projects in this ecosystem: +Xiaozhi is an ecosystem. When using this product, you might also want to check out other excellent projects in this ecosystem -| Project Name | Project Link | Description | +| Project Name | Project Address | Project Description | |:---------------------|:--------|:--------| -| Xiaozhi Android Client | [xiaozhi-android-client](https://github.com/TOM88812/xiaozhi-android-client) | A Flutter-based Android and iOS voice dialogue application supporting real-time voice interaction and text dialogue | -| Xiaozhi PC Client | [py-xiaozhi](https://github.com/Huang-junsen/py-xiaozhi) | A Python-based AI client that allows you to experience Xiaozhi AI functionality through code without physical hardware | -| Xiaozhi Java Server | [xiaozhi-esp32-server-java](https://github.com/joey-zhou/xiaozhi-esp32-server-java) | A Java-based open-source project providing complete backend service solutions | +| Xiaozhi Android Client | [xiaozhi-android-client](https://github.com/TOM88812/xiaozhi-android-client) | A Flutter-based Android and iOS voice dialogue application supporting real-time voice interaction and text dialogue. | +| Xiaozhi PC Client | [py-xiaozhi](https://github.com/Huang-junsen/py-xiaozhi) | This project provides a Python-based Xiaozhi AI client, allowing you to experience Xiaozhi AI's functionality through code even without physical hardware. | +| Xiaozhi Java Server | [xiaozhi-esp32-server-java](https://github.com/joey-zhou/xiaozhi-esp32-server-java) | The Java version of Xiaozhi open-source backend service is a Java-based open-source project.
It includes both frontend and backend services, aiming to provide users with a complete backend service solution. | --- -## Supported Platforms/Components 📋 +## Supported Platforms/Components List 📋 ### LLM Language Models | Usage Method | Supported Platforms | Free Platforms | |:---:|:---:|:---:| -| openai API | Ali Bailing, Volcano Engine Doubao, DeepSeek, ChatGLM, Gemini | ChatGLM, Gemini | -| ollama API | Ollama | - | -| dify API | Dify | - | -| fastgpt API | Fastgpt | - | -| coze API | Coze | - | +| openai interface call | Ali Bailian, Volcano Engine Doubao, DeepSeek, Zhipu ChatGLM, Gemini | Zhipu ChatGLM, Gemini | +| ollama interface call | Ollama | - | +| dify interface call | Dify | - | +| fastgpt interface call | Fastgpt | - | +| coze interface call | Coze | - | -Actually, any LLM supporting openai API calls can be integrated. - ---- - -### TTS Speech Synthesis - -| Usage Method | Supported Platforms | Free Platforms | -|:---:|:---:|:---:| -| API Calls | EdgeTTS, Volcano Engine Doubao TTS, Tencent Cloud, Aliyun TTS, CosyVoiceSiliconflow, TTS302AI, CozeCnTTS, GizwitsTTS, ACGNTTS, OpenAITTS | EdgeTTS, CosyVoiceSiliconflow(partial) | -| Local Service | FishSpeech, GPT_SOVITS_V2, GPT_SOVITS_V3, MinimaxTTS | FishSpeech, GPT_SOVITS_V2, GPT_SOVITS_V3, MinimaxTTS | - ---- - -### VAD Voice Activity Detection - -| Type | Platform Name | Usage Method | Pricing | Notes | -|:---:|:---------:|:----:|:----:|:--:| -| VAD | SileroVAD | Local Use | Free | | - ---- - -### ASR Speech Recognition - -| Usage Method | Supported Platforms | Free Platforms | -|:---:|:---:|:---:| -| Local Use | FunASR, SherpaASR | FunASR, SherpaASR | -| API Calls | DoubaoASR, FunASRServer, TencentASR, AliyunASR | FunASRServer | - ---- - -### Memory Storage - -| Type | Platform Name | Usage Method | Pricing | Notes | -|:------:|:---------------:|:----:|:---------:|:--:| -| Memory | mem0ai | API Calls | 1000 calls/month quota | | -| Memory | mem_local_short | Local Summary | Free | | - ---- - -### Intent Recognition - -| Type | Platform Name | Usage Method | Pricing | Notes | -|:------:|:-------------:|:----:|:-------:|:---------------------:| -| Intent | intent_llm | API Calls | Based on LLM pricing | Uses large model for intent recognition, highly versatile | -| Intent | function_call | API Calls | Based on LLM pricing | Uses large model function calls for intent, fast and effective | - ---- - -## Acknowledgments 🙏 - -| Logo | Project/Company | Description | -|:---:|:---:|:---| -| | [Bailing Voice Dialogue Robot](https://github.com/wwbin2017/bailing) | This project was inspired by [Bailing Voice Dialogue Robot](https://github.com/wwbin2017/bailing) and implemented based on it | -| | [Tenclass](https://www.tenclass.com/) | Thanks to [Tenclass](https://www.tenclass.com/) for developing standard communication protocols, multi-device compatibility solutions, and high-concurrency scenario practices for the Xiaozhi ecosystem; providing comprehensive technical documentation support for this project | -| | [Xuanfeng Technology](https://github.com/Eric0308) | Thanks to [Xuanfeng Technology](https://github.com/Eric0308) for contributing function call framework, MCP communication protocol, and plugin call mechanism implementation code, significantly improving front-end device (IoT) interaction efficiency and functional extensibility through standardized instruction scheduling system and dynamic expansion capabilities | -| | [Huiyuan Design](http://ui.kwd988.net/) | Thanks to [Huiyuan Design](http://ui.kwd988.net/) for providing professional visual solutions for this project, empowering product user experience with their design experience serving over a thousand enterprises | -| | [Xi'an Qinren Information Technology](https://www.029app.com/) | Thanks to [Xi'an Qinren Information Technology](https://www.029app.com/) for deepening this project's visual system, ensuring consistency and extensibility of overall design style in multi-scenario applications | - - - - - - - - Star History Chart - - \ No newline at end of file +In fact, any LLM that supports openai interface calls can be integrated and used. diff --git a/docs/dev-ops-integration.md b/docs/dev-ops-integration.md new file mode 100644 index 000000000..bb9640b1e --- /dev/null +++ b/docs/dev-ops-integration.md @@ -0,0 +1,163 @@ +# 全模块源码部署自动升级方法 + +本教程是方便全模块源码部署的爱好者,如何通过自动命令,自动拉取源码,自动编译,自动启动端口运行。实现最高效率的升级系统。 + +本项目的测试平台`https://2662r3426b.vicp.fun`,从开放以来就使用了该方法,效果良好。 + +# 开始条件 +- 你的电脑/服务器是linux操作系统 +- 你已经跑通了整个流程 +- 你喜欢跟进最新功能,但是觉得每次手动部署有点麻烦,期待有一个自动更新的方法 + +第二个条件必须满足,因为本教程所涉及的某些文件,JDK、Node.js环境、Conda环境等,是需要你跑通整个流程才有的,如果你没有跑通,当我讲到某个文件的时候,你可能就不知道什么意思。 + +# 教程效果 +- 解决国内不能拉取最新项目源码问题 +- 自动拉取代码编译前端文件 +- 自动拉取代码编译java文件,自动杀掉8002端口,自动启动8002端口 +- 自动拉取python代码,自动杀掉8000端口,自动启动8000端口 + +# 第一步 选好你的项目目录 + +例如,我规划了我的项目目录是,这是一个新建的空白的目录,如果你不想出错,可以和我一样 +``` +/home/system/xiaozhi +``` + +# 第二步 克隆本项目 +此刻,先要执行第一句话,拉取源码,这句命令适用于国内网络的服务器和电脑,无需翻墙 + +``` +cd /home/system/xiaozhi +git clone https://ghproxy.net/https://github.com/xinnan-tech/xiaozhi-esp32-server.git +``` + +执行完后,你的项目目录会多了一个文件夹`xiaozhi-esp32-server`,这个就是项目的源码 + +# 第三步 复制基础的文件 + +如果你之前已经跑通了整个流程,对funasr的模型文件`xiaozhi-server/models/SenseVoiceSmall/model.pt`和你的私有配置文件`xiaozhi-server/data/.config.yaml`这两个文件不会陌生。 + +此刻你需要把`model.pt`文件复制到新的目录去,你可以这样 +``` +cp 你原来的.config.yaml完整路径 /home/system/xiaozhi/xiaozhi-esp32-server/main/xiaozhi-server/data/.config.yaml +cp 你原来的model.pt完整路径 /home/system/xiaozhi/xiaozhi-esp32-server/main/xiaozhi-server/models/SenseVoiceSmall/model.pt +``` + +# 第四步 建立三个自动编译文件 + +## 4.1 自动编译mananger-web模块 +在`/home/system/xiaozhi/`目录下,创建名字为`update_8001.sh`的文件,内容如下 + +``` +cd /home/system/xiaozhi/xiaozhi-esp32-server +git fetch --all +git reset --hard +git pull origin main + + +cd /home/system/xiaozhi/xiaozhi-esp32-server/main/manager-web +npm install +npm run build +rm -rf /home/system/xiaozhi/manager-web +mv /home/system/xiaozhi/xiaozhi-esp32-server/main/manager-web/dist /home/system/xiaozhi/manager-web +``` + +保存好后执行赋权命令 +``` +chmod 777 update_8001.sh +``` +执行完后,继续往下 + +## 4.2 自动编译运行manager-api模块 +在`/home/system/xiaozhi/`目录下,创建名字为`update_8002.sh`的文件,内容如下 + +``` +cd /home/system/xiaozhi/xiaozhi-esp32-server +git pull origin main + + +cd /home/system/xiaozhi/xiaozhi-esp32-server/main/manager-api +rm -rf target +mvn clean package -Dmaven.test.skip=true +cd /home/system/xiaozhi/ + +# 查找占用8002端口的进程号 +PID=$(sudo netstat -tulnp | grep 8002 | awk '{print $7}' | cut -d'/' -f1) + +rm -rf /home/system/xiaozhi/xiaozhi-esp32-api.jar +mv /home/system/xiaozhi/xiaozhi-esp32-server/main/manager-api/target/xiaozhi-esp32-api.jar /home/system/xiaozhi/xiaozhi-esp32-api.jar + +# 检查是否找到进程号 +if [ -z "$PID" ]; then + echo "没有找到占用8002端口的进程" +else + echo "找到占用8002端口的进程,进程号为: $PID" + # 杀掉进程 + kill -9 $PID + kill -9 $PID + echo "已杀掉进程 $PID" +fi + +nohup java -jar xiaozhi-esp32-api.jar --spring.profiles.active=dev & +``` + +保存好后执行赋权命令 +``` +chmod 777 update_8002.sh +``` +执行完后,继续往下 + +## 4.3 自动编译运行Python项目 +在`/home/system/xiaozhi/`目录下,创建名字为`update_8000.sh`的文件,内容如下 + +``` +cd /home/system/xiaozhi/xiaozhi-esp32-server +git pull origin main + +# 查找占用8000端口的进程号 +PID=$(sudo netstat -tulnp | grep 8000 | awk '{print $7}' | cut -d'/' -f1) + +# 检查是否找到进程号 +if [ -z "$PID" ]; then + echo "没有找到占用8000端口的进程" +else + echo "找到占用8000端口的进程,进程号为: $PID" + # 杀掉进程 + kill -9 $PID + kill -9 $PID + echo "已杀掉进程 $PID" +fi +cd main/xiaozhi-server +pip install -r requirements.txt +nohup python app.py >/dev/null & +``` + +保存好后执行赋权命令 +``` +chmod 777 update_8000.sh +``` +执行完后,继续往下 + +# 日常更新 + +以上的脚本都建立好后,日常更新,我们只要依次执行以下命令就可以做到自动更新和启动 + +``` +# 进入pyhton环境 +conda activate xiaozhi-esp32-server +cd /home/system/xiaozhi +# 更新并启动Java程序 +./update_8001.sh +# 更新web程序 +./update_8002.sh +# 更新并启动python程序 +./update_8000.sh +# 查看Java日志 +tail -f nohup.out +# 查看Python日志 +tail -f /home/system/xiaozhi/xiaozhi-esp32-server/main/xiaozhi-server/tmp/server.log +``` + +# 注意事项 +测试平台`https://2662r3426b.vicp.fun`,是使用nginx做了反向代理。nginx.conf详细配置可以[参考这里](https://github.com/xinnan-tech/xiaozhi-esp32-server/issues/791) From e62f72810c28894f86ced45c6da4932bcfaa9c4f Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 3 Jun 2025 10:48:36 +0800 Subject: [PATCH 06/12] =?UTF-8?q?update:=E8=A1=A5=E5=85=85=E5=89=A9?= =?UTF-8?q?=E4=BD=99=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_en.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/README_en.md b/README_en.md index 6b4d2c1f3..d66cbe5d3 100644 --- a/README_en.md +++ b/README_en.md @@ -245,3 +245,67 @@ Xiaozhi is an ecosystem. When using this product, you might also want to check o | coze interface call | Coze | - | In fact, any LLM that supports openai interface calls can be integrated and used. + +### TTS Speech Synthesis + +| Usage Method | Supported Platforms | Free Platforms | +|:---:|:---:|:---:| +| API Call | EdgeTTS, Volcano Engine Doubao TTS, Tencent Cloud, Alibaba Cloud TTS, CosyVoiceSiliconflow, TTS302AI, CozeCnTTS, GizwitsTTS, ACGNTTS, OpenAITTS | EdgeTTS, CosyVoiceSiliconflow(partial) | +| Local Service | FishSpeech, GPT_SOVITS_V2, GPT_SOVITS_V3, MinimaxTTS | FishSpeech, GPT_SOVITS_V2, GPT_SOVITS_V3, MinimaxTTS | + +--- + +### VAD Voice Activity Detection + +| Type | Platform Name | Usage Method | Pricing Model | Notes | +|:---:|:---------:|:----:|:----:|:--:| +| VAD | SileroVAD | Local Usage | Free | | + +--- + +### ASR Speech Recognition + +| Usage Method | Supported Platforms | Free Platforms | +|:---:|:---:|:---:| +| Local Usage | FunASR, SherpaASR | FunASR, SherpaASR | +| API Call | DoubaoASR, FunASRServer, TencentASR, AliyunASR | FunASRServer | + +--- + +### Memory Storage + +| Type | Platform Name | Usage Method | Pricing Model | Notes | +|:------:|:---------------:|:----:|:---------:|:--:| +| Memory | mem0ai | API Call | 1000 calls/month quota | | +| Memory | mem_local_short | Local Summary | Free | | + +--- + +### Intent Recognition + +| Type | Platform Name | Usage Method | Pricing Model | Notes | +|:------:|:-------------:|:----:|:-------:|:---------------------:| +| Intent | intent_llm | API Call | Based on LLM pricing | Uses large model for intent recognition, highly versatile | +| Intent | function_call | API Call | Based on LLM pricing | Uses large model function calls for intent, fast and effective | + +--- + +## Acknowledgments 🙏 + +| Logo | Project/Company | Description | +|:---:|:---:|:---| +| | [Bailing Voice Dialogue Robot](https://github.com/wwbin2017/bailing) | This project was inspired by [Bailing Voice Dialogue Robot](https://github.com/wwbin2017/bailing) and implemented based on it | +| | [Tenclass](https://www.tenclass.com/) | Thanks to [Tenclass](https://www.tenclass.com/) for establishing standard communication protocols, multi-device compatibility solutions, and high-concurrency scenario practices for the Xiaozhi ecosystem; providing full-chain technical documentation support for this project | +| | [Xuanfeng Technology](https://github.com/Eric0308) | Thanks to [Xuanfeng Technology](https://github.com/Eric0308) for contributing the function call framework, MCP communication protocol, and plugin call mechanism implementation code, significantly improving front-end device (IoT) interaction efficiency and functional extensibility through standardized instruction scheduling system and dynamic expansion capabilities | +| | [Huiyuan Design](http://ui.kwd988.net/) | Thanks to [Huiyuan Design](http://ui.kwd988.net/) for providing professional visual solutions for this project, empowering the product user experience with their design experience serving over a thousand enterprises | +| | [Xi'an Qinren Information Technology](https://www.029app.com/) | Thanks to [Xi'an Qinren Information Technology](https://www.029app.com/) for deepening the visual system of this project, ensuring consistency and extensibility of the overall design style in multi-scenario applications | + + + + + + + + Star History Chart + + From 610fa4d10185fd6d122df9e27888a1735506faf7 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 3 Jun 2025 17:30:35 +0800 Subject: [PATCH 07/12] =?UTF-8?q?update:=E5=8C=BA=E5=88=86=E8=B1=86?= =?UTF-8?q?=E5=8C=85ASR=E6=8C=89=E6=AC=A1=E6=94=B6=E8=B4=B9=E5=92=8C?= =?UTF-8?q?=E6=8C=89=E6=97=B6=E6=94=B6=E8=B4=B9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi/common/constant/Constant.java | 2 +- .../resources/db/changelog/202506031639.sql | 45 ++ main/manager-web/src/views/ModelConfig.vue | 4 +- main/manager-web/src/views/roleConfig.vue | 2 +- main/xiaozhi-server/config.yaml | 15 + main/xiaozhi-server/config/logger.py | 2 +- .../core/providers/asr/doubao.py | 679 ++++++------------ .../core/providers/asr/doubao_stream.py | 534 ++++++++++++++ .../xiaozhi-server/core/providers/tts/base.py | 1 + 9 files changed, 806 insertions(+), 478 deletions(-) create mode 100644 main/manager-api/src/main/resources/db/changelog/202506031639.sql create mode 100644 main/xiaozhi-server/core/providers/asr/doubao_stream.py diff --git a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java index 2e3174af3..2590a5662 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java +++ b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java @@ -227,7 +227,7 @@ public interface Constant { /** * 版本号 */ - public static final String VERSION = "0.5.2"; + public static final String VERSION = "0.5.4"; /** * 无效固件URL diff --git a/main/manager-api/src/main/resources/db/changelog/202506031639.sql b/main/manager-api/src/main/resources/db/changelog/202506031639.sql new file mode 100644 index 000000000..95691b423 --- /dev/null +++ b/main/manager-api/src/main/resources/db/changelog/202506031639.sql @@ -0,0 +1,45 @@ +-- VLLM模型供应器 +delete from `ai_model_provider` where id = 'SYSTEM_ASR_DoubaoStreamASR'; +INSERT INTO `ai_model_provider` (`id`, `model_type`, `provider_code`, `name`, `fields`, `sort`, `creator`, `create_date`, `updater`, `update_date`) VALUES +('SYSTEM_ASR_DoubaoStreamASR', 'ASR', 'doubao_stream', '火山引擎语音识别(流式)', '[{"key":"appid","label":"应用ID","type":"string"},{"key":"access_token","label":"访问令牌","type":"string"},{"key":"cluster","label":"集群","type":"string"},{"key":"boosting_table_name","label":"热词文件名称","type":"string"},{"key":"correct_table_name","label":"替换词文件名称","type":"string"},{"key":"output_dir","label":"输出目录","type":"string"}]', 3, 1, NOW(), 1, NOW()); + + +-- VLLM模型配置 +delete from `ai_model_config` where id = 'ASR_DoubaoStreamASR'; +INSERT INTO `ai_model_config` VALUES ('ASR_DoubaoStreamASR', 'ASR', 'DoubaoStreamASR', '豆包语音识别(流式)', 0, 1, '{\"type\": \"doubao_stream\", \"appid\": \"\", \"access_token\": \"\", \"cluster\": \"volcengine_input_common\", \"output_dir\": \"tmp/\"}', NULL, NULL, 3, NULL, NULL, NULL, NULL); + + +-- 更新豆包ASR配置说明 +UPDATE `ai_model_config` SET +`doc_link` = 'https://console.volcengine.com/speech/app', +`remark` = '豆包ASR配置说明: +1. 豆包ASR和豆包(流式)ASR的区别是:豆包ASR是按次收费,豆包(流式)ASR是按时收费 +2. 一般来说按次收费的更便宜,但是豆包(流式)ASR使用了大模型技术,效果更好 +3. 需要在火山引擎控制台创建应用并获取appid和access_token +4. 支持中文语音识别 +5. 需要网络连接 +6. 输出文件保存在tmp/目录 +申请步骤: +1. 访问 https://console.volcengine.com/speech/app +2. 创建新应用 +3. 获取appid和access_token +4. 填入配置文件中 +如需设置热词,请参考:https://www.volcengine.com/docs/6561/155738 +' WHERE `id` = 'ASR_DoubaoASR'; + +UPDATE `ai_model_config` SET +`doc_link` = 'https://console.volcengine.com/speech/app', +`remark` = '豆包ASR配置说明: +1. 豆包ASR和豆包(流式)ASR的区别是:豆包ASR是按次收费,豆包(流式)ASR是按时收费 +2. 一般来说按次收费的更便宜,但是豆包(流式)ASR使用了大模型技术,效果更好 +3. 需要在火山引擎控制台创建应用并获取appid和access_token +4. 支持中文语音识别 +5. 需要网络连接 +6. 输出文件保存在tmp/目录 +申请步骤: +1. 访问 https://console.volcengine.com/speech/app +2. 创建新应用 +3. 获取appid和access_token +4. 填入配置文件中 +如需设置热词,请参考:https://www.volcengine.com/docs/6561/155738 +' WHERE `id` = 'ASR_DoubaoStreamASR'; diff --git a/main/manager-web/src/views/ModelConfig.vue b/main/manager-web/src/views/ModelConfig.vue index 6e5a76f80..5a88c9aab 100644 --- a/main/manager-web/src/views/ModelConfig.vue +++ b/main/manager-web/src/views/ModelConfig.vue @@ -31,7 +31,7 @@ 大语言模型 - 视觉大语言模型 + 视觉大模型 意图识别 @@ -176,7 +176,7 @@ export default { vad: '语言活动检测模型(VAD)', asr: '语音识别模型(ASR)', llm: '大语言模型(LLM)', - vllm: '视觉大语言模型(VLLM)', + vllm: '视觉大模型(VLLM)', intent: '意图识别模型(Intent)', tts: '语音合成模型(TTS)', memory: '记忆模型(Memory)' diff --git a/main/manager-web/src/views/roleConfig.vue b/main/manager-web/src/views/roleConfig.vue index 50cf278a6..06a5c09d4 100644 --- a/main/manager-web/src/views/roleConfig.vue +++ b/main/manager-web/src/views/roleConfig.vue @@ -177,7 +177,7 @@ export default { { label: '语音活动检测(VAD)', key: 'vadModelId', type: 'VAD' }, { label: '语音识别(ASR)', key: 'asrModelId', type: 'ASR' }, { label: '大语言模型(LLM)', key: 'llmModelId', type: 'LLM' }, - { label: '视觉大语言模型(VLLM)', key: 'vllmModelId', type: 'VLLM' }, + { label: '视觉大模型(VLLM)', key: 'vllmModelId', type: 'VLLM' }, { label: '意图识别(Intent)', key: 'intentModelId', type: 'Intent' }, { label: '记忆(Memory)', key: 'memModelId', type: 'Memory' }, { label: '语音合成(TTS)', key: 'ttsModelId', type: 'TTS' }, diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 37a7309d9..179d7204a 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -264,6 +264,8 @@ ASR: DoubaoASR: # 可以在这里申请相关Key等信息 # https://console.volcengine.com/speech/app + # DoubaoASR和DoubaoStreamASR的区别是:DoubaoASR是按次收费,DoubaoStreamASR是按时收费 + # 一般来说按次收费的更便宜,但是DoubaoStreamASR使用了大模型技术,效果更好 type: doubao appid: 你的火山引擎语音合成服务appid access_token: 你的火山引擎语音合成服务access_token @@ -272,6 +274,19 @@ ASR: boosting_table_name: (选填)你的热词文件名称 correct_table_name: (选填)你的替换词文件名称 output_dir: tmp/ + DoubaoStreamASR: + # 可以在这里申请相关Key等信息 + # https://console.volcengine.com/speech/app + # DoubaoASR和DoubaoStreamASR的区别是:DoubaoASR是按次收费,DoubaoStreamASR是按时收费 + # 一般来说按次收费的更便宜,但是DoubaoStreamASR使用了大模型技术,效果更好 + type: doubao_stream + appid: 你的火山引擎语音合成服务appid + access_token: 你的火山引擎语音合成服务access_token + cluster: volcengine_input_common + # 热词、替换词使用流程:https://www.volcengine.com/docs/6561/155738 + boosting_table_name: (选填)你的热词文件名称 + correct_table_name: (选填)你的替换词文件名称 + output_dir: tmp/ TencentASR: # token申请地址:https://console.cloud.tencent.com/cam/capi # 免费领取资源:https://console.cloud.tencent.com/asr/resourcebundle diff --git a/main/xiaozhi-server/config/logger.py b/main/xiaozhi-server/config/logger.py index 5f107f5da..643952dd8 100644 --- a/main/xiaozhi-server/config/logger.py +++ b/main/xiaozhi-server/config/logger.py @@ -4,7 +4,7 @@ from loguru import logger from config.config_loader import load_config from config.settings import check_config_file -SERVER_VERSION = "0.5.2" +SERVER_VERSION = "0.5.4" _logger_initialized = False diff --git a/main/xiaozhi-server/core/providers/asr/doubao.py b/main/xiaozhi-server/core/providers/asr/doubao.py index da4098adf..8a647508e 100644 --- a/main/xiaozhi-server/core/providers/asr/doubao.py +++ b/main/xiaozhi-server/core/providers/asr/doubao.py @@ -1,534 +1,267 @@ +import time +import os +import uuid import json import gzip -import uuid -import asyncio import websockets -import opuslib_next -from core.providers.asr.base import ASRProviderBase from config.logger import setup_logging +from typing import Optional, Tuple, List +from core.providers.asr.base import ASRProviderBase from core.providers.asr.dto.dto import InterfaceType -import threading + TAG = __name__ logger = setup_logging() CLIENT_FULL_REQUEST = 0b0001 CLIENT_AUDIO_ONLY_REQUEST = 0b0010 + +NO_SEQUENCE = 0b0000 +NEG_SEQUENCE = 0b0010 + SERVER_FULL_RESPONSE = 0b1001 SERVER_ACK = 0b1011 SERVER_ERROR_RESPONSE = 0b1111 -NO_SEQUENCE = 0b0000 -NEG_SEQUENCE = 0b0010 -JSON_SERIALIZATION = 0b0001 -GZIP_COMPRESSION = 0b0001 -PROTOCOL_VERSION = 0b0001 + +NO_SERIALIZATION = 0b0000 +JSON = 0b0001 +THRIFT = 0b0011 +CUSTOM_TYPE = 0b1111 +NO_COMPRESSION = 0b0000 +GZIP = 0b0001 +CUSTOM_COMPRESSION = 0b1111 + + +def parse_response(res): + """ + protocol_version(4 bits), header_size(4 bits), + message_type(4 bits), message_type_specific_flags(4 bits) + serialization_method(4 bits) message_compression(4 bits) + reserved (8bits) 保留字段 + header_extensions 扩展头(大小等于 8 * 4 * (header_size - 1) ) + payload 类似与http 请求体 + """ + protocol_version = res[0] >> 4 + header_size = res[0] & 0x0F + message_type = res[1] >> 4 + message_type_specific_flags = res[1] & 0x0F + serialization_method = res[2] >> 4 + message_compression = res[2] & 0x0F + reserved = res[3] + header_extensions = res[4 : header_size * 4] + payload = res[header_size * 4 :] + result = {} + payload_msg = None + payload_size = 0 + if message_type == SERVER_FULL_RESPONSE: + payload_size = int.from_bytes(payload[:4], "big", signed=True) + payload_msg = payload[4:] + elif message_type == SERVER_ACK: + seq = int.from_bytes(payload[:4], "big", signed=True) + result["seq"] = seq + if len(payload) >= 8: + payload_size = int.from_bytes(payload[4:8], "big", signed=False) + payload_msg = payload[8:] + elif message_type == SERVER_ERROR_RESPONSE: + code = int.from_bytes(payload[:4], "big", signed=False) + result["code"] = code + payload_size = int.from_bytes(payload[4:8], "big", signed=False) + payload_msg = payload[8:] + if payload_msg is None: + return result + if message_compression == GZIP: + payload_msg = gzip.decompress(payload_msg) + if serialization_method == JSON: + payload_msg = json.loads(str(payload_msg, "utf-8")) + elif serialization_method != NO_SERIALIZATION: + payload_msg = str(payload_msg, "utf-8") + result["payload_msg"] = payload_msg + result["payload_size"] = payload_size + return result class ASRProvider(ASRProviderBase): - def __init__(self, config, delete_audio_file): + def __init__(self, config: dict, delete_audio_file: bool): super().__init__() - self.interface_type = InterfaceType.STREAM - self.config = config - self.text = "" - self.max_retries = 3 - self.retry_delay = 2 # 重试延迟秒数 - self.recv_lock = asyncio.Lock() # 添加接收锁 - self.reconnect_lock = asyncio.Lock() # 添加重连锁 - self.last_reconnect_time = 0 # 上次重连时间 - self.reconnect_cooldown = 1 # 增加重连冷却时间到10秒 - self.reconnect_count = 0 # 当前重连次数 - self.max_reconnect_count = 3 # 减少最大重连次数到3次 - self.asr_thread = None # ASR监听线程 - self.thread_lock = threading.Lock() # 线程管理锁 - self.is_reconnecting = False # 添加重连状态标志 - - # 添加会话管理相关属性 - self._session_lock = asyncio.Lock() # 会话操作的并发锁 - self._current_session_id = None # 当前会话ID - self._session_started = False # 会话是否已开始 - self._session_finished = False # 会话是否已结束 - self._session_close_event = asyncio.Event() # 添加会话关闭事件 - - self.appid = str(config.get("appid")) + self.interface_type = InterfaceType.NON_STREAM + self.appid = config.get("appid") self.cluster = config.get("cluster") self.access_token = config.get("access_token") self.boosting_table_name = config.get("boosting_table_name", "") self.correct_table_name = config.get("correct_table_name", "") - self.output_dir = config.get("output_dir", "temp/") + self.output_dir = config.get("output_dir") self.delete_audio_file = delete_audio_file - self.ws_url = "wss://openspeech.bytedance.com/api/v2/asr" - self.uid = config.get("uid", "streaming_asr_service") - self.workflow = config.get( - "workflow", "audio_in,resample,partition,vad,fe,decode,itn,nlu_punctuate" - ) - self.result_type = config.get("result_type", "single") - self.format = config.get("format", "raw") - self.codec = config.get("codec", "pcm") - self.rate = config.get("sample_rate", 16000) - self.language = config.get("language", "zh-CN") - self.bits = config.get("bits", 16) - self.channel = config.get("channel", 1) - self.auth_method = config.get("auth_method", "token") - self.secret = config.get("secret", "access_secret") - self.decoder = opuslib_next.Decoder(16000, 1) - self.asr_ws = None - self.forward_task = None - self.conn = None + self.host = "openspeech.bytedance.com" + self.ws_url = f"wss://{self.host}/api/v2/asr" + self.success_code = 1000 + self.seg_duration = 15000 - ################################################################################### - # 豆包流式ASR重写父类的方法--开始 - ################################################################################### - async def open_audio_channels(self, conn): - await super().open_audio_channels(conn) + # 确保输出目录存在 + os.makedirs(self.output_dir, exist_ok=True) - async with self._session_lock: - # 如果正在重连,等待重连完成 - if self.is_reconnecting: - logger.bind(tag=TAG).info("等待当前重连完成...") - await self._session_close_event.wait() - self._session_close_event.clear() + @staticmethod + def _generate_header( + message_type=CLIENT_FULL_REQUEST, message_type_specific_flags=NO_SEQUENCE + ) -> bytearray: + """Generate protocol header.""" + header = bytearray() + header_size = 1 + header.append((0b0001 << 4) | header_size) # Protocol version + header.append((message_type << 4) | message_type_specific_flags) + header.append((0b0001 << 4) | 0b0001) # JSON serialization & GZIP compression + header.append(0x00) # reserved + return header - # 如果已有会话未结束,先关闭它 - if self._session_started and not self._session_finished: - logger.bind(tag=TAG).warning( - f"发现未关闭的会话 {self._current_session_id},正在关闭..." - ) - if self.asr_ws is not None: - try: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning(f"关闭旧连接时发生错误: {e}") - finally: - self.asr_ws = None - self._session_finished = True - self._session_close_event.set() - - # 重置会话状态 - self._current_session_id = str(uuid.uuid4()) - self._session_started = True - self._session_finished = False - self.is_reconnecting = True - - try: - retry_count = 0 - while retry_count < self.max_retries: - try: - headers = ( - self.token_auth() if self.auth_method == "token" else None - ) - self.asr_ws = await websockets.connect( - self.ws_url, - additional_headers=headers, - max_size=1000000000, - ping_interval=None, - ping_timeout=None, - close_timeout=10, - ) - - # 发送初始化请求 - request_params = self.construct_request( - self._current_session_id - ) - try: - payload_bytes = str.encode(json.dumps(request_params)) - payload_bytes = gzip.compress(payload_bytes) - full_client_request = self.generate_header() - full_client_request.extend( - (len(payload_bytes)).to_bytes(4, "big") - ) - full_client_request.extend(payload_bytes) - await self.asr_ws.send(full_client_request) - except Exception as e: - logger.bind(tag=TAG).error(f"发送初始化请求失败: {e}") - raise e - - # 等待初始化响应 - try: - init_res = await self.asr_ws.recv() - self.parse_response(init_res) - except Exception as e: - logger.bind(tag=TAG).error(f"ASR服务初始化失败: {e}") - raise e - - # 启动接收ASR结果的异步任务 - with self.thread_lock: - if ( - self.asr_thread is None - or not self.asr_thread.is_alive() - ): - logger.bind(tag=TAG).info("创建新的ASR监听线程...") - self.asr_thread = threading.Thread( - target=self._start_monitor_asr_response_thread, - daemon=True, - ) - self.asr_thread.start() - # 等待一小段时间确保线程启动 - await asyncio.sleep(0.1) - if not self.asr_thread.is_alive(): - logger.bind(tag=TAG).error("ASR监听线程启动失败") - raise Exception("ASR监听线程启动失败") - logger.bind(tag=TAG).info("ASR监听线程已启动") - return - - except websockets.exceptions.WebSocketException as e: - retry_count += 1 - if retry_count < self.max_retries: - logger.bind(tag=TAG).warning( - f"WebSocket连接失败,正在进行第{retry_count}次重试: {e}" - ) - await asyncio.sleep(self.retry_delay) - else: - logger.bind(tag=TAG).warning( - f"WebSocket连接失败,已达到最大重试次数: {e}" - ) - raise - except Exception as e: - logger.bind(tag=TAG).error(f"WebSocket连接发生未知错误: {e}") - raise - finally: - self.is_reconnecting = False - self._session_close_event.set() - - async def receive_audio(self, audio, _): - if not isinstance(audio, bytes): - return - - try: - # 解码opus得到PCM数据 - pcm_frame = self.decoder.decode(audio, 960) - payload = gzip.compress(pcm_frame) - audio_request = bytearray(self.generate_audio_default_header()) - audio_request.extend(len(payload).to_bytes(4, "big")) - audio_request.extend(payload) - if self.asr_ws: - await self.asr_ws.send(audio_request) - except Exception as e: - logger.bind(tag=TAG).debug(f"发送音频数据时发生错误: {e}") - - ################################################################################### - # 豆包流式ASR重写父类的方法--结束 - ################################################################################### - - def construct_request(self, reqid): - req = { + def _construct_request(self, reqid) -> dict: + """Construct the request payload.""" + return { "app": { - "appid": self.appid, + "appid": f"{self.appid}", "cluster": self.cluster, "token": self.access_token, }, - "user": {"uid": self.uid}, + "user": { + "uid": str(uuid.uuid4()), + }, "request": { "reqid": reqid, - "workflow": self.workflow, - "show_utterances": True, - "result_type": self.result_type, + "show_utterances": False, "sequence": 1, "boosting_table_name": self.boosting_table_name, "correct_table_name": self.correct_table_name, }, "audio": { - "format": self.format, - "codec": self.codec, - "rate": self.rate, - "language": self.language, - "bits": self.bits, - "channel": self.channel, + "format": "raw", + "rate": 16000, + "language": "zh-CN", + "bits": 16, + "channel": 1, + "codec": "raw", }, } - return req - def token_auth(self): - return {"Authorization": f"Bearer; {self.access_token}"} - - def generate_header( - self, - version=PROTOCOL_VERSION, - message_type=CLIENT_FULL_REQUEST, - message_type_specific_flags=NO_SEQUENCE, - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, - reserved_data=0x00, - extension_header: bytes = b"", - ): - """ - 生成协议头: - - 第1字节:高4位:协议版本,低4位:头部大小(单位 4 字节) - - 第2字节:高4位:消息类型,低4位:消息类型特定标志 - - 第3字节:高4位:序列化方式,低4位:压缩方式 - - 第4字节:保留字段 - - 后续:扩展头(如果有) - """ - header = bytearray() - header_size = int(len(extension_header) / 4) + 1 - header.append((version << 4) | header_size) - header.append((message_type << 4) | message_type_specific_flags) - header.append((serial_method << 4) | compression_type) - header.append(reserved_data) - header.extend(extension_header) - return header - - def generate_full_default_header(self): - # full client request 默认头 - return self.generate_header( - version=PROTOCOL_VERSION, - message_type=CLIENT_FULL_REQUEST, - message_type_specific_flags=NO_SEQUENCE, - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, - ) - - def generate_audio_default_header(self): - # 普通音频片段请求 - return self.generate_header( - version=PROTOCOL_VERSION, - message_type=CLIENT_AUDIO_ONLY_REQUEST, - message_type_specific_flags=NO_SEQUENCE, - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, - ) - - def generate_last_audio_default_header(self): - # 最后一个音频片段标志 - return self.generate_header( - version=PROTOCOL_VERSION, - message_type=CLIENT_AUDIO_ONLY_REQUEST, - message_type_specific_flags=NEG_SEQUENCE, # 用 NEG_SEQUENCE 表示结束 - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, - ) - - def _start_monitor_asr_response_thread(self): - # 初始化链接 + async def _send_request( + self, audio_data: List[bytes], segment_size: int + ) -> Optional[str]: + """Send request to Volcano ASR service.""" try: - with self.thread_lock: - if self.conn is None or self.conn.loop is None: - logger.bind(tag=TAG).error( - "无法启动ASR监听线程:conn或loop未初始化" - ) - return + auth_header = {"Authorization": "Bearer; {}".format(self.access_token)} + async with websockets.connect( + self.ws_url, additional_headers=auth_header + ) as websocket: + # Prepare request data + request_params = self._construct_request(str(uuid.uuid4())) + payload_bytes = str.encode(json.dumps(request_params)) + payload_bytes = gzip.compress(payload_bytes) + full_client_request = self._generate_header() + full_client_request.extend( + (len(payload_bytes)).to_bytes(4, "big") + ) # payload size(4 bytes) + full_client_request.extend(payload_bytes) # payload - try: - logger.bind(tag=TAG).info("开始启动ASR监听...") - asyncio.run_coroutine_threadsafe( - self._forward_asr_results(), loop=self.conn.loop - ) - logger.bind(tag=TAG).info("ASR监听已启动") - except Exception as e: - logger.bind(tag=TAG).error(f"启动ASR监听线程失败: {e}") - except Exception as e: - logger.bind(tag=TAG).error(f"ASR监听线程发生未预期的错误: {e}") + # Send header and metadata + # full_client_request + await websocket.send(full_client_request) + res = await websocket.recv() + result = parse_response(res) + if ( + "payload_msg" in result + and result["payload_msg"]["code"] != self.success_code + ): + logger.bind(tag=TAG).error(f"ASR error: {result}") + return None - async def _forward_asr_results(self): - try: - while not self.conn.stop_event.is_set(): - try: - if self.asr_ws is None: - # 检查是否需要重连 - async with self.reconnect_lock: - current_time = asyncio.get_event_loop().time() - if ( - current_time - self.last_reconnect_time - < self.reconnect_cooldown - ): - await asyncio.sleep(1) - continue - - if self.reconnect_count >= self.max_reconnect_count: - logger.bind(tag=TAG).error( - "达到最大重连次数限制,停止重连" - ) - await asyncio.sleep(self.reconnect_cooldown) - self.reconnect_count = 0 - continue - - self.last_reconnect_time = current_time - self.reconnect_count += 1 - logger.bind(tag=TAG).info( - f"尝试重新连接ASR服务... (第{self.reconnect_count}次)" - ) - await self.open_audio_channels(self.conn) - continue - - # 使用锁来确保同一时间只有一个协程在接收数据 - async with self.recv_lock: - response = await self.asr_ws.recv() - result = self.parse_response(response) - - # 检查是否需要重连 - if result.get("need_reconnect", False): - logger.bind(tag=TAG).info( - "检测到需要重连的错误,准备重新连接..." + for seq, (chunk, last) in enumerate( + self.slice_data(audio_data, segment_size), 1 + ): + if last: + audio_only_request = self._generate_header( + message_type=CLIENT_AUDIO_ONLY_REQUEST, + message_type_specific_flags=NEG_SEQUENCE, ) - if self.asr_ws is not None: - try: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning( - f"关闭旧连接时发生错误: {e}" - ) - finally: - self.asr_ws = None - continue + else: + audio_only_request = self._generate_header( + message_type=CLIENT_AUDIO_ONLY_REQUEST + ) + payload_bytes = gzip.compress(chunk) + audio_only_request.extend( + (len(payload_bytes)).to_bytes(4, "big") + ) # payload size(4 bytes) + audio_only_request.extend(payload_bytes) # payload + # Send audio data + await websocket.send(audio_only_request) - if "payload_msg" in result: - if "result" in result["payload_msg"]: - # 检查是否有utterances并且definite为True - utterances = result["payload_msg"]["result"][0].get( - "utterances", [] - ) - for utterance in utterances: - if utterance.get("definite", False): - self.text = utterance["text"] - await self.handle_voice_stop(None) - break + # Receive response + response = await websocket.recv() + result = parse_response(response) - except websockets.ConnectionClosed: - logger.bind(tag=TAG).debug("ASR服务连接已关闭,准备重连...") - # 确保关闭旧连接 - if self.asr_ws is not None: - try: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning(f"关闭旧连接时发生错误: {e}") - finally: - self.asr_ws = None - - # 等待冷却时间 - await asyncio.sleep(self.reconnect_cooldown) - continue - - except Exception as e: - if not self.conn.stop_event.is_set(): - logger.bind(tag=TAG).error(f"ASR监听发生错误: {e}") - await asyncio.sleep(self.retry_delay) - continue + if ( + "payload_msg" in result + and result["payload_msg"]["code"] == self.success_code + ): + if len(result["payload_msg"]["result"]) > 0: + return result["payload_msg"]["result"][0]["text"] + return None + else: + logger.bind(tag=TAG).error(f"ASR error: {result}") + return None except Exception as e: - logger.bind(tag=TAG).error(f"ASR监听线程发生错误: {e}") - # 确保在发生严重错误时也能继续尝试重连 - if not self.conn.stop_event.is_set(): - await asyncio.sleep(self.retry_delay) - await self._forward_asr_results() # 递归重试 + logger.bind(tag=TAG).error(f"ASR request failed: {e}", exc_info=True) + return None - async def speech_to_text(self, opus_data, session_id): - result = self.text - self.text = "" # 清空text - return result, None - - def parse_response(self, res: bytes) -> dict: + @staticmethod + def slice_data(data: bytes, chunk_size: int) -> (list, bool): """ - 解析 ASR 服务返回的二进制响应。 - 根据协议格式解析头部和 payload,若采用 GZIP 压缩则先解压,再根据 JSON 反序列化。 + slice data + :param data: wav data + :param chunk_size: the segment size in one request + :return: segment data, last flag """ - protocol_version = res[0] >> 4 - header_size = res[0] & 0x0F - message_type = res[1] >> 4 - serialization_method = res[2] >> 4 - message_compression = res[2] & 0x0F - payload = res[header_size * 4 :] - result = {} - payload_msg = None - payload_size = 0 - - if message_type == SERVER_FULL_RESPONSE: - payload_size = int.from_bytes(payload[:4], "big", signed=True) - payload_msg = payload[4:] - elif message_type == SERVER_ACK: - seq = int.from_bytes(payload[:4], "big", signed=True) - result["seq"] = seq - if len(payload) >= 8: - payload_size = int.from_bytes(payload[4:8], "big", signed=False) - payload_msg = payload[8:] - elif message_type == SERVER_ERROR_RESPONSE: - code = int.from_bytes(payload[:4], "big", signed=False) - result["code"] = code - payload_size = int.from_bytes(payload[4:8], "big", signed=False) - payload_msg = payload[8:] - - if payload_msg is None: - return result - if message_compression == GZIP_COMPRESSION: - payload_msg = gzip.decompress(payload_msg) - if serialization_method == JSON_SERIALIZATION: - payload_msg = json.loads(payload_msg.decode("utf-8")) + data_len = len(data) + offset = 0 + while offset + chunk_size < data_len: + yield data[offset : offset + chunk_size], False + offset += chunk_size else: - payload_msg = payload_msg.decode("utf-8") - result["payload_msg"] = payload_msg - result["payload_size"] = payload_size + yield data[offset:data_len], True - # 错误码处理 - if "code" in result: - error_code = result["code"] - error_message = "" + async def speech_to_text( + self, opus_data: List[bytes], session_id: str + ) -> Tuple[Optional[str], Optional[str]]: + """将语音数据转换为文本""" - if error_code == 1000: - error_message = "成功" - elif error_code == 1001: - error_message = "请求参数无效:请求参数缺失必需字段/字段值无效/重复请求" - elif error_code == 1002: - error_message = "无访问权限:token无效/过期/无权访问指定服务" - elif error_code == 1003: - error_message = "访问超频:当前appid访问QPS超出设定阈值" - elif error_code == 1004: - error_message = "访问超额:当前appid访问次数超出限制" - elif error_code == 1005: - error_message = "服务器繁忙:服务过载,无法处理当前请求" - elif error_code == 1010: - error_message = "音频过长:音频数据时长超出阈值" - elif error_code == 1011: - error_message = "音频过大:音频数据大小超出阈值" - elif error_code == 1012: - error_message = "音频格式无效:音频header有误/无法进行音频解码" - elif error_code == 1013: - error_message = "音频静音:音频未识别出任何文本结果" - elif error_code >= 1020 and error_code <= 1022: - error_message = "识别相关错误:需要重连" - if error_code == 1020: - error_message = "识别等待超时:等待下一包就绪超时" - elif error_code == 1021: - error_message = "识别处理超时:识别处理过程超时" - elif error_code == 1022: - error_message = "识别错误:识别过程中发生错误" + file_path = None + try: + # 合并所有opus数据包 + if self.audio_format == "pcm": + pcm_data = opus_data else: - error_message = "未知错误:未归类错误" + pcm_data = self.decode_opus(opus_data) + combined_pcm_data = b"".join(pcm_data) - logger.bind(tag=TAG).debug( - f"ASR错误: {error_message} (错误码: {error_code})" - ) + # 判断是否保存为WAV文件 + if self.delete_audio_file: + pass + else: + file_path = self.save_audio_to_file(pcm_data, session_id) - # 如果是识别相关错误,标记需要重连 - if error_code >= 1020 or error_code == 1001: - result["need_reconnect"] = True + # 直接使用PCM数据 + # 计算分段大小 (单声道, 16bit, 16kHz采样率) + size_per_sec = 1 * 2 * 16000 # nchannels * sampwidth * framerate + segment_size = int(size_per_sec * self.seg_duration / 1000) - return result - - async def close_session(self): - """关闭当前会话""" - async with self._session_lock: - if not self._session_started: - logger.bind(tag=TAG).warning("尝试关闭未开始的会话") - return - - if self._session_finished: - logger.bind(tag=TAG).warning( - f"会话 {self._current_session_id} 已经关闭" + # 语音识别 + start_time = time.time() + text = await self._send_request(combined_pcm_data, segment_size) + if text: + logger.bind(tag=TAG).debug( + f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}" ) - return + return text, file_path + return "", file_path - try: - if self.asr_ws is not None: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning(f"关闭WebSocket连接时发生错误: {e}") - finally: - self.asr_ws = None - self._session_finished = True - self._session_started = False - self._current_session_id = None - # 重置重连计数 - self.reconnect_count = 0 - - async def close(self): - """资源清理方法""" - await self.close_session() + except Exception as e: + logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True) + return "", file_path diff --git a/main/xiaozhi-server/core/providers/asr/doubao_stream.py b/main/xiaozhi-server/core/providers/asr/doubao_stream.py new file mode 100644 index 000000000..da4098adf --- /dev/null +++ b/main/xiaozhi-server/core/providers/asr/doubao_stream.py @@ -0,0 +1,534 @@ +import json +import gzip +import uuid +import asyncio +import websockets +import opuslib_next +from core.providers.asr.base import ASRProviderBase +from config.logger import setup_logging +from core.providers.asr.dto.dto import InterfaceType +import threading + +TAG = __name__ +logger = setup_logging() + +CLIENT_FULL_REQUEST = 0b0001 +CLIENT_AUDIO_ONLY_REQUEST = 0b0010 +SERVER_FULL_RESPONSE = 0b1001 +SERVER_ACK = 0b1011 +SERVER_ERROR_RESPONSE = 0b1111 +NO_SEQUENCE = 0b0000 +NEG_SEQUENCE = 0b0010 +JSON_SERIALIZATION = 0b0001 +GZIP_COMPRESSION = 0b0001 +PROTOCOL_VERSION = 0b0001 + + +class ASRProvider(ASRProviderBase): + def __init__(self, config, delete_audio_file): + super().__init__() + self.interface_type = InterfaceType.STREAM + self.config = config + self.text = "" + self.max_retries = 3 + self.retry_delay = 2 # 重试延迟秒数 + self.recv_lock = asyncio.Lock() # 添加接收锁 + self.reconnect_lock = asyncio.Lock() # 添加重连锁 + self.last_reconnect_time = 0 # 上次重连时间 + self.reconnect_cooldown = 1 # 增加重连冷却时间到10秒 + self.reconnect_count = 0 # 当前重连次数 + self.max_reconnect_count = 3 # 减少最大重连次数到3次 + self.asr_thread = None # ASR监听线程 + self.thread_lock = threading.Lock() # 线程管理锁 + self.is_reconnecting = False # 添加重连状态标志 + + # 添加会话管理相关属性 + self._session_lock = asyncio.Lock() # 会话操作的并发锁 + self._current_session_id = None # 当前会话ID + self._session_started = False # 会话是否已开始 + self._session_finished = False # 会话是否已结束 + self._session_close_event = asyncio.Event() # 添加会话关闭事件 + + self.appid = str(config.get("appid")) + self.cluster = config.get("cluster") + self.access_token = config.get("access_token") + self.boosting_table_name = config.get("boosting_table_name", "") + self.correct_table_name = config.get("correct_table_name", "") + self.output_dir = config.get("output_dir", "temp/") + self.delete_audio_file = delete_audio_file + + self.ws_url = "wss://openspeech.bytedance.com/api/v2/asr" + self.uid = config.get("uid", "streaming_asr_service") + self.workflow = config.get( + "workflow", "audio_in,resample,partition,vad,fe,decode,itn,nlu_punctuate" + ) + self.result_type = config.get("result_type", "single") + self.format = config.get("format", "raw") + self.codec = config.get("codec", "pcm") + self.rate = config.get("sample_rate", 16000) + self.language = config.get("language", "zh-CN") + self.bits = config.get("bits", 16) + self.channel = config.get("channel", 1) + self.auth_method = config.get("auth_method", "token") + self.secret = config.get("secret", "access_secret") + self.decoder = opuslib_next.Decoder(16000, 1) + self.asr_ws = None + self.forward_task = None + self.conn = None + + ################################################################################### + # 豆包流式ASR重写父类的方法--开始 + ################################################################################### + async def open_audio_channels(self, conn): + await super().open_audio_channels(conn) + + async with self._session_lock: + # 如果正在重连,等待重连完成 + if self.is_reconnecting: + logger.bind(tag=TAG).info("等待当前重连完成...") + await self._session_close_event.wait() + self._session_close_event.clear() + + # 如果已有会话未结束,先关闭它 + if self._session_started and not self._session_finished: + logger.bind(tag=TAG).warning( + f"发现未关闭的会话 {self._current_session_id},正在关闭..." + ) + if self.asr_ws is not None: + try: + await self.asr_ws.close() + except Exception as e: + logger.bind(tag=TAG).warning(f"关闭旧连接时发生错误: {e}") + finally: + self.asr_ws = None + self._session_finished = True + self._session_close_event.set() + + # 重置会话状态 + self._current_session_id = str(uuid.uuid4()) + self._session_started = True + self._session_finished = False + self.is_reconnecting = True + + try: + retry_count = 0 + while retry_count < self.max_retries: + try: + headers = ( + self.token_auth() if self.auth_method == "token" else None + ) + self.asr_ws = await websockets.connect( + self.ws_url, + additional_headers=headers, + max_size=1000000000, + ping_interval=None, + ping_timeout=None, + close_timeout=10, + ) + + # 发送初始化请求 + request_params = self.construct_request( + self._current_session_id + ) + try: + payload_bytes = str.encode(json.dumps(request_params)) + payload_bytes = gzip.compress(payload_bytes) + full_client_request = self.generate_header() + full_client_request.extend( + (len(payload_bytes)).to_bytes(4, "big") + ) + full_client_request.extend(payload_bytes) + await self.asr_ws.send(full_client_request) + except Exception as e: + logger.bind(tag=TAG).error(f"发送初始化请求失败: {e}") + raise e + + # 等待初始化响应 + try: + init_res = await self.asr_ws.recv() + self.parse_response(init_res) + except Exception as e: + logger.bind(tag=TAG).error(f"ASR服务初始化失败: {e}") + raise e + + # 启动接收ASR结果的异步任务 + with self.thread_lock: + if ( + self.asr_thread is None + or not self.asr_thread.is_alive() + ): + logger.bind(tag=TAG).info("创建新的ASR监听线程...") + self.asr_thread = threading.Thread( + target=self._start_monitor_asr_response_thread, + daemon=True, + ) + self.asr_thread.start() + # 等待一小段时间确保线程启动 + await asyncio.sleep(0.1) + if not self.asr_thread.is_alive(): + logger.bind(tag=TAG).error("ASR监听线程启动失败") + raise Exception("ASR监听线程启动失败") + logger.bind(tag=TAG).info("ASR监听线程已启动") + return + + except websockets.exceptions.WebSocketException as e: + retry_count += 1 + if retry_count < self.max_retries: + logger.bind(tag=TAG).warning( + f"WebSocket连接失败,正在进行第{retry_count}次重试: {e}" + ) + await asyncio.sleep(self.retry_delay) + else: + logger.bind(tag=TAG).warning( + f"WebSocket连接失败,已达到最大重试次数: {e}" + ) + raise + except Exception as e: + logger.bind(tag=TAG).error(f"WebSocket连接发生未知错误: {e}") + raise + finally: + self.is_reconnecting = False + self._session_close_event.set() + + async def receive_audio(self, audio, _): + if not isinstance(audio, bytes): + return + + try: + # 解码opus得到PCM数据 + pcm_frame = self.decoder.decode(audio, 960) + payload = gzip.compress(pcm_frame) + audio_request = bytearray(self.generate_audio_default_header()) + audio_request.extend(len(payload).to_bytes(4, "big")) + audio_request.extend(payload) + if self.asr_ws: + await self.asr_ws.send(audio_request) + except Exception as e: + logger.bind(tag=TAG).debug(f"发送音频数据时发生错误: {e}") + + ################################################################################### + # 豆包流式ASR重写父类的方法--结束 + ################################################################################### + + def construct_request(self, reqid): + req = { + "app": { + "appid": self.appid, + "cluster": self.cluster, + "token": self.access_token, + }, + "user": {"uid": self.uid}, + "request": { + "reqid": reqid, + "workflow": self.workflow, + "show_utterances": True, + "result_type": self.result_type, + "sequence": 1, + "boosting_table_name": self.boosting_table_name, + "correct_table_name": self.correct_table_name, + }, + "audio": { + "format": self.format, + "codec": self.codec, + "rate": self.rate, + "language": self.language, + "bits": self.bits, + "channel": self.channel, + }, + } + return req + + def token_auth(self): + return {"Authorization": f"Bearer; {self.access_token}"} + + def generate_header( + self, + version=PROTOCOL_VERSION, + message_type=CLIENT_FULL_REQUEST, + message_type_specific_flags=NO_SEQUENCE, + serial_method=JSON_SERIALIZATION, + compression_type=GZIP_COMPRESSION, + reserved_data=0x00, + extension_header: bytes = b"", + ): + """ + 生成协议头: + - 第1字节:高4位:协议版本,低4位:头部大小(单位 4 字节) + - 第2字节:高4位:消息类型,低4位:消息类型特定标志 + - 第3字节:高4位:序列化方式,低4位:压缩方式 + - 第4字节:保留字段 + - 后续:扩展头(如果有) + """ + header = bytearray() + header_size = int(len(extension_header) / 4) + 1 + header.append((version << 4) | header_size) + header.append((message_type << 4) | message_type_specific_flags) + header.append((serial_method << 4) | compression_type) + header.append(reserved_data) + header.extend(extension_header) + return header + + def generate_full_default_header(self): + # full client request 默认头 + return self.generate_header( + version=PROTOCOL_VERSION, + message_type=CLIENT_FULL_REQUEST, + message_type_specific_flags=NO_SEQUENCE, + serial_method=JSON_SERIALIZATION, + compression_type=GZIP_COMPRESSION, + ) + + def generate_audio_default_header(self): + # 普通音频片段请求 + return self.generate_header( + version=PROTOCOL_VERSION, + message_type=CLIENT_AUDIO_ONLY_REQUEST, + message_type_specific_flags=NO_SEQUENCE, + serial_method=JSON_SERIALIZATION, + compression_type=GZIP_COMPRESSION, + ) + + def generate_last_audio_default_header(self): + # 最后一个音频片段标志 + return self.generate_header( + version=PROTOCOL_VERSION, + message_type=CLIENT_AUDIO_ONLY_REQUEST, + message_type_specific_flags=NEG_SEQUENCE, # 用 NEG_SEQUENCE 表示结束 + serial_method=JSON_SERIALIZATION, + compression_type=GZIP_COMPRESSION, + ) + + def _start_monitor_asr_response_thread(self): + # 初始化链接 + try: + with self.thread_lock: + if self.conn is None or self.conn.loop is None: + logger.bind(tag=TAG).error( + "无法启动ASR监听线程:conn或loop未初始化" + ) + return + + try: + logger.bind(tag=TAG).info("开始启动ASR监听...") + asyncio.run_coroutine_threadsafe( + self._forward_asr_results(), loop=self.conn.loop + ) + logger.bind(tag=TAG).info("ASR监听已启动") + except Exception as e: + logger.bind(tag=TAG).error(f"启动ASR监听线程失败: {e}") + except Exception as e: + logger.bind(tag=TAG).error(f"ASR监听线程发生未预期的错误: {e}") + + async def _forward_asr_results(self): + try: + while not self.conn.stop_event.is_set(): + try: + if self.asr_ws is None: + # 检查是否需要重连 + async with self.reconnect_lock: + current_time = asyncio.get_event_loop().time() + if ( + current_time - self.last_reconnect_time + < self.reconnect_cooldown + ): + await asyncio.sleep(1) + continue + + if self.reconnect_count >= self.max_reconnect_count: + logger.bind(tag=TAG).error( + "达到最大重连次数限制,停止重连" + ) + await asyncio.sleep(self.reconnect_cooldown) + self.reconnect_count = 0 + continue + + self.last_reconnect_time = current_time + self.reconnect_count += 1 + logger.bind(tag=TAG).info( + f"尝试重新连接ASR服务... (第{self.reconnect_count}次)" + ) + await self.open_audio_channels(self.conn) + continue + + # 使用锁来确保同一时间只有一个协程在接收数据 + async with self.recv_lock: + response = await self.asr_ws.recv() + result = self.parse_response(response) + + # 检查是否需要重连 + if result.get("need_reconnect", False): + logger.bind(tag=TAG).info( + "检测到需要重连的错误,准备重新连接..." + ) + if self.asr_ws is not None: + try: + await self.asr_ws.close() + except Exception as e: + logger.bind(tag=TAG).warning( + f"关闭旧连接时发生错误: {e}" + ) + finally: + self.asr_ws = None + continue + + if "payload_msg" in result: + if "result" in result["payload_msg"]: + # 检查是否有utterances并且definite为True + utterances = result["payload_msg"]["result"][0].get( + "utterances", [] + ) + for utterance in utterances: + if utterance.get("definite", False): + self.text = utterance["text"] + await self.handle_voice_stop(None) + break + + except websockets.ConnectionClosed: + logger.bind(tag=TAG).debug("ASR服务连接已关闭,准备重连...") + # 确保关闭旧连接 + if self.asr_ws is not None: + try: + await self.asr_ws.close() + except Exception as e: + logger.bind(tag=TAG).warning(f"关闭旧连接时发生错误: {e}") + finally: + self.asr_ws = None + + # 等待冷却时间 + await asyncio.sleep(self.reconnect_cooldown) + continue + + except Exception as e: + if not self.conn.stop_event.is_set(): + logger.bind(tag=TAG).error(f"ASR监听发生错误: {e}") + await asyncio.sleep(self.retry_delay) + continue + + except Exception as e: + logger.bind(tag=TAG).error(f"ASR监听线程发生错误: {e}") + # 确保在发生严重错误时也能继续尝试重连 + if not self.conn.stop_event.is_set(): + await asyncio.sleep(self.retry_delay) + await self._forward_asr_results() # 递归重试 + + async def speech_to_text(self, opus_data, session_id): + result = self.text + self.text = "" # 清空text + return result, None + + def parse_response(self, res: bytes) -> dict: + """ + 解析 ASR 服务返回的二进制响应。 + 根据协议格式解析头部和 payload,若采用 GZIP 压缩则先解压,再根据 JSON 反序列化。 + """ + protocol_version = res[0] >> 4 + header_size = res[0] & 0x0F + message_type = res[1] >> 4 + serialization_method = res[2] >> 4 + message_compression = res[2] & 0x0F + payload = res[header_size * 4 :] + result = {} + payload_msg = None + payload_size = 0 + + if message_type == SERVER_FULL_RESPONSE: + payload_size = int.from_bytes(payload[:4], "big", signed=True) + payload_msg = payload[4:] + elif message_type == SERVER_ACK: + seq = int.from_bytes(payload[:4], "big", signed=True) + result["seq"] = seq + if len(payload) >= 8: + payload_size = int.from_bytes(payload[4:8], "big", signed=False) + payload_msg = payload[8:] + elif message_type == SERVER_ERROR_RESPONSE: + code = int.from_bytes(payload[:4], "big", signed=False) + result["code"] = code + payload_size = int.from_bytes(payload[4:8], "big", signed=False) + payload_msg = payload[8:] + + if payload_msg is None: + return result + if message_compression == GZIP_COMPRESSION: + payload_msg = gzip.decompress(payload_msg) + if serialization_method == JSON_SERIALIZATION: + payload_msg = json.loads(payload_msg.decode("utf-8")) + else: + payload_msg = payload_msg.decode("utf-8") + result["payload_msg"] = payload_msg + result["payload_size"] = payload_size + + # 错误码处理 + if "code" in result: + error_code = result["code"] + error_message = "" + + if error_code == 1000: + error_message = "成功" + elif error_code == 1001: + error_message = "请求参数无效:请求参数缺失必需字段/字段值无效/重复请求" + elif error_code == 1002: + error_message = "无访问权限:token无效/过期/无权访问指定服务" + elif error_code == 1003: + error_message = "访问超频:当前appid访问QPS超出设定阈值" + elif error_code == 1004: + error_message = "访问超额:当前appid访问次数超出限制" + elif error_code == 1005: + error_message = "服务器繁忙:服务过载,无法处理当前请求" + elif error_code == 1010: + error_message = "音频过长:音频数据时长超出阈值" + elif error_code == 1011: + error_message = "音频过大:音频数据大小超出阈值" + elif error_code == 1012: + error_message = "音频格式无效:音频header有误/无法进行音频解码" + elif error_code == 1013: + error_message = "音频静音:音频未识别出任何文本结果" + elif error_code >= 1020 and error_code <= 1022: + error_message = "识别相关错误:需要重连" + if error_code == 1020: + error_message = "识别等待超时:等待下一包就绪超时" + elif error_code == 1021: + error_message = "识别处理超时:识别处理过程超时" + elif error_code == 1022: + error_message = "识别错误:识别过程中发生错误" + else: + error_message = "未知错误:未归类错误" + + logger.bind(tag=TAG).debug( + f"ASR错误: {error_message} (错误码: {error_code})" + ) + + # 如果是识别相关错误,标记需要重连 + if error_code >= 1020 or error_code == 1001: + result["need_reconnect"] = True + + return result + + async def close_session(self): + """关闭当前会话""" + async with self._session_lock: + if not self._session_started: + logger.bind(tag=TAG).warning("尝试关闭未开始的会话") + return + + if self._session_finished: + logger.bind(tag=TAG).warning( + f"会话 {self._current_session_id} 已经关闭" + ) + return + + try: + if self.asr_ws is not None: + await self.asr_ws.close() + except Exception as e: + logger.bind(tag=TAG).warning(f"关闭WebSocket连接时发生错误: {e}") + finally: + self.asr_ws = None + self._session_finished = True + self._session_started = False + self._current_session_id = None + # 重置重连计数 + self.reconnect_count = 0 + + async def close(self): + """资源清理方法""" + await self.close_session() diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index 9c094f24d..70c68ee78 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -52,6 +52,7 @@ class TTSProviderBase(ABC): ) self.first_sentence_punctuations = ( ",", + "~", "~", "、", ",", From 109811199d89fb9d33258c9ebcffbde472446aac Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 3 Jun 2025 17:32:18 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E6=99=BA?= =?UTF-8?q?=E6=8E=A7=E5=8F=B0=E5=8C=BA=E5=88=86=E8=B1=86=E5=8C=85ASR?= =?UTF-8?q?=E6=8C=89=E6=AC=A1=E6=94=B6=E8=B4=B9=E5=92=8C=E6=8C=89=E6=97=B6?= =?UTF-8?q?=E6=94=B6=E8=B4=B9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/db/changelog/db.changelog-master.yaml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml index 437e8ea65..1bd070aa3 100755 --- a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml @@ -176,4 +176,11 @@ databaseChangeLog: changes: - sqlFile: encoding: utf8 - path: classpath:db/changelog/202506010920.sql \ No newline at end of file + path: classpath:db/changelog/202506010920.sql + - changeSet: + id: 202506031639 + author: hrz + changes: + - sqlFile: + encoding: utf8 + path: classpath:db/changelog/202506031639.sql \ No newline at end of file From 74226581b764d58a7f18e3ffc9aaf5f96dadbf6e Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 3 Jun 2025 23:09:27 +0800 Subject: [PATCH 09/12] =?UTF-8?q?add:=E5=A2=9E=E5=8A=A0=E5=8D=83=E9=97=AE?= =?UTF-8?q?=E6=94=B6=E8=B4=B9=E8=A7=86=E8=A7=89=E6=A8=A1=E5=9E=8B=EF=BC=8C?= =?UTF-8?q?=E9=80=9F=E5=BA=A6=E6=9B=B4=E7=A8=B3=E5=AE=9A=E4=B8=80=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../resources/db/changelog/202506032232.sql | 14 ++ .../db/changelog/db.changelog-master.yaml | 9 +- main/xiaozhi-server/config.yaml | 6 + .../core/providers/vllm/openai.py | 4 +- .../xiaozhi-server/performance_tester_vllm.py | 189 ++++++++++++++++++ 6 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 main/manager-api/src/main/resources/db/changelog/202506032232.sql create mode 100644 main/xiaozhi-server/performance_tester_vllm.py diff --git a/README.md b/README.md index 43a8acd44..e43554ce1 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ |---------|---------|------| | ASR(语音识别) | FunASR(本地) | ✅DoubaoASR(火山流式语音识别) | | LLM(大模型) | ChatGLMLLM(智谱glm-4-flash) | ✅DoubaoLLM(火山doubao-1-5-pro-32k-250115) | -| VLLM(视觉大模型) | ChatGLMVLLM(智谱glm-4v-flash) | ✅ChatGLMVLLM(智谱glm-4v-flash) | +| VLLM(视觉大模型) | ChatGLMVLLM(智谱glm-4v-flash) | ✅QwenVLVLLM(千问qwen2.5-vl-3b-instructh) | | TTS(语音合成) | EdgeTTS(微软语音) | ✅HuoshanDoubleStreamTTS(火山双流式语音合成) | | Intent(意图识别) | function_call(函数调用) | ✅function_call(函数调用) | | Memory(记忆功能) | mem_local_short(本地短期记忆) | ✅mem_local_short(本地短期记忆) | diff --git a/main/manager-api/src/main/resources/db/changelog/202506032232.sql b/main/manager-api/src/main/resources/db/changelog/202506032232.sql new file mode 100644 index 000000000..616119815 --- /dev/null +++ b/main/manager-api/src/main/resources/db/changelog/202506032232.sql @@ -0,0 +1,14 @@ +-- VLLM模型配置 +delete from `ai_model_config` where id = 'VLLM_QwenVLVLLM'; +INSERT INTO `ai_model_config` VALUES ('VLLM_QwenVLVLLM', 'VLLM', 'QwenVLVLLM', '千问视觉模型', 0, 1, '{\"type\": \"openai\", \"model_name\": \"qwen2.5-vl-3b-instruct\", \"base_url\": \"https://dashscope.aliyuncs.com/compatible-mode/v1\", \"api_key\": \"你的api_key\"}', NULL, NULL, 2, NULL, NULL, NULL, NULL); + +-- 更新文档 +UPDATE `ai_model_config` SET +`doc_link` = 'https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=https%3A%2F%2Fhelp.aliyun.com%2Fdocument_detail%2F2845564.html&renderType=iframe', +`remark` = '千问视觉模型配置说明: +1. 访问 https://bailian.console.aliyun.com/?tab=model#/api-key +2. 注册并获取API密钥 +3. 填入配置文件中' WHERE `id` = 'VLLM_QwenVLVLLM'; + +-- 删除参数,这两个参数已挪至python配置文件 +delete from `sys_params` where id in (113,114); diff --git a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml index 1bd070aa3..0583fce9e 100755 --- a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml @@ -183,4 +183,11 @@ databaseChangeLog: changes: - sqlFile: encoding: utf8 - path: classpath:db/changelog/202506031639.sql \ No newline at end of file + path: classpath:db/changelog/202506031639.sql + - changeSet: + id: 202506032232 + author: hrz + changes: + - sqlFile: + encoding: utf8 + path: classpath:db/changelog/202506032232.sql \ No newline at end of file diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 179d7204a..1bc151f0a 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -467,6 +467,12 @@ VLLM: model_name: glm-4v-flash # 智谱AI的视觉模型 url: https://open.bigmodel.cn/api/paas/v4/ api_key: 你的api_key + QwenVLVLLM: + type: openai + model_name: qwen2.5-vl-3b-instruct + url: https://dashscope.aliyuncs.com/compatible-mode/v1 + # 可在这里找到你的api key https://bailian.console.aliyun.com/?apiKey=1#/api-key + api_key: 你的api_key TTS: # 当前支持的type为edge、doubao,可自行适配 EdgeTTS: diff --git a/main/xiaozhi-server/core/providers/vllm/openai.py b/main/xiaozhi-server/core/providers/vllm/openai.py index 5eb8124bf..6ef89bcac 100644 --- a/main/xiaozhi-server/core/providers/vllm/openai.py +++ b/main/xiaozhi-server/core/providers/vllm/openai.py @@ -46,7 +46,9 @@ class VLLMProvider(VLLMProviderBase): {"type": "text", "text": question}, { "type": "image_url", - "image_url": {"url": f"{base64_image}"}, + "image_url": { + "url": f"data:image/jpeg;base64,{base64_image}" + }, }, ], } diff --git a/main/xiaozhi-server/performance_tester_vllm.py b/main/xiaozhi-server/performance_tester_vllm.py new file mode 100644 index 000000000..4dafddcc1 --- /dev/null +++ b/main/xiaozhi-server/performance_tester_vllm.py @@ -0,0 +1,189 @@ +import time +import asyncio +import logging +import statistics +import base64 +from typing import Dict +from tabulate import tabulate +from config.settings import load_config +from core.utils.vllm import create_instance + +# 设置全局日志级别为WARNING,抑制INFO级别日志 +logging.basicConfig(level=logging.WARNING) + + +class AsyncVisionPerformanceTester: + def __init__(self): + self.config = load_config() + self.test_images = [ + "../../docs/images/demo1.png", + "../../docs/images/demo2.png", + ] + self.test_questions = [ + "这张图片里有什么?", + "请详细描述这张图片的内容", + ] + + # 加载测试图片 + self.results = {"vllm": {}} + + async def _test_vllm(self, vllm_name: str, config: Dict) -> Dict: + """异步测试单个视觉大模型性能""" + try: + # 检查API密钥配置 + if "api_key" in config and any( + x in config["api_key"] for x in ["你的", "placeholder", "sk-xxx"] + ): + print(f"⏭️ VLLM {vllm_name} 未配置api_key,已跳过") + return {"name": vllm_name, "type": "vllm", "errors": 1} + + # 获取实际类型(兼容旧配置) + module_type = config.get("type", vllm_name) + vllm = create_instance(module_type, config) + + print(f"🖼️ 测试 VLLM: {vllm_name}") + + # 创建所有测试任务 + test_tasks = [] + for question in self.test_questions: + for image in self.test_images: + test_tasks.append( + self._test_single_vision(vllm_name, vllm, question, image) + ) + + # 并发执行所有测试 + test_results = await asyncio.gather(*test_tasks) + + # 处理结果 + valid_results = [r for r in test_results if r is not None] + if not valid_results: + print(f"⚠️ {vllm_name} 无有效数据,可能配置错误") + return {"name": vllm_name, "type": "vllm", "errors": 1} + + response_times = [r["response_time"] for r in valid_results] + + # 过滤异常数据 + mean = statistics.mean(response_times) + stdev = statistics.stdev(response_times) if len(response_times) > 1 else 0 + filtered_times = [t for t in response_times if t <= mean + 3 * stdev] + + if len(filtered_times) < len(test_tasks) * 0.5: + print(f"⚠️ {vllm_name} 有效数据不足,可能网络不稳定") + return {"name": vllm_name, "type": "vllm", "errors": 1} + + return { + "name": vllm_name, + "type": "vllm", + "avg_response": sum(response_times) / len(response_times), + "std_response": ( + statistics.stdev(response_times) if len(response_times) > 1 else 0 + ), + "errors": 0, + } + + except Exception as e: + print(f"⚠️ VLLM {vllm_name} 测试失败: {str(e)}") + return {"name": vllm_name, "type": "vllm", "errors": 1} + + async def _test_single_vision( + self, vllm_name: str, vllm, question: str, image: str + ) -> Dict: + """测试单个视觉问题的性能""" + try: + print(f"📝 {vllm_name} 开始测试: {question[:20]}...") + start_time = time.time() + + # 读取图片并转换为base64 + with open(image, "rb") as image_file: + image_data = image_file.read() + image_base64 = base64.b64encode(image_data).decode("utf-8") + + # 直接获取响应 + response = vllm.response(question, image_base64) + response_time = time.time() - start_time + print(f"✓ {vllm_name} 完成响应: {response_time:.3f}s") + + return { + "name": vllm_name, + "type": "vllm", + "response_time": response_time, + } + except Exception as e: + print(f"⚠️ {vllm_name} 测试失败: {str(e)}") + return None + + def _print_results(self): + """打印测试结果""" + vllm_table = [] + for name, data in self.results["vllm"].items(): + if data["errors"] == 0: + stability = data["std_response"] / data["avg_response"] + vllm_table.append( + [ + name, + f"{data['avg_response']:.3f}秒", + f"{stability:.3f}", + ] + ) + + if vllm_table: + print("\n视觉大模型性能排行:\n") + print( + tabulate( + vllm_table, + headers=["模型名称", "响应耗时", "稳定性"], + tablefmt="github", + colalign=("left", "right", "right"), + disable_numparse=True, + ) + ) + else: + print("\n⚠️ 没有可用的视觉大模型进行测试。") + + async def run(self): + """执行全量异步测试""" + print("🔍 开始筛选可用视觉大模型...") + + if not self.test_images: + print(f"\n⚠️ {self.image_root} 路径下没有图片文件,无法进行测试") + return + + # 创建所有测试任务 + all_tasks = [] + + # VLLM测试任务 + if self.config.get("VLLM") is not None: + for vllm_name, config in self.config.get("VLLM", {}).items(): + if "api_key" in config and any( + x in config["api_key"] for x in ["你的", "placeholder", "sk-xxx"] + ): + print(f"⏭️ VLLM {vllm_name} 未配置api_key,已跳过") + continue + print(f"🖼️ 添加VLLM测试任务: {vllm_name}") + all_tasks.append(self._test_vllm(vllm_name, config)) + + print(f"\n✅ 找到 {len(all_tasks)} 个可用视觉大模型") + print(f"✅ 使用 {len(self.test_images)} 张测试图片") + print(f"✅ 使用 {len(self.test_questions)} 个测试问题") + print("\n⏳ 开始并发测试所有模型...\n") + + # 并发执行所有测试任务 + all_results = await asyncio.gather(*all_tasks, return_exceptions=True) + + # 处理结果 + for result in all_results: + if isinstance(result, dict) and result["errors"] == 0: + self.results["vllm"][result["name"]] = result + + # 打印结果 + print("\n📊 生成测试报告...") + self._print_results() + + +async def main(): + tester = AsyncVisionPerformanceTester() + await tester.run() + + +if __name__ == "__main__": + asyncio.run(main()) From e52601a584afd9c03e30f9dad2e7e1fe167bfcdf Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 3 Jun 2025 23:29:39 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=86=E8=A7=89?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E5=93=8D=E5=BA=94=E6=B5=8B=E8=AF=95=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index e43554ce1..b1561bbc9 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,17 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ | Intent(意图识别) | function_call(函数调用) | ✅function_call(函数调用) | | Memory(记忆功能) | mem_local_short(本地短期记忆) | ✅mem_local_short(本地短期记忆) | +#### 🔧 测试工具 +本项目提供以下测试工具,帮助您验证系统和选择合适的模型: + +| 工具名称 | 位置 | 使用方法 | 功能说明 | +|---------|------|---------|---------| +| test_page | `main/xiaozhi-server/test/test_page.html` | 使用谷歌浏览器直接打开 | 测试音频播放和接收功能,验证Python端音频处理是否正常 | +| performance_tester | `main/xiaozhi-server/performance_tester.py` | 执行 `python performance_tester.py` | 测试ASR(语音识别)、LLM(大模型)、TTS(语音合成)三个核心模块的响应速度 | +| performance_tester_vllm | `main/xiaozhi-server/performance_tester_vllm.py` | 执行 `python performance_tester_vllm.py` | 测试VLLM(视觉模型)的响应速度 | + +> 💡 提示:测试模型速度时,只会测试配置了密钥的模型。 + --- ## 功能清单 ✨ ### 已实现 ✅ @@ -251,6 +262,16 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ --- +### VLLM 视觉模型 + +| 使用方式 | 支持平台 | 免费平台 | +|:---:|:---:|:---:| +| openai 接口调用 | 阿里百炼、智谱ChatGLMVLLM | 智谱ChatGLMVLLM | + +实际上,任何支持 openai 接口调用的 VLLM 均可接入使用。 + +--- + ### TTS 语音合成 | 使用方式 | 支持平台 | 免费平台 | From d06e297c2d980f10d8e2cb3ed4054d03784ab08e Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 4 Jun 2025 11:41:04 +0800 Subject: [PATCH 11/12] =?UTF-8?q?update:ASR=E5=8A=A0=E5=85=A5=E9=98=9F?= =?UTF-8?q?=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 12 ++- .../xiaozhi-server/core/handle/abortHandle.py | 2 - .../xiaozhi-server/core/handle/helloHandle.py | 2 - .../core/handle/intentHandler.py | 1 + .../core/handle/receiveAudioHandle.py | 6 +- .../core/handle/sendAudioHandle.py | 3 +- .../core/providers/asr/aliyun.py | 4 +- .../core/providers/asr/baidu.py | 4 +- .../xiaozhi-server/core/providers/asr/base.py | 80 ++++++++++++------- .../core/providers/asr/doubao.py | 4 +- .../core/providers/asr/doubao_stream.py | 4 +- .../core/providers/asr/fun_local.py | 4 +- .../core/providers/asr/fun_server.py | 4 +- .../core/providers/asr/sherpa_onnx_local.py | 4 +- .../core/providers/asr/tencent.py | 4 +- .../providers/intent/intent_llm/intent_llm.py | 7 +- main/xiaozhi-server/performance_tester.py | 6 +- 17 files changed, 85 insertions(+), 66 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 238eb562c..aa1aaf422 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -35,7 +35,6 @@ from plugins_func.loadplugins import auto_import_modules from plugins_func.register import Action, ActionResponse from core.auth import AuthMiddleware, AuthenticationError from config.config_loader import get_private_config_from_api -from core.handle.receiveAudioHandle import handleAudioMessage from core.providers.tts.dto.dto import ContentType, TTSMessageDTO, SentenceType from config.logger import setup_logging, build_module_string, update_module_string from config.manage_api_client import DeviceNotFoundException, DeviceBindException @@ -81,6 +80,7 @@ class ConnectionHandler: self.welcome_msg = None self.max_output_size = 0 self.chat_history_conf = 0 + self.audio_format = "opus" # 客户端状态相关 self.client_abort = False @@ -117,7 +117,10 @@ class ConnectionHandler: self.client_voice_stop = False # asr相关变量 + # 因为实际部署时可能会用到公共的本地ASR,不能把变量暴露给公共ASR + # 所以涉及到ASR的变量,需要在这里定义,属于connection的私有变量 self.asr_audio = [] + self.asr_audio_queue = queue.Queue() # llm相关变量 self.llm_finish_task = True @@ -146,7 +149,6 @@ class ConnectionHandler: int(self.config.get("close_connection_no_voice_time", 120)) + 60 ) # 在原来第一道关闭的基础上加60秒,进行二道关闭 - self.audio_format = "opus" # {"mcp":true} 表示启用MCP功能 self.features = None @@ -254,7 +256,11 @@ class ConnectionHandler: if isinstance(message, str): await handleTextMessage(self, message) elif isinstance(message, bytes): - await handleAudioMessage(self, message) + if self.vad is None: + return + if self.asr is None: + return + self.asr_audio_queue.put(message) async def handle_restart(self, message): """处理服务器重启请求""" diff --git a/main/xiaozhi-server/core/handle/abortHandle.py b/main/xiaozhi-server/core/handle/abortHandle.py index fe2715114..ce6f22b6c 100644 --- a/main/xiaozhi-server/core/handle/abortHandle.py +++ b/main/xiaozhi-server/core/handle/abortHandle.py @@ -1,6 +1,4 @@ import json -import queue -from config.logger import setup_logging TAG = __name__ diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index 6b8cb4fd4..8434cf22d 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -33,8 +33,6 @@ async def handleHelloMessage(conn, msg_json): format = audio_params.get("format") conn.logger.bind(tag=TAG).info(f"客户端音频格式: {format}") conn.audio_format = format - if conn.asr is not None: - conn.asr.set_audio_format(format) conn.welcome_msg["audio_params"] = audio_params features = msg_json.get("features") if features: diff --git a/main/xiaozhi-server/core/handle/intentHandler.py b/main/xiaozhi-server/core/handle/intentHandler.py index cb2e3c695..35660bdf2 100644 --- a/main/xiaozhi-server/core/handle/intentHandler.py +++ b/main/xiaozhi-server/core/handle/intentHandler.py @@ -96,6 +96,7 @@ async def process_intent_result(conn, intent_result, original_text): } await send_stt_message(conn, original_text) + conn.client_abort = False # 使用executor执行函数调用和结果处理 def process_function_call(): diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index a5947e5a1..ddce9d7ad 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -10,10 +10,6 @@ TAG = __name__ async def handleAudioMessage(conn, audio): - if conn.vad is None: - return - if conn.asr is None or not hasattr(conn.asr, "conn") or conn.asr.conn is None: - return # 当前片段是否有人说话 have_voice = conn.vad.is_vad(conn, audio) if have_voice: @@ -22,7 +18,7 @@ async def handleAudioMessage(conn, audio): # 设备长时间空闲检测,用于say goodbye await no_voice_close_connect(conn, have_voice) # 接收音频 - await conn.asr.receive_audio(audio, have_voice) + await conn.asr.receive_audio(conn, audio, have_voice) async def startToChat(conn, text): diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 85a71c8ee..ea81b7eda 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -89,8 +89,7 @@ async def sendAudio(conn, audios, pre_buffer=True): # 播放剩余音频帧 for opus_packet in remaining_audios: if conn.client_abort: - conn.client_abort = False - return + break # 每分钟重置一次计时器 if time.perf_counter() - last_reset_time > 60: diff --git a/main/xiaozhi-server/core/providers/asr/aliyun.py b/main/xiaozhi-server/core/providers/asr/aliyun.py index 7ed24c006..36f417457 100644 --- a/main/xiaozhi-server/core/providers/asr/aliyun.py +++ b/main/xiaozhi-server/core/providers/asr/aliyun.py @@ -213,7 +213,7 @@ class ASRProvider(ASRProviderBase): return None async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """将语音数据转换为文本""" if self._is_token_expired(): @@ -223,7 +223,7 @@ class ASRProvider(ASRProviderBase): file_path = None try: # 解码Opus为PCM - if self.audio_format == "pcm": + if audio_format == "pcm": pcm_data = opus_data else: pcm_data = self.decode_opus(opus_data) diff --git a/main/xiaozhi-server/core/providers/asr/baidu.py b/main/xiaozhi-server/core/providers/asr/baidu.py index 7df94ff31..aa8ea9015 100644 --- a/main/xiaozhi-server/core/providers/asr/baidu.py +++ b/main/xiaozhi-server/core/providers/asr/baidu.py @@ -30,7 +30,7 @@ class ASRProvider(ASRProviderBase): os.makedirs(self.output_dir, exist_ok=True) async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """将语音数据转换为文本""" if not opus_data: @@ -45,7 +45,7 @@ class ASRProvider(ASRProviderBase): return None, file_path # 将Opus音频数据解码为PCM - if self.audio_format == "pcm": + if audio_format == "pcm": pcm_data = opus_data else: pcm_data = self.decode_opus(opus_data) diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py index 707f80bb3..4267c2ff4 100644 --- a/main/xiaozhi-server/core/providers/asr/base.py +++ b/main/xiaozhi-server/core/providers/asr/base.py @@ -1,15 +1,19 @@ import os -import time +import wave import copy import uuid -import wave +import queue +import asyncio +import traceback +import threading import opuslib_next from abc import ABC, abstractmethod from config.logger import setup_logging from typing import Optional, Tuple, List -from core.utils.util import remove_punctuation_and_length -from core.handle.reportHandle import enqueue_asr_report from core.handle.receiveAudioHandle import startToChat +from core.handle.reportHandle import enqueue_asr_report +from core.utils.util import remove_punctuation_and_length +from core.handle.receiveAudioHandle import handleAudioMessage TAG = __name__ logger = setup_logging() @@ -17,53 +21,71 @@ logger = setup_logging() class ASRProviderBase(ABC): def __init__(self): - self.audio_format = "opus" - self.conn = None + pass # 打开音频通道 # 这里默认是非流式的处理方式 # 流式处理方式请在子类中重写 async def open_audio_channels(self, conn): - self.conn = conn + # tts 消化线程 + conn.asr_priority_thread = threading.Thread( + target=self.asr_text_priority_thread, args=(conn,), daemon=True + ) + conn.asr_priority_thread.start() + + # 有序处理ASR音频 + def asr_text_priority_thread(self, conn): + while not conn.stop_event.is_set(): + try: + message = conn.asr_audio_queue.get(timeout=1) + future = asyncio.run_coroutine_threadsafe( + handleAudioMessage(conn, message), + conn.loop, + ) + future.result() + except queue.Empty: + continue + except Exception as e: + logger.bind(tag=TAG).error( + f"处理ASR文本失败: {str(e)}, 类型: {type(e).__name__}, 堆栈: {traceback.format_exc()}" + ) + continue # 接收音频 # 这里默认是非流式的处理方式 # 流式处理方式请在子类中重写 - async def receive_audio(self, audio, audio_have_voice): - if ( - self.conn.client_listen_mode == "auto" - or self.conn.client_listen_mode == "realtime" - ): + async def receive_audio(self, conn, audio, audio_have_voice): + if conn.client_listen_mode == "auto" or conn.client_listen_mode == "realtime": have_voice = audio_have_voice else: - have_voice = self.conn.client_have_voice + have_voice = conn.client_have_voice # 如果本次没有声音,本段也没声音,就把声音丢弃了 - self.conn.asr_audio.append(audio) - if have_voice == False and self.conn.client_have_voice == False: - self.conn.asr_audio = self.conn.asr_audio[-10:] + conn.asr_audio.append(audio) + if have_voice == False and conn.client_have_voice == False: + conn.asr_audio = conn.asr_audio[-10:] return # 如果本段有声音,且已经停止了 - if self.conn.client_voice_stop: - asr_audio_task = copy.deepcopy(self.conn.asr_audio) - self.conn.asr_audio.clear() + if conn.client_voice_stop: + asr_audio_task = copy.deepcopy(conn.asr_audio) + conn.asr_audio.clear() # 音频太短了,无法识别 - self.conn.reset_vad_states() + conn.reset_vad_states() if len(asr_audio_task) > 15: - await self.handle_voice_stop(asr_audio_task) + await self.handle_voice_stop(conn, asr_audio_task) # 处理语音停止 - async def handle_voice_stop(self, asr_audio_task): + async def handle_voice_stop(self, conn, asr_audio_task): raw_text, _ = await self.speech_to_text( - asr_audio_task, self.conn.session_id + asr_audio_task, conn.session_id, conn.audio_format ) # 确保ASR模块返回原始文本 - self.conn.logger.bind(tag=TAG).info(f"识别文本: {raw_text}") + conn.logger.bind(tag=TAG).info(f"识别文本: {raw_text}") text_len, _ = remove_punctuation_and_length(raw_text) if text_len > 0: # 使用自定义模块进行上报 - await startToChat(self.conn, raw_text) - enqueue_asr_report(self.conn, raw_text, asr_audio_task) + await startToChat(conn, raw_text) + enqueue_asr_report(conn, raw_text, asr_audio_task) def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str: """PCM数据保存为WAV文件""" @@ -81,15 +103,11 @@ class ASRProviderBase(ABC): @abstractmethod async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """将语音数据转换为文本""" pass - def set_audio_format(self, format: str) -> None: - """设置音频格式""" - self.audio_format = format - @staticmethod def decode_opus(opus_data: List[bytes]) -> bytes: """将Opus音频数据解码为PCM数据""" diff --git a/main/xiaozhi-server/core/providers/asr/doubao.py b/main/xiaozhi-server/core/providers/asr/doubao.py index 8a647508e..ea590aad4 100644 --- a/main/xiaozhi-server/core/providers/asr/doubao.py +++ b/main/xiaozhi-server/core/providers/asr/doubao.py @@ -228,14 +228,14 @@ class ASRProvider(ASRProviderBase): yield data[offset:data_len], True async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """将语音数据转换为文本""" file_path = None try: # 合并所有opus数据包 - if self.audio_format == "pcm": + if audio_format == "pcm": pcm_data = opus_data else: pcm_data = self.decode_opus(opus_data) diff --git a/main/xiaozhi-server/core/providers/asr/doubao_stream.py b/main/xiaozhi-server/core/providers/asr/doubao_stream.py index da4098adf..da94e2fb7 100644 --- a/main/xiaozhi-server/core/providers/asr/doubao_stream.py +++ b/main/xiaozhi-server/core/providers/asr/doubao_stream.py @@ -190,7 +190,7 @@ class ASRProvider(ASRProviderBase): self.is_reconnecting = False self._session_close_event.set() - async def receive_audio(self, audio, _): + async def receive_audio(self, conn, audio, _): if not isinstance(audio, bytes): return @@ -411,7 +411,7 @@ class ASRProvider(ASRProviderBase): await asyncio.sleep(self.retry_delay) await self._forward_asr_results() # 递归重试 - async def speech_to_text(self, opus_data, session_id): + async def speech_to_text(self, opus_data, session_id, audio_format): result = self.text self.text = "" # 清空text return result, None diff --git a/main/xiaozhi-server/core/providers/asr/fun_local.py b/main/xiaozhi-server/core/providers/asr/fun_local.py index 8c7305c64..89a42b081 100644 --- a/main/xiaozhi-server/core/providers/asr/fun_local.py +++ b/main/xiaozhi-server/core/providers/asr/fun_local.py @@ -54,7 +54,7 @@ class ASRProvider(ASRProviderBase): ) async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """语音转文本主处理逻辑""" file_path = None @@ -63,7 +63,7 @@ class ASRProvider(ASRProviderBase): while retry_count < MAX_RETRIES: try: # 合并所有opus数据包 - if self.audio_format == "pcm": + if audio_format == "pcm": pcm_data = opus_data else: pcm_data = self.decode_opus(opus_data) diff --git a/main/xiaozhi-server/core/providers/asr/fun_server.py b/main/xiaozhi-server/core/providers/asr/fun_server.py index 60d5e4253..7eb9e7848 100644 --- a/main/xiaozhi-server/core/providers/asr/fun_server.py +++ b/main/xiaozhi-server/core/providers/asr/fun_server.py @@ -100,7 +100,7 @@ class ASRProvider(ASRProviderBase): logger.bind(tag=TAG).debug(f"Sent end message: {end_message}") async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """ Convert speech data to text using FunASR. @@ -109,7 +109,7 @@ class ASRProvider(ASRProviderBase): :return: Tuple containing recognized text and optional timestamp. """ file_path = None - if self.audio_format == "pcm": + if audio_format == "pcm": pcm_data = opus_data else: pcm_data = self.decode_opus(opus_data) diff --git a/main/xiaozhi-server/core/providers/asr/sherpa_onnx_local.py b/main/xiaozhi-server/core/providers/asr/sherpa_onnx_local.py index a27640dd5..9e5ade806 100644 --- a/main/xiaozhi-server/core/providers/asr/sherpa_onnx_local.py +++ b/main/xiaozhi-server/core/providers/asr/sherpa_onnx_local.py @@ -109,14 +109,14 @@ class ASRProvider(ASRProviderBase): return samples_float32, f.getframerate() async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """语音转文本主处理逻辑""" file_path = None try: # 保存音频文件 start_time = time.time() - if self.audio_format == "pcm": + if audio_format == "pcm": pcm_data = opus_data else: pcm_data = self.decode_opus(opus_data) diff --git a/main/xiaozhi-server/core/providers/asr/tencent.py b/main/xiaozhi-server/core/providers/asr/tencent.py index 03301568e..1fd1c6f42 100644 --- a/main/xiaozhi-server/core/providers/asr/tencent.py +++ b/main/xiaozhi-server/core/providers/asr/tencent.py @@ -32,7 +32,7 @@ class ASRProvider(ASRProviderBase): os.makedirs(self.output_dir, exist_ok=True) async def speech_to_text( - self, opus_data: List[bytes], session_id: str + self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: """将语音数据转换为文本""" if not opus_data: @@ -47,7 +47,7 @@ class ASRProvider(ASRProviderBase): return None, file_path # 将Opus音频数据解码为PCM - if self.audio_format == "pcm": + if audio_format == "pcm": pcm_data = opus_data else: pcm_data = self.decode_opus(opus_data) 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 51696bb75..296053932 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 @@ -122,6 +122,8 @@ class IntentProvider(IntentProviderBase): async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str: if not self.llm: raise ValueError("LLM provider not set") + if conn.func_handler is None: + return '{"function_call": {"name": "continue_chat"}}' # 记录整体开始时间 total_start_time = time.time() @@ -148,9 +150,8 @@ class IntentProvider(IntentProviderBase): self.clean_cache() if self.promot == "": - if hasattr(conn, "func_handler"): - functions = conn.func_handler.get_functions() - self.promot = self.get_intent_system_prompt(functions) + functions = conn.func_handler.get_functions() + self.promot = self.get_intent_system_prompt(functions) music_config = initialize_music_handler(conn) music_file_names = music_config["music_file_names"] diff --git a/main/xiaozhi-server/performance_tester.py b/main/xiaozhi-server/performance_tester.py index 80503166f..a5fc52219 100644 --- a/main/xiaozhi-server/performance_tester.py +++ b/main/xiaozhi-server/performance_tester.py @@ -140,7 +140,9 @@ class AsyncPerformanceTester: print(f"🎵 测试 STT: {stt_name}") - text, _ = await stt.speech_to_text([self.test_wav_list[0]], "1") + text, _ = await stt.speech_to_text( + [self.test_wav_list[0]], "1", stt.audio_format + ) if text is None: print(f"❌ {stt_name} 连接失败") @@ -151,7 +153,7 @@ class AsyncPerformanceTester: for i, sentence in enumerate(self.test_wav_list, 1): start = time.time() - text, _ = await stt.speech_to_text([sentence], "1") + text, _ = await stt.speech_to_text([sentence], "1", stt.audio_format) duration = time.time() - start total_time += duration From 03bcf5d2c760d5bb3e282a7baeea1b6b8ccca876 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 4 Jun 2025 16:43:15 +0800 Subject: [PATCH 12/12] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E8=B1=86?= =?UTF-8?q?=E5=8C=85=E6=B5=81=E5=BC=8FASR=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- main/xiaozhi-server/config.yaml | 1 + .../xiaozhi-server/core/handle/helloHandle.py | 2 + .../core/handle/receiveAudioHandle.py | 14 + main/xiaozhi-server/core/handle/textHandle.py | 5 +- .../xiaozhi-server/core/providers/asr/base.py | 4 + .../core/providers/asr/doubao_stream.py | 641 ++++++------------ 7 files changed, 249 insertions(+), 420 deletions(-) diff --git a/README.md b/README.md index b1561bbc9..e2954584f 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ | 模块名称 | 入门全免费设置 | 全流式配置 | |---------|---------|------| -| ASR(语音识别) | FunASR(本地) | ✅DoubaoASR(火山流式语音识别) | +| ASR(语音识别) | FunASR(本地) | ✅DoubaoStreamASR(火山流式语音识别) | | LLM(大模型) | ChatGLMLLM(智谱glm-4-flash) | ✅DoubaoLLM(火山doubao-1-5-pro-32k-250115) | | VLLM(视觉大模型) | ChatGLMVLLM(智谱glm-4v-flash) | ✅QwenVLVLLM(千问qwen2.5-vl-3b-instructh) | | TTS(语音合成) | EdgeTTS(微软语音) | ✅HuoshanDoubleStreamTTS(火山双流式语音合成) | diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 1bc151f0a..42f097b63 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -278,6 +278,7 @@ ASR: # 可以在这里申请相关Key等信息 # https://console.volcengine.com/speech/app # DoubaoASR和DoubaoStreamASR的区别是:DoubaoASR是按次收费,DoubaoStreamASR是按时收费 + # 开通地址https://console.volcengine.com/speech/service/10011 # 一般来说按次收费的更便宜,但是DoubaoStreamASR使用了大模型技术,效果更好 type: doubao_stream appid: 你的火山引擎语音合成服务appid diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index 8434cf22d..eec0123bc 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -63,6 +63,8 @@ async def checkWakeupWords(conn, text): """检查是否是唤醒词""" _, filtered_text = remove_punctuation_and_length(text) if filtered_text in conn.config.get("wakeup_words"): + # 设置刚刚被唤醒的标志 + conn.just_woken_up = True await send_stt_message(conn, text) file = getWakeupWordFile(WAKEUP_CONFIG["file_name"]) diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index ddce9d7ad..aee54cefc 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -3,6 +3,7 @@ from core.handle.intentHandler import handle_user_intent from core.utils.output_counter import check_device_output_limit from core.handle.abortHandle import handleAbortMessage import time +import asyncio from core.handle.sendAudioHandle import SentenceType from core.utils.util import audio_to_data @@ -12,6 +13,13 @@ TAG = __name__ async def handleAudioMessage(conn, audio): # 当前片段是否有人说话 have_voice = conn.vad.is_vad(conn, audio) + + # 如果设备刚刚被唤醒,短暂忽略VAD检测 + if hasattr(conn, "just_woken_up") and conn.just_woken_up: + have_voice = False + # 设置一个短暂延迟后恢复VAD检测 + asyncio.create_task(resume_vad_detection(conn)) + if have_voice: if conn.client_is_speaking: await handleAbortMessage(conn) @@ -21,6 +29,12 @@ async def handleAudioMessage(conn, audio): await conn.asr.receive_audio(conn, audio, have_voice) +async def resume_vad_detection(conn): + # 等待1秒后恢复VAD检测 + await asyncio.sleep(1) + conn.just_woken_up = False + + async def startToChat(conn, text): if conn.need_bind: await check_bind_device(conn) diff --git a/main/xiaozhi-server/core/handle/textHandle.py b/main/xiaozhi-server/core/handle/textHandle.py index 0fbae11ee..676e33ab1 100644 --- a/main/xiaozhi-server/core/handle/textHandle.py +++ b/main/xiaozhi-server/core/handle/textHandle.py @@ -61,6 +61,7 @@ async def handleTextMessage(conn, message): await send_tts_message(conn, "stop", None) conn.client_is_speaking = False elif is_wakeup_words: + conn.just_woken_up = True # 上报纯文字数据(复用ASR上报功能,但不提供音频数据) enqueue_asr_report(conn, "嘿,你好呀", []) await startToChat(conn, "嘿,你好呀") @@ -78,7 +79,9 @@ async def handleTextMessage(conn, message): elif msg_json["type"] == "mcp": conn.logger.bind(tag=TAG).info(f"收到mcp消息:{message}") if "payload" in msg_json: - asyncio.create_task(handle_mcp_message(conn, conn.mcp_client, msg_json["payload"])) + asyncio.create_task( + handle_mcp_message(conn, conn.mcp_client, msg_json["payload"]) + ) elif msg_json["type"] == "server": # 记录日志时过滤敏感信息 conn.logger.bind(tag=TAG).info( diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py index 4267c2ff4..5f2507c5f 100644 --- a/main/xiaozhi-server/core/providers/asr/base.py +++ b/main/xiaozhi-server/core/providers/asr/base.py @@ -84,9 +84,13 @@ class ASRProviderBase(ABC): text_len, _ = remove_punctuation_and_length(raw_text) if text_len > 0: # 使用自定义模块进行上报 + self.stop_ws_connection() await startToChat(conn, raw_text) enqueue_asr_report(conn, raw_text, asr_audio_task) + def stop_ws_connection(self): + pass + def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str: """PCM数据保存为WAV文件""" module_name = __name__.split(".")[-1] diff --git a/main/xiaozhi-server/core/providers/asr/doubao_stream.py b/main/xiaozhi-server/core/providers/asr/doubao_stream.py index da94e2fb7..a81caf4e1 100644 --- a/main/xiaozhi-server/core/providers/asr/doubao_stream.py +++ b/main/xiaozhi-server/core/providers/asr/doubao_stream.py @@ -7,22 +7,10 @@ import opuslib_next from core.providers.asr.base import ASRProviderBase from config.logger import setup_logging from core.providers.asr.dto.dto import InterfaceType -import threading TAG = __name__ logger = setup_logging() -CLIENT_FULL_REQUEST = 0b0001 -CLIENT_AUDIO_ONLY_REQUEST = 0b0010 -SERVER_FULL_RESPONSE = 0b1001 -SERVER_ACK = 0b1011 -SERVER_ERROR_RESPONSE = 0b1111 -NO_SEQUENCE = 0b0000 -NEG_SEQUENCE = 0b0010 -JSON_SERIALIZATION = 0b0001 -GZIP_COMPRESSION = 0b0001 -PROTOCOL_VERSION = 0b0001 - class ASRProvider(ASRProviderBase): def __init__(self, config, delete_audio_file): @@ -31,39 +19,29 @@ class ASRProvider(ASRProviderBase): self.config = config self.text = "" self.max_retries = 3 - self.retry_delay = 2 # 重试延迟秒数 - self.recv_lock = asyncio.Lock() # 添加接收锁 - self.reconnect_lock = asyncio.Lock() # 添加重连锁 - self.last_reconnect_time = 0 # 上次重连时间 - self.reconnect_cooldown = 1 # 增加重连冷却时间到10秒 - self.reconnect_count = 0 # 当前重连次数 - self.max_reconnect_count = 3 # 减少最大重连次数到3次 - self.asr_thread = None # ASR监听线程 - self.thread_lock = threading.Lock() # 线程管理锁 - self.is_reconnecting = False # 添加重连状态标志 - - # 添加会话管理相关属性 - self._session_lock = asyncio.Lock() # 会话操作的并发锁 - self._current_session_id = None # 当前会话ID - self._session_started = False # 会话是否已开始 - self._session_finished = False # 会话是否已结束 - self._session_close_event = asyncio.Event() # 添加会话关闭事件 + self.retry_delay = 2 + self.decoder = opuslib_next.Decoder(16000, 1) + self.asr_ws = None + self.forward_task = None + self.is_processing = False # 添加处理状态标志 + # 配置参数 self.appid = str(config.get("appid")) self.cluster = config.get("cluster") self.access_token = config.get("access_token") self.boosting_table_name = config.get("boosting_table_name", "") self.correct_table_name = config.get("correct_table_name", "") - self.output_dir = config.get("output_dir", "temp/") + self.output_dir = config.get("output_dir", "tmp/") self.delete_audio_file = delete_audio_file - self.ws_url = "wss://openspeech.bytedance.com/api/v2/asr" + # 火山引擎ASR配置 + self.ws_url = "wss://openspeech.bytedance.com/api/v3/sauc/bigmodel" self.uid = config.get("uid", "streaming_asr_service") self.workflow = config.get( "workflow", "audio_in,resample,partition,vad,fe,decode,itn,nlu_punctuate" ) self.result_type = config.get("result_type", "single") - self.format = config.get("format", "raw") + self.format = config.get("format", "pcm") self.codec = config.get("codec", "pcm") self.rate = config.get("sample_rate", 16000) self.language = config.get("language", "zh-CN") @@ -71,144 +49,163 @@ class ASRProvider(ASRProviderBase): self.channel = config.get("channel", 1) self.auth_method = config.get("auth_method", "token") self.secret = config.get("secret", "access_secret") - self.decoder = opuslib_next.Decoder(16000, 1) - self.asr_ws = None - self.forward_task = None - self.conn = None - ################################################################################### - # 豆包流式ASR重写父类的方法--开始 - ################################################################################### async def open_audio_channels(self, conn): await super().open_audio_channels(conn) - async with self._session_lock: - # 如果正在重连,等待重连完成 - if self.is_reconnecting: - logger.bind(tag=TAG).info("等待当前重连完成...") - await self._session_close_event.wait() - self._session_close_event.clear() - - # 如果已有会话未结束,先关闭它 - if self._session_started and not self._session_finished: - logger.bind(tag=TAG).warning( - f"发现未关闭的会话 {self._current_session_id},正在关闭..." - ) - if self.asr_ws is not None: - try: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning(f"关闭旧连接时发生错误: {e}") - finally: - self.asr_ws = None - self._session_finished = True - self._session_close_event.set() - - # 重置会话状态 - self._current_session_id = str(uuid.uuid4()) - self._session_started = True - self._session_finished = False - self.is_reconnecting = True - - try: - retry_count = 0 - while retry_count < self.max_retries: - try: - headers = ( - self.token_auth() if self.auth_method == "token" else None - ) - self.asr_ws = await websockets.connect( - self.ws_url, - additional_headers=headers, - max_size=1000000000, - ping_interval=None, - ping_timeout=None, - close_timeout=10, - ) - - # 发送初始化请求 - request_params = self.construct_request( - self._current_session_id - ) - try: - payload_bytes = str.encode(json.dumps(request_params)) - payload_bytes = gzip.compress(payload_bytes) - full_client_request = self.generate_header() - full_client_request.extend( - (len(payload_bytes)).to_bytes(4, "big") - ) - full_client_request.extend(payload_bytes) - await self.asr_ws.send(full_client_request) - except Exception as e: - logger.bind(tag=TAG).error(f"发送初始化请求失败: {e}") - raise e - - # 等待初始化响应 - try: - init_res = await self.asr_ws.recv() - self.parse_response(init_res) - except Exception as e: - logger.bind(tag=TAG).error(f"ASR服务初始化失败: {e}") - raise e - - # 启动接收ASR结果的异步任务 - with self.thread_lock: - if ( - self.asr_thread is None - or not self.asr_thread.is_alive() - ): - logger.bind(tag=TAG).info("创建新的ASR监听线程...") - self.asr_thread = threading.Thread( - target=self._start_monitor_asr_response_thread, - daemon=True, - ) - self.asr_thread.start() - # 等待一小段时间确保线程启动 - await asyncio.sleep(0.1) - if not self.asr_thread.is_alive(): - logger.bind(tag=TAG).error("ASR监听线程启动失败") - raise Exception("ASR监听线程启动失败") - logger.bind(tag=TAG).info("ASR监听线程已启动") - return - - except websockets.exceptions.WebSocketException as e: - retry_count += 1 - if retry_count < self.max_retries: - logger.bind(tag=TAG).warning( - f"WebSocket连接失败,正在进行第{retry_count}次重试: {e}" - ) - await asyncio.sleep(self.retry_delay) - else: - logger.bind(tag=TAG).warning( - f"WebSocket连接失败,已达到最大重试次数: {e}" - ) - raise - except Exception as e: - logger.bind(tag=TAG).error(f"WebSocket连接发生未知错误: {e}") - raise - finally: - self.is_reconnecting = False - self._session_close_event.set() - - async def receive_audio(self, conn, audio, _): + async def receive_audio(self, conn, audio, audio_have_voice): if not isinstance(audio, bytes): return - try: - # 解码opus得到PCM数据 - pcm_frame = self.decoder.decode(audio, 960) - payload = gzip.compress(pcm_frame) - audio_request = bytearray(self.generate_audio_default_header()) - audio_request.extend(len(payload).to_bytes(4, "big")) - audio_request.extend(payload) - if self.asr_ws: - await self.asr_ws.send(audio_request) - except Exception as e: - logger.bind(tag=TAG).debug(f"发送音频数据时发生错误: {e}") + conn.asr_audio.append(audio) + if audio_have_voice == False and conn.client_have_voice == False: + conn.asr_audio = conn.asr_audio[-10:] - ################################################################################### - # 豆包流式ASR重写父类的方法--结束 - ################################################################################### + # 如果本次有声音,且之前没有建立连接 + if audio_have_voice and self.asr_ws is None and not self.is_processing: + try: + self.is_processing = True + # 建立新的WebSocket连接 + headers = self.token_auth() if self.auth_method == "token" else None + logger.bind(tag=TAG).info(f"正在连接ASR服务,headers: {headers}") + + self.asr_ws = await websockets.connect( + self.ws_url, + additional_headers=headers, + max_size=1000000000, + ping_interval=None, + ping_timeout=None, + close_timeout=10, + ) + + # 发送初始化请求 + request_params = self.construct_request(str(uuid.uuid4())) + try: + payload_bytes = str.encode(json.dumps(request_params)) + payload_bytes = gzip.compress(payload_bytes) + full_client_request = self.generate_header() + full_client_request.extend((len(payload_bytes)).to_bytes(4, "big")) + full_client_request.extend(payload_bytes) + + logger.bind(tag=TAG).info(f"发送初始化请求: {request_params}") + await self.asr_ws.send(full_client_request) + + # 等待初始化响应 + init_res = await self.asr_ws.recv() + result = self.parse_response(init_res) + logger.bind(tag=TAG).info(f"收到初始化响应: {result}") + + # 检查初始化响应 + if "code" in result and result["code"] != 1000: + error_msg = f"ASR服务初始化失败: {result.get('payload_msg', {}).get('message', '未知错误')}" + if "payload_msg" in result: + error_msg += f"\n详细错误信息: {json.dumps(result['payload_msg'], ensure_ascii=False)}" + logger.bind(tag=TAG).error(error_msg) + raise Exception(error_msg) + + except Exception as e: + logger.bind(tag=TAG).error(f"发送初始化请求失败: {str(e)}") + if hasattr(e, "__cause__") and e.__cause__: + logger.bind(tag=TAG).error(f"错误原因: {str(e.__cause__)}") + raise e + + # 启动接收ASR结果的异步任务 + self.forward_task = asyncio.create_task(self._forward_asr_results(conn)) + + # 发送缓存的音频数据 + if conn.asr_audio and len(conn.asr_audio) > 0: + logger.bind(tag=TAG).info( + f"发送缓存音频数据: {len(conn.asr_audio)}" + ) + for cached_audio in conn.asr_audio[-10:]: + try: + pcm_frame = self.decoder.decode(cached_audio, 960) + payload = gzip.compress(pcm_frame) + audio_request = bytearray( + self.generate_audio_default_header() + ) + audio_request.extend(len(payload).to_bytes(4, "big")) + audio_request.extend(payload) + await self.asr_ws.send(audio_request) + except Exception as e: + logger.bind(tag=TAG).info( + f"发送缓存音频数据时发生错误: {e}" + ) + + except Exception as e: + logger.bind(tag=TAG).error(f"建立ASR连接失败: {str(e)}") + if hasattr(e, "__cause__") and e.__cause__: + logger.bind(tag=TAG).error(f"错误原因: {str(e.__cause__)}") + if self.asr_ws: + await self.asr_ws.close() + self.asr_ws = None + self.is_processing = False + return + + # 发送当前音频数据 + if self.asr_ws and self.is_processing: + try: + pcm_frame = self.decoder.decode(audio, 960) + payload = gzip.compress(pcm_frame) + audio_request = bytearray(self.generate_audio_default_header()) + audio_request.extend(len(payload).to_bytes(4, "big")) + audio_request.extend(payload) + await self.asr_ws.send(audio_request) + except Exception as e: + logger.bind(tag=TAG).info(f"发送音频数据时发生错误: {e}") + + async def _forward_asr_results(self, conn): + try: + while self.asr_ws and not conn.stop_event.is_set(): + try: + response = await self.asr_ws.recv() + result = self.parse_response(response) + logger.bind(tag=TAG).debug(f"收到ASR结果: {result}") + + if "payload_msg" in result: + payload = result["payload_msg"] + if "result" in payload: + utterances = payload["result"].get("utterances", []) + for utterance in utterances: + if utterance.get("definite", False): + self.text = utterance["text"] + logger.bind(tag=TAG).info( + f"识别到文本: {self.text}" + ) + conn.reset_vad_states() + await self.handle_voice_stop(conn, None) + break + elif "error" in payload: + error_msg = payload.get("error", "未知错误") + logger.bind(tag=TAG).error(f"ASR服务返回错误: {error_msg}") + break + + except websockets.ConnectionClosed: + logger.bind(tag=TAG).info("ASR服务连接已关闭") + self.is_processing = False + break + except Exception as e: + logger.bind(tag=TAG).error(f"处理ASR结果时发生错误: {str(e)}") + if hasattr(e, "__cause__") and e.__cause__: + logger.bind(tag=TAG).error(f"错误原因: {str(e.__cause__)}") + self.is_processing = False + break + + except Exception as e: + logger.bind(tag=TAG).error(f"ASR结果转发任务发生错误: {str(e)}") + if hasattr(e, "__cause__") and e.__cause__: + logger.bind(tag=TAG).error(f"错误原因: {str(e.__cause__)}") + finally: + if self.asr_ws: + await self.asr_ws.close() + self.asr_ws = None + self.is_processing = False + + def stop_ws_connection(self): + if self.asr_ws: + asyncio.create_task(self.asr_ws.close()) + self.asr_ws = None + self.is_processing = False def construct_request(self, reqid): req = { @@ -226,6 +223,7 @@ class ASRProvider(ASRProviderBase): "sequence": 1, "boosting_table_name": self.boosting_table_name, "correct_table_name": self.correct_table_name, + "end_window_size": 200, }, "audio": { "format": self.format, @@ -234,31 +232,33 @@ class ASRProvider(ASRProviderBase): "language": self.language, "bits": self.bits, "channel": self.channel, + "sample_rate": self.rate, }, } + logger.bind(tag=TAG).info( + f"构造请求参数: {json.dumps(req, ensure_ascii=False)}" + ) return req def token_auth(self): - return {"Authorization": f"Bearer; {self.access_token}"} + return { + "X-Api-App-Key": self.appid, + "X-Api-Access-Key": self.access_token, + "X-Api-Resource-Id": "volc.bigasr.sauc.duration", + "X-Api-Connect-Id": str(uuid.uuid4()), + "Host": "openspeech.bytedance.com", + } def generate_header( self, - version=PROTOCOL_VERSION, - message_type=CLIENT_FULL_REQUEST, - message_type_specific_flags=NO_SEQUENCE, - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, + version=0x01, + message_type=0x01, + message_type_specific_flags=0x00, + serial_method=0x01, + compression_type=0x01, reserved_data=0x00, extension_header: bytes = b"", ): - """ - 生成协议头: - - 第1字节:高4位:协议版本,低4位:头部大小(单位 4 字节) - - 第2字节:高4位:消息类型,低4位:消息类型特定标志 - - 第3字节:高4位:序列化方式,低4位:压缩方式 - - 第4字节:保留字段 - - 后续:扩展头(如果有) - """ header = bytearray() header_size = int(len(extension_header) / 4) + 1 header.append((version << 4) | header_size) @@ -268,267 +268,72 @@ class ASRProvider(ASRProviderBase): header.extend(extension_header) return header - def generate_full_default_header(self): - # full client request 默认头 - return self.generate_header( - version=PROTOCOL_VERSION, - message_type=CLIENT_FULL_REQUEST, - message_type_specific_flags=NO_SEQUENCE, - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, - ) - def generate_audio_default_header(self): - # 普通音频片段请求 return self.generate_header( - version=PROTOCOL_VERSION, - message_type=CLIENT_AUDIO_ONLY_REQUEST, - message_type_specific_flags=NO_SEQUENCE, - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, + version=0x01, + message_type=0x02, + message_type_specific_flags=0x00, + serial_method=0x01, + compression_type=0x01, ) def generate_last_audio_default_header(self): - # 最后一个音频片段标志 return self.generate_header( - version=PROTOCOL_VERSION, - message_type=CLIENT_AUDIO_ONLY_REQUEST, - message_type_specific_flags=NEG_SEQUENCE, # 用 NEG_SEQUENCE 表示结束 - serial_method=JSON_SERIALIZATION, - compression_type=GZIP_COMPRESSION, + version=0x01, + message_type=0x02, + message_type_specific_flags=0x02, + serial_method=0x01, + compression_type=0x01, ) - def _start_monitor_asr_response_thread(self): - # 初始化链接 + def parse_response(self, res: bytes) -> dict: try: - with self.thread_lock: - if self.conn is None or self.conn.loop is None: - logger.bind(tag=TAG).error( - "无法启动ASR监听线程:conn或loop未初始化" - ) - return + # 检查响应长度 + if len(res) < 4: + logger.bind(tag=TAG).error(f"响应数据长度不足: {len(res)}") + return {"error": "响应数据长度不足"} - try: - logger.bind(tag=TAG).info("开始启动ASR监听...") - asyncio.run_coroutine_threadsafe( - self._forward_asr_results(), loop=self.conn.loop - ) - logger.bind(tag=TAG).info("ASR监听已启动") - except Exception as e: - logger.bind(tag=TAG).error(f"启动ASR监听线程失败: {e}") - except Exception as e: - logger.bind(tag=TAG).error(f"ASR监听线程发生未预期的错误: {e}") + # 获取消息头 + header = res[:4] + message_type = header[1] >> 4 - async def _forward_asr_results(self): - try: - while not self.conn.stop_event.is_set(): - try: - if self.asr_ws is None: - # 检查是否需要重连 - async with self.reconnect_lock: - current_time = asyncio.get_event_loop().time() - if ( - current_time - self.last_reconnect_time - < self.reconnect_cooldown - ): - await asyncio.sleep(1) - continue + # 如果是错误响应 + if message_type == 0x0F: # SERVER_ERROR_RESPONSE + code = int.from_bytes(header[4:8], "big", signed=False) + error_msg = res[8:].decode("utf-8") + return {"code": code, "error": error_msg} - if self.reconnect_count >= self.max_reconnect_count: - logger.bind(tag=TAG).error( - "达到最大重连次数限制,停止重连" - ) - await asyncio.sleep(self.reconnect_cooldown) - self.reconnect_count = 0 - continue - - self.last_reconnect_time = current_time - self.reconnect_count += 1 - logger.bind(tag=TAG).info( - f"尝试重新连接ASR服务... (第{self.reconnect_count}次)" - ) - await self.open_audio_channels(self.conn) - continue - - # 使用锁来确保同一时间只有一个协程在接收数据 - async with self.recv_lock: - response = await self.asr_ws.recv() - result = self.parse_response(response) - - # 检查是否需要重连 - if result.get("need_reconnect", False): - logger.bind(tag=TAG).info( - "检测到需要重连的错误,准备重新连接..." - ) - if self.asr_ws is not None: - try: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning( - f"关闭旧连接时发生错误: {e}" - ) - finally: - self.asr_ws = None - continue - - if "payload_msg" in result: - if "result" in result["payload_msg"]: - # 检查是否有utterances并且definite为True - utterances = result["payload_msg"]["result"][0].get( - "utterances", [] - ) - for utterance in utterances: - if utterance.get("definite", False): - self.text = utterance["text"] - await self.handle_voice_stop(None) - break - - except websockets.ConnectionClosed: - logger.bind(tag=TAG).debug("ASR服务连接已关闭,准备重连...") - # 确保关闭旧连接 - if self.asr_ws is not None: - try: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning(f"关闭旧连接时发生错误: {e}") - finally: - self.asr_ws = None - - # 等待冷却时间 - await asyncio.sleep(self.reconnect_cooldown) - continue - - except Exception as e: - if not self.conn.stop_event.is_set(): - logger.bind(tag=TAG).error(f"ASR监听发生错误: {e}") - await asyncio.sleep(self.retry_delay) - continue + # 获取JSON数据(跳过12字节头部) + try: + json_data = res[12:].decode("utf-8") + result = json.loads(json_data) + logger.bind(tag=TAG).debug(f"成功解析JSON响应: {result}") + return {"payload_msg": result} + except (UnicodeDecodeError, json.JSONDecodeError) as e: + logger.bind(tag=TAG).error(f"JSON解析失败: {str(e)}") + logger.bind(tag=TAG).error(f"原始数据: {res}") + raise except Exception as e: - logger.bind(tag=TAG).error(f"ASR监听线程发生错误: {e}") - # 确保在发生严重错误时也能继续尝试重连 - if not self.conn.stop_event.is_set(): - await asyncio.sleep(self.retry_delay) - await self._forward_asr_results() # 递归重试 + logger.bind(tag=TAG).error(f"解析响应失败: {str(e)}") + logger.bind(tag=TAG).error(f"原始响应数据: {res.hex()}") + raise async def speech_to_text(self, opus_data, session_id, audio_format): result = self.text self.text = "" # 清空text return result, None - def parse_response(self, res: bytes) -> dict: - """ - 解析 ASR 服务返回的二进制响应。 - 根据协议格式解析头部和 payload,若采用 GZIP 压缩则先解压,再根据 JSON 反序列化。 - """ - protocol_version = res[0] >> 4 - header_size = res[0] & 0x0F - message_type = res[1] >> 4 - serialization_method = res[2] >> 4 - message_compression = res[2] & 0x0F - payload = res[header_size * 4 :] - result = {} - payload_msg = None - payload_size = 0 - - if message_type == SERVER_FULL_RESPONSE: - payload_size = int.from_bytes(payload[:4], "big", signed=True) - payload_msg = payload[4:] - elif message_type == SERVER_ACK: - seq = int.from_bytes(payload[:4], "big", signed=True) - result["seq"] = seq - if len(payload) >= 8: - payload_size = int.from_bytes(payload[4:8], "big", signed=False) - payload_msg = payload[8:] - elif message_type == SERVER_ERROR_RESPONSE: - code = int.from_bytes(payload[:4], "big", signed=False) - result["code"] = code - payload_size = int.from_bytes(payload[4:8], "big", signed=False) - payload_msg = payload[8:] - - if payload_msg is None: - return result - if message_compression == GZIP_COMPRESSION: - payload_msg = gzip.decompress(payload_msg) - if serialization_method == JSON_SERIALIZATION: - payload_msg = json.loads(payload_msg.decode("utf-8")) - else: - payload_msg = payload_msg.decode("utf-8") - result["payload_msg"] = payload_msg - result["payload_size"] = payload_size - - # 错误码处理 - if "code" in result: - error_code = result["code"] - error_message = "" - - if error_code == 1000: - error_message = "成功" - elif error_code == 1001: - error_message = "请求参数无效:请求参数缺失必需字段/字段值无效/重复请求" - elif error_code == 1002: - error_message = "无访问权限:token无效/过期/无权访问指定服务" - elif error_code == 1003: - error_message = "访问超频:当前appid访问QPS超出设定阈值" - elif error_code == 1004: - error_message = "访问超额:当前appid访问次数超出限制" - elif error_code == 1005: - error_message = "服务器繁忙:服务过载,无法处理当前请求" - elif error_code == 1010: - error_message = "音频过长:音频数据时长超出阈值" - elif error_code == 1011: - error_message = "音频过大:音频数据大小超出阈值" - elif error_code == 1012: - error_message = "音频格式无效:音频header有误/无法进行音频解码" - elif error_code == 1013: - error_message = "音频静音:音频未识别出任何文本结果" - elif error_code >= 1020 and error_code <= 1022: - error_message = "识别相关错误:需要重连" - if error_code == 1020: - error_message = "识别等待超时:等待下一包就绪超时" - elif error_code == 1021: - error_message = "识别处理超时:识别处理过程超时" - elif error_code == 1022: - error_message = "识别错误:识别过程中发生错误" - else: - error_message = "未知错误:未归类错误" - - logger.bind(tag=TAG).debug( - f"ASR错误: {error_message} (错误码: {error_code})" - ) - - # 如果是识别相关错误,标记需要重连 - if error_code >= 1020 or error_code == 1001: - result["need_reconnect"] = True - - return result - - async def close_session(self): - """关闭当前会话""" - async with self._session_lock: - if not self._session_started: - logger.bind(tag=TAG).warning("尝试关闭未开始的会话") - return - - if self._session_finished: - logger.bind(tag=TAG).warning( - f"会话 {self._current_session_id} 已经关闭" - ) - return - - try: - if self.asr_ws is not None: - await self.asr_ws.close() - except Exception as e: - logger.bind(tag=TAG).warning(f"关闭WebSocket连接时发生错误: {e}") - finally: - self.asr_ws = None - self._session_finished = True - self._session_started = False - self._current_session_id = None - # 重置重连计数 - self.reconnect_count = 0 - async def close(self): """资源清理方法""" - await self.close_session() + if self.asr_ws: + await self.asr_ws.close() + self.asr_ws = None + if self.forward_task: + self.forward_task.cancel() + try: + await self.forward_task + except asyncio.CancelledError: + pass + self.forward_task = None + self.is_processing = False