File size: 10,452 Bytes
42a0d15 4b8e879 42a0d15 37b5223 42a0d15 37b5223 42a0d15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | """
Ukweli — SQLAlchemy ORM Models
Defines the relational schema for PostgreSQL per Architecture Section 3.3.4.
"""
from __future__ import annotations
import uuid
from datetime import datetime
from sqlalchemy import (
Boolean,
DateTime,
Enum,
Float,
ForeignKey,
Index,
Integer,
String,
Text,
func,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
"""Declarative base for all ORM models."""
pass
class Document(Base):
"""
Catalog of all ingested documents.
Maps to the Metadata Registry described in Section 3.3.4.
"""
__tablename__ = "documents"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
title: Mapped[str] = mapped_column(String(1024), nullable=False)
source_url: Mapped[str | None] = mapped_column(String(2048), nullable=True)
pdf_url: Mapped[str | None] = mapped_column(String(2048), nullable=True)
fiscal_year: Mapped[str | None] = mapped_column(String(20), nullable=True, index=True)
auditee: Mapped[str | None] = mapped_column(String(512), nullable=True, index=True)
report_type: Mapped[str] = mapped_column(
Enum(
"financial_audit",
"performance_audit",
"special_audit",
"public_debt_audit",
"county_audit",
"summary_report",
"strategic_document",
"annual_corporate_report",
name="report_type_enum",
),
nullable=False,
default="financial_audit",
)
page_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
published_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
checksum_sha256: Mapped[str | None] = mapped_column(String(64), nullable=True, unique=True)
status: Mapped[str] = mapped_column(
Enum(
"pending",
"downloading",
"parsing",
"parsed",
"embedding",
"ready",
"failed",
name="document_status_enum",
),
nullable=False,
default="pending",
index=True,
)
raw_storage_path: Mapped[str | None] = mapped_column(String(1024), nullable=True)
language: Mapped[str] = mapped_column(String(5), nullable=False, default="en")
file_size_bytes: Mapped[int | None] = mapped_column(Integer, nullable=True)
error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
)
# Relationships
chunks: Mapped[list[Chunk]] = relationship("Chunk", back_populates="document", cascade="all, delete-orphan")
ingestion_logs: Mapped[list[IngestionLog]] = relationship(
"IngestionLog", back_populates="document", cascade="all, delete-orphan"
)
__table_args__ = (
Index("ix_documents_auditee_fy", "auditee", "fiscal_year"),
)
class Chunk(Base):
"""
Individual content chunks derived from parsed documents.
Schema matches Section 3.2.4 Chunk Metadata Schema.
"""
__tablename__ = "chunks"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
document_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("documents.id", ondelete="CASCADE"), nullable=False, index=True
)
chunk_type: Mapped[str] = mapped_column(
Enum(
"narrative",
"table",
"finding",
"recommendation",
"legal_reference",
name="chunk_type_enum",
),
nullable=False,
)
content: Mapped[str] = mapped_column(Text, nullable=False)
page_range_start: Mapped[int | None] = mapped_column(Integer, nullable=True)
page_range_end: Mapped[int | None] = mapped_column(Integer, nullable=True)
section_heading: Mapped[str | None] = mapped_column(String(512), nullable=True)
entities: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
language: Mapped[str] = mapped_column(String(5), nullable=False, default="en")
audit_period: Mapped[str | None] = mapped_column(String(20), nullable=True)
auditee: Mapped[str | None] = mapped_column(String(512), nullable=True)
finding_category: Mapped[str | None] = mapped_column(String(64), nullable=True)
embedding_vector_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
token_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
chunk_index: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
# Relationships
document: Mapped[Document] = relationship("Document", back_populates="chunks")
__table_args__ = (
Index("ix_chunks_document_index", "document_id", "chunk_index"),
Index("ix_chunks_auditee_period", "auditee", "audit_period"),
# Index(
# "ix_chunks_content_fts",
# func.to_tsvector("english", "content"),
# postgresql_using="gin",
# ),
)
class IngestionLog(Base):
"""
Audit trail for every document processing step.
Provides transparency per Section 3.3.4.
"""
__tablename__ = "ingestion_logs"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
document_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("documents.id", ondelete="CASCADE"), nullable=False, index=True
)
stage: Mapped[str] = mapped_column(
String(64), nullable=False
) # e.g. "download", "parse", "chunk", "embed"
status: Mapped[str] = mapped_column(String(32), nullable=False) # "started", "completed", "failed"
started_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
worker_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
metadata_extra: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
# Relationships
document: Mapped[Document] = relationship("Document", back_populates="ingestion_logs")
class QueryLog(Base):
"""
Immutable audit trail of every RAG query.
Required by Section 7.2 — transparency logging (WORM-style).
"""
__tablename__ = "query_logs"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
query_text: Mapped[str] = mapped_column(Text, nullable=False)
language: Mapped[str] = mapped_column(String(5), nullable=False, default="en")
mode: Mapped[str] = mapped_column(String(20), nullable=False, default="concise")
user_tier: Mapped[str] = mapped_column(String(20), nullable=False, default="public")
user_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("users.id"), nullable=True, index=True
)
api_key_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("api_keys.id"), nullable=True
)
fingerprint: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
filters: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
answer: Mapped[str | None] = mapped_column(Text, nullable=True)
citations: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
chunks_considered: Mapped[int | None] = mapped_column(Integer, nullable=True)
latency_ms: Mapped[int | None] = mapped_column(Integer, nullable=True)
confidence: Mapped[str | None] = mapped_column(String(32), nullable=True)
llm_model_used: Mapped[str | None] = mapped_column(String(128), nullable=True)
llm_tokens_used: Mapped[int | None] = mapped_column(Integer, nullable=True)
conversation_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
# Relationships
feedbacks: Mapped[list[Feedback]] = relationship("Feedback", back_populates="query_log")
class Feedback(Base):
"""
User feedback and corrections on RAG responses.
Maps to Section 4.2 POST /feedback.
"""
__tablename__ = "feedbacks"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
query_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("query_logs.id", ondelete="CASCADE"), nullable=False, index=True
)
rating: Mapped[int] = mapped_column(Integer, nullable=False) # -1, 0, 1
correction: Mapped[str | None] = mapped_column(Text, nullable=True)
suggested_citation: Mapped[str | None] = mapped_column(Text, nullable=True)
review_status: Mapped[str] = mapped_column(
String(20), nullable=False, default="pending"
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
# Relationships
query_log: Mapped[QueryLog] = relationship("QueryLog", back_populates="feedbacks")
class PageHash(Base):
"""
Stores content hashes of crawled pages for change detection.
Used by the change_detection service.
"""
__tablename__ = "page_hashes"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
url_path: Mapped[str] = mapped_column(String(2048), nullable=False, unique=True, index=True)
content_hash: Mapped[str] = mapped_column(String(64), nullable=False)
last_checked_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
changed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|