feat(llm): 新增 LLM 智能增强服务(Agnes client + 4 项 enrichment 任务 + admin API + migration)

This commit is contained in:
Mavis
2026-06-08 14:24:00 +08:00
parent 40be1e6861
commit ffd667f0dc
7 changed files with 698 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
"""LLM 设置相关 Pydantic schemas。"""
from __future__ import annotations
from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field
class LlmSettingOut(BaseModel):
model_config = ConfigDict(from_attributes=True)
format_prompt: str | None = None
classify_prompt: str | None = None
commentary_prompt: str | None = None
image_prompt_template: str | None = None
image_size: str = "1024x768"
chat_model: str = "agnes-2.0-flash"
image_model: str = "agnes-image-2.1-flash"
interval_sec: float = 2.0
enabled: bool = True
updated_at: datetime | None = None
class LlmSettingUpdate(BaseModel):
"""PATCH — 全部字段 optional,只更新传入的。"""
format_prompt: str | None = None
classify_prompt: str | None = None
commentary_prompt: str | None = None
image_prompt_template: str | None = None
image_size: str | None = Field(default=None, pattern=r"^\d{2,4}x\d{2,4}$")
chat_model: str | None = Field(default=None, min_length=1, max_length=64)
image_model: str | None = Field(default=None, min_length=1, max_length=64)
interval_sec: float | None = Field(default=None, ge=0.0, le=60.0)
enabled: bool | None = None
# === 默认提示词(模板,用户可改)===
DEFAULT_PROMPTS = {
"format_prompt": (
"你是中文新闻排版助手。请将以下译文改写为适合网页阅读的版式,要求:\n"
"1. 保留所有事实信息,不要增删内容\n"
"2. 按段落拆分(2-4 句一段),段间空行\n"
"3. 关键人物/机构/数字用 **加粗**\n"
"4. 如有并列要点,转为编号列表(1. 2. 3.)\n"
"5. 不要使用 # 标题,不要外层 markdown 代码块\n"
"6. 直接输出排版后的纯文本\n\n"
"原文:\n{body}\n"
),
"classify_prompt": (
"你是新闻分类助手。请阅读以下新闻,返回 1-2 个分类标签。\n"
"可选标签(可自由组合): 时政 / 经济 / 科技 / 军事 / 社会 / 国际 / 体育 / 文化 / 环境 / 健康 / 金融 / 能源 / 气候\n"
"严格要求:只返回 JSON,形如 {\"categories\": [\"时政\", \"国际\"]},不要其他内容。\n\n"
"标题:{title}\n摘要:{summary}\n"
),
"commentary_prompt": (
"你是资深新闻评论员。请基于以下新闻写一段 100-200 字的中文点评。\n"
"要求:客观、有深度、避免空洞套话,给出具体观察或背景。\n\n"
"标题:{title}\n正文:{body}\n"
),
"image_prompt_template": (
"Editorial news illustration about: {title}. "
"Cinematic, professional journalism style, soft natural lighting, "
"no text, no logos, no watermark."
),
}
def get_default_prompts() -> dict[str, str]:
return dict(DEFAULT_PROMPTS)