|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import json |
|
|
import uuid |
|
|
from typing import ClassVar, Optional, Union |
|
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, PrivateAttr |
|
|
|
|
|
from camel.prompts import PersonaHubPrompt, TextPrompt |
|
|
|
|
|
|
|
|
class Persona(BaseModel): |
|
|
r"""A persona is a character in the society. |
|
|
|
|
|
Attributes: |
|
|
name (Optional[str]): Name of the persona. |
|
|
description (Optional[str]): Description of the persona. |
|
|
text_to_persona_prompt (Union[TextPrompt, str]): The prompt to convert |
|
|
text into a persona. |
|
|
persona_to_persona_prompt (Union[TextPrompt, str]): Persona-to-Persona |
|
|
interaction prompt. |
|
|
id (uuid.UUID): The unique identifier for the persona, automatically |
|
|
generated. |
|
|
_id (uuid.UUID): Internal unique identifier for the persona, |
|
|
generated lazily using `uuid.uuid4`. |
|
|
model_config (ClassVar[ConfigDict]): Configuration for the Pydantic |
|
|
model. Allows arbitrary types and includes custom JSON schema |
|
|
settings. |
|
|
""" |
|
|
|
|
|
name: Optional[str] = None |
|
|
description: Optional[str] = None |
|
|
_id: uuid.UUID = PrivateAttr(default_factory=uuid.uuid4) |
|
|
|
|
|
|
|
|
|
|
|
text_to_persona_prompt: Union[TextPrompt, str] = Field( |
|
|
default_factory=lambda: PersonaHubPrompt.TEXT_TO_PERSONA, |
|
|
description="Text to Persona Prompt", |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
persona_to_persona_prompt: Union[TextPrompt, str] = Field( |
|
|
default_factory=lambda: PersonaHubPrompt.PERSONA_TO_PERSONA, |
|
|
description="Persona to Persona Prompt", |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
model_config: ClassVar[ConfigDict] = ConfigDict( |
|
|
|
|
|
arbitrary_types_allowed=True, |
|
|
|
|
|
json_schema_extra={ |
|
|
"properties": { |
|
|
|
|
|
|
|
|
"text_to_persona_prompt": {"type": "string"}, |
|
|
"persona_to_persona_prompt": {"type": "string"}, |
|
|
} |
|
|
}, |
|
|
) |
|
|
|
|
|
@property |
|
|
def id(self) -> uuid.UUID: |
|
|
return self._id |
|
|
|
|
|
@classmethod |
|
|
def model_json_schema(cls): |
|
|
schema = super().schema() |
|
|
schema['properties']['id'] = {'type': 'string', 'format': 'uuid'} |
|
|
return schema |
|
|
|
|
|
def dict(self, *args, **kwargs): |
|
|
|
|
|
d = super().model_dump(*args, **kwargs) |
|
|
d['id'] = str(self.id) |
|
|
return d |
|
|
|
|
|
def json(self, *args, **kwargs): |
|
|
|
|
|
d = self.dict(*args, **kwargs) |
|
|
return json.dumps( |
|
|
d, |
|
|
indent=4, |
|
|
sort_keys=True, |
|
|
separators=( |
|
|
",", |
|
|
": ", |
|
|
), |
|
|
) |
|
|
|