diff --git a/config.yaml b/config.yaml index 68b4b978..63baa8e5 100644 --- a/config.yaml +++ b/config.yaml @@ -53,6 +53,7 @@ LLM: url: https://api.deepseek.com api_key: 你的deepseek api key ChatGLMLLM: + # glm-4-flash 是免费的,但是还是需要注册填写api_key的 # 可在这里找到你的api key https://bigmodel.cn/usercenter/proj-mgmt/apikeys model_name: glm-4-flash url: https://open.bigmodel.cn/api/paas/v4/ diff --git a/core/handle/audioHandle.py b/core/handle/audioHandle.py index 7217178c..e3dadde0 100644 --- a/core/handle/audioHandle.py +++ b/core/handle/audioHandle.py @@ -35,11 +35,13 @@ async def handleAudioMessage(conn, audio): conn.asr_audio.clear() conn.reset_vad_states() + async def startToChat(conn, text): # 异步发送 stt 信息 - asyncio.create_task( + stt_task = asyncio.create_task( schedule_with_interrupt(0, send_stt_message(conn, text)) ) + conn.scheduled_tasks.append(stt_task) conn.executor.submit(conn.chat, text) @@ -52,7 +54,10 @@ async def sendAudioMessage(conn, audios, duration, text): conn.tts_start_speak_time = time.time() # 发送 sentence_start(每个音频文件之前发送一次) - await send_tts_message(conn, "sentence_start", text) + sentence_task = asyncio.create_task( + schedule_with_interrupt(base_delay, send_tts_message(conn, "sentence_start", text)) + ) + conn.scheduled_tasks.append(sentence_task) conn.tts_duration += duration @@ -60,10 +65,6 @@ async def sendAudioMessage(conn, audios, duration, text): for idx, opus_packet in enumerate(audios): await conn.websocket.send(opus_packet) - # 每个音频文件发送结束时,发送 sentence_end - if idx == len(audios) - 1: - await send_tts_message(conn, "sentence_end", text) - if conn.llm_finish_task and text == conn.tts_last_text: stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time) stop_task = asyncio.create_task( @@ -71,6 +72,7 @@ async def sendAudioMessage(conn, audios, duration, text): ) conn.scheduled_tasks.append(stop_task) + async def send_tts_message(conn, state, text=None): """发送 TTS 状态消息""" message = { @@ -85,6 +87,7 @@ async def send_tts_message(conn, state, text=None): if state == "stop": conn.clearSpeakStatus() + async def send_stt_message(conn, text): """发送 STT 状态消息""" stt_text = get_string_no_punctuation_or_emoji(text) diff --git a/core/utils/llm.py b/core/utils/llm.py index 0b1e8ccc..632b8874 100644 --- a/core/utils/llm.py +++ b/core/utils/llm.py @@ -12,6 +12,13 @@ logger = logging.getLogger(__name__) class LLM(ABC): + def __init__(self, config): + api_key = config.get("api_key") + # 如果api_key包含中文“你” + if "你" in api_key: + logger.error("你还没配置LLM的密钥,请在配置文件中配置密钥,否则无法正常工作") + pass + @abstractmethod def response(self, session_id, dialogue): """LLM response generator""" @@ -20,6 +27,7 @@ class LLM(ABC): class DeepSeekLLM(LLM): def __init__(self, config): + super().__init__(config) self.model_name = config.get("model_name") self.api_key = config.get("api_key") self.base_url = config.get("url") @@ -46,6 +54,7 @@ class DeepSeekLLM(LLM): class ChatGLMLLM(LLM): def __init__(self, config): + super().__init__(config) self.model_name = config.get("model_name") self.api_key = config.get("api_key") self.base_url = config.get("url") @@ -70,6 +79,7 @@ class ChatGLMLLM(LLM): class AliLLM(LLM): def __init__(self, config): + super().__init__(config) self.model_name = config.get("model_name") self.api_key = config.get("api_key") self.base_url = config.get("base_url") @@ -94,6 +104,7 @@ class AliLLM(LLM): class DifyLLM(LLM): def __init__(self, config): + super().__init__(config) self.api_key = config["api_key"] self.base_url = config.get("base_url", "https://api.dify.ai/v1").rstrip('/')