thomasm291678
VetCopilot-LCPS: LCPS 2.0 本地推理引擎替换 DeepSeek API
793e953
Raw History Blame Contribute Delete
2.44 kB
"""
知识库 ORM 模型
对应数据库表 knowledge_documents 和 knowledge_chunks。
"""
import uuid
from sqlalchemy import Column, String, Integer, Text, DateTime, ForeignKey, func
from app.database import Base
class KnowledgeDocument(Base):
"""知识库文档表"""
__tablename__ = "knowledge_documents"
id = Column(
String(36),
primary_key=True,
default=lambda: str(uuid.uuid4()),
comment="文档唯一标识(UUID)",
)
filename = Column(String(255), nullable=False, comment="原始文件名")
title = Column(String(500), default="", comment="文档标题")
author = Column(String(255), default="", comment="作者")
total_chunks = Column(Integer, default=0, comment="分块总数")
file_size = Column(Integer, default=0, comment="文件大小(字节)")
file_hash = Column(String(64), unique=True, nullable=False, comment="SHA256 哈希")
status = Column(
String(20), default="pending",
comment="处理状态: pending/processing/ready/error"
)
error_message = Column(Text, default="", comment="错误信息")
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
updated_at = Column(
DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间"
)
def __repr__(self):
return f"<KnowledgeDocument(id={self.id}, filename={self.filename}, status={self.status})>"
class KnowledgeChunk(Base):
"""知识库文本分块表"""
__tablename__ = "knowledge_chunks"
id = Column(
String(36),
primary_key=True,
default=lambda: str(uuid.uuid4()),
comment="分块唯一标识(UUID)",
)
document_id = Column(
String(36),
ForeignKey("knowledge_documents.id", ondelete="CASCADE"),
nullable=False,
comment="所属文档 ID",
)
chunk_index = Column(Integer, nullable=False, comment="分块序号(从 0 开始)")
content = Column(Text, nullable=False, comment="分块文本内容")
token_count = Column(Integer, default=0, comment="Token 数量(估算)")
embedding_id = Column(Integer, nullable=True, comment="FAISS 索引中的 ID")
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
def __repr__(self):
return f"<KnowledgeChunk(id={self.id}, doc_id={self.document_id}, index={self.chunk_index})>"