fix deploy bug
This commit is contained in:
@@ -37,20 +37,24 @@ class KnowledgePage(Base, TimestampMixin):
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
title: Mapped[str] = mapped_column(String(500), nullable=False, comment="页面标题")
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False, comment="页面内容")
|
||||
source: Mapped[str | None] = mapped_column(String(200), nullable=True, comment="来源文件名")
|
||||
source_file: Mapped[str | None] = mapped_column(String(500), nullable=True, comment="来源文件路径")
|
||||
page_number: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="原文件页码")
|
||||
# 向量嵌入(JSON 格式存储)
|
||||
course_name: Mapped[str | None] = mapped_column(String(200), nullable=True, comment="课程名称")
|
||||
teacher_name: Mapped[str | None] = mapped_column(String(100), nullable=True, comment="讲师名称")
|
||||
live_date: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="直播日期")
|
||||
metadata_json: Mapped[str | None] = mapped_column(Text, nullable=True, comment="额外元数据(JSON)")
|
||||
embedding: Mapped[str | None] = mapped_column(Text, nullable=True, comment="向量嵌入(JSON)")
|
||||
|
||||
@property
|
||||
def embedding_vector(self) -> list[float] | None:
|
||||
if self.embedding is None:
|
||||
return None
|
||||
return json.loads(self.embedding)
|
||||
|
||||
@embedding_vector.setter
|
||||
def embedding_vector(self, value: list[float] | None):
|
||||
self.embedding = json.dumps(value) if value else None
|
||||
class KnowledgeChunk(Base, TimestampMixin):
|
||||
"""知识页面分块"""
|
||||
__tablename__ = "knowledge_chunks"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
page_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="关联页面 ID")
|
||||
chunk_index: Mapped[int] = mapped_column(Integer, nullable=False, comment="分块索引")
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False, comment="分块内容")
|
||||
embedding: Mapped[str | None] = mapped_column(Text, nullable=True, comment="向量嵌入(JSON)")
|
||||
|
||||
|
||||
class OCRImage(Base, TimestampMixin):
|
||||
|
||||
@@ -181,22 +181,15 @@ class PageService:
|
||||
if embedding:
|
||||
embedding_str = "[" + ",".join(str(x) for x in embedding) + "]"
|
||||
|
||||
await self.db.execute(text("""
|
||||
INSERT INTO knowledge_chunks (page_id, chunk_index, content, embedding)
|
||||
VALUES (:page_id, :chunk_index, :content, :embedding::vector)
|
||||
"""), {
|
||||
await self.db.execute(text(
|
||||
"INSERT INTO knowledge_chunks (page_id, chunk_index, content, embedding) "
|
||||
"VALUES (:page_id, :chunk_index, :content, :embedding)"
|
||||
), {
|
||||
"page_id": page_id,
|
||||
"chunk_index": idx,
|
||||
"content": chunk_text,
|
||||
"embedding": embedding_str,
|
||||
})
|
||||
|
||||
# 更新全文搜索向量
|
||||
await self.db.execute(text("""
|
||||
UPDATE knowledge_chunks
|
||||
SET search_vector = to_tsvector('chinese_zh', content)
|
||||
WHERE page_id = :page_id
|
||||
"""), {"page_id": page_id})
|
||||
|
||||
await self.db.flush()
|
||||
return {"page_id": page_id, "chunks": len(chunks)}
|
||||
|
||||
Reference in New Issue
Block a user