lexrag / app /models.py
RV302001's picture
Initial commit: LexRAG hybrid legal RAG
cc0207f
Raw
History Blame Contribute Delete
1.31 kB
"""ORM model for the chunks table.
The `ts` column is a STORED generated tsvector (English config) derived from
`content`, so it stays in sync automatically and can back a GIN index.
"""
from __future__ import annotations
import uuid
from pgvector.sqlalchemy import Vector
from sqlalchemy import Computed, Integer, String, Text
from sqlalchemy.dialects.postgresql import TSVECTOR, UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.db import Base
EMBEDDING_DIM = 384 # BAAI/bge-small-en-v1.5
class Chunk(Base):
__tablename__ = "chunks"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
case_name: Mapped[str] = mapped_column(Text, nullable=False)
chunk_index: Mapped[int] = mapped_column(Integer, nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False)
embedding: Mapped[list[float]] = mapped_column(Vector(EMBEDDING_DIM), nullable=False)
# Stored generated tsvector — kept in sync with content by Postgres.
ts: Mapped[str] = mapped_column(
TSVECTOR,
Computed("to_tsvector('english', content)", persisted=True),
nullable=True,
)
def __repr__(self) -> str: # pragma: no cover
return f"<Chunk {self.case_name!r}#{self.chunk_index}>"