feat: 优化willing_manager逻辑,增加回复保底概率
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from .config import global_config
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
from .config import global_config
|
||||||
|
|
||||||
|
|
||||||
class WillingManager:
|
class WillingManager:
|
||||||
@@ -8,13 +8,18 @@ class WillingManager:
|
|||||||
self.group_reply_willing = {} # 存储每个群的回复意愿
|
self.group_reply_willing = {} # 存储每个群的回复意愿
|
||||||
self._decay_task = None
|
self._decay_task = None
|
||||||
self._started = False
|
self._started = False
|
||||||
|
self.min_reply_willing = 0.01
|
||||||
|
self.attenuation_coefficient = 0.75
|
||||||
|
|
||||||
async def _decay_reply_willing(self):
|
async def _decay_reply_willing(self):
|
||||||
"""定期衰减回复意愿"""
|
"""定期衰减回复意愿"""
|
||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
for group_id in self.group_reply_willing:
|
for group_id in self.group_reply_willing:
|
||||||
self.group_reply_willing[group_id] = max(0, self.group_reply_willing[group_id] * 0.6)
|
self.group_reply_willing[group_id] = max(
|
||||||
|
self.min_reply_willing,
|
||||||
|
self.group_reply_willing[group_id] * self.attenuation_coefficient
|
||||||
|
)
|
||||||
|
|
||||||
def get_willing(self, group_id: int) -> float:
|
def get_willing(self, group_id: int) -> float:
|
||||||
"""获取指定群组的回复意愿"""
|
"""获取指定群组的回复意愿"""
|
||||||
@@ -24,45 +29,66 @@ class WillingManager:
|
|||||||
"""设置指定群组的回复意愿"""
|
"""设置指定群组的回复意愿"""
|
||||||
self.group_reply_willing[group_id] = willing
|
self.group_reply_willing[group_id] = willing
|
||||||
|
|
||||||
def change_reply_willing_received(self, group_id: int, topic: str, is_mentioned_bot: bool, config, user_id: int = None, is_emoji: bool = False, interested_rate: float = 0) -> float:
|
def change_reply_willing_received(self, group_id: int, topic: str, is_mentioned_bot: bool, config,
|
||||||
"""改变指定群组的回复意愿并返回回复概率"""
|
user_id: int = None, is_emoji: bool = False, interested_rate: float = 0) -> float:
|
||||||
|
|
||||||
|
# 若非目标回复群组,则直接return
|
||||||
|
if group_id not in config.talk_allowed_groups:
|
||||||
|
reply_probability = 0
|
||||||
|
return reply_probability
|
||||||
|
|
||||||
current_willing = self.group_reply_willing.get(group_id, 0)
|
current_willing = self.group_reply_willing.get(group_id, 0)
|
||||||
|
|
||||||
# print(f"初始意愿: {current_willing}")
|
logger.debug(f"[{group_id}]的初始回复意愿: {current_willing}")
|
||||||
if is_mentioned_bot and current_willing < 1.0:
|
|
||||||
current_willing += 0.9
|
# 根据消息类型(被cue/表情包)调控
|
||||||
logger.info(f"被提及, 当前意愿: {current_willing}")
|
if is_mentioned_bot:
|
||||||
elif is_mentioned_bot:
|
current_willing = min(
|
||||||
current_willing += 0.05
|
3.0,
|
||||||
logger.info(f"被重复提及, 当前意愿: {current_willing}")
|
current_willing + 0.9
|
||||||
|
)
|
||||||
|
logger.debug(f"被提及, 当前意愿: {current_willing}")
|
||||||
|
|
||||||
if is_emoji:
|
if is_emoji:
|
||||||
current_willing *= 0.1
|
current_willing *= 0.1
|
||||||
logger.info(f"表情包, 当前意愿: {current_willing}")
|
logger.debug(f"表情包, 当前意愿: {current_willing}")
|
||||||
|
|
||||||
logger.debug(f"放大系数_interested_rate: {global_config.response_interested_rate_amplifier}")
|
# 兴趣放大系数,若兴趣 > 0.4则增加回复概率
|
||||||
interested_rate *= global_config.response_interested_rate_amplifier #放大回复兴趣度
|
interested_rate_amplifier = global_config.response_interested_rate_amplifier
|
||||||
if interested_rate > 0.4:
|
logger.debug(f"放大系数_interested_rate: {interested_rate_amplifier}")
|
||||||
# print(f"兴趣度: {interested_rate}, 当前意愿: {current_willing}")
|
interested_rate *= interested_rate_amplifier
|
||||||
current_willing += interested_rate-0.4
|
|
||||||
|
|
||||||
current_willing *= global_config.response_willing_amplifier #放大回复意愿
|
current_willing += max(
|
||||||
# print(f"放大系数_willing: {global_config.response_willing_amplifier}, 当前意愿: {current_willing}")
|
0.0,
|
||||||
|
interested_rate - 0.4
|
||||||
|
)
|
||||||
|
|
||||||
reply_probability = max((current_willing - 0.45) * 2, 0)
|
# 回复意愿系数调控,独立乘区
|
||||||
if group_id not in config.talk_allowed_groups:
|
willing_amplifier = max(
|
||||||
current_willing = 0
|
global_config.response_willing_amplifier,
|
||||||
reply_probability = 0
|
self.min_reply_willing
|
||||||
|
)
|
||||||
|
current_willing *= willing_amplifier
|
||||||
|
logger.debug(f"放大系数_willing: {global_config.response_willing_amplifier}, 当前意愿: {current_willing}")
|
||||||
|
|
||||||
|
# 回复概率迭代,保底0.01回复概率
|
||||||
|
reply_probability = max(
|
||||||
|
(current_willing - 0.45) * 2,
|
||||||
|
self.min_reply_willing
|
||||||
|
)
|
||||||
|
|
||||||
|
# 降低目标低频群组回复概率
|
||||||
|
down_frequency_rate = max(
|
||||||
|
1.0,
|
||||||
|
global_config.down_frequency_rate
|
||||||
|
)
|
||||||
if group_id in config.talk_frequency_down_groups:
|
if group_id in config.talk_frequency_down_groups:
|
||||||
reply_probability = reply_probability / global_config.down_frequency_rate
|
reply_probability = reply_probability / down_frequency_rate
|
||||||
|
|
||||||
reply_probability = min(reply_probability, 1)
|
reply_probability = min(reply_probability, 1)
|
||||||
if reply_probability < 0:
|
|
||||||
reply_probability = 0
|
|
||||||
|
|
||||||
|
|
||||||
self.group_reply_willing[group_id] = min(current_willing, 3.0)
|
self.group_reply_willing[group_id] = min(current_willing, 3.0)
|
||||||
|
logger.debug(f"当前群组{group_id}回复概率:{reply_probability}")
|
||||||
return reply_probability
|
return reply_probability
|
||||||
|
|
||||||
def change_reply_willing_sent(self, group_id: int):
|
def change_reply_willing_sent(self, group_id: int):
|
||||||
@@ -83,5 +109,6 @@ class WillingManager:
|
|||||||
self._decay_task = asyncio.create_task(self._decay_reply_willing())
|
self._decay_task = asyncio.create_task(self._decay_reply_willing())
|
||||||
self._started = True
|
self._started = True
|
||||||
|
|
||||||
|
|
||||||
# 创建全局实例
|
# 创建全局实例
|
||||||
willing_manager = WillingManager()
|
willing_manager = WillingManager()
|
||||||
|
|||||||
Reference in New Issue
Block a user