Files

114 lines
5.1 KiB
Python

"""
backend/app/models/content.py
用途:多语言、常用问题、热点 FAQ 候选、公告等内容模型。
依赖:models.base.Base。
"""
from __future__ import annotations
from datetime import datetime
from typing import Optional
from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Index, JSON, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
class ZlLanguage(Base):
"""
多语言配置表
用途:存储系统支持的语言列表
"""
__tablename__ = 'zl_languages'
id: Mapped[int] = mapped_column(primary_key=True)
code: Mapped[str] = mapped_column(String(10), unique=True)
name: Mapped[str] = mapped_column(String(50))
is_active: Mapped[bool] = mapped_column(default=True)
class QuickQuestion(Base):
"""
常用问题表
用途:存储全局/用户常用问题;全局条目(user_id 为空)可配置标准答案并启用 FAQ 缓存命中。
is_enabled 控制工作台首页快捷入口;cache_enabled 控制聊天是否跳过 Dify 直答,二者独立。
fuzzy_enabled / fuzzy_threshold 控制精确未命中后的模糊兜底直答(逐条目独立,阈值钳制在 0.5~1.0)。
"""
__tablename__ = 'quick_questions'
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'))
title: Mapped[str] = mapped_column(String(100))
question: Mapped[str] = mapped_column(Text)
answer: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
aliases: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
cache_enabled: Mapped[bool] = mapped_column(default=False)
is_enabled: Mapped[bool] = mapped_column(default=True)
fuzzy_enabled: Mapped[bool] = mapped_column(default=False)
fuzzy_threshold: Mapped[float] = mapped_column(default=0.80)
hit_count: Mapped[int] = mapped_column(default=0)
retriever_resources: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
source_kb_document_ids: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
stale_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
stale_reason: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
stale_kb_document_id: Mapped[Optional[int]] = mapped_column(ForeignKey('kb_documents.id'), nullable=True)
sort_order: Mapped[int] = mapped_column(default=0)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
class FaqHotCandidate(Base):
"""
热点 FAQ 候选表
用途:离线聚合 ChatLog 后的待审核热点问句与推荐答案(PostgreSQL 存储,不含在线命中)。
"""
__tablename__ = 'faq_hot_candidates'
__table_args__ = (
UniqueConstraint('agent_id', 'q_norm', name='uq_faq_hot_candidates_agent_qnorm'),
Index('ix_faq_hot_candidates_status_expires', 'status', 'expires_at'),
)
id: Mapped[int] = mapped_column(primary_key=True)
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'), index=True)
q_norm: Mapped[str] = mapped_column(String(255))
canonical_query: Mapped[str] = mapped_column(Text)
answer: Mapped[str] = mapped_column(Text)
source_chat_log_id: Mapped[Optional[int]] = mapped_column(ForeignKey('chat_logs.id'), nullable=True)
ask_count_7d: Mapped[int] = mapped_column(default=0)
user_count_7d: Mapped[int] = mapped_column(default=0)
avg_tokens: Mapped[int] = mapped_column(default=0)
avg_latency_ms: Mapped[int] = mapped_column(default=0)
answer_dominance: Mapped[float] = mapped_column(default=0.0)
sample_queries: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
retriever_resources: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
last_asked_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
status: Mapped[str] = mapped_column(String(20), default='candidate')
promoted_quick_question_id: Mapped[Optional[int]] = mapped_column(ForeignKey('quick_questions.id'), nullable=True)
deny_until: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
class Announcement(Base):
"""
公告表
用途:存储系统公告
"""
__tablename__ = 'announcements'
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
content: Mapped[str] = mapped_column(Text)
is_active: Mapped[bool] = mapped_column(default=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)