全面更换orjson
This commit is contained in:
@@ -2,7 +2,7 @@ import copy
|
||||
import hashlib
|
||||
import datetime
|
||||
import asyncio
|
||||
import json
|
||||
import orjson
|
||||
import time
|
||||
|
||||
from json_repair import repair_json
|
||||
@@ -155,9 +155,9 @@ class PersonInfoManager:
|
||||
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)
|
||||
final_data[key] = orjson.dumps(final_data[key]).decode('utf-8')
|
||||
elif final_data[key] is None: # Default for lists is [], store as "[]"
|
||||
final_data[key] = json.dumps([], ensure_ascii=False)
|
||||
final_data[key] = orjson.dumps([]).decode('utf-8')
|
||||
# If it's already a string, assume it's valid JSON or a non-JSON string field
|
||||
|
||||
def _db_create_sync(p_data: dict):
|
||||
@@ -204,9 +204,9 @@ class PersonInfoManager:
|
||||
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)
|
||||
final_data[key] = orjson.dumps(final_data[key]).decode('utf-8')
|
||||
elif final_data[key] is None: # Default for lists is [], store as "[]"
|
||||
final_data[key] = json.dumps([], ensure_ascii=False)
|
||||
final_data[key] = orjson.dumps([]).decode('utf-8')
|
||||
|
||||
def _db_safe_create_sync(p_data: dict):
|
||||
with get_db_session() as session:
|
||||
@@ -243,9 +243,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, indent=None)
|
||||
processed_value = orjson.dumps(value).decode('utf-8')
|
||||
elif value is None: # Store None as "[]" for JSON list fields
|
||||
processed_value = json.dumps([], ensure_ascii=False, indent=None)
|
||||
processed_value = orjson.dumps([]).decode('utf-8')
|
||||
|
||||
def _db_update_sync(p_id: str, f_name: str, val_to_set):
|
||||
|
||||
@@ -324,7 +324,7 @@ class PersonInfoManager:
|
||||
try:
|
||||
fixed_json = repair_json(text)
|
||||
if isinstance(fixed_json, str):
|
||||
parsed_json = json.loads(fixed_json)
|
||||
parsed_json = orjson.loads(fixed_json)
|
||||
else:
|
||||
parsed_json = fixed_json
|
||||
|
||||
@@ -494,8 +494,8 @@ class PersonInfoManager:
|
||||
if f_name in JSON_SERIALIZED_FIELDS:
|
||||
if isinstance(val, str):
|
||||
try:
|
||||
return json.loads(val)
|
||||
except json.JSONDecodeError:
|
||||
return orjson.loads(val)
|
||||
except orjson.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
|
||||
@@ -531,8 +531,8 @@ class PersonInfoManager:
|
||||
if field_name in JSON_SERIALIZED_FIELDS:
|
||||
if isinstance(val, str):
|
||||
try:
|
||||
return json.loads(val)
|
||||
except json.JSONDecodeError:
|
||||
return orjson.loads(val)
|
||||
except orjson.JSONDecodeError:
|
||||
logger.warning(f"字段 {field_name} for {person_id} 包含无效JSON: {val}. 返回默认值.")
|
||||
return []
|
||||
elif val is None:
|
||||
@@ -671,9 +671,9 @@ class PersonInfoManager:
|
||||
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)
|
||||
initial_data[key] = orjson.dumps(initial_data[key]).decode('utf-8')
|
||||
elif initial_data[key] is None:
|
||||
initial_data[key] = json.dumps([], ensure_ascii=False)
|
||||
initial_data[key] = orjson.dumps([]).decode('utf-8')
|
||||
|
||||
# 获取 SQLAlchemy 模odel的所有字段名
|
||||
model_fields = [column.name for column in PersonInfo.__table__.columns]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import time
|
||||
import traceback
|
||||
import json
|
||||
import orjson
|
||||
import random
|
||||
|
||||
from typing import List, Dict, Any
|
||||
@@ -187,7 +187,7 @@ class RelationshipFetcher:
|
||||
content, _ = await self.llm_model.generate_response_async(prompt=prompt)
|
||||
|
||||
if content:
|
||||
content_json = json.loads(repair_json(content))
|
||||
content_json = orjson.loads(repair_json(content))
|
||||
|
||||
# 检查是否返回了不需要查询的标志
|
||||
if "none" in content_json:
|
||||
@@ -329,7 +329,7 @@ class RelationshipFetcher:
|
||||
content, _ = await self.instant_llm_model.generate_response_async(prompt=prompt)
|
||||
|
||||
if content:
|
||||
content_json = json.loads(repair_json(content))
|
||||
content_json = orjson.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
|
||||
|
||||
@@ -5,7 +5,7 @@ import random
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.config.config import global_config, model_config
|
||||
from src.chat.utils.chat_message_builder import build_readable_messages
|
||||
import json
|
||||
import orjson
|
||||
from json_repair import repair_json
|
||||
from datetime import datetime
|
||||
from difflib import SequenceMatcher
|
||||
@@ -183,7 +183,7 @@ class RelationshipManager:
|
||||
# 解析JSON并转换为元组列表
|
||||
try:
|
||||
points = repair_json(points)
|
||||
points_data = json.loads(points)
|
||||
points_data = orjson.loads(points)
|
||||
|
||||
# 只处理正确的格式,错误格式直接跳过
|
||||
if points_data == "none" or not points_data:
|
||||
@@ -220,7 +220,7 @@ class RelationshipManager:
|
||||
logger_str += f"({discarded_count} 条因重要性低被丢弃)\n"
|
||||
logger.info(logger_str)
|
||||
|
||||
except json.JSONDecodeError:
|
||||
except orjson.JSONDecodeError:
|
||||
logger.error(f"解析points JSON失败: {points}")
|
||||
return
|
||||
except (KeyError, TypeError) as e:
|
||||
@@ -230,15 +230,15 @@ class RelationshipManager:
|
||||
current_points = await person_info_manager.get_value(person_id, "points") or []
|
||||
if isinstance(current_points, str):
|
||||
try:
|
||||
current_points = json.loads(current_points)
|
||||
except json.JSONDecodeError:
|
||||
current_points = orjson.loads(current_points)
|
||||
except orjson.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", json.dumps(current_points, ensure_ascii=False, indent=None)
|
||||
person_id, "points", orjson.dumps(current_points).decode('utf-8')
|
||||
)
|
||||
|
||||
# 将新记录添加到现有记录中
|
||||
@@ -286,7 +286,7 @@ class RelationshipManager:
|
||||
|
||||
# 更新数据库
|
||||
await person_info_manager.update_one_field(
|
||||
person_id, "points", json.dumps(current_points, ensure_ascii=False, indent=None)
|
||||
person_id, "points", orjson.dumps(current_points).decode('utf-8')
|
||||
)
|
||||
|
||||
await person_info_manager.update_one_field(person_id, "know_times", know_times + 1)
|
||||
@@ -331,8 +331,8 @@ class RelationshipManager:
|
||||
forgotten_points = await person_info_manager.get_value(person_id, "forgotten_points") or []
|
||||
if isinstance(forgotten_points, str):
|
||||
try:
|
||||
forgotten_points = json.loads(forgotten_points)
|
||||
except json.JSONDecodeError:
|
||||
forgotten_points = orjson.loads(forgotten_points)
|
||||
except orjson.JSONDecodeError:
|
||||
logger.error(f"解析forgotten_points JSON失败: {forgotten_points}")
|
||||
forgotten_points = []
|
||||
elif not isinstance(forgotten_points, list):
|
||||
@@ -461,7 +461,7 @@ class RelationshipManager:
|
||||
relation_value_response, _ = await self.relationship_llm.generate_response_async(
|
||||
prompt=relation_value_prompt
|
||||
)
|
||||
relation_value_json = json.loads(repair_json(relation_value_response))
|
||||
relation_value_json = orjson.loads(repair_json(relation_value_response))
|
||||
|
||||
# 从LLM获取新生成的值
|
||||
new_attitude = int(relation_value_json.get("attitude", 50))
|
||||
@@ -483,17 +483,17 @@ class RelationshipManager:
|
||||
|
||||
await person_info_manager.update_one_field(person_id, "attitude", attitude)
|
||||
logger.info(f"更新了与 {person_name} 的态度: {attitude}")
|
||||
except (json.JSONDecodeError, ValueError, TypeError) as e:
|
||||
except (orjson.JSONDecodeError, ValueError, TypeError) as e:
|
||||
logger.error(f"解析relation_value JSON失败或值无效: {e}, 响应: {relation_value_response}")
|
||||
|
||||
forgotten_points = []
|
||||
info_list = []
|
||||
await person_info_manager.update_one_field(
|
||||
person_id, "info_list", json.dumps(info_list, ensure_ascii=False, indent=None)
|
||||
person_id, "info_list", orjson.dumps(info_list).decode('utf-8')
|
||||
)
|
||||
|
||||
await person_info_manager.update_one_field(
|
||||
person_id, "forgotten_points", json.dumps(forgotten_points, ensure_ascii=False, indent=None)
|
||||
person_id, "forgotten_points", orjson.dumps(forgotten_points).decode('utf-8')
|
||||
)
|
||||
|
||||
return current_points
|
||||
|
||||
Reference in New Issue
Block a user