re-style: 格式化代码
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import orjson
|
||||
import os
|
||||
import hashlib
|
||||
import os
|
||||
import time
|
||||
|
||||
import orjson
|
||||
from rich.traceback import install
|
||||
|
||||
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.person_info.person_info import get_person_info_manager
|
||||
from rich.traceback import install
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
@@ -193,9 +194,9 @@ class Individuality:
|
||||
"""从JSON文件中加载元信息"""
|
||||
if os.path.exists(self.meta_info_file_path):
|
||||
try:
|
||||
with open(self.meta_info_file_path, "r", encoding="utf-8") as f:
|
||||
with open(self.meta_info_file_path, encoding="utf-8") as f:
|
||||
return orjson.loads(f.read())
|
||||
except (orjson.JSONDecodeError, IOError) as e:
|
||||
except (OSError, orjson.JSONDecodeError) as e:
|
||||
logger.error(f"读取meta_info文件失败: {e}, 将创建新文件。")
|
||||
return {}
|
||||
return {}
|
||||
@@ -206,16 +207,16 @@ class Individuality:
|
||||
os.makedirs(os.path.dirname(self.meta_info_file_path), exist_ok=True)
|
||||
with open(self.meta_info_file_path, "w", encoding="utf-8") as f:
|
||||
f.write(orjson.dumps(meta_info, option=orjson.OPT_INDENT_2).decode("utf-8"))
|
||||
except IOError as e:
|
||||
except OSError as e:
|
||||
logger.error(f"保存meta_info文件失败: {e}")
|
||||
|
||||
def _load_personality_data(self) -> dict:
|
||||
"""从JSON文件中加载personality数据"""
|
||||
if os.path.exists(self.personality_data_file_path):
|
||||
try:
|
||||
with open(self.personality_data_file_path, "r", encoding="utf-8") as f:
|
||||
with open(self.personality_data_file_path, encoding="utf-8") as f:
|
||||
return orjson.loads(f.read())
|
||||
except (orjson.JSONDecodeError, IOError) as e:
|
||||
except (OSError, orjson.JSONDecodeError) as e:
|
||||
logger.error(f"读取personality_data文件失败: {e}, 将创建新文件。")
|
||||
return {}
|
||||
return {}
|
||||
@@ -227,7 +228,7 @@ class Individuality:
|
||||
with open(self.personality_data_file_path, "w", encoding="utf-8") as f:
|
||||
f.write(orjson.dumps(personality_data, option=orjson.OPT_INDENT_2).decode("utf-8"))
|
||||
logger.debug(f"已保存personality数据到文件: {self.personality_data_file_path}")
|
||||
except IOError as e:
|
||||
except OSError as e:
|
||||
logger.error(f"保存personality_data文件失败: {e}")
|
||||
|
||||
def _get_personality_from_file(self) -> tuple[str, str]:
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from typing import Tuple, Union
|
||||
|
||||
import aiohttp
|
||||
import requests
|
||||
from rich.traceback import install
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.common.tcp_connector import get_tcp_connector
|
||||
from rich.traceback import install
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
@@ -26,7 +26,7 @@ class LLMRequestOff:
|
||||
|
||||
# logger.info(f"API URL: {self.base_url}") # 使用 logger 记录 base_url
|
||||
|
||||
def generate_response(self, prompt: str) -> Union[str, Tuple[str, str]]:
|
||||
def generate_response(self, prompt: str) -> str | tuple[str, str]:
|
||||
"""根据输入的提示生成模型的响应"""
|
||||
headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
|
||||
|
||||
@@ -67,16 +67,16 @@ class LLMRequestOff:
|
||||
except Exception as e:
|
||||
if retry < max_retries - 1: # 如果还有重试机会
|
||||
wait_time = base_wait_time * (2**retry)
|
||||
logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {str(e)}")
|
||||
logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {e!s}")
|
||||
time.sleep(wait_time)
|
||||
else:
|
||||
logger.error(f"请求失败: {str(e)}")
|
||||
return f"请求失败: {str(e)}", ""
|
||||
logger.error(f"请求失败: {e!s}")
|
||||
return f"请求失败: {e!s}", ""
|
||||
|
||||
logger.error("达到最大重试次数,请求仍然失败")
|
||||
return "达到最大重试次数,请求仍然失败", ""
|
||||
|
||||
async def generate_response_async(self, prompt: str) -> Union[str, Tuple[str, str]]:
|
||||
async def generate_response_async(self, prompt: str) -> str | tuple[str, str]:
|
||||
"""异步方式根据输入的提示生成模型的响应"""
|
||||
headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
|
||||
|
||||
@@ -117,11 +117,11 @@ class LLMRequestOff:
|
||||
except Exception as e:
|
||||
if retry < max_retries - 1: # 如果还有重试机会
|
||||
wait_time = base_wait_time * (2**retry)
|
||||
logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {str(e)}")
|
||||
logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {e!s}")
|
||||
await asyncio.sleep(wait_time)
|
||||
else:
|
||||
logger.error(f"请求失败: {str(e)}")
|
||||
return f"请求失败: {str(e)}", ""
|
||||
logger.error(f"请求失败: {e!s}")
|
||||
return f"请求失败: {e!s}", ""
|
||||
|
||||
logger.error("达到最大重试次数,请求仍然失败")
|
||||
return "达到最大重试次数,请求仍然失败", ""
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from typing import Dict, List
|
||||
import orjson
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import sys
|
||||
import toml
|
||||
import random
|
||||
import sys
|
||||
|
||||
import orjson
|
||||
import toml
|
||||
from dotenv import load_dotenv
|
||||
from tqdm import tqdm
|
||||
|
||||
# 添加项目根目录到 Python 路径
|
||||
@@ -13,13 +13,13 @@ sys.path.append(root_path)
|
||||
|
||||
# 加载配置文件
|
||||
config_path = os.path.join(root_path, "config", "bot_config.toml")
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
with open(config_path, encoding="utf-8") as f:
|
||||
config = toml.load(f)
|
||||
|
||||
# 现在可以导入src模块
|
||||
from individuality.not_using.scene import get_scene_by_factor, PERSONALITY_SCENES # noqa E402
|
||||
from individuality.not_using.questionnaire import FACTOR_DESCRIPTIONS # noqa E402
|
||||
from individuality.not_using.offline_llm import LLMRequestOff # noqa E402
|
||||
from individuality.not_using.questionnaire import FACTOR_DESCRIPTIONS
|
||||
from individuality.not_using.offline_llm import LLMRequestOff
|
||||
|
||||
# 加载环境变量
|
||||
env_path = os.path.join(root_path, ".env")
|
||||
@@ -75,7 +75,7 @@ def adapt_scene(scene: str) -> str:
|
||||
|
||||
return adapted_scene
|
||||
except Exception as e:
|
||||
print(f"场景改编过程出错:{str(e)},将使用原始场景")
|
||||
print(f"场景改编过程出错:{e!s},将使用原始场景")
|
||||
return scene
|
||||
|
||||
|
||||
@@ -83,8 +83,8 @@ class PersonalityEvaluatorDirect:
|
||||
def __init__(self):
|
||||
self.personality_traits = {"开放性": 0, "严谨性": 0, "外向性": 0, "宜人性": 0, "神经质": 0}
|
||||
self.scenarios = []
|
||||
self.final_scores: Dict[str, float] = {"开放性": 0, "严谨性": 0, "外向性": 0, "宜人性": 0, "神经质": 0}
|
||||
self.dimension_counts = {trait: 0 for trait in self.final_scores}
|
||||
self.final_scores: dict[str, float] = {"开放性": 0, "严谨性": 0, "外向性": 0, "宜人性": 0, "神经质": 0}
|
||||
self.dimension_counts = dict.fromkeys(self.final_scores, 0)
|
||||
|
||||
# 为每个人格特质获取对应的场景
|
||||
for trait in PERSONALITY_SCENES:
|
||||
@@ -112,7 +112,7 @@ class PersonalityEvaluatorDirect:
|
||||
|
||||
self.llm = LLMRequestOff()
|
||||
|
||||
def evaluate_response(self, scenario: str, response: str, dimensions: List[str]) -> Dict[str, float]:
|
||||
def evaluate_response(self, scenario: str, response: str, dimensions: list[str]) -> dict[str, float]:
|
||||
"""
|
||||
使用 DeepSeek AI 评估用户对特定场景的反应
|
||||
"""
|
||||
@@ -163,10 +163,10 @@ class PersonalityEvaluatorDirect:
|
||||
return {k: max(1, min(6, float(v))) for k, v in scores.items()}
|
||||
else:
|
||||
print("AI响应格式不正确,使用默认评分")
|
||||
return {dim: 3.5 for dim in dimensions}
|
||||
return dict.fromkeys(dimensions, 3.5)
|
||||
except Exception as e:
|
||||
print(f"评估过程出错:{str(e)}")
|
||||
return {dim: 3.5 for dim in dimensions}
|
||||
print(f"评估过程出错:{e!s}")
|
||||
return dict.fromkeys(dimensions, 3.5)
|
||||
|
||||
def run_evaluation(self):
|
||||
"""
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import orjson
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
|
||||
|
||||
def load_scenes() -> dict[str, Any]:
|
||||
"""
|
||||
@@ -13,7 +14,7 @@ def load_scenes() -> dict[str, Any]:
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
json_path = os.path.join(current_dir, "template_scene.json")
|
||||
|
||||
with open(json_path, "r", encoding="utf-8") as f:
|
||||
with open(json_path, encoding="utf-8") as f:
|
||||
return orjson.loads(f.read())
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user