打通流水线闭环: trends+metrics+optimize_sources → collector

三大数据流精准注入采集器:

1. 热点趋势(trends) -> collector
   _get_trend_context() 读取 trends.json
   热搜注入 LLM 选题 prompt

2. 历史表现(metrics) -> collector
   scheduler 同步指标后按field聚合写入 feedback.json
   collector 读取后高互动领域获优先级提升

3. AI策略(optimize_sources) -> collector
   从 DB SystemConfig 读取 AI 建议注入 prompt
This commit is contained in:
Yuzhiran Dev
2026-05-21 09:48:38 +08:00
parent 6b790e77f6
commit 56293a52a2
2 changed files with 99 additions and 9 deletions
+59
View File
@@ -377,6 +377,61 @@ class SustainabilityCollector:
logger.warning(f"web_search失败 {source.name}: {e}")
return []
def _get_trend_context(self) -> str:
"""读取热点趋势数据和指标反馈, 返回markdown上下文"""
parts = []
try:
from trends import load_trends
trends = load_trends()
if trends:
lines = ["## 当前热点趋势", ""]
for t in trends[:5]:
src = {"weibo": "🔥", "zhihu": "📖", "baidu": "🔍", "llm": "🤖"}.get(t.get("source", ""), "")
lines.append(f"- {src} **{t['topic']}**{t.get('platform','')}):{t.get('reason','')[:80]}")
kw = t.get("hot_keywords", [])
if kw:
lines.append(f" 搜索热词:{' '.join(kw[:3])}")
parts.append("\n".join(lines))
except Exception:
pass
metrics_file = DATA_DIR / "metrics_feedback.json"
if metrics_file.exists():
try:
feedback = json.loads(metrics_file.read_text(encoding='utf-8'))
top_domains = feedback.get("top_domains", [])
if top_domains:
lines = ["## 历史表现反馈(高互动领域优先", ""]
for d, s in top_domains[:3]:
lines.append(f"- {d}:平均分 {s}")
parts.append("\n".join(lines))
except Exception:
pass
try:
from app.database import SessionLocal
from app.models import SystemConfig
db = SessionLocal()
try:
sc = db.query(SystemConfig).filter(SystemConfig.key == "collector_ai_advice").first()
if sc and sc.value:
advice = json.loads(sc.value)
summary = advice.get("summary", "")
new_cats = advice.get("suggested_new_categories", [])
if summary:
parts.append(f"## AI策略建议\n{summary}")
if new_cats:
suggested = [f"- {c['name']}{c.get('reason','')[:50]}" for c in new_cats[:2]]
parts.append("建议关注的新方向:\n" + "\n".join(suggested))
except Exception:
pass
finally:
db.close()
except Exception:
pass
return "\n\n".join(parts)
def _generate_topics_with_llm(self, cases: List[SustainabilityCase] = None, search_results: List[Dict] = None) -> List[SustainabilityTopic]:
"""用LLM基于采集数据生成选题(数据充分时精确生成,无数据时凭知识生成)"""
try:
@@ -402,11 +457,15 @@ class SustainabilityCollector:
case_lines = [f"- {c.title[:40]}({c.category})" for c in cases[:5]]
data_section += "\n采集案例:\n" + "\n".join(case_lines) + "\n"
trend_context = self._get_trend_context()
prompt = f"""你是一个内容策略师。基于以下信息,为「{target_category}」类别生成一个高质量选题。
{data_section if data_section else "(当前无实时采集数据,请基于你对中文互联网趋势的了解直接生成)"}
{existing_hint}
{trend_context}
输出一个选题,格式JSON
{{{{
"title": "标题(20字内,含核心关键词)",