""" 知识库分段模型 """ from sqlalchemy import Column, String, Text, Integer, Boolean, JSON, Index from app.base_model import BaseModel class KnowledgeSegment(BaseModel): """ 知识库分段(Chunk) 文档经过分块后的最小检索单元 向量数据存储在 Qdrant 向量数据库中,此表只存业务数据 """ __tablename__ = "ai_knowledge_segment" knowledge_base_id = Column(String(21), nullable=False, index=True, comment="所属知识库ID(逻辑外键关联ai_knowledge_base)") document_id = Column(String(21), nullable=False, index=True, comment="所属文档ID(逻辑外键关联ai_knowledge_document)") # 内容 position = Column(Integer, default=0, comment="在文档中的位置序号") content = Column(Text, nullable=False, comment="分段文本内容") answer = Column(Text, nullable=True, comment="Q&A 模式的答案内容") token_count = Column(Integer, default=0, comment="Token 数") char_count = Column(Integer, default=0, comment="字符数") word_count = Column(Integer, default=0, comment="词数") # 元数据 page_number = Column(Integer, nullable=True, comment="来源页码(PDF/PPT)") keywords = Column(JSON, nullable=True, comment="关键词列表(用于全文检索增强)") extra_metadata = Column(JSON, nullable=True, comment="元数据(标题/来源等)") # 向量化状态(向量数据存在 Qdrant 中,这里只记录状态) embedding_status = Column(String(20), default="pending", comment="向量化状态: pending/completed/failed") # 父子分段(Small-to-Big) parent_segment_id = Column(String(21), nullable=True, index=True, comment="父分段ID(逻辑外键,用于 Small-to-Big 检索)") # 状态 enabled = Column(Boolean, default=True, comment="是否启用(禁用后不参与检索)") hit_count = Column(Integer, default=0, comment="命中次数") __table_args__ = ( Index('idx_segment_kb_doc', 'knowledge_base_id', 'document_id'), Index('idx_segment_kb_enabled', 'knowledge_base_id', 'enabled'), )