from sqlalchemy import Column, Text, ForeignKey, Integer, Index from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import relationship from pgvector.sqlalchemy import Vector from core.database import Base from core.config import get_settings from models.base import PrimaryKeyMixin, TimestampMixin settings = get_settings() class KBArticleChunk(Base, PrimaryKeyMixin, TimestampMixin): __tablename__ = "kb_article_chunks" article_id = Column(UUID(as_uuid=True), ForeignKey("kb_articles.id", ondelete="CASCADE"), nullable=False, index=True) content = Column(Text, nullable=False) chunk_index = Column(Integer, nullable=False) # Vector embedding for semantic search at the chunk level embedding = Column(Vector(settings.AI_TEXT_EMBEDDING_DIM), nullable=True) __table_args__ = ( Index( 'idx_kb_chunk_embedding_hnsw', "embedding", postgresql_using='hnsw', postgresql_with={'m': 16, 'ef_construction': 64}, postgresql_ops={'embedding': 'vector_cosine_ops'} ), ) # Relationships article = relationship("KBArticle", back_populates="chunks")