From 6db55055a24f938755aa0f7ff79c2705a18bbf9a Mon Sep 17 00:00:00 2001 From: FuQuan233 Date: Fri, 28 Feb 2025 11:15:22 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20set=20random=5Ftrigger=5Fprob=20per?= =?UTF-8?q?=20group?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_llmchat/__init__.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/nonebot_plugin_llmchat/__init__.py b/nonebot_plugin_llmchat/__init__.py index 57135e1..b7e54f9 100644 --- a/nonebot_plugin_llmchat/__init__.py +++ b/nonebot_plugin_llmchat/__init__.py @@ -82,6 +82,7 @@ class GroupState: self.past_events = deque(maxlen=plugin_config.past_events_size) self.group_prompt: Optional[str] = None self.output_reasoning_content = False + self.random_trigger_prob = plugin_config.random_trigger_prob group_states: dict[int, GroupState] = defaultdict(GroupState) @@ -337,7 +338,7 @@ async def handle_preset(event: GroupMessageEvent, args: Message = CommandArg()): edit_preset_handler = on_command( "修改设定", - priority=1, + priority=99, block=True, permission=(SUPERUSER | GROUP_ADMIN | GROUP_OWNER), ) @@ -368,6 +369,29 @@ async def handle_reset(event: GroupMessageEvent, args: Message = CommandArg()): group_states[group_id].history.clear() await reset_handler.finish("记忆已清空") +set_prob_handler = on_command( + "设置主动回复概率", + priority=99, + block=True, + permission=(SUPERUSER | GROUP_ADMIN | GROUP_OWNER), +) + + +@set_prob_handler.handle() +async def handle_set_prob(event: GroupMessageEvent, args: Message = CommandArg()): + group_id = event.group_id + prob = 0 + + try: + prob=float(args.extract_plain_text().strip()) + if prob<0 or prob>1: + raise ValueError + except Exception as e: + await reset_handler.finish(f"输入有误,请使用 [0,1] 的浮点数\n{e!s}") + + group_states[group_id].random_trigger_prob = prob + await reset_handler.finish(f"主动回复概率已设为 {prob}") + # 预设切换命令 think_handler = on_command( @@ -406,6 +430,7 @@ async def save_state(): "last_active": state.last_active, "group_prompt": state.group_prompt, "output_reasoning_content": state.output_reasoning_content, + "random_trigger_prob": state.random_trigger_prob, } for gid, state in group_states.items() } @@ -432,6 +457,7 @@ async def load_state(): state.last_active = state_data["last_active"] state.group_prompt = state_data["group_prompt"] state.output_reasoning_content = state_data["output_reasoning_content"] + state.random_trigger_prob = (state_data.get("random_trigger_prob") or plugin_config.random_trigger_prob) group_states[int(gid)] = state