Spaces:
Running
Running
File size: 7,775 Bytes
e70c280 2f1e7ff e70c280 | 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | """
File: /data.py
Created Date: Saturday October 25th 2025
Author: Christian Nonis <alch.infoemail@gmail.com>
-----
Last Modified: Sunday April 12th 2026 1:35:36 pm
Modified By: Christian Nonis <alch.infoemail@gmail.com>
-----
"""
import tomllib
import uuid
from datetime import datetime
from enum import Enum
from pathlib import Path
from typing import Annotated, Any, List, Literal, Optional, Union
from pydantic import BaseModel, Field
_PYPROJECT_PATH = Path(__file__).resolve().parent.parent.parent / "pyproject.toml"
with _PYPROJECT_PATH.open("rb") as _f:
BRAIN_VERSION = tomllib.load(_f)["project"]["version"]
class TextChunk(BaseModel):
"""
Text chunk model.
"""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
text: str = Field(description="The text of the chunk.")
metadata: Optional[dict] = None
inserted_at: datetime = Field(
default_factory=datetime.now,
description="The date and time the chunk was inserted.",
)
brain_version: str = Field(
description="The version of the brain when the chunk was inserted.",
default="0.0.0",
)
class StructuredData(BaseModel):
"""
Structured data model.
"""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
data: dict = Field(description="The json data rapresenting the structured element.")
types: List[str] = Field(
description="A list of types, used to categorize the data."
)
metadata: Optional[dict] = None
inserted_at: datetime = Field(
default_factory=datetime.now,
description="The date and time the structured data was inserted.",
)
brain_version: str = Field(
description="The version of the brain when the structured data was inserted.",
default="0.0.0",
)
class Observation(BaseModel):
"""
Observation model.
"""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
text: str = Field(description="The text of the observation.")
metadata: Optional[dict] = None
resource_id: str = Field(
description="The id of the resource the observation is about."
)
inserted_at: datetime = Field(
default_factory=datetime.now,
description="The date and time the observation was inserted.",
)
class KGChangesType(Enum):
"""
KG changes type.
"""
RELATIONSHIP_CREATED = "relationship_created"
RELATIONSHIP_DEPRECATED = "relationship_deprecated"
NODE_PROPERTIES_UPDATED = "node_properties_updated"
RELATIONSHIP_PROPERTIES_UPDATED = "relationship_properties_updated"
class PartialNode(BaseModel):
"""
Partial node model.
"""
uuid: str = Field(description="The id of the node.")
name: str = Field(description="The name of the node.")
labels: List[str] = Field(description="The labels of the node.")
description: Optional[str] = Field(description="The description of the node.")
properties: dict = Field(description="The properties of the node.")
class PartialPredicate(BaseModel):
"""
Partial relationship model.
"""
uuid: str = Field(description="The id of the relationship.")
name: str = Field(description="The name of the relationship.")
description: Optional[str] = Field(
description="The description of the relationship."
)
properties: Optional[dict] = Field(
default=None, description="The properties of the relationship."
)
amount: Optional[Union[int, float]] = Field(
default=None,
description="The amount of the relationship, if it is a quantitative relationship.",
)
class KGChangeLogRelationshipCreated(BaseModel):
"""
KG change log relationship created model.
"""
type: Literal[KGChangesType.RELATIONSHIP_CREATED] = Field(
default=KGChangesType.RELATIONSHIP_CREATED,
description="The type of the change.",
)
subject: PartialNode = Field(description="The subject of the relationship.")
predicate: PartialPredicate = Field(
description="The predicate of the relationship."
)
object: PartialNode = Field(description="The object of the relationship.")
class KGChangeLogRelationshipDeprecated(BaseModel):
"""
KG change log relationship deprecated model.
"""
type: Literal[KGChangesType.RELATIONSHIP_DEPRECATED] = Field(
default=KGChangesType.RELATIONSHIP_DEPRECATED,
description="The type of the change.",
)
subject: PartialNode = Field(description="The subject of the relationship.")
predicate: PartialPredicate = Field(
description="The predicate of the relationship."
)
object: PartialNode = Field(description="The object of the relationship.")
new_predicate: Optional[PartialPredicate] = Field(
description="The new predicate of the relationship."
)
class KGChangeLogPredicateUpdatedProperty(BaseModel):
"""
KG change log relationship updated property model.
"""
property: str = Field(description="The property that was updated.")
previous_value: Any = Field(description="The previous value of the property.")
new_value: Any = Field(description="The new value of the property.")
class KGChangeLogNodePropertiesUpdated(BaseModel):
"""
KG change log node properties updated model.
"""
type: Literal[KGChangesType.NODE_PROPERTIES_UPDATED] = Field(
default=KGChangesType.NODE_PROPERTIES_UPDATED,
description="The type of the change.",
)
node: PartialNode = Field(description="The node that was updated.")
properties: List[KGChangeLogPredicateUpdatedProperty] = Field(
description="The properties that were updated."
)
class KGChangeLogPredicatePropertiesUpdated(BaseModel):
"""
KG change log relationship properties updated model.
"""
type: Literal[KGChangesType.RELATIONSHIP_PROPERTIES_UPDATED] = Field(
default=KGChangesType.RELATIONSHIP_PROPERTIES_UPDATED,
description="The type of the change.",
)
predicate: PartialPredicate = Field(description="The predicate that was updated.")
properties: List[KGChangeLogPredicateUpdatedProperty] = Field(
description="The properties that were updated."
)
class KGChanges(BaseModel):
"""
KG changes model.
"""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
type: KGChangesType = Field(description="The type of the changes.")
change: Annotated[
Union[
KGChangeLogRelationshipCreated,
KGChangeLogRelationshipDeprecated,
KGChangeLogNodePropertiesUpdated,
KGChangeLogPredicatePropertiesUpdated,
],
Field(discriminator="type"),
] = Field(description="The change data, discriminated by type.")
timestamp: datetime = Field(
default_factory=datetime.now,
description="The timestamp of the changes.",
)
class Brain(BaseModel):
"""
Model for a single brain, stored into data db
"""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
name_key: str = Field(description="The key used to identify the brain.")
# Compatibility only: existing persisted PATs may be read during migration.
# They are excluded from every API/JSON serialization and model repr.
pat: str | None = Field(default=None, exclude=True, repr=False)
class Workspace(BaseModel):
"""Routing and display metadata for an existing brain."""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
brain_id: str
slug: str
display_name: str
description: str | None = None
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
archived: bool = False
|