Files
diary-news/backend/app/models/llm_setting.py
xiaji bc36a1fc38 feat(commentary): 双 provider 评论 — Angel(Agnes) + 美团大模型(LongCat)
- 新增 articles.commentary_meituan{_status,_model,_error} 4 列 + commentary_engine
- LlmSetting 加 meituan_api_key/base_url/chat_model/interval_sec/enabled/commentary_prompt
- 新 app/services/llm/providers.py 工厂,支持多 provider 客户端
- enrichment 流程改为 commentary_angel + commentary_meituan 并行(asyncio.gather),
  任一 provider 失败不影响另一个
- enrichment_loop 状态判定:任一 provider 状态不是 ok 都视为待 enrich
- alembic 0004_dual_commentary 迁移
- 前端 Feed 卡片 + ArticleDetail 详情页各加一条'美团评论'卡
- AdminLlmSettings 加美团 provider 配置卡(独立 api_key 编辑器,不回显明文)
- LlmSettingOut.meituan_api_key_set (bool) 替代直接回传 key
- 默认 URL https://api.longcat.chat/openai/v1 / 默认模型 LongCat-2.0-Preview
2026-06-12 19:00:00 +08:00

73 lines
2.8 KiB
Python

"""LLM 设置(单行,owner 可编辑)。
字段对应:
- 排版/分类/点评提示词(用户可改)
- 插图尺寸 + prompt 模板(用户可改)
- 总开关 enabled
- 模型名(默认指向 Agnes,但可改成任意 OpenAI 兼容端点)
"""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import Boolean, DateTime, Integer, String, Text, func
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class LlmSetting(Base):
__tablename__ = "llm_settings"
# 永远只有一行:id=1
id: Mapped[int] = mapped_column(Integer, primary_key=True, default=1)
# === 提示词 ===
format_prompt: Mapped[str | None] = mapped_column(Text)
classify_prompt: Mapped[str | None] = mapped_column(Text)
commentary_prompt: Mapped[str | None] = mapped_column(Text)
image_prompt_template: Mapped[str | None] = mapped_column(Text)
# === 全局屏蔽分类标签(如 ["体育", "娱乐"])===
# 与 sources.blocklist_tags 合并去重后注入 classify prompt;
# 命中则删文章(drop)
blocklist_tags: Mapped[list[str]] = mapped_column(
JSONB, nullable=False, default=list, server_default="[]"
)
# === 插图参数 ===
image_size: Mapped[str] = mapped_column(String(16), default="768x512", nullable=False)
# === 模型 ===
chat_model: Mapped[str] = mapped_column(String(64), default="agnes-2.0-flash", nullable=False)
image_model: Mapped[str] = mapped_column(String(64), default="agnes-image-2.1-flash", nullable=False)
# === 限速 ===
interval_sec: Mapped[float] = mapped_column(default=2.0, nullable=False)
# === 总开关 ===
enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
# === 美团大模型(LongCat,OpenAI 兼容)===
# 双 provider 评论架构:Angel + 美团并列,各跑各的 prompt,结果存到 articles 各自的列
# api_key 留空 = 不启用该 provider
meituan_api_key: Mapped[str] = mapped_column(Text, default="", nullable=False)
meituan_base_url: Mapped[str] = mapped_column(
String(255), default="https://api.longcat.chat/openai/v1", nullable=False
)
meituan_chat_model: Mapped[str] = mapped_column(
String(64), default="LongCat-2.0-Preview", nullable=False
)
meituan_interval_sec: Mapped[float] = mapped_column(default=2.0, nullable=False)
meituan_enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
meituan_commentary_prompt: Mapped[str | None] = mapped_column(Text) # 留空用默认
# === 时间 ===
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
)
def __repr__(self) -> str:
return f"<LlmSetting id={self.id} enabled={self.enabled}>"