Download app/db/models.py from Prabu17/askmydocs: direct link, hf CLI and curl.
- Browser
- Download file 5.73 kB
-
https://huggingface.co/spaces/Prabu17/askmydocs/resolve/main/app/db/models.py
- Command line
-
hf download hf://spaces/Prabu17/askmydocs/app/db/models.py
-
curl -L -o models.py https://huggingface.co/spaces/Prabu17/askmydocs/resolve/main/app/db/models.py
5.73 kB
| """SQLAlchemy ORM models for AskMyDocs application metadata. | |
| PostgreSQL stores metadata only — documents, chunks (metadata, not | |
| embeddings), keyword-search postings (tsvector), ingestion jobs, queries, | |
| and evaluation runs. Vector embeddings live in Qdrant. | |
| """ | |
| from __future__ import annotations | |
| import uuid | |
| from datetime import datetime | |
| from sqlalchemy import ( | |
| JSON, | |
| Boolean, | |
| DateTime, | |
| Float, | |
| ForeignKey, | |
| Integer, | |
| String, | |
| Text, | |
| func, | |
| ) | |
| from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship | |
| def gen_uuid() -> str: | |
| return str(uuid.uuid4()) | |
| class Base(DeclarativeBase): | |
| """Declarative base for all ORM models.""" | |
| class Document(Base): | |
| __tablename__ = "documents" | |
| id: Mapped[str] = mapped_column(String(36), primary_key=True, default=gen_uuid) | |
| document_id: Mapped[str] = mapped_column(String(128), unique=True, index=True) | |
| title: Mapped[str] = mapped_column(String(512)) | |
| module: Mapped[str] = mapped_column(String(128)) | |
| source: Mapped[str] = mapped_column(String(512)) | |
| format: Mapped[str] = mapped_column(String(16)) | |
| file_size_bytes: Mapped[int | None] = mapped_column(Integer, nullable=True) | |
| created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) | |
| updated_at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), server_default=func.now(), onupdate=func.now() | |
| ) | |
| chunks: Mapped[list[Chunk]] = relationship( | |
| back_populates="document", cascade="all, delete-orphan" | |
| ) | |
| ingestion_jobs: Mapped[list[IngestionJob]] = relationship( | |
| back_populates="document", cascade="all, delete-orphan" | |
| ) | |
| class Chunk(Base): | |
| __tablename__ = "chunks" | |
| id: Mapped[str] = mapped_column(String(64), primary_key=True) # {doc_id}:{index} | |
| document_id: Mapped[str] = mapped_column( | |
| ForeignKey("documents.document_id"), index=True | |
| ) | |
| chunk_index: Mapped[int] = mapped_column(Integer) | |
| page_number: Mapped[int | None] = mapped_column(Integer, nullable=True) | |
| section: Mapped[str | None] = mapped_column(String(512), nullable=True) | |
| text: Mapped[str] = mapped_column(Text) | |
| source: Mapped[str] = mapped_column(String(512)) | |
| content_hash: Mapped[str] = mapped_column(String(64), index=True) | |
| created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) | |
| document: Mapped[Document] = relationship(back_populates="chunks") | |
| class IngestionJob(Base): | |
| __tablename__ = "ingestion_jobs" | |
| id: Mapped[str] = mapped_column(String(36), primary_key=True, default=gen_uuid) | |
| document_id: Mapped[str] = mapped_column(ForeignKey("documents.document_id"), index=True) | |
| status: Mapped[str] = mapped_column(String(32), default="pending") # queued/running/success/failed | |
| error: Mapped[str | None] = mapped_column(Text, nullable=True) | |
| chunk_count: Mapped[int | None] = mapped_column(Integer, nullable=True) | |
| started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) | |
| finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) | |
| created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) | |
| document: Mapped[Document] = relationship(back_populates="ingestion_jobs") | |
| class QueryRecord(Base): | |
| __tablename__ = "queries" | |
| id: Mapped[str] = mapped_column(String(36), primary_key=True, default=gen_uuid) | |
| query_id: Mapped[str] = mapped_column(String(64), index=True) | |
| question: Mapped[str] = mapped_column(Text) | |
| answer: Mapped[str | None] = mapped_column(Text, nullable=True) | |
| grounded: Mapped[bool | None] = mapped_column(Boolean, nullable=True) | |
| confidence: Mapped[float | None] = mapped_column(Float, nullable=True) | |
| total_latency_ms: Mapped[float | None] = mapped_column(Float, nullable=True) | |
| retrieved_chunk_ids: Mapped[list] = mapped_column(JSON, default=list) | |
| citation_ids: Mapped[list] = mapped_column(JSON, default=list) | |
| created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) | |
| class EvaluationRun(Base): | |
| __tablename__ = "evaluation_runs" | |
| id: Mapped[str] = mapped_column(String(36), primary_key=True, default=gen_uuid) | |
| run_id: Mapped[str] = mapped_column(String(64), unique=True, index=True) | |
| experiment: Mapped[str] = mapped_column(String(32), default="final") # A/B/C/D label | |
| config: Mapped[dict] = mapped_column(JSON, default=dict) | |
| metrics: Mapped[dict] = mapped_column(JSON, default=dict) | |
| passed: Mapped[bool | None] = mapped_column(Boolean, nullable=True) | |
| created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) | |
| class KeywordPosting(Base): | |
| """Keyword-search posting for one chunk (Postgres tsvector BM25). | |
| One row per chunk, ``tsv`` maintained by a trigger (``text`` weight A, | |
| ``section`` weight B). Ranked with ``ts_rank_cd``. Kept in its own | |
| table — not a column on ``chunks`` — so posting lifecycle | |
| (index/delete/count) stays independent of chunk rows. | |
| """ | |
| __tablename__ = "keyword_postings" | |
| chunk_id: Mapped[str] = mapped_column(String(64), primary_key=True) # {doc_id}:{index} | |
| document_id: Mapped[str] = mapped_column(String(128), index=True) | |
| text: Mapped[str] = mapped_column(Text) | |
| source: Mapped[str] = mapped_column(String(512)) | |
| page_number: Mapped[int | None] = mapped_column(Integer, nullable=True) | |
| section: Mapped[str | None] = mapped_column(String(512), nullable=True) |