HNU-Final / knowledge /models.py
Mokhtar
feat(production): complete phase 1-4 refactor with hybrid retrieval CAG architecture, E5 dense embeddings, grounding validator, precomputed cold-start index, and 24/24 passing system tests
d54db1a
Raw History Blame Contribute Delete
1.4 kB
# knowledge/models.py — Core data structures for knowledge management
import hashlib
from pydantic import BaseModel, Field
class DocumentMetadata(BaseModel):
doc_id: str
filename: str
path: str
title: str = "Untitled Document"
section: str = ""
subsection: str = ""
language: str = "ar"
source_type: str = "markdown"
class Document(BaseModel):
metadata: DocumentMetadata
content: str
class Chunk(BaseModel):
chunk_id: str
doc_id: str
filename: str
path: str
title: str
section: str = ""
subsection: str = ""
text: str
language: str = "ar"
source_type: str = "markdown"
token_count: int = 0
heading_context: str = ""
chunk_index: int = 0
def formatted_for_context(self) -> str:
"""Format chunk with source metadata for LLM context payload."""
header_parts = []
if self.filename:
header_parts.append(f"المستند: {self.filename}")
if self.title and self.title != self.filename:
header_parts.append(f"العنوان: {self.title}")
if self.section:
header_parts.append(f"القسم: {self.section}")
if self.subsection:
header_parts.append(f"الفرع: {self.subsection}")
meta_header = " | ".join(header_parts)
return f"--- [{meta_header}] ---\n{self.text.strip()}\n"