DocWeave / backend /app /models /document_chunk.py
shak3008's picture
Add document chunk embeddings to workflow
93def79
Raw
History Blame Contribute Delete
1.89 kB
from __future__ import annotations
import uuid
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Integer, Text, func
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database.database import Base
from pgvector.sqlalchemy import Vector
class DocumentChunk(Base):
"""
A retrievable chunk of a specific document version.
Chunks preserve lightweight provenance so retrieval results
can be traced back to their original document location.
"""
__tablename__ = "document_chunks"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4,
)
document_version_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey(
"document_versions.id",
ondelete="CASCADE",
),
nullable=False,
index=True,
)
chunk_index: Mapped[int] = mapped_column(
Integer,
nullable=False,
)
text: Mapped[str] = mapped_column(
Text,
nullable=False,
)
page_number: Mapped[int | None] = mapped_column(
Integer,
nullable=True,
)
section: Mapped[str | None] = mapped_column(
Text,
nullable=True,
)
embedding: Mapped[list[float] | None] = mapped_column(
Vector(384),
nullable=True,
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
)
document_version: Mapped["DocumentVersion"] = relationship(
back_populates="chunks",
)
def __repr__(self) -> str:
return (
f"<DocumentChunk("
f"document_version={self.document_version_id}, "
f"index={self.chunk_index})>"
)