analyze_gaps.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. """步骤4 跨论文 Gap 识别工具:聚合结构化字段,调用强推理模型识别四类研究空白"""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Optional
  5. from backend.llm.client import DeepSeekClient
  6. from backend.llm.json_utils import parse_json_safe
  7. from backend.tools.base import Tool
  8. logger = logging.getLogger(__name__)
  9. # 模块级懒加载单例
  10. _client: Optional[DeepSeekClient] = None
  11. def get_client() -> DeepSeekClient:
  12. """懒加载 DeepSeek 客户端单例"""
  13. global _client
  14. if _client is None:
  15. _client = DeepSeekClient()
  16. return _client
  17. def _build_comparison_matrix(papers_records: list[dict]) -> str:
  18. """将多篇论文的结构化字段按维度聚合成对比矩阵文本
  19. 每条记录是 PaperRecord dict,字段为 FieldSource 结构 {value, source_sections, confidence}
  20. """
  21. # 字段中文名映射
  22. field_labels = {
  23. "task_problem": "研究任务/问题",
  24. "method": "核心方法",
  25. "key_contributions": "主要贡献",
  26. "datasets": "数据集",
  27. "metrics": "评估指标",
  28. "results": "实验结果",
  29. "limitations": "局限性",
  30. "future_work": "未来工作",
  31. }
  32. lines: list[str] = []
  33. for idx, rec in enumerate(papers_records, 1):
  34. title = rec.get("title", f"论文{idx}")
  35. lines.append(f"===== 论文 {idx}: {title} =====")
  36. for field, label in field_labels.items():
  37. val = rec.get(field)
  38. if val is None:
  39. continue
  40. # FieldSource 结构:{value, source_sections, confidence}
  41. if isinstance(val, dict):
  42. text = val.get("value", "")
  43. else:
  44. text = str(val)
  45. if text:
  46. lines.append(f"[{label}] {text}")
  47. lines.append("")
  48. return "\n".join(lines)
  49. async def _enrich_with_hybrid_search(gap: dict) -> str:
  50. """对单个 Gap 调用混合搜索检索原文细节,补充论证
  51. 懒导入 hybrid_search,避免模块加载期依赖未就绪的模块。
  52. """
  53. query = gap.get("description", "")
  54. if not query:
  55. return gap.get("evidence", "")
  56. try:
  57. # 懒导入,避免模块加载期依赖
  58. from backend.tools.hybrid_search_tool import hybrid_search
  59. result = await hybrid_search(query)
  60. snippets: list[str] = []
  61. for r in result.get("results", [])[:3]:
  62. title = r.get("title", "")
  63. section = r.get("section", "")
  64. text = r.get("text", "")[:200]
  65. snippets.append(f" - 《{title}》{section}: {text}")
  66. if snippets:
  67. extra = "\n[原文佐证]\n" + "\n".join(snippets)
  68. return gap.get("evidence", "") + extra
  69. except Exception as exc:
  70. logger.warning("hybrid_search 增强 Gap 论证失败: %s", exc)
  71. return gap.get("evidence", "")
  72. async def analyze_gaps(papers_records: list[dict]) -> dict:
  73. """跨论文 Gap 识别
  74. 输入:papers.jsonl 的记录列表(每条是 PaperRecord dict)
  75. 输出:{"gaps": [{description, gap_type, source_papers, evidence, confidence}]}
  76. """
  77. if not papers_records:
  78. return {"gaps": []}
  79. # 1. 聚合对比矩阵
  80. matrix = _build_comparison_matrix(papers_records)
  81. # 2. 构造强推理提示词
  82. n_papers = len(papers_records)
  83. if n_papers == 1:
  84. single_paper_hint = (
  85. f"\n## 重要提示\n当前仅 1 篇论文。请基于该论文自身的 limitations(局限性)和 future_work(未来工作),"
  86. "识别可深化的研究空白。source_papers 只需包含这 1 篇论文即可。"
  87. "至少识别 2 个有价值的 Gap(基于论文内部字段证据,不要求跨论文对比)。"
  88. )
  89. source_constraint = "source_papers 至少包含 1 篇论文。"
  90. else:
  91. single_paper_hint = ""
  92. source_constraint = "每个 Gap 的 source_papers 至少包含 2 篇论文。"
  93. prompt = f"""你是一位资深学术研究者,需要从以下多篇论文的结构化对比中识别研究空白(Gap)。
  94. ## 论文对比矩阵
  95. {matrix}
  96. {single_paper_hint}
  97. ## 任务
  98. 请综合分析上述论文,识别以下四类 Gap 信号:
  99. 1. repeated_limitation(重复局限):多篇论文提到同一局限性,但无人解决
  100. 2. method_gap(方法空白):某类问题只有少数方法尝试且效果不佳
  101. 3. contradictory(矛盾结论):不同论文对同一问题给出冲突结论
  102. 4. unfulfilled_future(未兑现的未来工作):多篇论文指向同一未来方向但无人完成
  103. 对于单篇论文场景,重点关注该论文自身的 limitations 与 future_work 字段,
  104. 识别方法层面、数据层面、评估层面的可深化方向。
  105. ## 输出要求
  106. 严格输出 JSON,不要包含任何额外说明文本或代码块标记。格式如下:
  107. {{
  108. "gaps": [
  109. {{
  110. "description": "Gap 的清晰描述",
  111. "gap_type": "repeated_limitation|method_gap|contradictory|unfulfilled_future",
  112. "source_papers": ["来源论文标题1"],
  113. "evidence": "论证依据,引用具体论文的具体字段",
  114. "confidence": 0.0到1.0之间的置信度
  115. }}
  116. ]
  117. }}
  118. 只输出真正有证据支撑的 Gap,不要编造。{source_constraint}"""
  119. messages = [
  120. {
  121. "role": "system",
  122. "content": "你是一位严谨的学术研究分析专家,擅长跨论文综合推理与 Gap 识别。只输出严格 JSON,不要输出任何其他内容。",
  123. },
  124. {"role": "user", "content": prompt},
  125. ]
  126. client = get_client()
  127. response = await client.chat(messages=messages)
  128. content = response.choices[0].message.content or ""
  129. data = parse_json_safe(content)
  130. if not data or not isinstance(data, dict):
  131. logger.warning("Gap 识别 JSON 解析失败,原始内容: %s", content[:200])
  132. return {"gaps": []}
  133. gaps = data.get("gaps", [])
  134. if not isinstance(gaps, list):
  135. gaps = []
  136. logger.info(
  137. "[analyze_gaps] LLM 返回 %d gaps (papers=%d, content_len=%d)",
  138. len(gaps), len(papers_records), len(content),
  139. )
  140. if len(gaps) == 0:
  141. logger.warning("[analyze_gaps] Gap 数量为 0!原始内容前 500 字: %s", content[:500])
  142. # 3. 对每个 Gap 调用混合搜索补充原文佐证
  143. enriched: list[dict] = []
  144. for gap in gaps:
  145. if not isinstance(gap, dict):
  146. continue
  147. # 规范化字段
  148. normalized = {
  149. "description": gap.get("description", ""),
  150. "gap_type": gap.get("gap_type", "method_gap"),
  151. "source_papers": gap.get("source_papers", []),
  152. "evidence": gap.get("evidence", ""),
  153. "confidence": float(gap.get("confidence", 0.5)),
  154. }
  155. # 调用混合搜索深化论证
  156. normalized["evidence"] = await _enrich_with_hybrid_search(normalized)
  157. enriched.append(normalized)
  158. return {"gaps": enriched}
  159. # 工具参数 schema
  160. analyze_gaps_parameters = {
  161. "type": "object",
  162. "properties": {
  163. "papers_records": {
  164. "type": "array",
  165. "items": {"type": "object"},
  166. "description": "papers.jsonl 的记录列表,每条是 PaperRecord dict",
  167. }
  168. },
  169. "required": ["papers_records"],
  170. }
  171. # 工具实例
  172. analyze_gaps_tool = Tool(
  173. name="analyze_gaps",
  174. description=(
  175. "步骤4:跨论文 Gap 识别。将多篇论文结构化字段聚合成对比矩阵,"
  176. "调用强推理模型识别四类研究空白(重复局限/方法空白/矛盾结论/未兑现未来工作),"
  177. "需要时调用混合搜索检索原文细节佐证。"
  178. ),
  179. parameters=analyze_gaps_parameters,
  180. func=analyze_gaps,
  181. )