Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: add storage_unit to get_total_storage_size for embeddings (#1928)

Andy Li8ee56f01dffb
diff --git a/letta/server/rest_api/routers/v1/embeddings.py b/letta/server/rest_api/routers/v1/embeddings.py
index 5b5d51cd0..16a429467 100644
--- a/letta/server/rest_api/routers/v1/embeddings.py
+++ b/letta/server/rest_api/routers/v1/embeddings.py
@@ -9,12 +9,13 @@ router = APIRouter(prefix="/embeddings", tags=["embeddings"])
@router.get("/total_storage_size", response_model=float, operation_id="get_total_storage_size")
-def get_embeddings_storage_size(
+def get_embeddings_total_storage_size(
server: SyncServer = Depends(get_letta_server),
actor_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present
+ storage_unit: Optional[str] = Header("GB", alias="storage_unit"), # Extract storage unit from header, default to GB
):
"""
- Get the total size of all embeddings in the database for a user in GB.
+ Get the total size of all embeddings in the database for a user in the storage unit given.
"""
actor = server.user_manager.get_user_or_default(user_id=actor_id)
- return server.passage_manager.estimate_embeddings_size_GB(actor=actor)
+ return server.passage_manager.estimate_embeddings_size(actor=actor, storage_unit=storage_unit)
diff --git a/letta/services/passage_manager.py b/letta/services/passage_manager.py
index 6cc3b3a66..b891657e0 100644
--- a/letta/services/passage_manager.py
+++ b/letta/services/passage_manager.py
@@ -220,15 +220,24 @@ class PassageManager:
with self.session_maker() as session:
return AgentPassage.size(db_session=session, actor=actor, agent_id=agent_id)
- def estimate_embeddings_size_GB(
+ def estimate_embeddings_size(
self,
actor: PydanticUser,
agent_id: Optional[str] = None,
+ storage_unit: str = "GB",
) -> float:
"""
- Estimate the size of the embeddings in GB.
+ Estimate the size of the embeddings. Defaults to GB.
"""
+ BYTES_PER_STORAGE_UNIT = {
+ "B": 1,
+ "KB": 1024,
+ "MB": 1024**2,
+ "GB": 1024**3,
+ "TB": 1024**4,
+ }
+ if storage_unit not in BYTES_PER_STORAGE_UNIT:
+ raise ValueError(f"Invalid storage unit: {storage_unit}. Must be one of {list(BYTES_PER_STORAGE_UNIT.keys())}.")
BYTES_PER_EMBEDDING_DIM = 4
- BYTES_PER_GB = 1024 * 1024 * 1024
- GB_PER_EMBEDDING = BYTES_PER_EMBEDDING_DIM / BYTES_PER_GB * MAX_EMBEDDING_DIM
+ GB_PER_EMBEDDING = BYTES_PER_EMBEDDING_DIM / BYTES_PER_STORAGE_UNIT[storage_unit] * MAX_EMBEDDING_DIM
return self.size(actor=actor, agent_id=agent_id) * GB_PER_EMBEDDING