Spaces:
Runtime error
Runtime error
Download src/models/knowledge.py from BattleFiven/ai-tester-platform: direct link, hf CLI and curl.
- Browser
- Download file 6.17 kB
-
https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/models/knowledge.py
- Command line
-
hf download hf://spaces/BattleFiven/ai-tester-platform/src/models/knowledge.py
-
curl -L -o knowledge.py https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/models/knowledge.py
6.17 kB
| """Knowledge source and memory indexing models.""" | |
| from datetime import datetime | |
| from enum import Enum | |
| from typing import Any, Dict, List, Optional | |
| from uuid import UUID, uuid4 | |
| from pydantic import BaseModel, ConfigDict, Field | |
| class KnowledgeSourceType(str, Enum): | |
| EXCEL = "excel" | |
| CSV = "csv" | |
| URL = "url" | |
| PDF = "pdf" | |
| DOC = "doc" | |
| DOCX = "docx" | |
| TXT = "txt" | |
| DOCUMENT = "document" | |
| API_STUB = "api_stub" | |
| class KnowledgeVersionState(str, Enum): | |
| DRAFT = "draft" | |
| ACTIVE = "active" | |
| ARCHIVED = "archived" | |
| FAILED = "failed" | |
| class KnowledgeCrawlTrigger(str, Enum): | |
| COMMIT = "commit" | |
| MANUAL_BOT_RECRAWL = "manual_bot_recrawl" | |
| class KnowledgeCrawlStatus(str, Enum): | |
| QUEUED = "queued" | |
| RUNNING = "running" | |
| AWAITING_BROWSER_FALLBACK = "awaiting_browser_fallback" | |
| COMPLETED = "completed" | |
| FAILED = "failed" | |
| CANCELLED = "cancelled" | |
| class KnowledgeCrawlPhase(str, Enum): | |
| HTTP_FETCH = "http_fetch" | |
| BROWSER_FALLBACK = "browser_fallback" | |
| INDEXING = "indexing" | |
| class KnowledgeSource(BaseModel): | |
| """Logical source container.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: UUID | |
| name: str | |
| source_type: KnowledgeSourceType | |
| description: Optional[str] = None | |
| metadata: Dict[str, Any] = Field(default_factory=dict) | |
| is_active: bool = True | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| updated_at: datetime = Field(default_factory=datetime.now) | |
| created_by: Optional[str] = None | |
| class KnowledgeSourceVersion(BaseModel): | |
| """Versioned payload for a knowledge source.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| source_id: UUID | |
| workspace_id: UUID | |
| version_label: str | |
| state: KnowledgeVersionState = KnowledgeVersionState.DRAFT | |
| content_hash: Optional[str] = None | |
| storage_ref: Optional[str] = None | |
| parsed_payload: Dict[str, Any] = Field(default_factory=dict) | |
| validation_report: Dict[str, Any] = Field(default_factory=dict) | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| updated_at: datetime = Field(default_factory=datetime.now) | |
| created_by: Optional[str] = None | |
| class KnowledgeSourceAssignment(BaseModel): | |
| """Scenario/subscenario assignment with required/optional policy.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: UUID | |
| source_id: UUID | |
| source_version_id: UUID | |
| scenario_id: UUID | |
| subscenario_id: Optional[UUID] = None | |
| required: bool = True | |
| weight: float = 1.0 | |
| effective_from: Optional[datetime] = None | |
| effective_to: Optional[datetime] = None | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| updated_at: datetime = Field(default_factory=datetime.now) | |
| created_by: Optional[str] = None | |
| class KnowledgeImportJob(BaseModel): | |
| """Import/validation job status.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: UUID | |
| source_id: Optional[UUID] = None | |
| filename: Optional[str] = None | |
| source_type: KnowledgeSourceType = KnowledgeSourceType.CSV | |
| status: str = "pending" | |
| row_count: int = 0 | |
| valid_count: int = 0 | |
| invalid_count: int = 0 | |
| validation_report: Dict[str, Any] = Field(default_factory=dict) | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| completed_at: Optional[datetime] = None | |
| created_by: Optional[str] = None | |
| class KnowledgeCrawlRun(BaseModel): | |
| """Persisted crawl/index lifecycle for a source version.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: UUID | |
| source_id: UUID | |
| source_version_id: UUID | |
| trigger: KnowledgeCrawlTrigger = KnowledgeCrawlTrigger.COMMIT | |
| status: KnowledgeCrawlStatus = KnowledgeCrawlStatus.QUEUED | |
| phase: KnowledgeCrawlPhase = KnowledgeCrawlPhase.HTTP_FETCH | |
| totals: Dict[str, Any] = Field( | |
| default_factory=lambda: { | |
| "total": 0, | |
| "success": 0, | |
| "failed": 0, | |
| "unreadable": 0, | |
| "indexed_pages": 0, | |
| "indexed_chunks": 0, | |
| } | |
| ) | |
| started_at: Optional[datetime] = None | |
| completed_at: Optional[datetime] = None | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| created_by: Optional[str] = None | |
| class KnowledgePage(BaseModel): | |
| """Fetched canonical page payload belonging to a source version.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: UUID | |
| source_id: UUID | |
| source_version_id: UUID | |
| url: str | |
| url_canonical: str | |
| title: str = "" | |
| content_type: str = "" | |
| http_status: Optional[int] = None | |
| fetch_status: str = "unknown" | |
| fetch_method: str = "http" | |
| content_hash: Optional[str] = None | |
| raw_chars: int = 0 | |
| cleaned_text: str = "" | |
| fetched_at: Optional[datetime] = None | |
| error: Optional[str] = None | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| updated_at: datetime = Field(default_factory=datetime.now) | |
| class KnowledgeChunk(BaseModel): | |
| """Searchable chunk extracted from a knowledge page.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| workspace_id: UUID | |
| page_id: UUID | |
| source_id: UUID | |
| source_version_id: UUID | |
| chunk_index: int = 0 | |
| chunk_text: str | |
| token_estimate: int = 0 | |
| chunk_hash: str | |
| embedding_json: Optional[List[float]] = None | |
| created_at: datetime = Field(default_factory=datetime.now) | |
| class SessionKnowledgeSnapshot(BaseModel): | |
| """Frozen reference of memory scope used by a session.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: UUID = Field(default_factory=uuid4) | |
| session_id: UUID | |
| workspace_id: UUID | |
| bot_id: UUID | |
| source_version_ids: List[str] = Field(default_factory=list) | |
| crawl_run_ids: List[str] = Field(default_factory=list) | |
| created_at: datetime = Field(default_factory=datetime.now) | |