Spaces:
Running
Running
File size: 10,357 Bytes
e70c280 6663ad4 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | """
File: /data.py
Created Date: Sunday October 19th 2025
Author: Christian Nonis <alch.infoemail@gmail.com>
-----
Last Modified: Wednesday May 20th 2026 8:41:53 pm
Modified By: Christian Nonis <alch.infoemail@gmail.com>
-----
"""
from typing import List, Literal, Tuple
from src.adapters.interfaces.data import DataClient, SearchResult
from src.constants.data import Brain, KGChanges, Observation, StructuredData, TextChunk, Workspace
class DataAdapter:
"""
Data adapter.
"""
def __init__(self):
self.data = None
def add_client(self, client: DataClient) -> None:
"""
Add a data client to the adapter.
"""
self.data = client
def save_text_chunk(
self, text_chunk: TextChunk, brain_id: str = "default"
) -> TextChunk:
"""
Save a text chunk to the data client.
"""
return self.data.save_text_chunk(text_chunk=text_chunk, brain_id=brain_id)
def save_observations(
self, observations: List[Observation], brain_id: str = "default"
) -> List[Observation]:
"""
Save a list of observations to the data client.
"""
return self.data.save_observations(observations=observations, brain_id=brain_id)
def search(self, text: str, brain_id: str = "default") -> SearchResult:
"""
Search data by text and return a list of text chunks and observations.
"""
return self.data.search(text=text, brain_id=brain_id)
def search_bm25(
self, text: str, brain_id: str = "default", limit: int = 10
) -> List[Tuple[TextChunk, float]]:
return self.data.search_bm25(text=text, brain_id=brain_id, limit=limit)
def get_text_chunks_by_ids(
self, ids: List[str], with_observations: bool, brain_id: str = "default"
) -> Tuple[List[TextChunk], List[Observation]]:
"""
Get data by their IDs.
"""
return self.data.get_text_chunks_by_ids(
ids=ids, with_observations=with_observations, brain_id=brain_id
)
def get_text_chunks(
self,
brain_id: str = "default",
limit: int = 10,
skip: int = 0,
query_text: str = None,
metadata_eq: dict[str, str] | None = None,
order: Literal["asc", "desc"] = "desc",
) -> Tuple[List[TextChunk], int]:
"""
Get text chunks with pagination, optional text search, and metadata equality filters.
"""
return self.data.get_text_chunks(
brain_id=brain_id,
limit=limit,
skip=skip,
query_text=query_text,
metadata_eq=metadata_eq,
order=order,
)
def save_structured_data(
self, structured_data: StructuredData, brain_id: str = "default"
) -> StructuredData:
"""
Save a structured data to the data client.
"""
return self.data.save_structured_data(
structured_data=structured_data, brain_id=brain_id
)
def create_brain(self, name_key: str) -> Brain:
"""
Create a new brain in the data client.
"""
brain = self.data.create_brain(name_key=name_key)
from src.config import config
if config.data_db.strip().lower() != "postgresql" and (
config.vector_db.strip().lower() == "postgresql"
or config.graph_db.strip().lower() == "networkx"
):
# A Mongo registry with PostgreSQL graph/vectors also provisions only
# at this explicit creation boundary, never from backend reads.
from src.lib.postgresql._provisioning import ensure_brain_database
ensure_brain_database(brain.name_key)
return brain
def get_brain(self, name_key: str) -> Brain:
"""
Get a brain from the data client.
"""
return self.data.get_brain(name_key=name_key)
def get_brain_by_pat(self, pat: str) -> Brain | None:
"""
Get a brain from the data client by its PAT.
"""
return self.data.get_brain_by_pat(pat=pat)
def get_brains_list(self) -> List[Brain]:
"""
Get the list of brains from the data client.
"""
return self.data.get_brains_list()
def create_workspace(self, workspace: Workspace) -> Workspace:
return self.data.create_workspace(workspace)
def get_workspace(self, slug: str) -> Workspace | None:
return self.data.get_workspace(slug)
def get_workspace_by_brain_id(self, brain_id: str) -> Workspace | None:
return self.data.get_workspace_by_brain_id(brain_id)
def get_workspaces(self) -> list[Workspace]:
return self.data.get_workspaces()
def update_workspace(self, workspace: Workspace) -> Workspace:
return self.data.update_workspace(workspace)
def save_kg_changes(
self, kg_changes: KGChanges, brain_id: str = "default"
) -> KGChanges:
"""
Save a KG changes to the data client.
"""
return self.data.save_kg_changes(kg_changes=kg_changes, brain_id=brain_id)
def get_structured_data_by_id(
self, id: str, brain_id: str = "default"
) -> StructuredData:
"""
Get structured data by ID.
"""
return self.data.get_structured_data_by_id(id=id, brain_id=brain_id)
def get_structured_data_list(
self,
brain_id: str = "default",
limit: int = 10,
skip: int = 0,
types: list[str] = None,
query_text: str = None,
) -> Tuple[list[StructuredData], int]:
"""
Get a list of structured data.
"""
return self.data.get_structured_data_list(
brain_id=brain_id,
limit=limit,
skip=skip,
types=types,
query_text=query_text,
)
def get_structured_data_types(self, brain_id: str = "default") -> list[str]:
"""
Get all unique types from structured data.
"""
return self.data.get_structured_data_types(brain_id=brain_id)
def get_observation_by_id(self, id: str, brain_id: str = "default") -> Observation:
"""
Get observation by ID.
"""
return self.data.get_observation_by_id(id=id, brain_id=brain_id)
def get_observations_list(
self,
brain_id: str = "default",
limit: int = 10,
skip: int = 0,
resource_id: str = None,
labels: list[str] = None,
query_text: str = None,
) -> list[Observation]:
"""
Get a list of observations.
"""
return self.data.get_observations_list(
brain_id=brain_id,
limit=limit,
skip=skip,
resource_id=resource_id,
labels=labels,
query_text=query_text,
)
def get_observation_labels(self, brain_id: str = "default") -> list[str]:
"""
Get all unique observation labels for the specified brain.
Parameters:
brain_id (str): Identifier of the brain to query.
Returns:
list[str]: A list of unique observation label strings for the specified brain.
"""
return self.data.get_observation_labels(brain_id=brain_id)
def get_changelog_by_id(self, id: str, brain_id: str = "default") -> KGChanges:
"""
Retrieve a changelog entry by its identifier.
Parameters:
id (str): Identifier of the changelog entry to retrieve.
brain_id (str): Brain namespace key to query; defaults to "default".
Returns:
KGChanges: The changelog entry matching the given `id`.
"""
return self.data.get_changelog_by_id(id=id, brain_id=brain_id)
def get_changelogs_list(
self,
brain_id: str = "default",
limit: int = 10,
skip: int = 0,
types: list[str] = None,
query_text: str = None,
) -> list[KGChanges]:
"""
Retrieve a paginated list of knowledge-graph changelogs for a brain.
Parameters:
brain_id (str): Identifier of the brain to query.
limit (int): Maximum number of changelogs to return.
skip (int): Number of changelogs to skip (offset).
types (list[str] | None): If provided, restrict results to these changelog types.
query_text (str | None): If provided, filter changelogs by matching text.
Returns:
list[KGChanges]: Changelogs matching the filters and pagination parameters.
"""
return self.data.get_changelogs_list(
brain_id=brain_id,
limit=limit,
skip=skip,
types=types,
query_text=query_text,
)
def get_changelog_types(self, brain_id: str = "default") -> list[str]:
"""
Retrieve distinct changelog types for a brain.
Parameters:
brain_id (str): Identifier of the brain to query; defaults to "default".
Returns:
list[str]: List of changelog type names.
"""
return self.data.get_changelog_types(brain_id=brain_id)
def update_structured_data(
self, structured_data: StructuredData, brain_id: str = "default"
) -> StructuredData:
"""
Update an existing structured data entry.
Parameters:
structured_data (StructuredData): The structured data object with updated information.
brain_id (str): Identifier of the brain context for the update.
Returns:
StructuredData: The updated structured data object.
"""
return self.data.update_structured_data(
structured_data=structured_data, brain_id=brain_id
)
def get_last_text_chunks(
self, brain_id: str = "default", limit: int = 10
) -> list[TextChunk]:
"""
Get the last text chunks from the data client.
"""
return self.data.get_last_text_chunks(brain_id=brain_id, limit=limit)
def get_last_structured_data(
self, brain_id: str = "default", limit: int = 10
) -> list[StructuredData]:
"""
Get the last structured data from the data client.
"""
return self.data.get_last_structured_data(brain_id=brain_id, limit=limit)
|