fix:简化了身份配置

This commit is contained in:
SengokuCola
2025-05-27 10:50:47 +08:00
parent 8d2e649527
commit 57c9dacb99
9 changed files with 24 additions and 694 deletions

View File

@@ -7,99 +7,24 @@ class Identity:
"""身份特征类"""
identity_detail: List[str] # 身份细节描述
height: int # 身高(厘米)
weight: float # 体重(千克)
age: int # 年龄
gender: str # 性别
appearance: str # 外貌特征
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(
self,
identity_detail: List[str] = None,
height: int = 0,
weight: float = 0,
age: int = 0,
gender: str = "",
appearance: str = "",
):
def __init__(self, identity_detail: List[str] = None):
"""初始化身份特征
Args:
identity_detail: 身份细节描述列表
height: 身高(厘米)
weight: 体重(千克)
age: 年龄
gender: 性别
appearance: 外貌特征
"""
if identity_detail is None:
identity_detail = []
self.identity_detail = identity_detail
self.height = height
self.weight = weight
self.age = age
self.gender = gender
self.appearance = appearance
@classmethod
def get_instance(cls) -> "Identity":
"""获取Identity单例实例
Returns:
Identity: 单例实例
"""
if cls._instance is None:
cls._instance = cls()
return cls._instance
@classmethod
def initialize(
cls, identity_detail: List[str], height: int, weight: float, age: int, gender: str, appearance: str
) -> "Identity":
"""初始化身份特征
Args:
identity_detail: 身份细节描述列表
height: 身高(厘米)
weight: 体重(千克)
age: 年龄
gender: 性别
appearance: 外貌特征
Returns:
Identity: 初始化后的身份特征实例
"""
instance = cls.get_instance()
instance.identity_detail = identity_detail
instance.height = height
instance.weight = weight
instance.age = age
instance.gender = gender
instance.appearance = appearance
return instance
def to_dict(self) -> dict:
"""将身份特征转换为字典格式"""
return {
"identity_detail": self.identity_detail,
"height": self.height,
"weight": self.weight,
"age": self.age,
"gender": self.gender,
"appearance": self.appearance,
}
@classmethod
def from_dict(cls, data: dict) -> "Identity":
"""从字典创建身份特征实例"""
instance = cls.get_instance()
for key, value in data.items():
setattr(instance, key, value)
return instance
return cls(identity_detail=data.get("identity_detail", []))

View File

@@ -1,6 +1,4 @@
from typing import Optional
from numpy import double
from .personality import Personality
from .identity import Identity
from .expression_style import PersonalityExpression
@@ -27,11 +25,6 @@ class Individuality:
personality_core: str,
personality_sides: list,
identity_detail: list,
height: int,
weight: double,
age: int,
gender: str,
appearance: str,
) -> None:
"""初始化个体特征
@@ -40,11 +33,6 @@ class Individuality:
personality_core: 人格核心特点
personality_sides: 人格侧面描述
identity_detail: 身份细节描述
height: 身高(厘米)
weight: 体重(千克)
age: 年龄
gender: 性别
appearance: 外貌特征
"""
# 初始化人格
self.personality = Personality.initialize(
@@ -52,9 +40,7 @@ class Individuality:
)
# 初始化身份
self.identity = Identity.initialize(
identity_detail=identity_detail, height=height, weight=weight, age=age, gender=gender, appearance=appearance
)
self.identity = Identity(identity_detail=identity_detail)
await self.express_style.extract_and_store_personality_expressions()
@@ -120,7 +106,7 @@ class Individuality:
获取身份特征的prompt
Args:
level (int): 详细程度 (1: 随机细节, 2: 所有细节+外貌年龄性别, 3: 同2)
level (int): 详细程度 (1: 随机细节, 2: 所有细节, 3: 同2)
x_person (int, optional): 人称代词 (0: 无人称, 1: 我, 2: 你). 默认为 2.
Returns:
@@ -145,23 +131,10 @@ class Individuality:
identity_detail = list(self.identity.identity_detail)
random.shuffle(identity_detail)
if level == 1:
identity_parts.append(f"身份是{identity_detail[0]}")
identity_parts.append(f"{identity_detail[0]}")
elif level >= 2:
details_str = "".join(identity_detail)
identity_parts.append(f"身份是{details_str}")
# 根据level添加其他身份信息
if level >= 3:
if self.identity.appearance:
identity_parts.append(f"{self.identity.appearance}")
if self.identity.age > 0:
identity_parts.append(f"年龄大约{self.identity.age}")
if self.identity.gender:
identity_parts.append(f"性别是{self.identity.gender}")
if self.identity.height:
identity_parts.append(f"身高大约{self.identity.height}厘米")
if self.identity.weight:
identity_parts.append(f"体重大约{self.identity.weight}千克")
identity_parts.append(f"{details_str}")
if identity_parts:
details_str = "".join(identity_parts)