specialized.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """6 个专职 Agent:每个绑定角色提示词与工具子集"""
  2. from __future__ import annotations
  3. from backend.agents.base import BaseAgent
  4. from backend.llm.client import DeepSeekClient
  5. from backend.tools.base import ToolRegistry
  6. class PaperParserAgent(BaseAgent):
  7. """步骤1:扫描文件夹并解析 PDF 为结构化文本"""
  8. def __init__(self, client: DeepSeekClient, registry: ToolRegistry):
  9. super().__init__(
  10. name="PaperParser",
  11. role=(
  12. "你是论文解析专家。你的任务是扫描本地文件夹中的 PDF 论文,"
  13. "使用 parse_pdf 工具将每篇 PDF 解析为保留章节结构的文本。"
  14. "输出格式:每篇论文含 {path, title, sections:[{heading, content, figure_captions}]}。"
  15. "若某篇解析失败,记录错误并跳过,继续处理其余论文。"
  16. "你可以自主决定调用顺序,支持并行处理多篇。"
  17. ),
  18. client=client,
  19. registry=registry,
  20. tool_names=["scan_folder", "parse_pdf"],
  21. )
  22. class ExtractorAgent(BaseAgent):
  23. """步骤2:单篇论文结构化抽取(核心主干)"""
  24. def __init__(self, client: DeepSeekClient, registry: ToolRegistry):
  25. super().__init__(
  26. name="Extractor",
  27. role=(
  28. "你是论文结构化抽取专家。你的任务是对每篇已解析论文调用 extract_paper_structure 工具,"
  29. "抽取固定 schema 的结构化记录(title/task_problem/method/key_contributions/"
  30. "datasets/metrics/results/limitations/future_work),每字段标注 source_sections 与 confidence。"
  31. "limitations 是重点字段,必须是论文明确承认或可从结果推断的,不允许臆造。"
  32. "缺失字段触发针对性补抽。可调用 hybrid_search 回溯原文细节。"
  33. "每篇结果独立缓存,失败可单独重跑。"
  34. ),
  35. client=client,
  36. registry=registry,
  37. tool_names=["extract_paper_structure", "hybrid_search"],
  38. )
  39. class IndexerAgent(BaseAgent):
  40. """步骤3:向量索引构建"""
  41. def __init__(self, client: DeepSeekClient, registry: ToolRegistry):
  42. super().__init__(
  43. name="Indexer",
  44. role=(
  45. "你是向量索引构建专家。你的任务是对已解析论文调用 build_vector_index 工具,"
  46. "将论文全文按章节分块向量化存入 Chroma 向量库,为后续混合搜索提供点查询能力。"
  47. "索引构建是增强能力,失败时可降级(跳过 RAG 仅用结构化字段)。"
  48. ),
  49. client=client,
  50. registry=registry,
  51. tool_names=["build_vector_index"],
  52. )
  53. class GapAnalyzerAgent(BaseAgent):
  54. """步骤4:跨论文综合分析与 Gap 识别"""
  55. def __init__(self, client: DeepSeekClient, registry: ToolRegistry):
  56. super().__init__(
  57. name="GapAnalyzer",
  58. role=(
  59. "你是研究空白(Gap)识别专家。你的任务是对结构化字段调用 analyze_gaps 工具,"
  60. "识别四类 Gap 信号:重复局限、方法空白、矛盾结论、未兑现的未来工作。"
  61. "需要原文细节佐证时调用 hybrid_search 检索相关段落。"
  62. "输出候选 Gap 列表,每个 Gap 标注来源论文、Gap 类型、论证与置信度。"
  63. ),
  64. client=client,
  65. registry=registry,
  66. tool_names=["analyze_gaps", "hybrid_search"],
  67. )
  68. class InnovationGeneratorAgent(BaseAgent):
  69. """步骤5:创新点生成与筛选"""
  70. def __init__(self, client: DeepSeekClient, registry: ToolRegistry):
  71. super().__init__(
  72. name="InnovationGenerator",
  73. role=(
  74. "你是创新点生成专家。你的任务是对候选 Gap 调用 generate_innovations 工具,"
  75. "生成四类来源的创新方向(方法组合/局限改进/跨域迁移/新场景应用),"
  76. "用三维评分(新颖性/可行性/显著性)筛选,取综合得分最高的 2-3 个。"
  77. "调用 web_search 检索近 1-2 年相关工作做新颖性去重。"
  78. "调用 load_memory 加载历史创新点避免重复,调用 save_memory 保存新创新点。"
  79. ),
  80. client=client,
  81. registry=registry,
  82. tool_names=["generate_innovations", "web_search", "load_memory", "save_memory"],
  83. )
  84. class ExperimentDesignerAgent(BaseAgent):
  85. """步骤6:实验方案设计"""
  86. def __init__(self, client: DeepSeekClient, registry: ToolRegistry):
  87. super().__init__(
  88. name="ExperimentDesigner",
  89. role=(
  90. "你是实验方案设计专家。你的任务是对每个创新点调用 design_experiments 工具,"
  91. "输出可执行研究方案:研究假设、数据集、基线方法、评估指标、消融实验、"
  92. "实验步骤、预期结果与风险。方案须具体可执行。"
  93. ),
  94. client=client,
  95. registry=registry,
  96. tool_names=["design_experiments"],
  97. )
  98. def build_all_specialized_agents(client: DeepSeekClient, registry: ToolRegistry) -> dict:
  99. """构建全部 6 个专职 Agent,返回 name->agent 字典"""
  100. return {
  101. "PaperParser": PaperParserAgent(client, registry),
  102. "Extractor": ExtractorAgent(client, registry),
  103. "Indexer": IndexerAgent(client, registry),
  104. "GapAnalyzer": GapAnalyzerAgent(client, registry),
  105. "InnovationGenerator": InnovationGeneratorAgent(client, registry),
  106. "ExperimentDesigner": ExperimentDesignerAgent(client, registry),
  107. }