86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
from pydantic import BaseModel, Field
|
|
from typing import List, Optional
|
|
from pathlib import Path
|
|
import uuid
|
|
import json
|
|
from datetime import datetime
|
|
import os
|
|
|
|
class Character():
|
|
"""Основной класс персонажа"""
|
|
def __init__(
|
|
self,
|
|
name: str,
|
|
display_name: str,
|
|
char_description: str = "",
|
|
avatar_path: Optional[str] = None,
|
|
backgrounds: Optional[str] = None,
|
|
persona: str = "",
|
|
scenario: str = "",
|
|
first_message: str = "",
|
|
id: Optional[str] = None,
|
|
temperature: Optional[float] = None,
|
|
max_tokens: Optional[int] = None
|
|
|
|
):
|
|
self.id = id if id is not None else str(uuid.uuid4())
|
|
self.name = name
|
|
self.display_name = display_name
|
|
self.char_description = char_description
|
|
self.avatar_path = avatar_path
|
|
self.backgrounds = backgrounds
|
|
self.persona = persona
|
|
self.scenario = scenario
|
|
self.first_message = first_message
|
|
self.temperature = temperature
|
|
self.max_tokens = max_tokens
|
|
|
|
#методы:
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"id": self.id,
|
|
"name": self.name,
|
|
"display_name": self.display_name,
|
|
"char_description": self.char_description,
|
|
"avatar_path": self.avatar_path,
|
|
"backgrounds": self.backgrounds,
|
|
"persona": self.persona,
|
|
"scenario": self.scenario,
|
|
"first_message": self.first_message,
|
|
"temperature": self.temperature,
|
|
"max_tokens": self.max_tokens
|
|
|
|
}
|
|
|
|
def save(self):
|
|
# Используем абсолютный путь от корня проекта
|
|
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
char_path = os.path.join(project_root, "data", "characters", f"{self.id}.json")
|
|
|
|
with open(char_path, "w", encoding="utf-8") as f:
|
|
json.dump(self.to_dict(), f, ensure_ascii=False, indent=2)
|
|
|
|
@classmethod
|
|
def load(cls, char_id):
|
|
# Используем абсолютный путь от корня проекта
|
|
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
char_path = os.path.join(project_root, "data", "characters", f"{char_id}.json")
|
|
|
|
with open(char_path, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
return cls(
|
|
id=data.get("id"),
|
|
name=data.get("name", ""),
|
|
display_name=data.get("display_name", ""),
|
|
char_description=data.get("char_description", ""),
|
|
avatar_path=data.get("avatar_path"),
|
|
backgrounds=data.get("backgrounds"),
|
|
persona=data.get("persona", ""),
|
|
scenario=data.get("scenario", ""),
|
|
first_message=data.get("first_message", ""),
|
|
temperature=data.get("temperature"),
|
|
max_tokens=data.get("max_tokens")
|
|
|
|
) |