report_agent.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. """步骤7 整合输出 Agent:将前序步骤产物整合为结构化研究提案(Markdown 报告)
  2. 输入:pipeline_results 含 papers_records / gaps / innovations / experiment_plans
  3. 输出:{background_review, innovations, experiment_plans, references, markdown}
  4. """
  5. from __future__ import annotations
  6. import json
  7. import logging
  8. from typing import Any, Optional
  9. from backend.llm.client import DeepSeekClient
  10. from backend.llm.json_utils import parse_json_safe
  11. logger = logging.getLogger(__name__)
  12. # 模块级懒加载单例 DeepSeekClient
  13. _client: Optional[DeepSeekClient] = None
  14. def get_default_client() -> DeepSeekClient:
  15. """懒加载 DeepSeek 客户端单例,供未显式注入 client 时使用"""
  16. global _client
  17. if _client is None:
  18. _client = DeepSeekClient()
  19. return _client
  20. class ReportAgent:
  21. """步骤7 整合输出 Agent:调用 LLM 把前序产物整合为结构化研究提案
  22. 职责:
  23. 1. 基于 Gap 分析梳理领域背景综述;
  24. 2. 对每个创新点论证 Gap 来源 + 创新思路 + 三维评分 + 论据支撑;
  25. 3. 汇总每个创新点的完整实验方案;
  26. 4. 整理参考文献溯源(含 source_sections,可追溯至具体章节);
  27. 5. 输出 Markdown 格式报告字符串与结构化 dict。
  28. """
  29. def __init__(self, client: Optional[DeepSeekClient] = None) -> None:
  30. # 未显式注入则使用模块级懒加载单例
  31. self.client = client or get_default_client()
  32. # ------------------------------------------------------------------
  33. # 公共入口
  34. # ------------------------------------------------------------------
  35. async def generate_report(self, pipeline_results: dict) -> dict:
  36. """整合前序步骤产物,生成结构化研究提案
  37. 输入 pipeline_results 字段:
  38. - papers_records:结构化记录列表(PaperRecord dict)
  39. - gaps:Gap 列表(Gap dict)
  40. - innovations:创新点列表(Innovation dict)
  41. - experiment_plans:实验方案列表(ExperimentPlan dict)
  42. 返回 dict:
  43. - background_review:背景综述字符串
  44. - innovations:创新点列表(含论据支撑)
  45. - experiment_plans:实验方案列表
  46. - references:参考文献溯源列表 [{title, path, sections}]
  47. - markdown:完整 Markdown 报告字符串
  48. """
  49. import time as _time
  50. _t_total = _time.perf_counter()
  51. papers_records = pipeline_results.get("papers_records", []) or []
  52. gaps = pipeline_results.get("gaps", []) or []
  53. innovations = pipeline_results.get("innovations", []) or []
  54. experiment_plans = pipeline_results.get("experiment_plans", []) or []
  55. logger.info(
  56. "[ReportAgent] 输入规模: papers=%d gaps=%d innovations=%d plans=%d",
  57. len(papers_records), len(gaps), len(innovations), len(experiment_plans),
  58. )
  59. # 1. 构造整合提示词
  60. _t_prompt0 = _time.perf_counter()
  61. prompt = self._build_report_prompt(pipeline_results)
  62. logger.info("[ReportAgent timing] 构造 prompt: %.3fs", _time.perf_counter() - _t_prompt0)
  63. messages = [
  64. {
  65. "role": "system",
  66. "content": (
  67. "你是学术研究提案撰写专家,擅长把多篇论文的 Gap 分析、创新点与实验方案"
  68. "整合为结构清晰、可追溯的研究提案。严格输出 JSON,不要输出任何额外说明文本。"
  69. ),
  70. },
  71. {"role": "user", "content": prompt},
  72. ]
  73. # 2. 调用 LLM(ReportAgent 输出较长,显式放大 max_tokens 避免 JSON 被截断)
  74. try:
  75. _t_llm0 = _time.perf_counter()
  76. response = await self.client.chat(messages=messages, max_tokens=8192)
  77. content = response.choices[0].message.content or ""
  78. # 记录 finish_reason,length 表示被 max_tokens 截断
  79. finish_reason = response.choices[0].finish_reason
  80. usage = getattr(response, "usage", None)
  81. logger.info(
  82. "[ReportAgent timing] LLM 调用: %.2fs, 输出 %d 字符, finish_reason=%s, usage=%s",
  83. _time.perf_counter() - _t_llm0, len(content), finish_reason, usage,
  84. )
  85. except Exception as exc:
  86. logger.warning("ReportAgent LLM 调用失败,降级为本地格式化: %s", exc)
  87. content = ""
  88. # 3. 解析 LLM 返回的结构化数据
  89. _t_parse0 = _time.perf_counter()
  90. structured_from_llm: dict = {}
  91. if content:
  92. parsed = parse_json_safe(content)
  93. if isinstance(parsed, dict):
  94. structured_from_llm = parsed
  95. else:
  96. logger.warning(
  97. "ReportAgent JSON 解析失败,原始内容前 200 字: %s",
  98. content[:200],
  99. )
  100. # 4. 组装最终结构化结果(LLM 输出优先,缺失字段用原始输入补齐)
  101. background_review = (
  102. structured_from_llm.get("background_review")
  103. or self._build_default_background(gaps, papers_records)
  104. )
  105. # 创新点:保留原始输入,合并 LLM 补充的论据支撑
  106. merged_innovations = self._merge_innovations(
  107. innovations, structured_from_llm.get("innovations", [])
  108. )
  109. # 实验方案:优先用 LLM 输出,缺失则回退原始输入
  110. merged_plans = self._merge_experiment_plans(
  111. experiment_plans, structured_from_llm.get("experiment_plans", [])
  112. )
  113. # 参考文献溯源:从 papers_records 抽取,保证可追溯
  114. references = self._build_references(papers_records)
  115. # 5. 生成 Markdown 报告:LLM 已返回 markdown 则直接用,否则本地格式化
  116. markdown = structured_from_llm.get("markdown") or ""
  117. if not markdown.strip():
  118. markdown = self._format_markdown(
  119. {
  120. "background_review": background_review,
  121. "innovations": merged_innovations,
  122. "experiment_plans": merged_plans,
  123. "references": references,
  124. }
  125. )
  126. logger.info(
  127. "[ReportAgent timing] 解析+组装+markdown: %.3fs",
  128. _time.perf_counter() - _t_parse0,
  129. )
  130. logger.info(
  131. "[ReportAgent timing] 总耗时: %.2fs, markdown %d 字符",
  132. _time.perf_counter() - _t_total, len(markdown),
  133. )
  134. return {
  135. "background_review": background_review,
  136. "innovations": merged_innovations,
  137. "experiment_plans": merged_plans,
  138. "references": references,
  139. "markdown": markdown,
  140. }
  141. # ------------------------------------------------------------------
  142. # 提示词构造
  143. # ------------------------------------------------------------------
  144. def _build_report_prompt(self, pipeline_results: dict) -> str:
  145. """构造整合提示词(中文)
  146. 把 papers_records / gaps / innovations / experiment_plans 序列化为
  147. 可读文本,要求 LLM 输出严格 JSON。
  148. """
  149. papers_records = pipeline_results.get("papers_records", []) or []
  150. gaps = pipeline_results.get("gaps", []) or []
  151. innovations = pipeline_results.get("innovations", []) or []
  152. experiment_plans = pipeline_results.get("experiment_plans", []) or []
  153. # 论文记录精简版(含 source_sections 便于溯源)
  154. papers_block = self._serialize_papers(papers_records)
  155. gaps_block = self._serialize_gaps(gaps)
  156. innovations_block = self._serialize_innovations(innovations)
  157. plans_block = self._serialize_experiment_plans(experiment_plans)
  158. # 推断研究主题:取首个创新点标题或首个 Gap 描述
  159. topic = ""
  160. if innovations:
  161. topic = innovations[0].get("title", "")
  162. if not topic and gaps:
  163. topic = gaps[0].get("description", "")[:40]
  164. if not topic:
  165. topic = "未指定主题"
  166. return f"""请基于以下前序步骤产物,整合为一份结构化研究提案。
  167. ## 研究主题
  168. {topic}
  169. ## 一、论文结构化记录(含 source_sections,可追溯)
  170. {papers_block}
  171. ## 二、识别到的 Gap 列表
  172. {gaps_block}
  173. ## 三、创新点列表(含三维评分)
  174. {innovations_block}
  175. ## 四、实验方案列表
  176. {plans_block}
  177. ## 整合任务
  178. 1. 背景综述:基于 Gap 分析梳理领域现状与脉络,说明研究空白所在;
  179. 2. 创新点论证:对每个创新点补充 Gap 来源 + 创新思路 + 三维评分 + 论据支撑(论据须引用具体论文或 Gap 证据);
  180. 3. 实验方案:保留每个创新点的完整实验方案要素;
  181. 4. 参考文献溯源:所有引用论文的来源标注,可追溯至具体章节(source_sections);
  182. 5. Markdown 报告:按以下结构生成完整 Markdown 文本。
  183. ## Markdown 报告结构要求
  184. ```
  185. # 研究提案:[主题]
  186. ## 一、背景综述
  187. ...
  188. ## 二、创新点论证
  189. ### 创新点 1:[标题]
  190. - Gap 来源:...
  191. - 创新思路:...
  192. - 三维评分:新颖性 X / 可行性 X / 显著性 X
  193. - 论据支撑:...
  194. ### 创新点 2:...
  195. ## 三、实验方案
  196. ### 创新点 1 实验方案
  197. - 研究假设:...
  198. - 数据集:...
  199. - 基线方法:...
  200. - 评估指标:...
  201. - 消融实验:...
  202. - 实验步骤:...
  203. - 预期结果:...
  204. - 风险:...
  205. ## 四、参考文献溯源
  206. 1. [论文标题] - 出处章节:...
  207. ```
  208. ## 输出要求
  209. 严格输出 JSON,不要包含任何额外说明文本或代码块标记。格式如下:
  210. {{
  211. "background_review": "背景综述文本(可含换行)",
  212. "innovations": [
  213. {{
  214. "title": "创新点标题",
  215. "idea": "创新思路",
  216. "gap_origin": "Gap 来源描述",
  217. "score": {{"novelty": 8.0, "feasibility": 7.5, "significance": 8.0}},
  218. "supporting_evidence": "论据支撑,引用具体论文或 Gap 证据"
  219. }}
  220. ],
  221. "experiment_plans": [
  222. {{
  223. "innovation_title": "对应创新点标题",
  224. "hypothesis": "研究假设",
  225. "datasets": ["数据集1"],
  226. "baselines": ["基线方法1"],
  227. "metrics": ["评估指标1"],
  228. "ablation": ["消融实验1"],
  229. "steps": ["实验步骤1"],
  230. "expected_results": "预期结果",
  231. "risks": "风险"
  232. }}
  233. ],
  234. "references": [
  235. {{"title": "论文标题", "path": "论文路径", "sections": ["出处章节1", "出处章节2"]}}
  236. ],
  237. "markdown": "完整 Markdown 报告字符串"
  238. }}
  239. 注意:
  240. - references 必须可追溯至具体章节(source_sections);
  241. - 不得编造论文或证据,所有引用须来自上述论文记录;
  242. - 若某字段在输入中缺失,对应位置写"暂无"而非编造。"""
  243. # ------------------------------------------------------------------
  244. # Markdown 格式化(降级路径)
  245. # ------------------------------------------------------------------
  246. def _format_markdown(self, structured: dict) -> str:
  247. """把结构化数据格式化为清晰 Markdown(LLM 未返回 markdown 时使用)"""
  248. background_review = structured.get("background_review", "") or "暂无"
  249. innovations = structured.get("innovations", []) or []
  250. experiment_plans = structured.get("experiment_plans", []) or []
  251. references = structured.get("references", []) or []
  252. # 主题推断
  253. topic = ""
  254. if innovations:
  255. topic = innovations[0].get("title", "")
  256. if not topic:
  257. topic = "未指定主题"
  258. lines: list[str] = []
  259. lines.append(f"# 研究提案:{topic}")
  260. lines.append("")
  261. # 一、背景综述
  262. lines.append("## 一、背景综述")
  263. lines.append(background_review.strip() or "暂无")
  264. lines.append("")
  265. # 二、创新点论证
  266. lines.append("## 二、创新点论证")
  267. if innovations:
  268. for idx, inn in enumerate(innovations, 1):
  269. title = inn.get("title", f"创新点{idx}")
  270. idea = inn.get("idea", "暂无")
  271. gap_origin = inn.get("gap_origin", "暂无")
  272. score = inn.get("score", {}) or {}
  273. novelty = score.get("novelty", "-")
  274. feasibility = score.get("feasibility", "-")
  275. significance = score.get("significance", "-")
  276. evidence = inn.get("supporting_evidence", "暂无")
  277. lines.append(f"### 创新点 {idx}:{title}")
  278. lines.append(f"- Gap 来源:{gap_origin}")
  279. lines.append(f"- 创新思路:{idea}")
  280. lines.append(
  281. f"- 三维评分:新颖性 {novelty} / 可行性 {feasibility} / 显著性 {significance}"
  282. )
  283. lines.append(f"- 论据支撑:{evidence}")
  284. lines.append("")
  285. else:
  286. lines.append("暂无创新点")
  287. lines.append("")
  288. # 三、实验方案
  289. lines.append("## 三、实验方案")
  290. if experiment_plans:
  291. for idx, plan in enumerate(experiment_plans, 1):
  292. inn_title = plan.get("innovation_title", f"创新点{idx}")
  293. hypothesis = plan.get("hypothesis", "暂无")
  294. datasets = plan.get("datasets", []) or []
  295. baselines = plan.get("baselines", []) or []
  296. metrics = plan.get("metrics", []) or []
  297. ablation = plan.get("ablation", []) or []
  298. steps = plan.get("steps", []) or []
  299. expected = plan.get("expected_results", "暂无")
  300. risks = plan.get("risks", "暂无")
  301. lines.append(f"### 创新点 {idx} 实验方案({inn_title})")
  302. lines.append(f"- 研究假设:{hypothesis}")
  303. lines.append(f"- 数据集:{', '.join(datasets) if datasets else '暂无'}")
  304. lines.append(
  305. f"- 基线方法:{', '.join(baselines) if baselines else '暂无'}"
  306. )
  307. lines.append(
  308. f"- 评估指标:{', '.join(metrics) if metrics else '暂无'}"
  309. )
  310. lines.append(
  311. f"- 消融实验:{'; '.join(ablation) if ablation else '暂无'}"
  312. )
  313. if steps:
  314. steps_text = "; ".join(steps)
  315. else:
  316. steps_text = "暂无"
  317. lines.append(f"- 实验步骤:{steps_text}")
  318. lines.append(f"- 预期结果:{expected}")
  319. lines.append(f"- 风险:{risks}")
  320. lines.append("")
  321. else:
  322. lines.append("暂无实验方案")
  323. lines.append("")
  324. # 四、参考文献溯源
  325. lines.append("## 四、参考文献溯源")
  326. if references:
  327. for idx, ref in enumerate(references, 1):
  328. title = ref.get("title", "未知标题")
  329. sections = ref.get("sections", []) or []
  330. if sections:
  331. sections_text = "; ".join(sections)
  332. else:
  333. sections_text = "未标注"
  334. lines.append(f"{idx}. [{title}] - 出处章节:{sections_text}")
  335. else:
  336. lines.append("暂无参考文献")
  337. lines.append("")
  338. return "\n".join(lines)
  339. # ------------------------------------------------------------------
  340. # 私有辅助方法
  341. # ------------------------------------------------------------------
  342. def _serialize_papers(self, papers_records: list[dict]) -> str:
  343. """把论文记录序列化为可读文本(含 source_sections)"""
  344. if not papers_records:
  345. return "(无论文记录)"
  346. field_labels = {
  347. "task_problem": "研究任务/问题",
  348. "method": "核心方法",
  349. "key_contributions": "主要贡献",
  350. "datasets": "数据集",
  351. "metrics": "评估指标",
  352. "results": "实验结果",
  353. "limitations": "局限性",
  354. "future_work": "未来工作",
  355. }
  356. lines: list[str] = []
  357. for idx, rec in enumerate(papers_records, 1):
  358. title = rec.get("title", f"论文{idx}")
  359. path = rec.get("path", "")
  360. lines.append(f"----- 论文 {idx}: {title} (path={path}) -----")
  361. for field, label in field_labels.items():
  362. val = rec.get(field)
  363. if val is None:
  364. continue
  365. # FieldSource 结构:{value, source_sections, confidence}
  366. if isinstance(val, dict):
  367. text = val.get("value", "")
  368. src = val.get("source_sections", []) or []
  369. src_text = "; ".join(src) if src else "未标注"
  370. lines.append(f" [{label}] {text} (出处章节: {src_text})")
  371. else:
  372. lines.append(f" [{label}] {val}")
  373. lines.append("")
  374. return "\n".join(lines)
  375. def _serialize_gaps(self, gaps: list[dict]) -> str:
  376. """把 Gap 列表序列化为可读文本"""
  377. if not gaps:
  378. return "(无 Gap)"
  379. lines: list[str] = []
  380. for idx, gap in enumerate(gaps, 1):
  381. desc = gap.get("description", "")
  382. gtype = gap.get("gap_type", "")
  383. src = gap.get("source_papers", []) or []
  384. evidence = gap.get("evidence", "")
  385. conf = gap.get("confidence", "")
  386. src_text = "; ".join(src) if src else "未标注"
  387. lines.append(
  388. f"Gap {idx} [{gtype}] (置信度={conf}): {desc}\n"
  389. f" 来源论文: {src_text}\n"
  390. f" 论证: {evidence}"
  391. )
  392. return "\n".join(lines)
  393. def _serialize_innovations(self, innovations: list[dict]) -> str:
  394. """把创新点列表序列化为可读文本"""
  395. if not innovations:
  396. return "(无创新点)"
  397. lines: list[str] = []
  398. for idx, inn in enumerate(innovations, 1):
  399. title = inn.get("title", f"创新点{idx}")
  400. idea = inn.get("idea", "")
  401. source = inn.get("source", "")
  402. gap_origin = inn.get("gap_origin", "")
  403. score = inn.get("score", {}) or {}
  404. novelty = score.get("novelty", "-")
  405. feasibility = score.get("feasibility", "-")
  406. significance = score.get("significance", "-")
  407. novelty_check = inn.get("novelty_check", "")
  408. evidence = inn.get("supporting_evidence", "")
  409. lines.append(
  410. f"创新点 {idx}: {title} (来源={source})\n"
  411. f" 创新思路: {idea}\n"
  412. f" Gap 来源: {gap_origin}\n"
  413. f" 三维评分: 新颖性 {novelty} / 可行性 {feasibility} / 显著性 {significance}\n"
  414. f" 新颖性验证: {novelty_check}\n"
  415. f" 论据支撑: {evidence}"
  416. )
  417. return "\n".join(lines)
  418. def _serialize_experiment_plans(self, plans: list[dict]) -> str:
  419. """把实验方案列表序列化为可读文本"""
  420. if not plans:
  421. return "(无实验方案)"
  422. lines: list[str] = []
  423. for idx, plan in enumerate(plans, 1):
  424. inn_title = plan.get("innovation_title", f"创新点{idx}")
  425. hypothesis = plan.get("hypothesis", "")
  426. datasets = plan.get("datasets", []) or []
  427. baselines = plan.get("baselines", []) or []
  428. metrics = plan.get("metrics", []) or []
  429. ablation = plan.get("ablation", []) or []
  430. steps = plan.get("steps", []) or []
  431. expected = plan.get("expected_results", "")
  432. risks = plan.get("risks", "")
  433. lines.append(
  434. f"方案 {idx} (对应: {inn_title})\n"
  435. f" 研究假设: {hypothesis}\n"
  436. f" 数据集: {', '.join(datasets)}\n"
  437. f" 基线方法: {', '.join(baselines)}\n"
  438. f" 评估指标: {', '.join(metrics)}\n"
  439. f" 消融实验: {'; '.join(ablation)}\n"
  440. f" 实验步骤: {'; '.join(steps)}\n"
  441. f" 预期结果: {expected}\n"
  442. f" 风险: {risks}"
  443. )
  444. return "\n".join(lines)
  445. def _build_default_background(self, gaps: list[dict], papers_records: list[dict]) -> str:
  446. """LLM 未返回背景综述时,基于 Gap 与论文记录生成默认综述"""
  447. if not gaps:
  448. return (
  449. f"本次分析共覆盖 {len(papers_records)} 篇论文,"
  450. "未识别到显著跨论文 Gap,建议结合具体场景进一步分析。"
  451. )
  452. lines: list[str] = []
  453. lines.append(
  454. f"本次分析共覆盖 {len(papers_records)} 篇论文,识别到 {len(gaps)} 个研究空白:"
  455. )
  456. for idx, gap in enumerate(gaps, 1):
  457. desc = gap.get("description", "")
  458. gtype = gap.get("gap_type", "")
  459. lines.append(f"{idx}. [{gtype}] {desc}")
  460. return "\n".join(lines)
  461. def _merge_innovations(
  462. self, original: list[dict], from_llm: list[dict]
  463. ) -> list[dict]:
  464. """合并原始创新点与 LLM 补充的论据支撑
  465. 以原始输入为基准,按 title 匹配 LLM 输出,补充 supporting_evidence。
  466. """
  467. if not original:
  468. return from_llm if isinstance(from_llm, list) else []
  469. # 以 title 为键建立 LLM 输出索引
  470. llm_index: dict[str, dict] = {}
  471. for item in from_llm:
  472. if isinstance(item, dict):
  473. title = item.get("title", "")
  474. if title:
  475. llm_index[title] = item
  476. merged: list[dict] = []
  477. for inn in original:
  478. if not isinstance(inn, dict):
  479. continue
  480. title = inn.get("title", "")
  481. llm_item = llm_index.get(title, {})
  482. # LLM 补充的论据支撑优先,否则保留原始
  483. evidence = llm_item.get("supporting_evidence") or inn.get(
  484. "supporting_evidence", ""
  485. )
  486. merged_item = dict(inn)
  487. if evidence:
  488. merged_item["supporting_evidence"] = evidence
  489. merged.append(merged_item)
  490. return merged
  491. def _merge_experiment_plans(
  492. self, original: list[dict], from_llm: list[dict]
  493. ) -> list[dict]:
  494. """合并原始实验方案与 LLM 输出
  495. 优先使用 LLM 输出(若存在且非空),否则回退原始输入。
  496. """
  497. if isinstance(from_llm, list) and from_llm:
  498. return from_llm
  499. return original
  500. def _build_references(self, papers_records: list[dict]) -> list[dict]:
  501. """从 papers_records 抽取参考文献溯源列表
  502. 每条含 {title, path, sections},sections 汇总该论文所有字段的 source_sections,
  503. 保证可追溯至具体章节。
  504. """
  505. references: list[dict] = []
  506. for rec in papers_records:
  507. if not isinstance(rec, dict):
  508. continue
  509. title = rec.get("title", "未知标题")
  510. path = rec.get("path", "")
  511. sections: list[str] = []
  512. # 遍历所有 FieldSource 字段,收集 source_sections
  513. for key, val in rec.items():
  514. if isinstance(val, dict) and "source_sections" in val:
  515. src = val.get("source_sections", []) or []
  516. for s in src:
  517. if s and s not in sections:
  518. sections.append(s)
  519. references.append(
  520. {"title": title, "path": path, "sections": sections}
  521. )
  522. return references