Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ Status: implemented — 既有口径门禁见 fc26985;#151 整理 AIHOT/Easel
页面区分原始维度、权重贡献、基础分、来源加分与软降权,并标明本地降级待复核及未入选原因。硬规则被排除的数量与软降权分开表达;载入后的额外排除数量不冒充数据库查询前的完整排除量。正式入选后的个性化展示顺序仍由精选页处理,本页解释个性化排序之前的基础评分,不能承诺复现首屏每个位置。

解释缓存绑定用户和事件修订,另有有限有效期与显式失效。合成页面验收和隔离数据库回归证明数据契约及交互,不证明真实 Provider、模型质量或用户收益;本次没有调整评分权重或提示词意图。

## 按形态细分评分证据(#233)

实现工单为 [#233](https://github.com/fxbin/TopicEye/issues/233),本节维护类型化标尺的运行合同。

- [content_types](../../../../backend/app/core/content_types.py) 是形态列表、别名和兼容归一化的共同入口;分类服务保持原有导出和未知值截断透传行为。主题 `category` 不用于选择评分标尺。
- [content_type_rubrics](../../../../backend/app/core/content_type_rubrics.py) 维护十种形态和通用标尺的中英证据要求、四档质量/使用价值、信息密度、创作者价值及反例。档位取自 `scoring_caliber.ANCHOR_BANDS`;版本标记随实际系统消息进入现有响应缓存身份。标尺字段优先于用户模板的通用描述,不产生类型固定加分,不改权重、风险和精选门槛。
- 完整分析只附当前类型的一段。Lite预筛消费同源段落;证据不足或标签与正文冲突时要求低置信并升级,不再以所有内容是否具有操作步骤判断价值。未知或缺失形态明确使用通用规则,不为缺分类额外调用模型、不猜主题映射、不反写分类。
- `论文`及其既有别名、或原有arXiv来源检测,进入论文专用流程并跳过Lite;`deep_read`按既有归一化合同保存。论文可以由实验或证明支持,预印本、名校/名企、只有摘要都不自动证明严谨。非arXiv论文的本地降级不宣称来自arXiv、不制造精读已核验结果。
- 标尺支持新闻的跟进判断、观点的解释判断、工具的采用判断,同时对教程复现、评测实测、项目运行材料提出相应证据要求。类型只改变需要何种证据,不把同类内容自动排到别类前面。系统提示词不回显不受信任的未知类型原文。
- Prompt目录登记完整双语规则快照供检查;不是每次请求发送所有规则。统计仍按scene归属,不从当前目录或规则版本倒推历史请求。旧分析不自动重评,历史缓存不删除;实际新请求使用带标尺的消息身份。

放弃“按类型换权重/固定加分”与“新闻无步骤必低分/个人经历必低分”的做法:前者需独立校准,后者混淆用途与内容形态。仍保留本地降级估分与Lite简化分数的既有语义;它们不因此成为完整Pro维度或真实质量证明。

回归见 [test_content_type_rubrics](../../../../backend/tests/test_content_type_rubrics.py),验证类型、语言、预筛/完整调用与论文保存合同;mock不证明模型真实理解或跨类型分数已校准。来源名称/发布时间等既有元数据仍未额外注入title/body模板,时效与权威判断仅能依赖输入片段,不能宣称已作真实校准。真实Provider与独立人工留出集另行验收,沿用SelectBench证据边界。回退通过恢复本批提示词、路由及规则代码,不批量回写历史数据。
508 changes: 508 additions & 0 deletions backend/app/core/content_type_rubrics.py

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions backend/app/core/content_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""分类与评分共用的内容形态;主题 category 与形态 content_type 正交。"""

# ── 内容形态(双轴分类的 format 轴) ────────────────────────────────
# 与 category(主题轴)正交,描述内容的呈现形态/意图
CONTENT_TYPES: list[str] = [
"论文", # 学术论文 / 预印本
"技术", # 技术文章 / 工程实践
"资讯", # 新闻 / 行业动态
"教程", # 教程 / 指南 / How-to
"观点", # 评论 / 分析 / 观点
"工具", # 工具 / 产品发布
"体验", # 体验测评 / 用户研究
"成长", # 个人成长 / 思维方法
"讨论", # 社区讨论 / 问答
"项目", # 开源项目 / Side Project
]

# 信源 category 中 `/` 后的形态别名 -> 标准化 content_type
CONTENT_TYPE_ALIASES: dict[str, str] = {
"论文": "论文",
"paper": "论文",
"学术": "论文",
"技术": "技术",
"tech": "技术",
"资讯": "资讯",
"新闻": "资讯",
"news": "资讯",
"教程": "教程",
"tutorial": "教程",
"指南": "教程",
"观点": "观点",
"评论": "观点",
"opinion": "观点",
"工具": "工具",
"tool": "工具",
"产品": "工具",
"体验": "体验",
"review": "体验",
"测评": "体验",
"成长": "成长",
"思维": "成长",
"讨论": "讨论",
"discussion": "讨论",
"项目": "项目",
"project": "项目",
"开源": "项目",
}


def normalize_content_type(value: str | None) -> str | None:
"""兼容既有分类合同:规范已知别名,未知形态截断后透传。"""
if not value:
return None
key = str(value).strip().lower()
if not key:
return None
return CONTENT_TYPE_ALIASES.get(key, str(value).strip()[:50])
61 changes: 38 additions & 23 deletions backend/app/services/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.config import settings
from app.core.content_type_rubrics import content_type_rubric_section, resolve_rubric_type
from app.core.database import async_session
from app.models.analysis import AiAnalysis
from app.models.content import ContentItem, ContentStatus
Expand Down Expand Up @@ -152,10 +153,13 @@ def _analysis_result_from_prescreen(


async def _run_lite_prescreen(
content: ContentItem, *, title: str, truncated: str
content: ContentItem, *, title: str, truncated: str, lang: str
) -> tuple[dict[str, Any] | None, dict[str, Any]]:
messages = [
{"role": "system", "content": PRESCREEN_SYSTEM_PROMPT},
{
"role": "system",
"content": PRESCREEN_SYSTEM_PROMPT + "\n\n" + content_type_rubric_section(content.content_type, lang),
},
{
"role": "user",
"content": PRESCREEN_PROMPT.format(
Expand All @@ -174,7 +178,7 @@ async def _run_lite_prescreen(
return _normalize_prescreen_result(result), metadata


def _local_analysis_result(content: ContentItem, *, lang: str, is_arxiv: bool = False) -> dict[str, Any]:
def _local_analysis_result(content: ContentItem, *, lang: str, is_paper: bool = False) -> dict[str, Any]:
"""Build a deterministic baseline analysis when the LLM response is empty."""
text = f"{content.title}\n{content.summary or ''}\n{content.raw_content or ''}".strip()
source = f"{content.source_name or ''} {content.source_type or ''} {content.platform or ''}".lower()
Expand Down Expand Up @@ -208,10 +212,10 @@ def _local_analysis_result(content: ContentItem, *, lang: str, is_arxiv: bool =
if len(summary) > 180:
summary = summary[:180].rstrip() + "..."

if is_arxiv:
if is_paper:
# 论文 LLM 兜底:用标题生成中文占位概述,避免英文原文直接展示
summary = f"《{title}》是 arXiv 上的学术论文。LLM 暂不可用,建议查看原文获取详细方法与结论。"
recommendation = "这篇论文来自 arXiv,涉及前沿研究。LLM 限流未能生成详细中文解读,可先收藏待精读。"
summary = f"《{title}》按论文流程处理。LLM 暂不可用,方法与结论尚未核验,请查看原文。"
recommendation = "论文解读暂不可用,当前为本地估分;研究贡献与精读价值仍待核验。"
elif lang == "en":
recommendation = f"这条内容来自 {content.source_name or '外部信源'},适合作为跨市场趋势素材先观察,再结合中文语境提炼选题角度。"
else:
Expand Down Expand Up @@ -262,22 +266,31 @@ def _analysis_retryable_status_filter(stale_cutoff: datetime):
def _select_analysis_prompts(content: ContentItem, *, title: str, content_text: str) -> tuple[str, str, str, bool]:
"""按内容特征选择分析 prompt。

优先级:arXiv 论文 > 英文内容 > 中文内容。
优先级:arXiv 或论文形态 > 英文内容 > 中文内容。类型不由主题 category 推断。

Returns:
(system_prompt, analysis_prompt, lang, is_arxiv)
(system_prompt, analysis_prompt, lang, is_paper)
"""
platform_lower = (content.platform or "").lower()
is_arxiv = "arxiv" in platform_lower or "arxiv" in (content.source_name or "").lower()
is_paper = (
resolve_rubric_type(content.content_type) == "论文"
or "arxiv" in platform_lower
or "arxiv" in (content.source_name or "").lower()
)
lang = _detect_lang(title, content_text or "")

if is_arxiv:
logger.info("Detected arXiv paper, using paper prompts for content id=%d", content.id)
return PAPER_SYSTEM_PROMPT, PAPER_ANALYSIS_PROMPT, lang, True
if is_paper:
logger.info("Detected paper, using paper prompts for content id=%d", content.id)
return PAPER_SYSTEM_PROMPT + "\n\n" + content_type_rubric_section("论文"), PAPER_ANALYSIS_PROMPT, lang, True
if lang == "en":
logger.info("Detected English content, using EN prompts for content id=%d", content.id)
return SYSTEM_PROMPT_EN, ANALYSIS_PROMPT_EN, lang, False
return SYSTEM_PROMPT, ANALYSIS_PROMPT, lang, False
return (
SYSTEM_PROMPT_EN + "\n\n" + content_type_rubric_section(content.content_type, "en"),
ANALYSIS_PROMPT_EN,
lang,
False,
)
return SYSTEM_PROMPT + "\n\n" + content_type_rubric_section(content.content_type), ANALYSIS_PROMPT, lang, False


def _apply_cross_market_bonus(content: ContentItem, *, lang: str, curation_score: float) -> float:
Expand Down Expand Up @@ -309,7 +322,7 @@ def _build_analysis_record(
content: ContentItem,
result: dict[str, Any],
*,
is_arxiv: bool,
is_paper: bool,
curation_score: float,
analysis_mode: str,
prescreen_model: str | None,
Expand All @@ -330,9 +343,9 @@ def _build_analysis_record(

scores = result.get("scores", {})
curation = result.get("curation", {})
# arXiv 论文的精读判定嵌套进 enrichment.deep_read(与其他 enrichment schema 兼容)
# 论文的精读判定嵌套进 enrichment.deep_read(与其他 enrichment schema 兼容)
# 归一化:调和 worth_deep_read(deep_read_score≥70) 矛盾 + 字符串 bool 解析
_deep_read = _normalize_deep_read(result.get("deep_read")) if is_arxiv else None
_deep_read = _normalize_deep_read(result.get("deep_read")) if is_paper else None
return AiAnalysis(
content_id=content.id,
quality_score=scores.get("quality_score", 0),
Expand Down Expand Up @@ -451,7 +464,7 @@ async def analyze_content(content: ContentItem, db: AsyncSession) -> AiAnalysis:
title = readable_content_title(content.title, content.summary, content.raw_content)
truncated = content_text[:3000] if content_text else "无正文内容"

system_prompt, analysis_prompt, lang, is_arxiv = _select_analysis_prompts(
system_prompt, analysis_prompt, lang, is_paper = _select_analysis_prompts(
content, title=title, content_text=content_text
)

Expand All @@ -475,13 +488,15 @@ async def analyze_content(content: ContentItem, db: AsyncSession) -> AiAnalysis:
prescreen_confidence = None
result: dict[str, Any] | None = None

if _cascade_enabled() and not is_arxiv:
if _cascade_enabled() and not is_paper:
# 论文跳过 prescreen:结构化概述 + 精读判定需要完整论文 prompt,
# prescreen 的 lite 简化结果无法产出 deep_read 字段。
analysis_mode = "cascade"
prescreen_model = _cascade_lite_routing_group()
try:
prescreen, prescreen_metadata = await _run_lite_prescreen(content, title=title, truncated=truncated)
prescreen, prescreen_metadata = await _run_lite_prescreen(
content, title=title, truncated=truncated, lang=lang
)
prescreen_model = prescreen_metadata.get("actual_model") or prescreen_model
except Exception as exc:
logger.warning("Lite prescreen failed for content id=%d, escalating to pro: %s", content.id, exc)
Expand Down Expand Up @@ -527,7 +542,7 @@ async def analyze_content(content: ContentItem, db: AsyncSession) -> AiAnalysis:
content.id,
type(llm_exc).__name__,
)
result = _local_analysis_result(content, lang=lang, is_arxiv=is_arxiv)
result = _local_analysis_result(content, lang=lang, is_paper=is_paper)
fallback_used = True
else:
raise
Expand All @@ -549,7 +564,7 @@ async def analyze_content(content: ContentItem, db: AsyncSession) -> AiAnalysis:
content.id,
str(result)[:200],
)
result = _local_analysis_result(content, lang=lang, is_arxiv=is_arxiv)
result = _local_analysis_result(content, lang=lang, is_paper=is_paper)
fallback_used = True
result = _normalize_analysis_result(result)
else:
Expand All @@ -563,7 +578,7 @@ async def analyze_content(content: ContentItem, db: AsyncSession) -> AiAnalysis:
analysis = _build_analysis_record(
content,
result,
is_arxiv=is_arxiv,
is_paper=is_paper,
curation_score=curation_score,
analysis_mode=analysis_mode,
prescreen_model=prescreen_model,
Expand Down
59 changes: 3 additions & 56 deletions backend/app/services/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from sqlalchemy.ext.asyncio import AsyncSession

from app.core.content_types import CONTENT_TYPES as CONTENT_TYPES, normalize_content_type

logger = logging.getLogger(__name__)

# ── Keyword-based fallback (unchanged) ─────────────────────────────────
Expand All @@ -37,52 +39,6 @@
"其他",
]

# ── 内容形态(双轴分类的 format 轴) ────────────────────────────────
# 与 category(主题轴)正交,描述内容的呈现形态/意图
CONTENT_TYPES: list[str] = [
"论文", # 学术论文 / 预印本
"技术", # 技术文章 / 工程实践
"资讯", # 新闻 / 行业动态
"教程", # 教程 / 指南 / How-to
"观点", # 评论 / 分析 / 观点
"工具", # 工具 / 产品发布
"体验", # 体验测评 / 用户研究
"成长", # 个人成长 / 思维方法
"讨论", # 社区讨论 / 问答
"项目", # 开源项目 / Side Project
]

# 信源 category 中 `/` 后的形态别名 -> 标准化 content_type
_CONTENT_TYPE_ALIASES: dict[str, str] = {
"论文": "论文",
"paper": "论文",
"学术": "论文",
"技术": "技术",
"tech": "技术",
"资讯": "资讯",
"新闻": "资讯",
"news": "资讯",
"教程": "教程",
"tutorial": "教程",
"指南": "教程",
"观点": "观点",
"评论": "观点",
"opinion": "观点",
"工具": "工具",
"tool": "工具",
"产品": "工具",
"体验": "体验",
"review": "体验",
"测评": "体验",
"成长": "成长",
"思维": "成长",
"讨论": "讨论",
"discussion": "讨论",
"项目": "项目",
"project": "项目",
"开源": "项目",
}

CATEGORY_KEYWORDS: dict[str, list[str]] = {
"AI": [
"AI",
Expand Down Expand Up @@ -287,16 +243,7 @@ def _normalize_content_type(value: str | None) -> str | None:
>>> _normalize_content_type("")
None
"""
if not value:
return None
key = str(value).strip().lower()
if not key:
return None
# 精确匹配优先
if key in _CONTENT_TYPE_ALIASES:
return _CONTENT_TYPE_ALIASES[key]
# 原值回退(允许新形态值透传,但截断长度)
return str(value).strip()[:50]
return normalize_content_type(value)


def _get_keyword_score(text: str) -> float:
Expand Down
Loading
Loading