Enhance knowledge base functionality and integrate S3 storage support

- Add new models for `KnowledgeDocument` and `KnowledgeChunk` to manage document ingestion and chunking.
- Implement S3-compatible storage integration for knowledge documents, allowing for file uploads and retrieval.
- Introduce API endpoints for managing knowledge bases and documents, including creation, deletion, and searching.
- Update frontend components to support knowledge base configuration and document management, improving user interaction.
- Enhance backend services for knowledge processing and retrieval, ensuring robust handling of document statuses and errors.
This commit is contained in:
Xin Wang
2026-07-12 13:58:47 +08:00
parent 01c563a3e7
commit de58f30014
21 changed files with 995 additions and 34 deletions

View File

@@ -22,6 +22,7 @@ from sqlalchemy import (
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from pgvector.sqlalchemy import Vector
class Base(DeclarativeBase):
@@ -93,6 +94,49 @@ class KnowledgeBase(Base):
)
class KnowledgeDocument(Base):
"""A file or pasted text ingested into one knowledge base."""
__tablename__ = "knowledge_documents"
id: Mapped[str] = mapped_column(String(40), primary_key=True)
knowledge_base_id: Mapped[str] = mapped_column(
String(40), ForeignKey("knowledge_bases.id", ondelete="CASCADE"), index=True
)
name: Mapped[str] = mapped_column(String(255))
source_type: Mapped[str] = mapped_column(String(16)) # file|text
mime_type: Mapped[str] = mapped_column(String(128), default="text/plain")
storage_key: Mapped[str | None] = mapped_column(String(1024), nullable=True)
size_bytes: Mapped[int] = mapped_column(Integer, default=0)
status: Mapped[str] = mapped_column(String(16), default="processing")
error_message: Mapped[str] = mapped_column(String(2048), default="")
chunk_count: Mapped[int] = mapped_column(Integer, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
class KnowledgeChunk(Base):
"""Searchable text chunk; Vector has no fixed dimension to support configured models."""
__tablename__ = "knowledge_chunks"
__table_args__ = (
UniqueConstraint("document_id", "chunk_index", name="uq_knowledge_chunk_position"),
)
id: Mapped[str] = mapped_column(String(40), primary_key=True)
knowledge_base_id: Mapped[str] = mapped_column(
String(40), ForeignKey("knowledge_bases.id", ondelete="CASCADE"), index=True
)
document_id: Mapped[str] = mapped_column(
String(40), ForeignKey("knowledge_documents.id", ondelete="CASCADE"), index=True
)
chunk_index: Mapped[int] = mapped_column(Integer)
content: Mapped[str] = mapped_column(Text)
embedding: Mapped[list[float]] = mapped_column(Vector())
class Assistant(Base):
"""助手(单表,无版本化)。type 为可变普通列,5 种类型共用此表。