Spaces:
Sleeping
Sleeping
File size: 7,117 Bytes
01d5a5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
from sqlalchemy import Column, Integer, String, DateTime, JSON, ForeignKey
from sqlalchemy.orm import relationship
from lpm_kernel.common.repository.database_session import Base
from datetime import datetime
from dataclasses import dataclass
from typing import Dict, List, Optional
from lpm_kernel.L1.bio import Bio
class L1Version(Base):
__tablename__ = "l1_versions"
version = Column(Integer, primary_key=True)
create_time = Column(DateTime, nullable=False, default=datetime.now)
status = Column(String(50), nullable=False)
description = Column(String(500))
# add relationship
bios = relationship("L1Bio", back_populates="version_info")
shades = relationship("L1Shade", back_populates="version_info")
clusters = relationship("L1Cluster", back_populates="version_info")
chunk_topics = relationship("L1ChunkTopic", back_populates="version_info")
class L1Bio(Base):
__tablename__ = "l1_bios"
id = Column(Integer, primary_key=True)
version = Column(Integer, ForeignKey("l1_versions.version"), nullable=False)
content = Column(String(10000))
content_third_view = Column(String(10000))
summary = Column(String(2000))
summary_third_view = Column(String(2000))
create_time = Column(DateTime, nullable=False, default=datetime.now)
# add relationship
version_info = relationship("L1Version", back_populates="bios")
class L1Shade(Base):
__tablename__ = "l1_shades"
id = Column(Integer, primary_key=True)
version = Column(Integer, ForeignKey("l1_versions.version"), nullable=False)
name = Column(String(200))
aspect = Column(String(200))
icon = Column(String(100))
desc_third_view = Column(String(1000))
content_third_view = Column(String(2000))
desc_second_view = Column(String(1000))
content_second_view = Column(String(2000))
create_time = Column(DateTime, nullable=False, default=datetime.now)
# add relationship
version_info = relationship("L1Version", back_populates="shades")
class L1Cluster(Base):
__tablename__ = "l1_clusters"
id = Column(Integer, primary_key=True)
version = Column(Integer, ForeignKey("l1_versions.version"), nullable=False)
cluster_id = Column(String(100))
memory_ids = Column(JSON)
cluster_center = Column(JSON)
create_time = Column(DateTime, nullable=False, default=datetime.now)
# add relationship
version_info = relationship("L1Version", back_populates="clusters")
class L1ChunkTopic(Base):
__tablename__ = "l1_chunk_topics"
id = Column(Integer, primary_key=True)
version = Column(Integer, ForeignKey("l1_versions.version"), nullable=False)
chunk_id = Column(String(100))
topic = Column(String(500))
tags = Column(JSON)
create_time = Column(DateTime, nullable=False, default=datetime.now)
# add relationship
version_info = relationship("L1Version", back_populates="chunk_topics")
class L1VersionDTO:
def __init__(
self, version: int, status: str, description: str, create_time: datetime
):
self.version = version
self.status = status
self.description = description
self.create_time = create_time
@classmethod
def from_model(cls, model: "L1Version") -> "L1VersionDTO":
return cls(
version=model.version,
status=model.status,
description=model.description,
create_time=model.create_time,
)
@dataclass
class L1GenerationResult:
"""L1 generation result data class"""
bio: Bio
clusters: Dict[str, List] # {"clusterList": [...]}
chunk_topics: Dict[str, Dict] # {cluster_id: {"indices": [], "docIds": [], ...}}
generate_time: datetime = datetime.now()
def to_dict(self) -> dict:
"""Convert to dictionary format"""
return {
"bio": self.bio,
"clusters": self.clusters,
"chunk_topics": self.chunk_topics,
"generate_time": self.generate_time.isoformat(),
}
@classmethod
def from_dict(cls, data: dict) -> "L1GenerationResult":
"""Create instance from dictionary"""
return cls(
bio=data.get("bio"),
clusters=data.get("clusters", {"clusterList": []}),
chunk_topics=data.get("chunk_topics", {}),
generate_time=datetime.fromisoformat(data["generate_time"])
if "generate_time" in data
else datetime.now(),
)
@dataclass
class GlobalBioDTO:
"""Global biography data transfer object"""
content: str
content_third_view: str
summary: str
summary_third_view: str
create_time: datetime
shades: List[Dict] = None # add shades field
@classmethod
def from_model(cls, model: "L1Bio") -> "GlobalBioDTO":
"""
Create DTO from database model
Args:
model (L1Bio): database model object
Returns:
GlobalBioDTO: data transfer object
"""
return cls(
content=model.content,
content_third_view=model.content_third_view,
summary=model.summary,
summary_third_view=model.summary_third_view,
create_time=model.create_time,
shades=[], # initialize as empty list
)
def to_dict(self) -> dict:
"""
Convert to dictionary format
Returns:
dict: dictionary format data
"""
return {
"content": self.content,
"content_third_view": self.content_third_view,
"summary": self.summary,
"summary_third_view": self.summary_third_view,
"create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
"shades": self.shades or [], # ensure return list
}
@dataclass
class StatusBioDTO:
"""Status biography data transfer object"""
content: str
content_third_view: str
summary: str
summary_third_view: str
create_time: datetime
update_time: datetime
@classmethod
def from_model(cls, model: "StatusBiography") -> "StatusBioDTO":
"""Create DTO from database model
Args:
model (StatusBiography): database model object
Returns:
StatusBioDTO: data transfer object
"""
return cls(
content=model.content,
content_third_view=model.content_third_view,
summary=model.summary,
summary_third_view=model.summary_third_view,
create_time=model.create_time,
update_time=model.update_time,
)
def to_dict(self) -> dict:
"""Convert to dictionary format
Returns:
dict: dictionary format data
"""
return {
"content": self.content,
"content_third_view": self.content_third_view,
"summary": self.summary,
"summary_third_view": self.summary_third_view,
"create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
"update_time": self.update_time.strftime("%Y-%m-%d %H:%M:%S"),
}
|