Spaces:
Paused
Paused
Download backend/domain/models.py from data-llm-hub/tensorwall: direct link, hf CLI and curl.
- Browser
- Download file 3.85 kB
-
https://huggingface.co/spaces/data-llm-hub/tensorwall/resolve/main/backend/domain/models.py
- Command line
-
hf download hf://spaces/data-llm-hub/tensorwall/backend/domain/models.py
-
curl -L -o models.py https://huggingface.co/spaces/data-llm-hub/tensorwall/resolve/main/backend/domain/models.py
3.85 kB
| """Domain Models - Pure Python, AUCUNE dépendance externe. | |
| Ce module contient les entités métier pures. | |
| INTERDIT: SQLAlchemy, Pydantic, imports de backend.* | |
| AUTORISÉ: dataclasses, enum, typing, uuid, datetime | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass, field | |
| from enum import Enum | |
| from typing import Any | |
| from uuid import uuid4 | |
| # ============================================================================= | |
| # Enums | |
| # ============================================================================= | |
| class Environment(str, Enum): | |
| """Environnements d'exécution.""" | |
| DEVELOPMENT = "development" | |
| STAGING = "staging" | |
| PRODUCTION = "production" | |
| class PolicyAction(str, Enum): | |
| """Actions possibles d'une policy.""" | |
| ALLOW = "allow" | |
| WARN = "warn" | |
| DENY = "deny" | |
| class BudgetPeriod(str, Enum): | |
| """Périodes de budget.""" | |
| HOURLY = "hourly" | |
| DAILY = "daily" | |
| WEEKLY = "weekly" | |
| MONTHLY = "monthly" | |
| class DecisionOutcome(str, Enum): | |
| """Résultat d'une décision.""" | |
| ALLOW = "allow" | |
| DENY = "deny" | |
| WARN = "warn" | |
| # ============================================================================= | |
| # Domain Entities | |
| # ============================================================================= | |
| class PolicyRule: | |
| """Règle de policy.""" | |
| id: str | |
| name: str | |
| action: PolicyAction | |
| priority: int = 0 | |
| conditions: dict[str, Any] = field(default_factory=dict) | |
| enabled: bool = True | |
| class Budget: | |
| """Budget pour une application.""" | |
| id: str | |
| app_id: str | |
| limit_usd: float | |
| spent_usd: float = 0.0 | |
| period: BudgetPeriod = BudgetPeriod.MONTHLY | |
| def remaining_usd(self) -> float: | |
| return max(0.0, self.limit_usd - self.spent_usd) | |
| def usage_percent(self) -> float: | |
| if self.limit_usd <= 0: | |
| return 0.0 | |
| return min(100.0, (self.spent_usd / self.limit_usd) * 100) | |
| def is_exceeded(self) -> bool: | |
| return self.spent_usd >= self.limit_usd | |
| class GatewayDecision: | |
| """Décision du gateway.""" | |
| outcome: DecisionOutcome | |
| reason: str | |
| decision_id: str = field(default_factory=lambda: str(uuid4())) | |
| details: dict[str, Any] = field(default_factory=dict) | |
| warnings: list[str] = field(default_factory=list) | |
| def is_allowed(self) -> bool: | |
| return self.outcome == DecisionOutcome.ALLOW | |
| def is_denied(self) -> bool: | |
| return self.outcome == DecisionOutcome.DENY | |
| # ============================================================================= | |
| # LLM DTOs (pour le domain, pas pour l'API) | |
| # ============================================================================= | |
| class ChatMessage: | |
| """Message de chat.""" | |
| role: str | |
| content: str | |
| class ChatRequest: | |
| """Requête de chat.""" | |
| model: str | |
| messages: list[ChatMessage] | |
| max_tokens: int | None = None | |
| temperature: float | None = None | |
| stream: bool = False | |
| class ChatResponse: | |
| """Réponse de chat.""" | |
| id: str | |
| model: str | |
| content: str | |
| input_tokens: int | |
| output_tokens: int | |
| finish_reason: str | None = None | |
| # ============================================================================= | |
| # Embedding DTOs | |
| # ============================================================================= | |
| class EmbeddingRequest: | |
| """Requête d'embedding.""" | |
| model: str | |
| input: list[str] | |
| encoding_format: str = "float" | |
| class EmbeddingData: | |
| """Données d'embedding pour un input.""" | |
| index: int | |
| embedding: list[float] | |
| class EmbeddingResponse: | |
| """Réponse d'embedding.""" | |
| model: str | |
| data: list[EmbeddingData] | |
| total_tokens: int | |