fix:胡乱修复mxp,略微提高关系构建频率
This commit is contained in:
@@ -2,7 +2,7 @@ import asyncio
|
||||
import time
|
||||
import traceback
|
||||
from random import random
|
||||
from typing import List, Optional # 导入 Optional
|
||||
from typing import List, Optional, Dict, Any # 导入类型提示
|
||||
from maim_message import UserInfo, Seg
|
||||
from src.common.logger import get_logger
|
||||
from src.chat.heart_flow.utils_chat import get_chat_type_and_target_info
|
||||
@@ -811,10 +811,10 @@ class NormalChat:
|
||||
|
||||
# 检查是否满足关系构建条件
|
||||
should_build_relation = (
|
||||
total_messages >= 40 # 40条消息必定满足
|
||||
or (total_messages >= 25 and time_elapsed >= 600) # 25条且10分钟
|
||||
or (total_messages >= 20 and time_elapsed >= 900) # 20条且30分钟
|
||||
or (total_messages >= 10 and time_elapsed >= 1800) # 10条且1小时
|
||||
total_messages >= 30 # 30条消息必定满足
|
||||
or (total_messages >= 15 and time_elapsed >= 600) # 15条且10分钟
|
||||
or (total_messages >= 10 and time_elapsed >= 900) # 10条且15分钟
|
||||
or (total_messages >= 5 and time_elapsed >= 1800) # 5条且30
|
||||
)
|
||||
|
||||
if should_build_relation:
|
||||
@@ -894,21 +894,73 @@ class NormalChat:
|
||||
end_time = stats["last_time"]
|
||||
|
||||
# 获取该时间段的所有消息用于关系构建
|
||||
messages = get_raw_msg_by_timestamp_with_chat(self.stream_id, start_time, end_time)
|
||||
main_messages = get_raw_msg_by_timestamp_with_chat(self.stream_id, start_time, end_time)
|
||||
|
||||
if messages:
|
||||
logger.info(f"[{self.stream_name}] 为用户 {person_id} 获取到 {len(messages)} 条消息用于关系构建")
|
||||
|
||||
# 调用关系管理器更新印象
|
||||
relationship_manager = get_relationship_manager()
|
||||
await relationship_manager.update_person_impression(
|
||||
person_id=person_id, timestamp=end_time, bot_engaged_messages=messages
|
||||
)
|
||||
|
||||
logger.info(f"[{self.stream_name}] 用户 {person_id} 关系构建完成")
|
||||
else:
|
||||
if not main_messages:
|
||||
logger.warning(f"[{self.stream_name}] 未找到用户 {person_id} 的消息,关系构建跳过")
|
||||
return
|
||||
|
||||
# 获取第一条消息的时间戳,然后获取之前的5条消息
|
||||
first_message_time = main_messages[0]["time"]
|
||||
before_messages = self._get_messages_before_timestamp(first_message_time, 5)
|
||||
|
||||
# 获取最后一条消息的时间戳,然后获取之后的5条消息
|
||||
last_message_time = main_messages[-1]["time"]
|
||||
after_messages = self._get_messages_after_timestamp(last_message_time, 5)
|
||||
|
||||
# 合并所有消息并去重
|
||||
all_messages = before_messages + main_messages + after_messages
|
||||
|
||||
# 根据消息ID去重并按时间排序
|
||||
seen_ids = set()
|
||||
unique_messages = []
|
||||
for msg in all_messages:
|
||||
msg_id = msg["message_id"]
|
||||
if msg_id not in seen_ids:
|
||||
seen_ids.add(msg_id)
|
||||
unique_messages.append(msg)
|
||||
|
||||
# 按时间排序
|
||||
unique_messages.sort(key=lambda x: x["time"])
|
||||
|
||||
logger.info(
|
||||
f"[{self.stream_name}] 为用户 {person_id} 获取到消息用于关系构建: "
|
||||
f"原时间段内 {len(main_messages)} 条,之前 {len(before_messages)} 条,"
|
||||
f"之后 {len(after_messages)} 条,去重后总计 {len(unique_messages)} 条"
|
||||
)
|
||||
|
||||
# 调用关系管理器更新印象
|
||||
relationship_manager = get_relationship_manager()
|
||||
await relationship_manager.update_person_impression(
|
||||
person_id=person_id, timestamp=end_time, bot_engaged_messages=unique_messages
|
||||
)
|
||||
|
||||
logger.info(f"[{self.stream_name}] 用户 {person_id} 关系构建完成")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[{self.stream_name}] 为用户 {person_id} 构建关系时出错: {e}")
|
||||
traceback.print_exc()
|
||||
|
||||
def _get_messages_before_timestamp(self, timestamp: float, limit: int = 5) -> List[Dict[str, Any]]:
|
||||
"""获取指定时间戳之前的指定数量消息"""
|
||||
try:
|
||||
from src.common.message_repository import find_messages
|
||||
filter_query = {"chat_id": self.stream_id, "time": {"$lt": timestamp}}
|
||||
sort_order = [("time", -1)] # 倒序排列,取最近的几条
|
||||
messages = find_messages(message_filter=filter_query, sort=sort_order, limit=limit)
|
||||
# 返回时保持正序
|
||||
return sorted(messages, key=lambda x: x["time"])
|
||||
except Exception as e:
|
||||
logger.error(f"[{self.stream_name}] 获取时间戳之前的消息失败: {e}")
|
||||
return []
|
||||
|
||||
def _get_messages_after_timestamp(self, timestamp: float, limit: int = 5) -> List[Dict[str, Any]]:
|
||||
"""获取指定时间戳之后的指定数量消息"""
|
||||
try:
|
||||
from src.common.message_repository import find_messages
|
||||
filter_query = {"chat_id": self.stream_id, "time": {"$gt": timestamp}}
|
||||
sort_order = [("time", 1)] # 正序排列,取最早的几条
|
||||
return find_messages(message_filter=filter_query, sort=sort_order, limit=limit)
|
||||
except Exception as e:
|
||||
logger.error(f"[{self.stream_name}] 获取时间戳之后的消息失败: {e}")
|
||||
return []
|
||||
|
||||
@@ -5,7 +5,7 @@ Mxp 模式:梦溪畔独家赞助
|
||||
此模式的可变参数暂时比较草率,需要调参仙人的大手
|
||||
此模式的特点:
|
||||
1.每个聊天流的每个用户的意愿是独立的
|
||||
2.接入关系系统,关系会影响意愿值
|
||||
2.接入关系系统,关系会影响意愿值(已移除,因为关系系统重构)
|
||||
3.会根据群聊的热度来调整基础意愿值
|
||||
4.限制同时思考的消息数量,防止喷射
|
||||
5.拥有单聊增益,无论在群里还是私聊,只要bot一直和你聊,就会增加意愿值
|
||||
@@ -83,9 +83,10 @@ class MxpWillingManager(BaseWillingManager):
|
||||
"""回复后处理"""
|
||||
async with self.lock:
|
||||
w_info = self.ongoing_messages[message_id]
|
||||
rel_value = await w_info.person_info_manager.get_value(w_info.person_id, "relationship_value")
|
||||
rel_level = self._get_relationship_level_num(rel_value)
|
||||
self.chat_person_reply_willing[w_info.chat_id][w_info.person_id] += rel_level * 0.05
|
||||
# 移除关系值相关代码
|
||||
# rel_value = await w_info.person_info_manager.get_value(w_info.person_id, "relationship_value")
|
||||
# rel_level = self._get_relationship_level_num(rel_value)
|
||||
# self.chat_person_reply_willing[w_info.chat_id][w_info.person_id] += rel_level * 0.05
|
||||
|
||||
now_chat_new_person = self.last_response_person.get(w_info.chat_id, [w_info.person_id, 0])
|
||||
if now_chat_new_person[0] == w_info.person_id:
|
||||
@@ -135,12 +136,7 @@ class MxpWillingManager(BaseWillingManager):
|
||||
|
||||
self.chat_person_reply_willing[w_info.chat_id][w_info.person_id] = current_willing
|
||||
|
||||
rel_value = await w_info.person_info_manager.get_value(w_info.person_id, "relationship_value")
|
||||
rel_level = self._get_relationship_level_num(rel_value)
|
||||
current_willing += rel_level * 0.1
|
||||
if self.is_debug and rel_level != 0:
|
||||
self.logger.debug(f"关系增益:{rel_level * 0.1}")
|
||||
|
||||
# 添加单聊增益
|
||||
if (
|
||||
w_info.chat_id in self.last_response_person
|
||||
and self.last_response_person[w_info.chat_id][0] == w_info.person_id
|
||||
@@ -288,25 +284,6 @@ class MxpWillingManager(BaseWillingManager):
|
||||
if self.is_debug:
|
||||
self.logger.debug(f"聊天流意愿值更新:{self.chat_reply_willing}")
|
||||
|
||||
@staticmethod
|
||||
def _get_relationship_level_num(relationship_value) -> int:
|
||||
"""关系等级计算"""
|
||||
if -1000 <= relationship_value < -227:
|
||||
level_num = 0
|
||||
elif -227 <= relationship_value < -73:
|
||||
level_num = 1
|
||||
elif -73 <= relationship_value < 227:
|
||||
level_num = 2
|
||||
elif 227 <= relationship_value < 587:
|
||||
level_num = 3
|
||||
elif 587 <= relationship_value < 900:
|
||||
level_num = 4
|
||||
elif 900 <= relationship_value <= 1000:
|
||||
level_num = 5
|
||||
else:
|
||||
level_num = 5 if relationship_value > 1000 else 0
|
||||
return level_num - 2
|
||||
|
||||
def _basic_willing_culculate(self, t: float) -> float:
|
||||
"""基础意愿值计算"""
|
||||
return math.tan(t * self.expected_replies_per_min * math.pi / 120 / self.number_of_message_storage) / 2
|
||||
|
||||
Reference in New Issue
Block a user