feat:重构的关系构建,关系处理器,更加精准
This commit is contained in:
70
src/person_info/fix_points_format.py
Normal file
70
src/person_info/fix_points_format.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import os
|
||||
import sys
|
||||
# 添加项目根目录到Python路径
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
project_root = os.path.dirname(os.path.dirname(current_dir))
|
||||
sys.path.append(project_root)
|
||||
|
||||
from loguru import logger
|
||||
import json
|
||||
from src.common.database.database_model import PersonInfo
|
||||
|
||||
def fix_points_format():
|
||||
"""修复数据库中的points和forgotten_points格式"""
|
||||
fixed_count = 0
|
||||
error_count = 0
|
||||
|
||||
try:
|
||||
# 获取所有用户
|
||||
all_persons = PersonInfo.select()
|
||||
|
||||
for person in all_persons:
|
||||
try:
|
||||
# 修复points
|
||||
if person.points:
|
||||
try:
|
||||
# 尝试解析JSON
|
||||
points_data = json.loads(person.points)
|
||||
except json.JSONDecodeError:
|
||||
logger.error(f"无法解析points数据: {person.points}")
|
||||
points_data = []
|
||||
|
||||
# 确保数据是列表格式
|
||||
if not isinstance(points_data, list):
|
||||
points_data = []
|
||||
|
||||
# 直接更新数据库
|
||||
person.points = json.dumps(points_data, ensure_ascii=False)
|
||||
person.save()
|
||||
fixed_count += 1
|
||||
|
||||
# 修复forgotten_points
|
||||
if person.forgotten_points:
|
||||
try:
|
||||
# 尝试解析JSON
|
||||
forgotten_data = json.loads(person.forgotten_points)
|
||||
except json.JSONDecodeError:
|
||||
logger.error(f"无法解析forgotten_points数据: {person.forgotten_points}")
|
||||
forgotten_data = []
|
||||
|
||||
# 确保数据是列表格式
|
||||
if not isinstance(forgotten_data, list):
|
||||
forgotten_data = []
|
||||
|
||||
# 直接更新数据库
|
||||
person.forgotten_points = json.dumps(forgotten_data, ensure_ascii=False)
|
||||
person.save()
|
||||
fixed_count += 1
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理用户 {person.person_id} 时出错: {str(e)}")
|
||||
error_count += 1
|
||||
continue
|
||||
|
||||
logger.info(f"修复完成!成功修复 {fixed_count} 条记录,失败 {error_count} 条记录")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"数据库操作出错: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
fix_points_format()
|
||||
@@ -8,7 +8,6 @@ import datetime
|
||||
import asyncio
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.config.config import global_config
|
||||
from src.individuality.individuality import individuality
|
||||
|
||||
import json # 新增导入
|
||||
from json_repair import repair_json
|
||||
@@ -29,7 +28,7 @@ PersonInfoManager 类方法功能摘要:
|
||||
|
||||
logger = get_logger("person_info")
|
||||
|
||||
JSON_SERIALIZED_FIELDS = ["hobby", "hates", "meme", "relationship_others", "interaction"]
|
||||
JSON_SERIALIZED_FIELDS = ["points", "forgotten_points"]
|
||||
|
||||
person_info_default = {
|
||||
"person_id": None,
|
||||
@@ -169,9 +168,9 @@ class PersonInfoManager:
|
||||
processed_value = value
|
||||
if field_name in JSON_SERIALIZED_FIELDS:
|
||||
if isinstance(value, (list, dict)):
|
||||
processed_value = json.dumps(value, ensure_ascii=False)
|
||||
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)
|
||||
processed_value = json.dumps([], ensure_ascii=False, indent=None)
|
||||
# If value is already a string, assume it's pre-serialized or a non-JSON string.
|
||||
|
||||
def _db_update_sync(p_id: str, f_name: str, val_to_set):
|
||||
@@ -280,7 +279,7 @@ class PersonInfoManager:
|
||||
|
||||
while current_try < max_retries:
|
||||
# prompt_personality = individuality.get_prompt(x_person=2, level=1)
|
||||
bot_name = individuality.personality.bot_nickname
|
||||
bot_name = global_config.bot.nickname
|
||||
|
||||
qv_name_prompt = f"你是{bot_name},一个伪装成人类的AI,你不能让别人发现这一点,"
|
||||
qv_name_prompt += f"现在你想给一个用户取一个昵称,用户的qq昵称是{user_nickname},"
|
||||
|
||||
@@ -134,7 +134,7 @@ class RelationshipManager:
|
||||
except (SyntaxError, ValueError):
|
||||
points = []
|
||||
|
||||
random_points = random.sample(points, min(3, len(points))) if points else []
|
||||
random_points = random.sample(points, min(5, len(points))) if points else []
|
||||
|
||||
nickname_str = await person_info_manager.get_value(person_id, "nickname")
|
||||
platform = await person_info_manager.get_value(person_id, "platform")
|
||||
@@ -312,13 +312,14 @@ class RelationshipManager:
|
||||
current_points = await person_info_manager.get_value(person_id, "points") or []
|
||||
if isinstance(current_points, str):
|
||||
try:
|
||||
current_points = ast.literal_eval(current_points)
|
||||
except (SyntaxError, ValueError):
|
||||
current_points = json.loads(current_points)
|
||||
except json.JSONDecodeError:
|
||||
logger.error(f"解析points JSON失败: {current_points}")
|
||||
current_points = []
|
||||
elif not isinstance(current_points, list):
|
||||
current_points = []
|
||||
current_points.extend(points_list)
|
||||
await person_info_manager.update_one_field(person_id, "points", str(current_points).replace("(", "[").replace(")", "]"))
|
||||
await person_info_manager.update_one_field(person_id, "points", json.dumps(current_points, ensure_ascii=False, indent=None))
|
||||
|
||||
# 将新记录添加到现有记录中
|
||||
if isinstance(current_points, list):
|
||||
@@ -365,8 +366,9 @@ class RelationshipManager:
|
||||
forgotten_points = await person_info_manager.get_value(person_id, "forgotten_points") or []
|
||||
if isinstance(forgotten_points, str):
|
||||
try:
|
||||
forgotten_points = ast.literal_eval(forgotten_points)
|
||||
except (SyntaxError, ValueError):
|
||||
forgotten_points = json.loads(forgotten_points)
|
||||
except json.JSONDecodeError:
|
||||
logger.error(f"解析forgotten_points JSON失败: {forgotten_points}")
|
||||
forgotten_points = []
|
||||
elif not isinstance(forgotten_points, list):
|
||||
forgotten_points = []
|
||||
@@ -487,10 +489,12 @@ class RelationshipManager:
|
||||
return
|
||||
|
||||
# 更新数据库
|
||||
await person_info_manager.update_one_field(person_id, "forgotten_points", str(forgotten_points).replace("(", "[").replace(")", "]"))
|
||||
await person_info_manager.update_one_field(person_id, "forgotten_points", json.dumps(forgotten_points, ensure_ascii=False, indent=None))
|
||||
|
||||
# 更新数据库
|
||||
await person_info_manager.update_one_field(person_id, "points", str(current_points).replace("(", "[").replace(")", "]"))
|
||||
await person_info_manager.update_one_field(person_id, "points", json.dumps(current_points, ensure_ascii=False, indent=None))
|
||||
know_times = await person_info_manager.get_value(person_id, "know_times") or 0
|
||||
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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user