diff --git a/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java index dd2a28810..f7c06fdb7 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java @@ -91,6 +91,7 @@ public class ConfigServiceImpl implements ConfigService { } throw new RenException(ErrorCode.OTA_DEVICE_NOT_FOUND, "not found device"); } + // 获取智能体信息 AgentEntity agent = agentService.getAgentById(device.getAgentId()); if (agent == null) { @@ -104,7 +105,9 @@ public class ConfigServiceImpl implements ConfigService { } // 构建返回数据 Map result = new HashMap<>(); - + // 获取单台设备每天最多输出字数 + String deviceMaxOutputSize = sysParamsService.getValue("device_max_output_size", true); + result.put("device_max_output_size", deviceMaxOutputSize); // 如果客户端已实例化模型,则不返回 String alreadySelectedVadModelId = (String) selectedModule.get("VAD"); if (alreadySelectedVadModelId != null && alreadySelectedVadModelId.equals(agent.getVadModelId())) { diff --git a/main/manager-api/src/main/resources/db/changelog/202504181534.sql b/main/manager-api/src/main/resources/db/changelog/202504181536.sql similarity index 50% rename from main/manager-api/src/main/resources/db/changelog/202504181534.sql rename to main/manager-api/src/main/resources/db/changelog/202504181536.sql index dcf7d3361..327aa605f 100644 --- a/main/manager-api/src/main/resources/db/changelog/202504181534.sql +++ b/main/manager-api/src/main/resources/db/changelog/202504181536.sql @@ -1,2 +1,7 @@ +-- 调整意图识别配置 delete from `ai_model_config` where id = 'Intent_function_call'; INSERT INTO `ai_model_config` VALUES ('Intent_function_call', 'Intent', 'function_call', '函数调用意图识别', 0, 1, '{\"type\": \"function_call\", \"functions\": \"change_role;get_weather;get_news;play_music\"}', NULL, NULL, 3, NULL, NULL, NULL, NULL); + +-- 增加单台设备每天最多聊天句数 +delete from `sys_params` where id = 105; +INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (105, 'device_max_output_size', '0', 'number', 1, '单台设备每天最多输出字数,0表示不限制'); \ No newline at end of file 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 a2a322a8b..fa4170b06 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 @@ -59,9 +59,9 @@ databaseChangeLog: encoding: utf8 path: classpath:db/changelog/202504151206.sql - changeSet: - id: 202504181534 + id: 202504181536 author: John changes: - sqlFile: encoding: utf8 - path: classpath:db/changelog/202504181534.sql \ No newline at end of file + path: classpath:db/changelog/202504181536.sql \ No newline at end of file diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index c77de6380..6b2de546e 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -29,6 +29,7 @@ from core.auth import AuthMiddleware, AuthenticationError from core.mcp.manager import MCPManager from config.config_loader import get_private_config_from_api from config.manage_api_client import DeviceNotFoundException, DeviceBindException +from core.utils.output_counter import add_device_output TAG = __name__ @@ -57,6 +58,7 @@ class ConnectionHandler: self.session_id = None self.prompt = None self.welcome_msg = None + self.max_output_size = 0 # 客户端状态相关 self.client_abort = False @@ -315,7 +317,6 @@ class ConnectionHandler: self.config["selected_module"]["LLM"] = private_config["selected_module"][ "LLM" ] - if private_config.get("Memory", None) is not None: init_memory = True self.config["Memory"] = private_config["Memory"] @@ -328,6 +329,8 @@ class ConnectionHandler: self.config["selected_module"]["Intent"] = private_config[ "selected_module" ]["Intent"] + if private_config.get("device_max_output_size", None) is not None: + self.max_output_size = int(private_config["device_max_output_size"]) try: modules = initialize_modules( self.logger, @@ -845,6 +848,8 @@ class ConnectionHandler: self.logger.bind(tag=TAG).error(f"tts转换失败,{text}") return None, text, text_index self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}") + if self.max_output_size > 0: + add_device_output(self.headers.get("device-id"), len(text)) return tts_file, text, text_index def clearSpeakStatus(self): diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index 09f342dc7..485544f75 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -1,9 +1,9 @@ from config.logger import setup_logging import time -import asyncio from core.utils.util import remove_punctuation_and_length from core.handle.sendAudioHandle import send_stt_message from core.handle.intentHandler import handle_user_intent +from core.utils.output_counter import check_device_output_limit TAG = __name__ logger = setup_logging() @@ -51,6 +51,15 @@ async def startToChat(conn, text): if conn.need_bind: await check_bind_device(conn) return + + # 如果当日的输出字数大于限定的字数 + if conn.max_output_size > 0: + if check_device_output_limit( + conn.headers.get("device-id"), conn.max_output_size + ): + await max_out_size(conn) + return + # 首先进行意图分析 intent_handled = await handle_user_intent(conn, text) @@ -89,6 +98,18 @@ async def no_voice_close_connect(conn): await startToChat(conn, prompt) +async def max_out_size(conn): + text = "不好意思,我现在有点事情要忙,明天这个时候我们再聊,约好了哦!明天不见不散,拜拜!" + await send_stt_message(conn, text) + conn.tts_first_text_index = 0 + conn.tts_last_text_index = 0 + conn.llm_finish_task = True + file_path = "config/assets/max_output_size.wav" + opus_packets, _ = conn.tts.audio_to_opus_data(file_path) + conn.audio_play_queue.put((opus_packets, text, 0)) + conn.close_after_chat = True + + async def check_bind_device(conn): if conn.bind_code: # 确保bind_code是6位数字 diff --git a/main/xiaozhi-server/core/utils/output_counter.py b/main/xiaozhi-server/core/utils/output_counter.py new file mode 100644 index 000000000..1e7ae8df6 --- /dev/null +++ b/main/xiaozhi-server/core/utils/output_counter.py @@ -0,0 +1,50 @@ +import datetime +from typing import Dict, Tuple + +# 全局字典,用于存储每个设备的每日输出字数 +_device_daily_output: Dict[Tuple[str, datetime.date], int] = {} +# 记录最后一次检查的日期 +_last_check_date: datetime.date = None + + +def reset_device_output(): + """ + 重置所有设备的每日输出字数 + 每天0点调用此函数 + """ + _device_daily_output.clear() + + +def get_device_output(device_id: str) -> int: + """ + 获取设备当日的输出字数 + """ + current_date = datetime.datetime.now().date() + return _device_daily_output.get((device_id, current_date), 0) + + +def add_device_output(device_id: str, char_count: int): + """ + 增加设备的输出字数 + """ + current_date = datetime.datetime.now().date() + global _last_check_date + + # 如果是第一次调用或者日期发生变化,清空计数器 + if _last_check_date is None or _last_check_date != current_date: + _device_daily_output.clear() + _last_check_date = current_date + + current_count = _device_daily_output.get((device_id, current_date), 0) + _device_daily_output[(device_id, current_date)] = current_count + char_count + + +def check_device_output_limit(device_id: str, max_output_size: int) -> bool: + """ + 检查设备是否超过输出限制 + :return: True 如果超过限制,False 如果未超过 + """ + if not device_id: + return False + current_output = get_device_output(device_id) + return current_output >= max_output_size