Spaces:
Sleeping
Sleeping
Upload app/models/schemas.py with huggingface_hub
Browse files- app/models/schemas.py +112 -0
app/models/schemas.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Pydantic models for API request/response schemas.
|
| 3 |
+
"""
|
| 4 |
+
from pydantic import BaseModel, Field
|
| 5 |
+
from typing import List, Optional, Dict, Any
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from enum import Enum
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class DocumentStatus(str, Enum):
|
| 11 |
+
PENDING = "pending"
|
| 12 |
+
PROCESSING = "processing"
|
| 13 |
+
COMPLETED = "completed"
|
| 14 |
+
FAILED = "failed"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class ChunkMetadata(BaseModel):
|
| 18 |
+
"""Metadata for a document chunk."""
|
| 19 |
+
tenant_id: str # CRITICAL: Multi-tenant isolation
|
| 20 |
+
kb_id: str
|
| 21 |
+
user_id: str
|
| 22 |
+
file_name: str
|
| 23 |
+
file_type: str
|
| 24 |
+
chunk_id: str
|
| 25 |
+
chunk_index: int
|
| 26 |
+
page_number: Optional[int] = None
|
| 27 |
+
total_chunks: int
|
| 28 |
+
document_id: Optional[str] = None # Track original document
|
| 29 |
+
created_at: datetime = Field(default_factory=datetime.utcnow)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class DocumentChunk(BaseModel):
|
| 33 |
+
"""A chunk of text with metadata."""
|
| 34 |
+
id: str
|
| 35 |
+
content: str
|
| 36 |
+
metadata: ChunkMetadata
|
| 37 |
+
embedding: Optional[List[float]] = None
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class UploadRequest(BaseModel):
|
| 41 |
+
"""Request model for file upload."""
|
| 42 |
+
tenant_id: str # CRITICAL: Multi-tenant isolation
|
| 43 |
+
user_id: str
|
| 44 |
+
kb_id: str
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class UploadResponse(BaseModel):
|
| 48 |
+
"""Response model for file upload."""
|
| 49 |
+
success: bool
|
| 50 |
+
message: str
|
| 51 |
+
document_id: Optional[str] = None
|
| 52 |
+
file_name: str
|
| 53 |
+
chunks_created: int = 0
|
| 54 |
+
status: DocumentStatus = DocumentStatus.PENDING
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class Citation(BaseModel):
|
| 58 |
+
"""Citation reference for an answer."""
|
| 59 |
+
file_name: str
|
| 60 |
+
chunk_id: str
|
| 61 |
+
page_number: Optional[int] = None
|
| 62 |
+
relevance_score: float
|
| 63 |
+
excerpt: str # Short excerpt from the chunk
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class ChatRequest(BaseModel):
|
| 67 |
+
"""Request model for chat endpoint."""
|
| 68 |
+
tenant_id: str # CRITICAL: Multi-tenant isolation
|
| 69 |
+
user_id: str
|
| 70 |
+
kb_id: str
|
| 71 |
+
conversation_id: Optional[str] = None
|
| 72 |
+
question: str
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class ChatResponse(BaseModel):
|
| 76 |
+
"""Response model for chat endpoint."""
|
| 77 |
+
success: bool
|
| 78 |
+
answer: str
|
| 79 |
+
citations: List[Citation] = []
|
| 80 |
+
confidence: float # 0-1 score
|
| 81 |
+
from_knowledge_base: bool = True
|
| 82 |
+
escalation_suggested: bool = False
|
| 83 |
+
conversation_id: str
|
| 84 |
+
metadata: Dict[str, Any] = {}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class RetrievalResult(BaseModel):
|
| 88 |
+
"""Result from vector store retrieval."""
|
| 89 |
+
chunk_id: str
|
| 90 |
+
content: str
|
| 91 |
+
metadata: Dict[str, Any]
|
| 92 |
+
similarity_score: float
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class KnowledgeBaseStats(BaseModel):
|
| 96 |
+
"""Statistics for a knowledge base."""
|
| 97 |
+
tenant_id: str # CRITICAL: Multi-tenant isolation
|
| 98 |
+
kb_id: str
|
| 99 |
+
user_id: str
|
| 100 |
+
total_documents: int
|
| 101 |
+
total_chunks: int
|
| 102 |
+
file_names: List[str]
|
| 103 |
+
last_updated: Optional[datetime] = None
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class HealthResponse(BaseModel):
|
| 107 |
+
"""Health check response."""
|
| 108 |
+
status: str
|
| 109 |
+
version: str = "1.0.0"
|
| 110 |
+
vector_db_connected: bool
|
| 111 |
+
llm_configured: bool
|
| 112 |
+
|