b8253b86a0
- Add global FastAPI exception handler for unhandled errors - Add get_current_user_obj() dependency for cleaner auth patterns - Switch VLM image description from serial to asyncio.gather concurrency - Extract score conversion to shared score_utils module - Docker: use env vars for passwords, remove hardcoded API key default - Add .gitattributes and update .gitignore for tar.gz and tsbuildinfo Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
"""
|
|
文档数据模型
|
|
"""
|
|
from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey, Boolean, Float
|
|
from sqlalchemy.sql import func
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from ..core.database import Base
|
|
|
|
|
|
class Document(Base):
|
|
"""文档模型"""
|
|
__tablename__ = "documents"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
user_id = Column(Integer, ForeignKey("users.id"), nullable=True) # None表示系统文档
|
|
knowledge_base_id = Column(Integer, ForeignKey("knowledge_bases.id"), nullable=True, index=True)
|
|
filename = Column(String(255), nullable=False)
|
|
original_filename = Column(String(255), nullable=False)
|
|
file_path = Column(String(500), nullable=False)
|
|
file_size = Column(Integer, nullable=False)
|
|
file_type = Column(String(50), nullable=False)
|
|
title = Column(String(200), nullable=True)
|
|
description = Column(Text, nullable=True)
|
|
is_processed = Column(Boolean, default=False)
|
|
is_public = Column(Boolean, default=False)
|
|
|
|
# 新增字段:知识库管理
|
|
source_type = Column(String(50), default="upload") # upload, knowledge_base
|
|
last_modified = Column(DateTime(timezone=True), nullable=True) # 文件最后修改时间
|
|
file_hash = Column(String(64), nullable=True) # 文件内容哈希
|
|
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
# 关联关系
|
|
user = relationship("User", back_populates="documents")
|
|
knowledge_base = relationship("KnowledgeBase", back_populates="documents")
|
|
chunks = relationship("DocumentChunk", back_populates="document", cascade="all, delete-orphan")
|
|
|
|
def __repr__(self):
|
|
return f"<Document(id={self.id}, filename='{self.filename}', title='{self.title}')>"
|
|
|
|
|
|
class DocumentChunk(Base):
|
|
"""文档分块模型"""
|
|
__tablename__ = "document_chunks"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
document_id = Column(Integer, ForeignKey("documents.id"), nullable=False, index=True)
|
|
chunk_index = Column(Integer, nullable=False)
|
|
content = Column(Text, nullable=False)
|
|
content_hash = Column(String(64), nullable=False) # 内容哈希
|
|
embedding_vector = Column(Text, nullable=True) # 向量嵌入(JSON格式)
|
|
chunk_metadata = Column(Text, nullable=True) # 分块元数据(JSON格式)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# 关联关系
|
|
document = relationship("Document", back_populates="chunks")
|
|
|
|
def __repr__(self):
|
|
return f"<DocumentChunk(id={self.id}, document_id={self.document_id}, chunk_index={self.chunk_index})>"
|