Spaces:
Runtime error
Runtime error
Download src/models/bot.py from BattleFiven/ai-tester-platform: direct link, hf CLI and curl.
- Browser
- Download file 6.13 kB
-
https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/models/bot.py
- Command line
-
hf download hf://spaces/BattleFiven/ai-tester-platform/src/models/bot.py
-
curl -L -o bot.py https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/models/bot.py
6.13 kB
| """Models for Bot configuration and RAG documents.""" | |
| from datetime import datetime | |
| from typing import Any, Dict, List, Optional | |
| from uuid import UUID, uuid4 | |
| from pydantic import BaseModel, ConfigDict, Field, HttpUrl | |
| class ToneConfig(BaseModel): | |
| """Configurazione tone-of-voice atteso dal bot.""" | |
| formality_level: int = Field(default=5, ge=1, le=10, description="1=casual, 10=formal") | |
| use_emoji: bool = Field(default=False) | |
| emoji_style: Optional[str] = Field(default=None, description="professional, friendly, etc") | |
| avoid_jargon: bool = Field(default=False) | |
| preferred_phrases: List[str] = Field(default_factory=list) | |
| avoid_phrases: List[str] = Field(default_factory=list) | |
| max_response_length: Optional[int] = Field(default=None) | |
| language_style: str = Field( | |
| default="professional", description="professional, casual, technical" | |
| ) | |
| # Valutazione | |
| tone_match_threshold: float = Field(default=0.7, ge=0.0, le=1.0) | |
| class RAGConfig(BaseModel): | |
| """Configurazione per valutazione RAG.""" | |
| faithfulness_threshold: float = Field(default=0.8, ge=0.0, le=1.0) | |
| coverage_threshold: float = Field(default=0.7, ge=0.0, le=1.0) | |
| citation_required: bool = Field(default=False) | |
| max_hallucination_tolerance: float = Field(default=0.1, ge=0.0, le=1.0) | |
| # Comportamento | |
| strict_mode: bool = Field(default=False, description="Se True, qualsiasi hallucination = fail") | |
| class Selectors(BaseModel): | |
| """CSS Selectors per interagire con il bot.""" | |
| chat_input: str = Field( | |
| default="[data-testid='chat-input'], textarea[placeholder*='message'], #chat-input" | |
| ) | |
| chat_messages: str = Field(default=".message, .chat-bubble, [data-testid='chat-message']") | |
| send_button: str = Field(default="button[type='submit'], .send-btn") | |
| user_messages: str = Field(default=".user-message, .message-user") | |
| bot_messages: str = Field(default=".bot-message, .message-bot, .assistant-message") | |
| class Bot(BaseModel): | |
| """Configurazione di un Bot da testare.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: Optional[UUID] = Field(default=None, description="Workspace scope") | |
| name: str = Field(..., description="Nome identificativo del bot") | |
| description: Optional[str] = Field(default=None) | |
| url: HttpUrl = Field(..., description="URL dove risiede il bot") | |
| # Knowledge Boundary - Limite di conoscenza di Duolly | |
| knowledge_boundary_url: Optional[HttpUrl] = Field( | |
| default=None, description="URL alla guida che definisce cosa Duolly può/non può fare" | |
| ) | |
| knowledge_boundary_content: Optional[str] = Field( | |
| default=None, description="Contenuto cache della guida (popolato automaticamente)" | |
| ) | |
| knowledge_boundary_updated_at: Optional[datetime] = Field( | |
| default=None, description="Ultimo aggiornamento della guida" | |
| ) | |
| # Configurazioni | |
| selectors: Selectors = Field(default_factory=Selectors) | |
| expected_tone: ToneConfig = Field(default_factory=ToneConfig) | |
| rag_config: RAGConfig = Field(default_factory=RAGConfig) | |
| # Metadata | |
| is_active: bool = Field(default=True) | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| updated_at: datetime = Field(default_factory=datetime.now) | |
| created_by: Optional[str] = Field(default=None) | |
| # Tags per categorizzazione | |
| tags: List[str] = Field(default_factory=list) | |
| # Comportamento | |
| auto_click_new_chat: bool = Field( | |
| default=False, | |
| description="Clicca automaticamente 'Nuova Chat' prima di iniziare la sessione", | |
| ) | |
| deterministic_fallbacks: List[str] = Field( | |
| default_factory=list, | |
| description="Fallback responses considered valid when the query is out-of-scope.", | |
| ) | |
| def get_full_config(self) -> Dict[str, Any]: | |
| """Restituisce configurazione completa come dict.""" | |
| return self.model_dump() | |
| class RAGDocument(BaseModel): | |
| """Documento RAG di riferimento per il bot.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: Optional[UUID] = Field(default=None, description="Workspace scope") | |
| bot_id: UUID = Field(..., description="ID del bot a cui appartiene") | |
| # File info | |
| filename: str = Field(..., description="Nome file originale") | |
| content_hash: str = Field(..., description="Hash SHA256 del contenuto") | |
| file_size: int = Field(..., description="Dimensione in bytes") | |
| mime_type: str = Field(default="text/plain") | |
| # Storage (locale per ora, Supabase Storage in futuro) | |
| storage_path: str = Field(..., description="Path dove è salvato il file") | |
| # Contenuto estratto (per ricerca/test) | |
| extracted_text: Optional[str] = Field(default=None) | |
| # Metadata | |
| description: Optional[str] = Field(default=None) | |
| metadata: Dict[str, Any] = Field(default_factory=dict) | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| def get_summary(self) -> str: | |
| """Restituisce summary del documento.""" | |
| text_preview = (self.extracted_text or "")[:200] | |
| return f"{self.filename} ({self.file_size} bytes): {text_preview}..." | |
| class BotVersion(BaseModel): | |
| """Versione specifica del bot testata (per regression testing).""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: Optional[UUID] = Field(default=None, description="Workspace scope") | |
| bot_id: UUID = Field(...) | |
| version: str = Field(..., description="Versione semantic (es: v1.2.3)") | |
| release_notes: Optional[str] = Field(default=None) | |
| # Snapshot della configurazione al momento del test | |
| config_snapshot: Dict[str, Any] = Field(default_factory=dict) | |
| rag_documents_snapshot: List[UUID] = Field(default_factory=list) | |
| # Stato | |
| is_tested: bool = Field(default=False) | |
| is_approved: bool = Field(default=False) | |
| approved_at: Optional[datetime] = Field(default=None) | |
| approved_by: Optional[str] = Field(default=None) | |
| created_at: datetime = Field(default_factory=datetime.now) | |