better:重构personinfo,使用Person类和类属性
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
from .person_info import get_person_info_manager
|
||||
from .group_info import get_group_info_manager
|
||||
|
||||
__all__ = ["get_person_info_manager", "get_group_info_manager"]
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import copy
|
||||
import hashlib
|
||||
import datetime
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
import random
|
||||
|
||||
from json_repair import repair_json
|
||||
from typing import Any, Callable, Dict, Union, Optional
|
||||
from typing import Union
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.common.database.database import db
|
||||
@@ -14,45 +14,276 @@ from src.llm_models.utils_model import LLMRequest
|
||||
from src.config.config import global_config, model_config
|
||||
|
||||
|
||||
"""
|
||||
PersonInfoManager 类方法功能摘要:
|
||||
1. get_person_id - 根据平台和用户ID生成MD5哈希的唯一person_id
|
||||
2. create_person_info - 创建新个人信息文档(自动合并默认值)
|
||||
3. update_one_field - 更新单个字段值(若文档不存在则创建)
|
||||
4. del_one_document - 删除指定person_id的文档
|
||||
5. get_value - 获取单个字段值(返回实际值或默认值)
|
||||
6. get_values - 批量获取字段值(任一字段无效则返回空字典)
|
||||
7. del_all_undefined_field - 清理全集合中未定义的字段
|
||||
8. get_specific_value_list - 根据指定条件,返回person_id,value字典
|
||||
"""
|
||||
|
||||
|
||||
logger = get_logger("person_info")
|
||||
|
||||
JSON_SERIALIZED_FIELDS = ["points"]
|
||||
def get_person_id(platform: str, user_id: Union[int, str]) -> str:
|
||||
"""获取唯一id"""
|
||||
if "-" in platform:
|
||||
platform = platform.split("-")[1]
|
||||
components = [platform, str(user_id)]
|
||||
key = "_".join(components)
|
||||
return hashlib.md5(key.encode()).hexdigest()
|
||||
|
||||
person_info_default = {
|
||||
"person_id": None,
|
||||
"person_name": None,
|
||||
"name_reason": None, # Corrected from person_name_reason to match common usage if intended
|
||||
"platform": "unknown",
|
||||
"user_id": "unknown",
|
||||
"nickname": "Unknown",
|
||||
"know_times": 0,
|
||||
"know_since": None,
|
||||
"last_know": None,
|
||||
"attitude_to_me": "0,1",
|
||||
"friendly_value": 50,
|
||||
"rudeness":50,
|
||||
"neuroticism":"5,1",
|
||||
"conscientiousness": 50,
|
||||
"likeness": 50,
|
||||
"points": None,
|
||||
}
|
||||
def get_person_id_by_person_name(person_name: str) -> str:
|
||||
"""根据用户名获取用户ID"""
|
||||
try:
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_name == person_name)
|
||||
return record.person_id if record else ""
|
||||
except Exception as e:
|
||||
logger.error(f"根据用户名 {person_name} 获取用户ID时出错 (Peewee): {e}")
|
||||
return ""
|
||||
|
||||
class Person:
|
||||
def __init__(self, platform: str = "", user_id: str = "",person_id: str = "",person_name: str = "",nickname: str = ""):
|
||||
if person_id:
|
||||
self.person_id = person_id
|
||||
elif person_name:
|
||||
self.person_id = get_person_id_by_person_name(person_name)
|
||||
if not self.person_id:
|
||||
logger.error(f"根据用户名 {person_name} 获取用户ID时出错,不存在用户{person_name}")
|
||||
return ""
|
||||
elif platform and user_id:
|
||||
self.person_id = get_person_id(platform, user_id)
|
||||
else:
|
||||
logger.error("Person 初始化失败,缺少必要参数")
|
||||
return ""
|
||||
|
||||
self.is_known = False
|
||||
self.platform = platform
|
||||
self.user_id = user_id
|
||||
|
||||
# 初始化默认值
|
||||
self.nickname = nickname
|
||||
self.person_name = None
|
||||
self.name_reason = None
|
||||
self.know_times = 0
|
||||
self.know_since = None
|
||||
self.last_know = None
|
||||
self.points = []
|
||||
|
||||
# 初始化性格特征相关字段
|
||||
self.attitude_to_me:float = 0
|
||||
self.attitude_to_me_confidence:float = 1
|
||||
|
||||
self.neuroticism:float = 5
|
||||
self.neuroticism_confidence:float = 1
|
||||
|
||||
self.friendly_value:float = 50
|
||||
self.friendly_value_confidence:float = 1
|
||||
|
||||
self.rudeness:float = 50
|
||||
self.rudeness_confidence:float = 1
|
||||
|
||||
self.conscientiousness:float = 50
|
||||
self.conscientiousness_confidence:float = 1
|
||||
|
||||
self.likeness:float = 50
|
||||
self.likeness_confidence:float = 1
|
||||
|
||||
# 从数据库加载数据
|
||||
self.load_from_database()
|
||||
|
||||
def load_from_database(self):
|
||||
"""从数据库加载个人信息数据"""
|
||||
try:
|
||||
# 查询数据库中的记录
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_id == self.person_id)
|
||||
|
||||
if record:
|
||||
self.is_known = record.is_known if record.is_known else False
|
||||
self.nickname = record.nickname if record.nickname else self.nickname
|
||||
|
||||
if not self.is_known:
|
||||
if self.nickname:
|
||||
self.is_known = True
|
||||
self.person_name = self.nickname
|
||||
logger.info(f"用户 {self.person_id} 已认识,昵称:{self.nickname}")
|
||||
else:
|
||||
logger.warning(f"用户 {self.person_id} 尚未认识,昵称为空")
|
||||
else:
|
||||
self.person_name = record.person_name if record.person_name else self.nickname
|
||||
self.name_reason = record.name_reason if record.name_reason else None
|
||||
self.know_times = record.know_times if record.know_times else 0
|
||||
self.know_since = record.know_since if record.know_since else time.time()
|
||||
self.last_know = record.last_know if record.last_know else time.time()
|
||||
|
||||
# 处理points字段(JSON格式的列表)
|
||||
if record.points:
|
||||
try:
|
||||
self.points = json.loads(record.points)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
logger.warning(f"解析用户 {self.person_id} 的points字段失败,使用默认值")
|
||||
self.points = []
|
||||
else:
|
||||
self.points = []
|
||||
|
||||
# 加载性格特征相关字段
|
||||
if record.attitude_to_me and not isinstance(record.attitude_to_me, str):
|
||||
self.attitude_to_me = record.attitude_to_me
|
||||
|
||||
if record.attitude_to_me_confidence is not None:
|
||||
self.attitude_to_me_confidence = float(record.attitude_to_me_confidence)
|
||||
|
||||
if record.friendly_value is not None:
|
||||
self.friendly_value = float(record.friendly_value)
|
||||
|
||||
if record.friendly_value_confidence is not None:
|
||||
self.friendly_value_confidence = float(record.friendly_value_confidence)
|
||||
|
||||
if record.rudeness is not None:
|
||||
self.rudeness = float(record.rudeness)
|
||||
|
||||
if record.rudeness_confidence is not None:
|
||||
self.rudeness_confidence = float(record.rudeness_confidence)
|
||||
|
||||
if record.neuroticism and not isinstance(record.neuroticism, str):
|
||||
self.neuroticism = float(record.neuroticism)
|
||||
|
||||
if record.neuroticism_confidence is not None:
|
||||
self.neuroticism_confidence = float(record.neuroticism_confidence)
|
||||
|
||||
if record.conscientiousness is not None:
|
||||
self.conscientiousness = float(record.conscientiousness)
|
||||
|
||||
if record.conscientiousness_confidence is not None:
|
||||
self.conscientiousness_confidence = float(record.conscientiousness_confidence)
|
||||
|
||||
if record.likeness is not None:
|
||||
self.likeness = float(record.likeness)
|
||||
|
||||
if record.likeness_confidence is not None:
|
||||
self.likeness_confidence = float(record.likeness_confidence)
|
||||
|
||||
logger.info(f"已从数据库加载用户 {self.person_id} 的信息")
|
||||
else:
|
||||
self.sync_to_database()
|
||||
logger.info(f"用户 {self.person_id} 在数据库中不存在,使用默认值并创建")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"从数据库加载用户 {self.person_id} 信息时出错: {e}")
|
||||
# 出错时保持默认值
|
||||
|
||||
def sync_to_database(self):
|
||||
"""将所有属性同步回数据库"""
|
||||
try:
|
||||
# 准备数据
|
||||
data = {
|
||||
'person_id': self.person_id,
|
||||
'is_known': self.is_known,
|
||||
'platform': self.platform,
|
||||
'user_id': self.user_id,
|
||||
'nickname': self.nickname,
|
||||
'person_name': self.person_name,
|
||||
'name_reason': self.name_reason,
|
||||
'know_times': self.know_times,
|
||||
'know_since': self.know_since,
|
||||
'last_know': self.last_know,
|
||||
'points': json.dumps(self.points, ensure_ascii=False) if self.points else json.dumps([], ensure_ascii=False),
|
||||
'attitude_to_me': self.attitude_to_me,
|
||||
'attitude_to_me_confidence': self.attitude_to_me_confidence,
|
||||
'friendly_value': self.friendly_value,
|
||||
'friendly_value_confidence': self.friendly_value_confidence,
|
||||
'rudeness': self.rudeness,
|
||||
'rudeness_confidence': self.rudeness_confidence,
|
||||
'neuroticism': self.neuroticism,
|
||||
'neuroticism_confidence': self.neuroticism_confidence,
|
||||
'conscientiousness': self.conscientiousness,
|
||||
'conscientiousness_confidence': self.conscientiousness_confidence,
|
||||
'likeness': self.likeness,
|
||||
'likeness_confidence': self.likeness_confidence,
|
||||
}
|
||||
|
||||
# 检查记录是否存在
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_id == self.person_id)
|
||||
|
||||
if record:
|
||||
# 更新现有记录
|
||||
for field, value in data.items():
|
||||
if hasattr(record, field):
|
||||
setattr(record, field, value)
|
||||
record.save()
|
||||
logger.debug(f"已同步用户 {self.person_id} 的信息到数据库")
|
||||
else:
|
||||
# 创建新记录
|
||||
PersonInfo.create(**data)
|
||||
logger.debug(f"已创建用户 {self.person_id} 的信息到数据库")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"同步用户 {self.person_id} 信息到数据库时出错: {e}")
|
||||
|
||||
def build_relationship(self,points_num=3):
|
||||
if not self.is_known:
|
||||
return ""
|
||||
|
||||
# 按时间排序forgotten_points
|
||||
current_points = self.points
|
||||
current_points.sort(key=lambda x: x[2])
|
||||
# 按权重加权随机抽取最多3个不重复的points,point[1]的值在1-10之间,权重越高被抽到概率越大
|
||||
if len(current_points) > points_num:
|
||||
# point[1] 取值范围1-10,直接作为权重
|
||||
weights = [max(1, min(10, int(point[1]))) for point in current_points]
|
||||
# 使用加权采样不放回,保证不重复
|
||||
indices = list(range(len(current_points)))
|
||||
points = []
|
||||
for _ in range(points_num):
|
||||
if not indices:
|
||||
break
|
||||
sub_weights = [weights[i] for i in indices]
|
||||
chosen_idx = random.choices(indices, weights=sub_weights, k=1)[0]
|
||||
points.append(current_points[chosen_idx])
|
||||
indices.remove(chosen_idx)
|
||||
else:
|
||||
points = current_points
|
||||
|
||||
# 构建points文本
|
||||
points_text = "\n".join([f"{point[2]}:{point[0]}" for point in points])
|
||||
|
||||
nickname_str = ""
|
||||
if self.person_name != nickname_str:
|
||||
nickname_str = f"(ta在{self.platform}上的昵称是{nickname_str})"
|
||||
|
||||
relation_info = ""
|
||||
|
||||
attitude_info = ""
|
||||
if self.attitude_to_me:
|
||||
if self.attitude_to_me > 8:
|
||||
attitude_info = f"{self.person_name}对你的态度十分好,"
|
||||
elif self.attitude_to_me > 5:
|
||||
attitude_info = f"{self.person_name}对你的态度较好,"
|
||||
|
||||
|
||||
if self.attitude_to_me < -8:
|
||||
attitude_info = f"{self.person_name}对你的态度十分恶劣,"
|
||||
elif self.attitude_to_me < -4:
|
||||
attitude_info = f"{self.person_name}对你的态度不好,"
|
||||
elif self.attitude_to_me < 0:
|
||||
attitude_info = f"{self.person_name}对你的态度一般,"
|
||||
|
||||
neuroticism_info = ""
|
||||
if self.neuroticism:
|
||||
if self.neuroticism > 8:
|
||||
neuroticism_info = f"{self.person_name}的情绪十分活跃,容易情绪化,"
|
||||
elif self.neuroticism > 6:
|
||||
neuroticism_info = f"{self.person_name}的情绪比较活跃,"
|
||||
elif self.neuroticism > 4:
|
||||
neuroticism_info = ""
|
||||
elif self.neuroticism > 2:
|
||||
neuroticism_info = f"{self.person_name}的情绪比较稳定,"
|
||||
else:
|
||||
neuroticism_info = f"{self.person_name}的情绪非常稳定,毫无波动"
|
||||
|
||||
points_info = ""
|
||||
if points_text:
|
||||
points_info = f"你还记得ta最近做的事:{points_text}"
|
||||
|
||||
relation_info = f"{self.person_name}:{nickname_str}{attitude_info}{neuroticism_info}{points_info}"
|
||||
|
||||
return relation_info
|
||||
|
||||
|
||||
class PersonInfoManager:
|
||||
def __init__(self):
|
||||
|
||||
self.person_name_list = {}
|
||||
self.qv_name_llm = LLMRequest(model_set=model_config.model_task_config.utils, request_type="relation.qv_name")
|
||||
try:
|
||||
@@ -77,158 +308,11 @@ class PersonInfoManager:
|
||||
logger.debug(f"已加载 {len(self.person_name_list)} 个用户名称 (Peewee)")
|
||||
except Exception as e:
|
||||
logger.error(f"从 Peewee 加载 person_name_list 失败: {e}")
|
||||
|
||||
def get_person(self, platform: str, user_id: Union[int, str]) -> Person:
|
||||
person = Person(platform, user_id)
|
||||
return person
|
||||
|
||||
@staticmethod
|
||||
def get_person_id(platform: str, user_id: Union[int, str]) -> str:
|
||||
"""获取唯一id"""
|
||||
# 添加空值检查,防止 platform 为 None 时出错
|
||||
if platform is None:
|
||||
platform = "unknown"
|
||||
elif "-" in platform:
|
||||
platform = platform.split("-")[1]
|
||||
|
||||
components = [platform, str(user_id)]
|
||||
key = "_".join(components)
|
||||
return hashlib.md5(key.encode()).hexdigest()
|
||||
|
||||
async def is_person_known(self, platform: str, user_id: int):
|
||||
"""判断是否认识某人"""
|
||||
person_id = self.get_person_id(platform, user_id)
|
||||
|
||||
def _db_check_known_sync(p_id: str):
|
||||
return PersonInfo.get_or_none(PersonInfo.person_id == p_id) is not None
|
||||
|
||||
try:
|
||||
return await asyncio.to_thread(_db_check_known_sync, person_id)
|
||||
except Exception as e:
|
||||
logger.error(f"检查用户 {person_id} 是否已知时出错 (Peewee): {e}")
|
||||
return False
|
||||
|
||||
def get_person_id_by_person_name(self, person_name: str) -> str:
|
||||
"""根据用户名获取用户ID"""
|
||||
try:
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_name == person_name)
|
||||
return record.person_id if record else ""
|
||||
except Exception as e:
|
||||
logger.error(f"根据用户名 {person_name} 获取用户ID时出错 (Peewee): {e}")
|
||||
return ""
|
||||
|
||||
async def _safe_create_person_info(self, person_id: str, data: Optional[dict] = None):
|
||||
"""安全地创建用户信息,处理竞态条件"""
|
||||
if not person_id:
|
||||
logger.debug("创建失败,person_id不存在")
|
||||
return
|
||||
|
||||
_person_info_default = copy.deepcopy(person_info_default)
|
||||
model_fields = PersonInfo._meta.fields.keys() # type: ignore
|
||||
|
||||
final_data = {"person_id": person_id}
|
||||
|
||||
# Start with defaults for all model fields
|
||||
for key, default_value in _person_info_default.items():
|
||||
if key in model_fields:
|
||||
final_data[key] = default_value
|
||||
|
||||
# Override with provided data
|
||||
if data:
|
||||
for key, value in data.items():
|
||||
if key in model_fields:
|
||||
final_data[key] = value
|
||||
|
||||
# Ensure person_id is correctly set from the argument
|
||||
final_data["person_id"] = person_id
|
||||
|
||||
# Serialize JSON fields
|
||||
for key in JSON_SERIALIZED_FIELDS:
|
||||
if key in final_data:
|
||||
if isinstance(final_data[key], (list, dict)):
|
||||
final_data[key] = json.dumps(final_data[key], ensure_ascii=False)
|
||||
elif final_data[key] is None: # Default for lists is [], store as "[]"
|
||||
final_data[key] = json.dumps([], ensure_ascii=False)
|
||||
|
||||
def _db_safe_create_sync(p_data: dict):
|
||||
try:
|
||||
# 首先检查是否已存在
|
||||
existing = PersonInfo.get_or_none(PersonInfo.person_id == p_data["person_id"])
|
||||
if existing:
|
||||
logger.debug(f"用户 {p_data['person_id']} 已存在,跳过创建")
|
||||
return True
|
||||
|
||||
# 尝试创建
|
||||
PersonInfo.create(**p_data)
|
||||
return True
|
||||
except Exception as e:
|
||||
if "UNIQUE constraint failed" in str(e):
|
||||
logger.debug(f"检测到并发创建用户 {p_data.get('person_id')},跳过错误")
|
||||
return True # 其他协程已创建,视为成功
|
||||
else:
|
||||
logger.error(f"创建 PersonInfo 记录 {p_data.get('person_id')} 失败 (Peewee): {e}")
|
||||
return False
|
||||
|
||||
await asyncio.to_thread(_db_safe_create_sync, final_data)
|
||||
|
||||
async def update_one_field(self, person_id: str, field_name: str, value, data: Optional[Dict] = None):
|
||||
"""更新某一个字段,会补全"""
|
||||
if field_name not in PersonInfo._meta.fields: # type: ignore
|
||||
logger.debug(f"更新'{field_name}'失败,未在 PersonInfo Peewee 模型中定义的字段。")
|
||||
return
|
||||
|
||||
processed_value = value
|
||||
if field_name in JSON_SERIALIZED_FIELDS:
|
||||
if isinstance(value, (list, dict)):
|
||||
processed_value = json.dumps(value, ensure_ascii=False, indent=None)
|
||||
elif value is None: # Store None as "[]" for JSON list fields
|
||||
processed_value = json.dumps([], ensure_ascii=False, indent=None)
|
||||
|
||||
def _db_update_sync(p_id: str, f_name: str, val_to_set):
|
||||
import time
|
||||
|
||||
start_time = time.time()
|
||||
try:
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_id == p_id)
|
||||
query_time = time.time()
|
||||
|
||||
if record:
|
||||
setattr(record, f_name, val_to_set)
|
||||
record.save()
|
||||
save_time = time.time()
|
||||
|
||||
total_time = save_time - start_time
|
||||
if total_time > 0.5: # 如果超过500ms就记录日志
|
||||
logger.warning(
|
||||
f"数据库更新操作耗时 {total_time:.3f}秒 (查询: {query_time - start_time:.3f}s, 保存: {save_time - query_time:.3f}s) person_id={p_id}, field={f_name}"
|
||||
)
|
||||
|
||||
return True, False # Found and updated, no creation needed
|
||||
else:
|
||||
total_time = time.time() - start_time
|
||||
if total_time > 0.5:
|
||||
logger.warning(f"数据库查询操作耗时 {total_time:.3f}秒 person_id={p_id}, field={f_name}")
|
||||
return False, True # Not found, needs creation
|
||||
except Exception as e:
|
||||
total_time = time.time() - start_time
|
||||
logger.error(f"数据库操作异常,耗时 {total_time:.3f}秒: {e}")
|
||||
raise
|
||||
|
||||
found, needs_creation = await asyncio.to_thread(_db_update_sync, person_id, field_name, processed_value)
|
||||
|
||||
if needs_creation:
|
||||
logger.info(f"{person_id} 不存在,将新建。")
|
||||
creation_data = data if data is not None else {}
|
||||
# Ensure platform and user_id are present for context if available from 'data'
|
||||
# but primarily, set the field that triggered the update.
|
||||
# The create_person_info will handle defaults and serialization.
|
||||
creation_data[field_name] = value # Pass original value to create_person_info
|
||||
|
||||
# Ensure platform and user_id are in creation_data if available,
|
||||
# otherwise create_person_info will use defaults.
|
||||
if data and "platform" in data:
|
||||
creation_data["platform"] = data["platform"]
|
||||
if data and "user_id" in data:
|
||||
creation_data["user_id"] = data["user_id"]
|
||||
|
||||
# 使用安全的创建方法,处理竞态条件
|
||||
await self._safe_create_person_info(person_id, creation_data)
|
||||
|
||||
@staticmethod
|
||||
def _extract_json_from_text(text: str) -> dict:
|
||||
@@ -279,8 +363,9 @@ class PersonInfoManager:
|
||||
logger.debug("取名失败:person_id不能为空")
|
||||
return None
|
||||
|
||||
old_name = await self.get_value(person_id, "person_name")
|
||||
old_reason = await self.get_value(person_id, "name_reason")
|
||||
person = Person(person_id=person_id)
|
||||
old_name = person.person_name
|
||||
old_reason = person.name_reason
|
||||
|
||||
max_retries = 8
|
||||
current_try = 0
|
||||
@@ -338,8 +423,9 @@ class PersonInfoManager:
|
||||
current_name_set.add(generated_nickname)
|
||||
|
||||
if not is_duplicate:
|
||||
await self.update_one_field(person_id, "person_name", generated_nickname)
|
||||
await self.update_one_field(person_id, "name_reason", result.get("reason", "未提供理由"))
|
||||
person.person_name = generated_nickname
|
||||
person.name_reason = result.get("reason", "未提供理由")
|
||||
person.sync_to_database()
|
||||
|
||||
logger.info(
|
||||
f"成功给用户{user_nickname} {person_id} 取名 {generated_nickname},理由:{result.get('reason', '未提供理由')}"
|
||||
@@ -357,186 +443,11 @@ class PersonInfoManager:
|
||||
# 如果多次尝试后仍未成功,使用唯一的 user_nickname 作为默认值
|
||||
unique_nickname = await self._generate_unique_person_name(user_nickname)
|
||||
logger.warning(f"在{max_retries}次尝试后未能生成唯一昵称,使用默认昵称 {unique_nickname}")
|
||||
await self.update_one_field(person_id, "person_name", unique_nickname)
|
||||
await self.update_one_field(person_id, "name_reason", "使用用户原始昵称作为默认值")
|
||||
person.person_name = unique_nickname
|
||||
person.name_reason = "使用用户原始昵称作为默认值"
|
||||
person.sync_to_database()
|
||||
self.person_name_list[person_id] = unique_nickname
|
||||
return {"nickname": unique_nickname, "reason": "使用用户原始昵称作为默认值"}
|
||||
|
||||
|
||||
@staticmethod
|
||||
async def get_value(person_id: str, field_name: str):
|
||||
"""获取指定用户指定字段的值"""
|
||||
default_value_for_field = person_info_default.get(field_name)
|
||||
if field_name in JSON_SERIALIZED_FIELDS and default_value_for_field is None:
|
||||
default_value_for_field = [] # Ensure JSON fields default to [] if not in DB
|
||||
|
||||
def _db_get_value_sync(p_id: str, f_name: str):
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_id == p_id)
|
||||
if record:
|
||||
val = getattr(record, f_name, None)
|
||||
if f_name in JSON_SERIALIZED_FIELDS:
|
||||
if isinstance(val, str):
|
||||
try:
|
||||
return json.loads(val)
|
||||
except json.JSONDecodeError:
|
||||
logger.warning(f"字段 {f_name} for {p_id} 包含无效JSON: {val}. 返回默认值.")
|
||||
return [] # Default for JSON fields on error
|
||||
elif val is None: # Field exists in DB but is None
|
||||
return [] # Default for JSON fields
|
||||
# If val is already a list/dict (e.g. if somehow set without serialization)
|
||||
return val # Should ideally not happen if update_one_field is always used
|
||||
return val
|
||||
return None # Record not found
|
||||
|
||||
try:
|
||||
value_from_db = await asyncio.to_thread(_db_get_value_sync, person_id, field_name)
|
||||
if value_from_db is not None:
|
||||
return value_from_db
|
||||
if field_name in person_info_default:
|
||||
return default_value_for_field
|
||||
logger.warning(f"字段 {field_name} 在 person_info_default 中未定义,且在数据库中未找到。")
|
||||
return None # Ultimate fallback
|
||||
except Exception as e:
|
||||
logger.error(f"获取字段 {field_name} for {person_id} 时出错 (Peewee): {e}")
|
||||
# Fallback to default in case of any error during DB access
|
||||
return default_value_for_field if field_name in person_info_default else None
|
||||
|
||||
@staticmethod
|
||||
def get_value_sync(person_id: str, field_name: str):
|
||||
"""同步获取指定用户指定字段的值"""
|
||||
default_value_for_field = person_info_default.get(field_name)
|
||||
if field_name in JSON_SERIALIZED_FIELDS and default_value_for_field is None:
|
||||
default_value_for_field = []
|
||||
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_id == person_id)
|
||||
if record:
|
||||
val = getattr(record, field_name, None)
|
||||
if field_name in JSON_SERIALIZED_FIELDS:
|
||||
if isinstance(val, str):
|
||||
try:
|
||||
return json.loads(val)
|
||||
except json.JSONDecodeError:
|
||||
logger.warning(f"字段 {field_name} for {person_id} 包含无效JSON: {val}. 返回默认值.")
|
||||
return []
|
||||
elif val is None:
|
||||
return []
|
||||
return val
|
||||
return val
|
||||
|
||||
if field_name in person_info_default:
|
||||
return default_value_for_field
|
||||
logger.warning(f"字段 {field_name} 在 person_info_default 中未定义,且在数据库中未找到。")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
async def get_values(person_id: str, field_names: list) -> dict:
|
||||
"""获取指定person_id文档的多个字段值,若不存在该字段,则返回该字段的全局默认值"""
|
||||
if not person_id:
|
||||
logger.debug("get_values获取失败:person_id不能为空")
|
||||
return {}
|
||||
|
||||
result = {}
|
||||
|
||||
def _db_get_record_sync(p_id: str):
|
||||
return PersonInfo.get_or_none(PersonInfo.person_id == p_id)
|
||||
|
||||
record = await asyncio.to_thread(_db_get_record_sync, person_id)
|
||||
|
||||
for field_name in field_names:
|
||||
if field_name not in PersonInfo._meta.fields: # type: ignore
|
||||
if field_name in person_info_default:
|
||||
result[field_name] = copy.deepcopy(person_info_default[field_name])
|
||||
logger.debug(f"字段'{field_name}'不在Peewee模型中,使用默认配置值。")
|
||||
else:
|
||||
logger.debug(f"get_values查询失败:字段'{field_name}'未在Peewee模型和默认配置中定义。")
|
||||
result[field_name] = None
|
||||
continue
|
||||
|
||||
if record:
|
||||
value = getattr(record, field_name)
|
||||
if value is not None:
|
||||
result[field_name] = value
|
||||
else:
|
||||
result[field_name] = copy.deepcopy(person_info_default.get(field_name))
|
||||
else:
|
||||
result[field_name] = copy.deepcopy(person_info_default.get(field_name))
|
||||
|
||||
return result
|
||||
|
||||
async def get_or_create_person(
|
||||
self, platform: str, user_id: int, nickname: str, user_cardname: str, user_avatar: Optional[str] = None
|
||||
) -> str:
|
||||
"""
|
||||
根据 platform 和 user_id 获取 person_id。
|
||||
如果对应的用户不存在,则使用提供的可选信息创建新用户。
|
||||
使用try-except处理竞态条件,避免重复创建错误。
|
||||
"""
|
||||
person_id = self.get_person_id(platform, user_id)
|
||||
|
||||
def _db_get_or_create_sync(p_id: str, init_data: dict):
|
||||
"""原子性的获取或创建操作"""
|
||||
# 首先尝试获取现有记录
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_id == p_id)
|
||||
if record:
|
||||
return record, False # 记录存在,未创建
|
||||
|
||||
# 记录不存在,尝试创建
|
||||
try:
|
||||
PersonInfo.create(**init_data)
|
||||
return PersonInfo.get(PersonInfo.person_id == p_id), True # 创建成功
|
||||
except Exception as e:
|
||||
# 如果创建失败(可能是因为竞态条件),再次尝试获取
|
||||
if "UNIQUE constraint failed" in str(e):
|
||||
logger.debug(f"检测到并发创建用户 {p_id},获取现有记录")
|
||||
record = PersonInfo.get_or_none(PersonInfo.person_id == p_id)
|
||||
if record:
|
||||
return record, False # 其他协程已创建,返回现有记录
|
||||
# 如果仍然失败,重新抛出异常
|
||||
raise e
|
||||
|
||||
unique_nickname = await self._generate_unique_person_name(nickname)
|
||||
initial_data = {
|
||||
"person_id": person_id,
|
||||
"platform": platform,
|
||||
"user_id": str(user_id),
|
||||
"nickname": nickname,
|
||||
"person_name": unique_nickname, # 使用群昵称作为person_name
|
||||
"name_reason": "从群昵称获取",
|
||||
"know_times": 0,
|
||||
"know_since": int(datetime.datetime.now().timestamp()),
|
||||
"last_know": int(datetime.datetime.now().timestamp()),
|
||||
"impression": None,
|
||||
"points": [],
|
||||
"forgotten_points": [],
|
||||
}
|
||||
|
||||
# 序列化JSON字段
|
||||
for key in JSON_SERIALIZED_FIELDS:
|
||||
if key in initial_data:
|
||||
if isinstance(initial_data[key], (list, dict)):
|
||||
initial_data[key] = json.dumps(initial_data[key], ensure_ascii=False)
|
||||
elif initial_data[key] is None:
|
||||
initial_data[key] = json.dumps([], ensure_ascii=False)
|
||||
|
||||
model_fields = PersonInfo._meta.fields.keys() # type: ignore
|
||||
filtered_initial_data = {k: v for k, v in initial_data.items() if v is not None and k in model_fields}
|
||||
|
||||
record, was_created = await asyncio.to_thread(_db_get_or_create_sync, person_id, filtered_initial_data)
|
||||
|
||||
if was_created:
|
||||
logger.info(f"用户 {platform}:{user_id} (person_id: {person_id}) 不存在,将创建新记录 (Peewee)。")
|
||||
logger.info(f"已为 {person_id} 创建新记录,初始数据 (filtered for model): {filtered_initial_data}")
|
||||
else:
|
||||
logger.debug(f"用户 {platform}:{user_id} (person_id: {person_id}) 已存在,返回现有记录。")
|
||||
|
||||
return person_id
|
||||
|
||||
|
||||
|
||||
person_info_manager = None
|
||||
|
||||
def get_person_info_manager():
|
||||
global person_info_manager
|
||||
if person_info_manager is None:
|
||||
person_info_manager = PersonInfoManager()
|
||||
return person_info_manager
|
||||
person_info_manager = PersonInfoManager()
|
||||
|
||||
@@ -7,7 +7,7 @@ from typing import List, Dict, Any
|
||||
from src.config.config import global_config
|
||||
from src.common.logger import get_logger
|
||||
from src.person_info.relationship_manager import get_relationship_manager
|
||||
from src.person_info.person_info import get_person_info_manager, PersonInfoManager
|
||||
from src.person_info.person_info import Person,get_person_id
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.chat.utils.chat_message_builder import (
|
||||
get_raw_msg_by_timestamp_with_chat,
|
||||
@@ -15,6 +15,7 @@ from src.chat.utils.chat_message_builder import (
|
||||
get_raw_msg_before_timestamp_with_chat,
|
||||
num_new_messages_since,
|
||||
)
|
||||
import asyncio
|
||||
|
||||
logger = get_logger("relationship_builder")
|
||||
|
||||
@@ -142,7 +143,8 @@ class RelationshipBuilder:
|
||||
}
|
||||
segments.append(new_segment)
|
||||
|
||||
person_name = get_person_info_manager().get_value_sync(person_id, "person_name") or person_id
|
||||
person = Person(person_id=person_id)
|
||||
person_name = person.person_name or person_id
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 眼熟用户 {person_name} 在 {time.strftime('%H:%M:%S', time.localtime(potential_start_time))} - {time.strftime('%H:%M:%S', time.localtime(message_time))} 之间有 {new_segment['message_count']} 条消息"
|
||||
)
|
||||
@@ -188,8 +190,8 @@ class RelationshipBuilder:
|
||||
"message_count": self._count_messages_in_timerange(potential_start_time, message_time),
|
||||
}
|
||||
segments.append(new_segment)
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_name = person_info_manager.get_value_sync(person_id, "person_name") or person_id
|
||||
person = Person(person_id=person_id)
|
||||
person_name = person.person_name or person_id
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 重新眼熟用户 {person_name} 创建新消息段(超过10条消息间隔): {new_segment}"
|
||||
)
|
||||
@@ -375,7 +377,7 @@ class RelationshipBuilder:
|
||||
and user_id != global_config.bot.qq_account
|
||||
and msg_time > self.last_processed_message_time
|
||||
):
|
||||
person_id = PersonInfoManager.get_person_id(platform, user_id)
|
||||
person_id = get_person_id(platform, user_id)
|
||||
self._update_message_segments(person_id, msg_time)
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 更新用户 {person_id} 的消息段,消息时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg_time))}"
|
||||
@@ -386,7 +388,8 @@ class RelationshipBuilder:
|
||||
users_to_build_relationship = []
|
||||
for person_id, segments in self.person_engaged_cache.items():
|
||||
total_message_count = self._get_total_message_count(person_id)
|
||||
person_name = get_person_info_manager().get_value_sync(person_id, "person_name") or person_id
|
||||
person = Person(person_id=person_id)
|
||||
person_name = person.person_name or person_id
|
||||
|
||||
if total_message_count >= max_build_threshold or (total_message_count >= 5 and (immediate_build == person_id or immediate_build == "all")):
|
||||
users_to_build_relationship.append(person_id)
|
||||
@@ -403,9 +406,9 @@ class RelationshipBuilder:
|
||||
for person_id in users_to_build_relationship:
|
||||
segments = self.person_engaged_cache[person_id]
|
||||
# 异步执行关系构建
|
||||
import asyncio
|
||||
|
||||
asyncio.create_task(self.update_impression_on_segments(person_id, self.chat_id, segments))
|
||||
person = Person(person_id=person_id)
|
||||
if person.is_known:
|
||||
asyncio.create_task(self.update_impression_on_segments(person_id, self.chat_id, segments))
|
||||
# 移除已处理的用户缓存
|
||||
del self.person_engaged_cache[person_id]
|
||||
self._save_cache()
|
||||
|
||||
@@ -1,468 +0,0 @@
|
||||
import time
|
||||
import traceback
|
||||
import json
|
||||
import random
|
||||
|
||||
from typing import List, Dict, Any
|
||||
from json_repair import repair_json
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config, model_config
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.person_info.person_info import get_person_info_manager
|
||||
|
||||
|
||||
logger = get_logger("relationship_fetcher")
|
||||
|
||||
|
||||
def init_real_time_info_prompts():
|
||||
"""初始化实时信息提取相关的提示词"""
|
||||
relationship_prompt = """
|
||||
<聊天记录>
|
||||
{chat_observe_info}
|
||||
</聊天记录>
|
||||
|
||||
{name_block}
|
||||
现在,你想要回复{person_name}的消息,消息内容是:{target_message}。请根据聊天记录和你要回复的消息,从你对{person_name}的了解中提取有关的信息:
|
||||
1.你需要提供你想要提取的信息具体是哪方面的信息,例如:年龄,性别,你们之间的交流方式,最近发生的事等等。
|
||||
2.请注意,请不要重复调取相同的信息,已经调取的信息如下:
|
||||
{info_cache_block}
|
||||
3.如果当前聊天记录中没有需要查询的信息,或者现有信息已经足够回复,请返回{{"none": "不需要查询"}}
|
||||
|
||||
请以json格式输出,例如:
|
||||
|
||||
{{
|
||||
"info_type": "信息类型",
|
||||
}}
|
||||
|
||||
请严格按照json输出格式,不要输出多余内容:
|
||||
"""
|
||||
Prompt(relationship_prompt, "real_time_info_identify_prompt")
|
||||
|
||||
fetch_info_prompt = """
|
||||
|
||||
{name_block}
|
||||
以下是你在之前与{person_name}的交流中,产生的对{person_name}的了解:
|
||||
{person_impression_block}
|
||||
{points_text_block}
|
||||
|
||||
请从中提取用户"{person_name}"的有关"{info_type}"信息
|
||||
请以json格式输出,例如:
|
||||
|
||||
{{
|
||||
{info_json_str}
|
||||
}}
|
||||
|
||||
请严格按照json输出格式,不要输出多余内容:
|
||||
"""
|
||||
Prompt(fetch_info_prompt, "real_time_fetch_person_info_prompt")
|
||||
|
||||
|
||||
class RelationshipFetcher:
|
||||
def __init__(self, chat_id):
|
||||
self.chat_id = chat_id
|
||||
|
||||
# 信息获取缓存:记录正在获取的信息请求
|
||||
self.info_fetching_cache: List[Dict[str, Any]] = []
|
||||
|
||||
# 信息结果缓存:存储已获取的信息结果,带TTL
|
||||
self.info_fetched_cache: Dict[str, Dict[str, Any]] = {}
|
||||
# 结构:{person_id: {info_type: {"info": str, "ttl": int, "start_time": float, "person_name": str, "unknown": bool}}}
|
||||
|
||||
# LLM模型配置
|
||||
self.llm_model = LLMRequest(
|
||||
model_set=model_config.model_task_config.utils_small, request_type="relation.fetcher"
|
||||
)
|
||||
|
||||
# 小模型用于即时信息提取
|
||||
self.instant_llm_model = LLMRequest(
|
||||
model_set=model_config.model_task_config.utils_small, request_type="relation.fetch"
|
||||
)
|
||||
|
||||
name = get_chat_manager().get_stream_name(self.chat_id)
|
||||
self.log_prefix = f"[{name}] 实时信息"
|
||||
|
||||
def _cleanup_expired_cache(self):
|
||||
"""清理过期的信息缓存"""
|
||||
for person_id in list(self.info_fetched_cache.keys()):
|
||||
for info_type in list(self.info_fetched_cache[person_id].keys()):
|
||||
self.info_fetched_cache[person_id][info_type]["ttl"] -= 1
|
||||
if self.info_fetched_cache[person_id][info_type]["ttl"] <= 0:
|
||||
del self.info_fetched_cache[person_id][info_type]
|
||||
if not self.info_fetched_cache[person_id]:
|
||||
del self.info_fetched_cache[person_id]
|
||||
|
||||
async def build_relation_info(self, person_id, points_num=3):
|
||||
# 清理过期的信息缓存
|
||||
self._cleanup_expired_cache()
|
||||
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_name = await person_info_manager.get_value(person_id, "person_name")
|
||||
attitude_to_me = await person_info_manager.get_value(person_id, "attitude_to_me")
|
||||
neuroticism = await person_info_manager.get_value(person_id, "neuroticism")
|
||||
conscientiousness = await person_info_manager.get_value(person_id, "conscientiousness")
|
||||
likeness = await person_info_manager.get_value(person_id, "likeness")
|
||||
|
||||
nickname_str = await person_info_manager.get_value(person_id, "nickname")
|
||||
platform = await person_info_manager.get_value(person_id, "platform")
|
||||
|
||||
current_points = await person_info_manager.get_value(person_id, "points") or []
|
||||
|
||||
# 按时间排序forgotten_points
|
||||
current_points.sort(key=lambda x: x[2])
|
||||
# 按权重加权随机抽取最多3个不重复的points,point[1]的值在1-10之间,权重越高被抽到概率越大
|
||||
if len(current_points) > points_num:
|
||||
# point[1] 取值范围1-10,直接作为权重
|
||||
weights = [max(1, min(10, int(point[1]))) for point in current_points]
|
||||
# 使用加权采样不放回,保证不重复
|
||||
indices = list(range(len(current_points)))
|
||||
points = []
|
||||
for _ in range(points_num):
|
||||
if not indices:
|
||||
break
|
||||
sub_weights = [weights[i] for i in indices]
|
||||
chosen_idx = random.choices(indices, weights=sub_weights, k=1)[0]
|
||||
points.append(current_points[chosen_idx])
|
||||
indices.remove(chosen_idx)
|
||||
else:
|
||||
points = current_points
|
||||
|
||||
# 构建points文本
|
||||
points_text = "\n".join([f"{point[2]}:{point[0]}" for point in points])
|
||||
|
||||
nickname_str = ""
|
||||
if person_name != nickname_str:
|
||||
nickname_str = f"(ta在{platform}上的昵称是{nickname_str})"
|
||||
|
||||
relation_info = ""
|
||||
|
||||
attitude_info = ""
|
||||
attitude_parts = attitude_to_me.split(',')
|
||||
current_attitude_score = float(attitude_parts[0]) if len(attitude_parts) > 0 else 0.0
|
||||
total_confidence = float(attitude_parts[1]) if len(attitude_parts) > 1 else 1.0
|
||||
if attitude_to_me:
|
||||
if current_attitude_score > 8:
|
||||
attitude_info = f"{person_name}对你的态度十分好,"
|
||||
elif current_attitude_score > 5:
|
||||
attitude_info = f"{person_name}对你的态度较好,"
|
||||
|
||||
|
||||
if current_attitude_score < -8:
|
||||
attitude_info = f"{person_name}对你的态度十分恶劣,"
|
||||
elif current_attitude_score < -4:
|
||||
attitude_info = f"{person_name}对你的态度不好,"
|
||||
elif current_attitude_score < 0:
|
||||
attitude_info = f"{person_name}对你的态度一般,"
|
||||
|
||||
neuroticism_info = ""
|
||||
neuroticism_parts = neuroticism.split(',')
|
||||
current_neuroticism_score = float(neuroticism_parts[0]) if len(neuroticism_parts) > 0 else 0.0
|
||||
total_confidence = float(neuroticism_parts[1]) if len(neuroticism_parts) > 1 else 1.0
|
||||
if neuroticism:
|
||||
if current_neuroticism_score > 8:
|
||||
neuroticism_info = f"{person_name}的情绪十分活跃,容易情绪化,"
|
||||
elif current_neuroticism_score > 6:
|
||||
neuroticism_info = f"{person_name}的情绪比较活跃,"
|
||||
elif current_neuroticism_score > 4:
|
||||
neuroticism_info = ""
|
||||
elif current_neuroticism_score > 2:
|
||||
neuroticism_info = f"{person_name}的情绪比较稳定,"
|
||||
else:
|
||||
neuroticism_info = f"{person_name}的情绪非常稳定,毫无波动"
|
||||
|
||||
points_info = ""
|
||||
if points_text:
|
||||
points_info = f"你还记得ta最近做的事:{points_text}"
|
||||
|
||||
|
||||
|
||||
relation_info = f"{person_name}:{nickname_str}{attitude_info}{neuroticism_info}{points_info}"
|
||||
|
||||
|
||||
return relation_info
|
||||
|
||||
async def _build_fetch_query(self, person_id, target_message, chat_history):
|
||||
nickname_str = ",".join(global_config.bot.alias_names)
|
||||
name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。"
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_name: str = await person_info_manager.get_value(person_id, "person_name") # type: ignore
|
||||
|
||||
info_cache_block = self._build_info_cache_block()
|
||||
|
||||
prompt = (await global_prompt_manager.get_prompt_async("real_time_info_identify_prompt")).format(
|
||||
chat_observe_info=chat_history,
|
||||
name_block=name_block,
|
||||
info_cache_block=info_cache_block,
|
||||
person_name=person_name,
|
||||
target_message=target_message,
|
||||
)
|
||||
|
||||
try:
|
||||
logger.debug(f"{self.log_prefix} 信息识别prompt: \n{prompt}\n")
|
||||
content, _ = await self.llm_model.generate_response_async(prompt=prompt)
|
||||
|
||||
if content:
|
||||
content_json = json.loads(repair_json(content))
|
||||
|
||||
# 检查是否返回了不需要查询的标志
|
||||
if "none" in content_json:
|
||||
logger.debug(f"{self.log_prefix} LLM判断当前不需要查询任何信息:{content_json.get('none', '')}")
|
||||
return None
|
||||
|
||||
if info_type := content_json.get("info_type"):
|
||||
# 记录信息获取请求
|
||||
self.info_fetching_cache.append(
|
||||
{
|
||||
"person_id": get_person_info_manager().get_person_id_by_person_name(person_name),
|
||||
"person_name": person_name,
|
||||
"info_type": info_type,
|
||||
"start_time": time.time(),
|
||||
"forget": False,
|
||||
}
|
||||
)
|
||||
|
||||
# 限制缓存大小
|
||||
if len(self.info_fetching_cache) > 10:
|
||||
self.info_fetching_cache.pop(0)
|
||||
|
||||
logger.info(f"{self.log_prefix} 识别到需要调取用户 {person_name} 的[{info_type}]信息")
|
||||
return info_type
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} LLM未返回有效的info_type。响应: {content}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 执行信息识别LLM请求时出错: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
return None
|
||||
|
||||
def _build_info_cache_block(self) -> str:
|
||||
"""构建已获取信息的缓存块"""
|
||||
info_cache_block = ""
|
||||
if self.info_fetching_cache:
|
||||
# 对于每个(person_id, info_type)组合,只保留最新的记录
|
||||
latest_records = {}
|
||||
for info_fetching in self.info_fetching_cache:
|
||||
key = (info_fetching["person_id"], info_fetching["info_type"])
|
||||
if key not in latest_records or info_fetching["start_time"] > latest_records[key]["start_time"]:
|
||||
latest_records[key] = info_fetching
|
||||
|
||||
# 按时间排序并生成显示文本
|
||||
sorted_records = sorted(latest_records.values(), key=lambda x: x["start_time"])
|
||||
for info_fetching in sorted_records:
|
||||
info_cache_block += (
|
||||
f"你已经调取了[{info_fetching['person_name']}]的[{info_fetching['info_type']}]信息\n"
|
||||
)
|
||||
return info_cache_block
|
||||
|
||||
async def _extract_single_info(self, person_id: str, info_type: str, person_name: str):
|
||||
"""提取单个信息类型
|
||||
|
||||
Args:
|
||||
person_id: 用户ID
|
||||
info_type: 信息类型
|
||||
person_name: 用户名
|
||||
"""
|
||||
start_time = time.time()
|
||||
person_info_manager = get_person_info_manager()
|
||||
|
||||
# 首先检查 info_list 缓存
|
||||
info_list = await person_info_manager.get_value(person_id, "info_list") or []
|
||||
cached_info = None
|
||||
|
||||
# 查找对应的 info_type
|
||||
for info_item in info_list:
|
||||
if info_item.get("info_type") == info_type:
|
||||
cached_info = info_item.get("info_content")
|
||||
logger.debug(f"{self.log_prefix} 在info_list中找到 {person_name} 的 {info_type} 信息: {cached_info}")
|
||||
break
|
||||
|
||||
# 如果缓存中有信息,直接使用
|
||||
if cached_info:
|
||||
if person_id not in self.info_fetched_cache:
|
||||
self.info_fetched_cache[person_id] = {}
|
||||
|
||||
self.info_fetched_cache[person_id][info_type] = {
|
||||
"info": cached_info,
|
||||
"ttl": 2,
|
||||
"start_time": start_time,
|
||||
"person_name": person_name,
|
||||
"unknown": cached_info == "none",
|
||||
}
|
||||
logger.info(f"{self.log_prefix} 记得 {person_name} 的 {info_type}: {cached_info}")
|
||||
return
|
||||
|
||||
# 如果缓存中没有,尝试从用户档案中提取
|
||||
try:
|
||||
person_impression = await person_info_manager.get_value(person_id, "impression")
|
||||
points = await person_info_manager.get_value(person_id, "points")
|
||||
|
||||
# 构建印象信息块
|
||||
if person_impression:
|
||||
person_impression_block = (
|
||||
f"<对{person_name}的总体了解>\n{person_impression}\n</对{person_name}的总体了解>"
|
||||
)
|
||||
else:
|
||||
person_impression_block = ""
|
||||
|
||||
# 构建要点信息块
|
||||
if points:
|
||||
points_text = "\n".join([f"{point[2]}:{point[0]}" for point in points])
|
||||
points_text_block = f"<对{person_name}的近期了解>\n{points_text}\n</对{person_name}的近期了解>"
|
||||
else:
|
||||
points_text_block = ""
|
||||
|
||||
# 如果完全没有用户信息
|
||||
if not points_text_block and not person_impression_block:
|
||||
if person_id not in self.info_fetched_cache:
|
||||
self.info_fetched_cache[person_id] = {}
|
||||
self.info_fetched_cache[person_id][info_type] = {
|
||||
"info": "none",
|
||||
"ttl": 2,
|
||||
"start_time": start_time,
|
||||
"person_name": person_name,
|
||||
"unknown": True,
|
||||
}
|
||||
logger.info(f"{self.log_prefix} 完全不认识 {person_name}")
|
||||
await self._save_info_to_cache(person_id, info_type, "none")
|
||||
return
|
||||
|
||||
# 使用LLM提取信息
|
||||
nickname_str = ",".join(global_config.bot.alias_names)
|
||||
name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。"
|
||||
|
||||
prompt = (await global_prompt_manager.get_prompt_async("real_time_fetch_person_info_prompt")).format(
|
||||
name_block=name_block,
|
||||
info_type=info_type,
|
||||
person_impression_block=person_impression_block,
|
||||
person_name=person_name,
|
||||
info_json_str=f'"{info_type}": "有关{info_type}的信息内容"',
|
||||
points_text_block=points_text_block,
|
||||
)
|
||||
|
||||
# 使用小模型进行即时提取
|
||||
content, _ = await self.instant_llm_model.generate_response_async(prompt=prompt)
|
||||
|
||||
if content:
|
||||
content_json = json.loads(repair_json(content))
|
||||
if info_type in content_json:
|
||||
info_content = content_json[info_type]
|
||||
is_unknown = info_content == "none" or not info_content
|
||||
|
||||
# 保存到运行时缓存
|
||||
if person_id not in self.info_fetched_cache:
|
||||
self.info_fetched_cache[person_id] = {}
|
||||
self.info_fetched_cache[person_id][info_type] = {
|
||||
"info": "unknown" if is_unknown else info_content,
|
||||
"ttl": 3,
|
||||
"start_time": start_time,
|
||||
"person_name": person_name,
|
||||
"unknown": is_unknown,
|
||||
}
|
||||
|
||||
# 保存到持久化缓存 (info_list)
|
||||
await self._save_info_to_cache(person_id, info_type, "none" if is_unknown else info_content)
|
||||
|
||||
if not is_unknown:
|
||||
logger.info(f"{self.log_prefix} 思考得到,{person_name} 的 {info_type}: {info_content}")
|
||||
else:
|
||||
logger.info(f"{self.log_prefix} 思考了也不知道{person_name} 的 {info_type} 信息")
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} 小模型返回空结果,获取 {person_name} 的 {info_type} 信息失败。")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 执行信息提取时出错: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str):
|
||||
# sourcery skip: use-next
|
||||
"""将提取到的信息保存到 person_info 的 info_list 字段中
|
||||
|
||||
Args:
|
||||
person_id: 用户ID
|
||||
info_type: 信息类型
|
||||
info_content: 信息内容
|
||||
"""
|
||||
try:
|
||||
person_info_manager = get_person_info_manager()
|
||||
|
||||
# 获取现有的 info_list
|
||||
info_list = await person_info_manager.get_value(person_id, "info_list") or []
|
||||
|
||||
# 查找是否已存在相同 info_type 的记录
|
||||
found_index = -1
|
||||
for i, info_item in enumerate(info_list):
|
||||
if isinstance(info_item, dict) and info_item.get("info_type") == info_type:
|
||||
found_index = i
|
||||
break
|
||||
|
||||
# 创建新的信息记录
|
||||
new_info_item = {
|
||||
"info_type": info_type,
|
||||
"info_content": info_content,
|
||||
}
|
||||
|
||||
if found_index >= 0:
|
||||
# 更新现有记录
|
||||
info_list[found_index] = new_info_item
|
||||
logger.info(f"{self.log_prefix} [缓存更新] 更新 {person_id} 的 {info_type} 信息缓存")
|
||||
else:
|
||||
# 添加新记录
|
||||
info_list.append(new_info_item)
|
||||
logger.info(f"{self.log_prefix} [缓存保存] 新增 {person_id} 的 {info_type} 信息缓存")
|
||||
|
||||
# 保存更新后的 info_list
|
||||
await person_info_manager.update_one_field(person_id, "info_list", info_list)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} [缓存保存] 保存信息到缓存失败: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
class RelationshipFetcherManager:
|
||||
"""关系提取器管理器
|
||||
|
||||
管理不同 chat_id 的 RelationshipFetcher 实例
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._fetchers: Dict[str, RelationshipFetcher] = {}
|
||||
|
||||
def get_fetcher(self, chat_id: str) -> RelationshipFetcher:
|
||||
"""获取或创建指定 chat_id 的 RelationshipFetcher
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
|
||||
Returns:
|
||||
RelationshipFetcher: 关系提取器实例
|
||||
"""
|
||||
if chat_id not in self._fetchers:
|
||||
self._fetchers[chat_id] = RelationshipFetcher(chat_id)
|
||||
return self._fetchers[chat_id]
|
||||
|
||||
def remove_fetcher(self, chat_id: str):
|
||||
"""移除指定 chat_id 的 RelationshipFetcher
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
"""
|
||||
if chat_id in self._fetchers:
|
||||
del self._fetchers[chat_id]
|
||||
|
||||
def clear_all(self):
|
||||
"""清空所有 RelationshipFetcher"""
|
||||
self._fetchers.clear()
|
||||
|
||||
def get_active_chat_ids(self) -> List[str]:
|
||||
"""获取所有活跃的 chat_id 列表"""
|
||||
return list(self._fetchers.keys())
|
||||
|
||||
|
||||
# 全局管理器实例
|
||||
relationship_fetcher_manager = RelationshipFetcherManager()
|
||||
|
||||
|
||||
init_real_time_info_prompts()
|
||||
@@ -1,6 +1,5 @@
|
||||
from src.common.logger import get_logger
|
||||
from .person_info import PersonInfoManager, get_person_info_manager
|
||||
import time
|
||||
from .person_info import Person
|
||||
import random
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.config.config import global_config, model_config
|
||||
@@ -8,11 +7,7 @@ from src.chat.utils.chat_message_builder import build_readable_messages
|
||||
import json
|
||||
from json_repair import repair_json
|
||||
from datetime import datetime
|
||||
from difflib import SequenceMatcher
|
||||
import jieba
|
||||
from sklearn.feature_extraction.text import TfidfVectorizer
|
||||
from sklearn.metrics.pairwise import cosine_similarity
|
||||
from typing import List, Dict, Any, Tuple
|
||||
from typing import List, Dict, Any
|
||||
from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
|
||||
import traceback
|
||||
|
||||
@@ -52,8 +47,7 @@ def init_prompt():
|
||||
}}
|
||||
]
|
||||
|
||||
如果没有,就输出none,或返回空数组:
|
||||
[]
|
||||
如果没有,就只输出空数组:[]
|
||||
""",
|
||||
"relation_points",
|
||||
)
|
||||
@@ -83,7 +77,9 @@ def init_prompt():
|
||||
"attitude": 0,
|
||||
"confidence": 0.5
|
||||
}}
|
||||
现在,请你输出json:
|
||||
如果无法看出对方对你的态度,就只输出空数组:[]
|
||||
|
||||
现在,请你输出:
|
||||
""",
|
||||
"attitude_to_me_prompt",
|
||||
)
|
||||
@@ -115,7 +111,9 @@ def init_prompt():
|
||||
"neuroticism": 0,
|
||||
"confidence": 0.5
|
||||
}}
|
||||
现在,请你输出json:
|
||||
如果无法看出对方的神经质程度,就只输出空数组:[]
|
||||
|
||||
现在,请你输出:
|
||||
""",
|
||||
"neuroticism_prompt",
|
||||
)
|
||||
@@ -124,46 +122,15 @@ class RelationshipManager:
|
||||
def __init__(self):
|
||||
self.relationship_llm = LLMRequest(
|
||||
model_set=model_config.model_task_config.utils, request_type="relationship.person"
|
||||
) # 用于动作规划
|
||||
|
||||
@staticmethod
|
||||
async def is_known_some_one(platform, user_id):
|
||||
"""判断是否认识某人"""
|
||||
person_info_manager = get_person_info_manager()
|
||||
return await person_info_manager.is_person_known(platform, user_id)
|
||||
|
||||
@staticmethod
|
||||
async def first_knowing_some_one(platform: str, user_id: str, user_nickname: str, user_cardname: str):
|
||||
"""判断是否认识某人"""
|
||||
person_id = PersonInfoManager.get_person_id(platform, user_id)
|
||||
# 生成唯一的 person_name
|
||||
person_info_manager = get_person_info_manager()
|
||||
unique_nickname = await person_info_manager._generate_unique_person_name(user_nickname)
|
||||
data = {
|
||||
"platform": platform,
|
||||
"user_id": user_id,
|
||||
"nickname": user_nickname,
|
||||
"konw_time": int(time.time()),
|
||||
"person_name": unique_nickname, # 使用唯一的 person_name
|
||||
}
|
||||
# 先创建用户基本信息,使用安全创建方法避免竞态条件
|
||||
await person_info_manager._safe_create_person_info(person_id=person_id, data=data)
|
||||
# 更新昵称
|
||||
await person_info_manager.update_one_field(
|
||||
person_id=person_id, field_name="nickname", value=user_nickname, data=data
|
||||
)
|
||||
# 尝试生成更好的名字
|
||||
# await person_info_manager.qv_person_name(
|
||||
# person_id=person_id, user_nickname=user_nickname, user_cardname=user_cardname, user_avatar=user_avatar
|
||||
# )
|
||||
)
|
||||
|
||||
async def get_points(self,
|
||||
person_name: str,
|
||||
nickname: str,
|
||||
readable_messages: str,
|
||||
name_mapping: Dict[str, str],
|
||||
timestamp: float,
|
||||
current_points: List[Tuple[str, float, str]]):
|
||||
person_name: str,
|
||||
nickname: str,
|
||||
readable_messages: str,
|
||||
name_mapping: Dict[str, str],
|
||||
timestamp: float,
|
||||
person: Person):
|
||||
alias_str = ", ".join(global_config.bot.alias_names)
|
||||
current_time = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
@@ -198,9 +165,7 @@ class RelationshipManager:
|
||||
points_data = json.loads(points)
|
||||
|
||||
# 只处理正确的格式,错误格式直接跳过
|
||||
if points_data == "none" or not points_data:
|
||||
points_list = []
|
||||
elif isinstance(points_data, str) and points_data.lower() == "none":
|
||||
if points_data == "none" or not points_data or (isinstance(points_data, str) and points_data.lower() == "none") or (isinstance(points_data, list) and len(points_data) == 0):
|
||||
points_list = []
|
||||
elif isinstance(points_data, list):
|
||||
points_list = [(item["point"], float(item["weight"]), current_time) for item in points_data]
|
||||
@@ -238,15 +203,15 @@ class RelationshipManager:
|
||||
return
|
||||
|
||||
|
||||
current_points.extend(points_list)
|
||||
person.points.extend(points_list)
|
||||
# 如果points超过10条,按权重随机选择多余的条目移动到forgotten_points
|
||||
if len(current_points) > 20:
|
||||
if len(person.points) > 20:
|
||||
# 计算当前时间
|
||||
current_time = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 计算每个点的最终权重(原始权重 * 时间权重)
|
||||
weighted_points = []
|
||||
for point in current_points:
|
||||
for point in person.points:
|
||||
time_weight = self.calculate_time_weight(point[2], current_time)
|
||||
final_weight = point[1] * time_weight
|
||||
weighted_points.append((point, final_weight))
|
||||
@@ -270,16 +235,15 @@ class RelationshipManager:
|
||||
idx_to_remove = random.randrange(len(remaining_points))
|
||||
remaining_points[idx_to_remove] = point
|
||||
|
||||
return remaining_points
|
||||
return current_points
|
||||
person.points = remaining_points
|
||||
return person
|
||||
|
||||
async def get_attitude_to_me(self, person_name, nickname, readable_messages, timestamp, current_attitude):
|
||||
async def get_attitude_to_me(self, person_name, nickname, readable_messages, timestamp, person: Person):
|
||||
alias_str = ", ".join(global_config.bot.alias_names)
|
||||
current_time = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
# 解析当前态度值
|
||||
attitude_parts = current_attitude.split(',')
|
||||
current_attitude_score = float(attitude_parts[0]) if len(attitude_parts) > 0 else 0.0
|
||||
total_confidence = float(attitude_parts[1]) if len(attitude_parts) > 1 else 1.0
|
||||
current_attitude_score = person.attitude_to_me
|
||||
total_confidence = person.attitude_to_me_confidence
|
||||
|
||||
prompt = await global_prompt_manager.format_prompt(
|
||||
"attitude_to_me_prompt",
|
||||
@@ -301,23 +265,31 @@ class RelationshipManager:
|
||||
attitude = repair_json(attitude)
|
||||
attitude_data = json.loads(attitude)
|
||||
|
||||
if attitude_data == "none" or not attitude_data or (isinstance(attitude_data, str) and attitude_data.lower() == "none") or (isinstance(attitude_data, list) and len(attitude_data) == 0):
|
||||
return ""
|
||||
|
||||
# 确保 attitude_data 是字典格式
|
||||
if not isinstance(attitude_data, dict):
|
||||
logger.warning(f"LLM返回了错误的JSON格式,跳过解析: {type(attitude_data)}, 内容: {attitude_data}")
|
||||
return ""
|
||||
|
||||
attitude_score = attitude_data["attitude"]
|
||||
confidence = attitude_data["confidence"]
|
||||
|
||||
new_confidence = total_confidence + confidence
|
||||
|
||||
new_attitude_score = (current_attitude_score * total_confidence + attitude_score * confidence)/new_confidence
|
||||
|
||||
person.attitude_to_me = new_attitude_score
|
||||
person.attitude_to_me_confidence = new_confidence
|
||||
|
||||
return f"{new_attitude_score:.3f},{new_confidence:.3f}"
|
||||
return person
|
||||
|
||||
async def get_neuroticism(self, person_name, nickname, readable_messages, timestamp, current_neuroticism):
|
||||
async def get_neuroticism(self, person_name, nickname, readable_messages, timestamp, person: Person):
|
||||
alias_str = ", ".join(global_config.bot.alias_names)
|
||||
current_time = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
# 解析当前态度值
|
||||
neuroticism_parts = current_neuroticism.split(',')
|
||||
current_neuroticism_score = float(neuroticism_parts[0]) if len(neuroticism_parts) > 0 else 0.0
|
||||
total_confidence = float(neuroticism_parts[1]) if len(neuroticism_parts) > 1 else 1.0
|
||||
current_neuroticism_score = person.neuroticism
|
||||
total_confidence = person.neuroticism_confidence
|
||||
|
||||
prompt = await global_prompt_manager.format_prompt(
|
||||
"neuroticism_prompt",
|
||||
@@ -339,6 +311,14 @@ class RelationshipManager:
|
||||
neuroticism = repair_json(neuroticism)
|
||||
neuroticism_data = json.loads(neuroticism)
|
||||
|
||||
if neuroticism_data == "none" or not neuroticism_data or (isinstance(neuroticism_data, str) and neuroticism_data.lower() == "none") or (isinstance(neuroticism_data, list) and len(neuroticism_data) == 0):
|
||||
return ""
|
||||
|
||||
# 确保 neuroticism_data 是字典格式
|
||||
if not isinstance(neuroticism_data, dict):
|
||||
logger.warning(f"LLM返回了错误的JSON格式,跳过解析: {type(neuroticism_data)}, 内容: {neuroticism_data}")
|
||||
return ""
|
||||
|
||||
neuroticism_score = neuroticism_data["neuroticism"]
|
||||
confidence = neuroticism_data["confidence"]
|
||||
|
||||
@@ -346,8 +326,10 @@ class RelationshipManager:
|
||||
|
||||
new_neuroticism_score = (current_neuroticism_score * total_confidence + neuroticism_score * confidence)/new_confidence
|
||||
|
||||
person.neuroticism = new_neuroticism_score
|
||||
person.neuroticism_confidence = new_confidence
|
||||
|
||||
return f"{new_neuroticism_score:.3f},{new_confidence:.3f}"
|
||||
return person
|
||||
|
||||
|
||||
async def update_person_impression(self, person_id, timestamp, bot_engaged_messages: List[Dict[str, Any]]):
|
||||
@@ -360,21 +342,13 @@ class RelationshipManager:
|
||||
timestamp: 时间戳 (用于记录交互时间)
|
||||
bot_engaged_messages: bot参与的消息列表
|
||||
"""
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_name = await person_info_manager.get_value(person_id, "person_name")
|
||||
nickname = await person_info_manager.get_value(person_id, "nickname")
|
||||
know_times: float = await person_info_manager.get_value(person_id, "know_times") or 0 # type: ignore
|
||||
current_points = await person_info_manager.get_value(person_id, "points") or []
|
||||
attitude_to_me = await person_info_manager.get_value(person_id, "attitude_to_me") or "0,1"
|
||||
neuroticism = await person_info_manager.get_value(person_id, "neuroticism") or "5,1"
|
||||
|
||||
# personality_block =get_individuality().get_personality_prompt(x_person=2, level=2)
|
||||
# identity_block =get_individuality().get_identity_prompt(x_person=2, level=2)
|
||||
person = Person(person_id=person_id)
|
||||
person_name = person.person_name
|
||||
nickname = person.nickname
|
||||
know_times: float = person.know_times
|
||||
|
||||
user_messages = bot_engaged_messages
|
||||
|
||||
current_time = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 匿名化消息
|
||||
# 创建用户名称映射
|
||||
name_mapping = {}
|
||||
@@ -383,33 +357,23 @@ class RelationshipManager:
|
||||
|
||||
# 遍历消息,构建映射
|
||||
for msg in user_messages:
|
||||
await person_info_manager.get_or_create_person(
|
||||
platform=msg.get("chat_info_platform"), # type: ignore
|
||||
user_id=msg.get("user_id"), # type: ignore
|
||||
nickname=msg.get("user_nickname"), # type: ignore
|
||||
user_cardname=msg.get("user_cardname"), # type: ignore
|
||||
)
|
||||
replace_user_id: str = msg.get("user_id") # type: ignore
|
||||
replace_platform: str = msg.get("chat_info_platform") # type: ignore
|
||||
replace_person_id = PersonInfoManager.get_person_id(replace_platform, replace_user_id)
|
||||
replace_person_name = await person_info_manager.get_value(replace_person_id, "person_name")
|
||||
|
||||
msg_person = Person(user_id=msg.get("user_id"), platform=msg.get("chat_info_platform"))
|
||||
# 跳过机器人自己
|
||||
if replace_user_id == global_config.bot.qq_account:
|
||||
if msg_person.user_id == global_config.bot.qq_account:
|
||||
name_mapping[f"{global_config.bot.nickname}"] = f"{global_config.bot.nickname}"
|
||||
continue
|
||||
|
||||
# 跳过目标用户
|
||||
if replace_person_name == person_name:
|
||||
name_mapping[replace_person_name] = f"{person_name}"
|
||||
if msg_person.person_name == person_name:
|
||||
name_mapping[msg_person.person_name] = f"{person_name}"
|
||||
continue
|
||||
|
||||
# 其他用户映射
|
||||
if replace_person_name not in name_mapping:
|
||||
if msg_person.person_name not in name_mapping:
|
||||
if current_user > "Z":
|
||||
current_user = "A"
|
||||
user_count += 1
|
||||
name_mapping[replace_person_name] = f"用户{current_user}{user_count if user_count > 1 else ''}"
|
||||
name_mapping[msg_person.person_name] = f"用户{current_user}{user_count if user_count > 1 else ''}"
|
||||
current_user = chr(ord(current_user) + 1)
|
||||
|
||||
readable_messages = build_readable_messages(
|
||||
@@ -420,23 +384,16 @@ class RelationshipManager:
|
||||
# print(f"original_name: {original_name}, mapped_name: {mapped_name}")
|
||||
readable_messages = readable_messages.replace(f"{original_name}", f"{mapped_name}")
|
||||
|
||||
|
||||
print(name_mapping)
|
||||
|
||||
remaining_points = await self.get_points(person_name, nickname, readable_messages, name_mapping, timestamp, current_points)
|
||||
attitude_to_me = await self.get_attitude_to_me(person_name, nickname, readable_messages, timestamp, attitude_to_me)
|
||||
neuroticism = await self.get_neuroticism(person_name, nickname, readable_messages, timestamp, neuroticism)
|
||||
person = await self.get_points(person_name, nickname, readable_messages, name_mapping, timestamp, person)
|
||||
person = await self.get_attitude_to_me(person_name, nickname, readable_messages, timestamp, person)
|
||||
person = await self.get_neuroticism(person_name, nickname, readable_messages, timestamp, person)
|
||||
|
||||
# 更新数据库
|
||||
await person_info_manager.update_one_field(
|
||||
person_id, "points", json.dumps(remaining_points, ensure_ascii=False, indent=None)
|
||||
)
|
||||
await person_info_manager.update_one_field(person_id, "neuroticism", neuroticism)
|
||||
await person_info_manager.update_one_field(person_id, "attitude_to_me", attitude_to_me)
|
||||
await person_info_manager.update_one_field(person_id, "know_times", know_times + 1)
|
||||
await person_info_manager.update_one_field(person_id, "last_know", timestamp)
|
||||
know_since = await person_info_manager.get_value(person_id, "know_since") or 0
|
||||
if know_since == 0:
|
||||
await person_info_manager.update_one_field(person_id, "know_since", timestamp)
|
||||
person.know_times = know_times + 1
|
||||
person.last_know = timestamp
|
||||
|
||||
person.sync_to_database()
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user