design_experiments.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """步骤6 实验方案设计工具:为每个创新点输出可执行研究方案"""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Any, 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 _ensure_list(val: Any) -> list:
  18. """确保返回 list 类型"""
  19. if isinstance(val, list):
  20. return val
  21. if isinstance(val, str):
  22. return [val]
  23. return []
  24. async def design_experiments(innovations: list[dict]) -> dict:
  25. """为每个创新点输出可执行研究方案
  26. 输入:创新点列表 [{title, idea, source, gap_origin, score, novelty_check, supporting_evidence}]
  27. 输出:{"experiment_plans": [{innovation_title, hypothesis, datasets, baselines, metrics, ablation, steps, expected_results, risks}]}
  28. """
  29. if not innovations:
  30. return {"experiment_plans": []}
  31. # 1. 汇总创新点信息
  32. innovations_text = "\n".join(
  33. f"- {inv.get('title', '')}: {inv.get('idea', '')}"
  34. f"(来源: {inv.get('source', '')},Gap: {inv.get('gap_origin', '')})"
  35. for inv in innovations
  36. )
  37. # 2. 构造实验设计提示词
  38. prompt = f"""你是一位资深学术研究者,需要为以下创新点设计可执行的研究方案。
  39. ## 创新点列表
  40. {innovations_text}
  41. ## 任务
  42. 为每个创新点设计完整、可执行的研究方案,包含:
  43. - hypothesis(研究假设):清晰可验证的假设
  44. - datasets(数据集列表):适合该研究的公开数据集
  45. - baselines(基线方法列表):需要对比的现有方法
  46. - metrics(评估指标列表):量化评估指标
  47. - ablation(消融实验设计列表):验证各组件贡献
  48. - steps(实验步骤列表):可复现的实验流程
  49. - expected_results(预期结果):预期达到的性能或发现
  50. - risks(风险):可能的失败点与应对
  51. ## 输出要求
  52. 严格输出 JSON,不要包含任何额外说明文本或代码块标记。格式如下:
  53. {{
  54. "experiment_plans": [
  55. {{
  56. "innovation_title": "对应的创新点标题",
  57. "hypothesis": "研究假设",
  58. "datasets": ["数据集1", "数据集2"],
  59. "baselines": ["基线方法1", "基线方法2"],
  60. "metrics": ["指标1", "指标2"],
  61. "ablation": ["消融实验1", "消融实验2"],
  62. "steps": ["步骤1", "步骤2", "步骤3"],
  63. "expected_results": "预期结果描述",
  64. "risks": "风险与应对描述"
  65. }}
  66. ]
  67. }}
  68. 每个创新点对应一条方案,方案须具体可执行。datasets/baselines/metrics/ablation/steps 均为字符串数组。"""
  69. messages = [
  70. {
  71. "role": "system",
  72. "content": "你是一位学术实验设计专家,擅长设计严谨可执行的研究方案。只输出严格 JSON,不要输出任何其他内容。",
  73. },
  74. {"role": "user", "content": prompt},
  75. ]
  76. client = get_client()
  77. response = await client.chat(messages=messages)
  78. content = response.choices[0].message.content or ""
  79. data = parse_json_safe(content)
  80. if not data or not isinstance(data, dict):
  81. logger.warning("实验方案 JSON 解析失败: %s", content[:200])
  82. return {"experiment_plans": []}
  83. plans = data.get("experiment_plans", [])
  84. if not isinstance(plans, list):
  85. plans = []
  86. # 3. 规范化字段,确保列表类型
  87. normalized: list[dict] = []
  88. for plan in plans:
  89. if not isinstance(plan, dict):
  90. continue
  91. normalized.append(
  92. {
  93. "innovation_title": plan.get("innovation_title", ""),
  94. "hypothesis": plan.get("hypothesis", ""),
  95. "datasets": _ensure_list(plan.get("datasets", [])),
  96. "baselines": _ensure_list(plan.get("baselines", [])),
  97. "metrics": _ensure_list(plan.get("metrics", [])),
  98. "ablation": _ensure_list(plan.get("ablation", [])),
  99. "steps": _ensure_list(plan.get("steps", [])),
  100. "expected_results": plan.get("expected_results", ""),
  101. "risks": plan.get("risks", ""),
  102. }
  103. )
  104. return {"experiment_plans": normalized}
  105. # 工具参数 schema
  106. design_experiments_parameters = {
  107. "type": "object",
  108. "properties": {
  109. "innovations": {
  110. "type": "array",
  111. "items": {"type": "object"},
  112. "description": "创新点列表,每条含 title/idea/source/gap_origin/score/novelty_check/supporting_evidence",
  113. }
  114. },
  115. "required": ["innovations"],
  116. }
  117. # 工具实例
  118. design_experiments_tool = Tool(
  119. name="design_experiments",
  120. description=(
  121. "步骤6:实验方案设计。为每个创新点输出可执行研究方案,"
  122. "包含研究假设、数据集、基线方法、评估指标、消融实验、实验步骤、预期结果与风险。"
  123. ),
  124. parameters=design_experiments_parameters,
  125. func=design_experiments,
  126. )