generate_innovations.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. """步骤5 创新生成与筛选工具:针对 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. async def _novelty_check(title: str, idea: str) -> str:
  18. """调用联网搜索做新颖性去重,检索近 1-2 年相关工作
  19. 懒导入 web_search,避免模块加载期依赖未就绪的模块。
  20. """
  21. query = f"{title} {idea[:100]}"
  22. try:
  23. # 懒导入
  24. from backend.tools.web_search import web_search
  25. result = await web_search(query)
  26. items = result.get("results", [])[:5]
  27. if not items:
  28. return "未检索到高度相关工作,新颖性较高"
  29. lines: list[str] = []
  30. for r in items:
  31. t = r.get("title", "")
  32. snippet = r.get("snippet", "")[:120]
  33. url = r.get("url", "")
  34. lines.append(f" - {t}: {snippet} ({url})")
  35. return "近 1-2 年相关工作:\n" + "\n".join(lines)
  36. except Exception as exc:
  37. logger.warning("web_search 新颖性检查失败: %s", exc)
  38. return "新颖性检查失败(联网搜索不可用)"
  39. async def generate_innovations(
  40. gaps: list[dict],
  41. research_direction: Optional[str] = None,
  42. ) -> dict:
  43. """针对 Gap 生成候选创新方向,三维评分筛选,取综合得分最高的 2-3 个
  44. 输入:Gap 列表 [{description, gap_type, source_papers, evidence, confidence}]
  45. 输出:{"innovations": [{title, idea, source, gap_origin, score, novelty_check, supporting_evidence}]}
  46. """
  47. if not gaps:
  48. return {"innovations": []}
  49. # 1. 汇总 Gap 信息
  50. gaps_text = "\n".join(
  51. f"- [{g.get('gap_type', 'unknown')}] {g.get('description', '')}"
  52. f"(来源: {', '.join(g.get('source_papers', []))})"
  53. for g in gaps
  54. )
  55. # 1.1 研究方向约束文本
  56. direction_text = ""
  57. if research_direction and research_direction.strip():
  58. direction_text = (
  59. f"\n## 用户研究方向约束\n"
  60. f"用户的研究方向为:{research_direction.strip()}\n"
  61. f"生成的创新点应尽量与该方向相关,"
  62. f"并在 supporting_evidence 中说明创新点与该方向的关联。\n"
  63. )
  64. # 2. 构造创新生成提示词
  65. prompt = f"""你是一位富有创造力的学术研究者,需要基于以下研究空白(Gap)生成候选创新方向。
  66. {direction_text}
  67. ## 研究空白列表
  68. {gaps_text}
  69. ## 任务
  70. 针对每个 Gap,从以下四类来源生成候选创新方向:
  71. 1. method_combination(方法组合):把 A 论文方法迁移到 B 论文问题
  72. 2. limitation_fix(局限改进):直接解决被反复提及的局限
  73. 3. cross_domain(跨域迁移):引入其他领域方法
  74. 4. new_scenario(新场景应用):把现有方法用到未被覆盖的数据/场景
  75. 对每个候选创新方向,用三维评分(0-10 分):
  76. - novelty(新颖性)
  77. - feasibility(可行性)
  78. - significance(显著性)
  79. ## 输出要求
  80. 严格输出 JSON,不要包含任何额外说明文本或代码块标记。格式如下:
  81. {{
  82. "innovations": [
  83. {{
  84. "title": "创新点标题",
  85. "idea": "创新思路详细描述",
  86. "source": "method_combination|limitation_fix|cross_domain|new_scenario",
  87. "gap_origin": "对应的 Gap 描述",
  88. "score": {{
  89. "novelty": 0.0到10.0,
  90. "feasibility": 0.0到10.0,
  91. "significance": 0.0到10.0
  92. }},
  93. "supporting_evidence": "论据支撑"
  94. }}
  95. ]
  96. }}
  97. 生成多个候选(每个 Gap 至少 1-2 个),按综合得分(三项之和)从高到低排序。"""
  98. messages = [
  99. {
  100. "role": "system",
  101. "content": "你是一位学术创新生成专家,擅长从研究空白中挖掘可执行的创新方向。只输出严格 JSON,不要输出任何其他内容。",
  102. },
  103. {"role": "user", "content": prompt},
  104. ]
  105. client = get_client()
  106. response = await client.chat(messages=messages)
  107. content = response.choices[0].message.content or ""
  108. data = parse_json_safe(content)
  109. if not data or not isinstance(data, dict):
  110. logger.warning("创新生成 JSON 解析失败: %s", content[:200])
  111. return {"innovations": []}
  112. innovations = data.get("innovations", [])
  113. if not isinstance(innovations, list):
  114. innovations = []
  115. # 3. 规范化 + 计算综合得分排序
  116. normalized: list[dict] = []
  117. for inv in innovations:
  118. if not isinstance(inv, dict):
  119. continue
  120. score = inv.get("score", {})
  121. if not isinstance(score, dict):
  122. score = {}
  123. novelty = float(score.get("novelty", 0.0))
  124. feasibility = float(score.get("feasibility", 0.0))
  125. significance = float(score.get("significance", 0.0))
  126. normalized.append(
  127. {
  128. "title": inv.get("title", ""),
  129. "idea": inv.get("idea", ""),
  130. "source": inv.get("source", "limitation_fix"),
  131. "gap_origin": inv.get("gap_origin", ""),
  132. "score": {
  133. "novelty": novelty,
  134. "feasibility": feasibility,
  135. "significance": significance,
  136. },
  137. "_total": novelty + feasibility + significance,
  138. "novelty_check": None,
  139. "supporting_evidence": inv.get("supporting_evidence", ""),
  140. }
  141. )
  142. # 4. 按综合得分降序,取前 2-3 个
  143. normalized.sort(key=lambda x: x["_total"], reverse=True)
  144. top_count = 3 if len(normalized) >= 3 else len(normalized)
  145. top = normalized[:top_count]
  146. # 5. 对每个创新点做联网新颖性去重
  147. for inv in top:
  148. inv["novelty_check"] = await _novelty_check(inv["title"], inv["idea"])
  149. inv.pop("_total", None)
  150. return {"innovations": top}
  151. # 工具参数 schema
  152. generate_innovations_parameters = {
  153. "type": "object",
  154. "properties": {
  155. "gaps": {
  156. "type": "array",
  157. "items": {"type": "object"},
  158. "description": "Gap 列表,每条含 description/gap_type/source_papers/evidence/confidence",
  159. },
  160. "research_direction": {
  161. "type": "string",
  162. "description": "用户的研究方向(可选),生成的创新点应尽量与该方向相关",
  163. },
  164. },
  165. "required": ["gaps"],
  166. }
  167. # 工具实例
  168. generate_innovations_tool = Tool(
  169. name="generate_innovations",
  170. description=(
  171. "步骤5:创新生成与筛选。针对每个 Gap 从四类来源(方法组合/局限改进/跨域迁移/新场景应用)"
  172. "生成候选创新方向,三维评分筛选取综合得分最高的 2-3 个,"
  173. "并联网检索近 1-2 年相关工作做新颖性去重。"
  174. ),
  175. parameters=generate_innovations_parameters,
  176. func=generate_innovations,
  177. )