schemas.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """共享数据模型:Pydantic schemas,跨模块复用"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from enum import Enum
  5. from typing import Any, Optional
  6. from pydantic import BaseModel, Field
  7. # ---------- 步骤1:PDF 解析 ----------
  8. class Section(BaseModel):
  9. """论文章节"""
  10. heading: str = Field(description="章节标题")
  11. content: str = Field(description="章节正文")
  12. figure_captions: list[str] = Field(default_factory=list, description="章节内图表标题")
  13. class ParsedPaper(BaseModel):
  14. """步骤1 输出:解析后的论文"""
  15. path: str
  16. title: str
  17. sections: list[Section]
  18. parsed_at: datetime = Field(default_factory=datetime.now)
  19. # ---------- 步骤2:结构化抽取 ----------
  20. class FieldSource(BaseModel):
  21. """字段来源标注,防编造、可回溯"""
  22. value: str
  23. source_sections: list[str] = Field(description="原文出处章节")
  24. confidence: float = Field(ge=0.0, le=1.0, default=1.0)
  25. class PaperRecord(BaseModel):
  26. """步骤2 输出:单篇论文结构化记录(papers.jsonl 一行一条)"""
  27. path: str
  28. title: str
  29. task_problem: FieldSource = Field(description="研究任务/问题")
  30. method: FieldSource = Field(description="核心方法")
  31. key_contributions: FieldSource = Field(description="主要贡献")
  32. datasets: FieldSource = Field(description="使用的数据集")
  33. metrics: FieldSource = Field(description="评估指标")
  34. results: FieldSource = Field(description="实验结果")
  35. limitations: FieldSource = Field(description="局限性(重点字段,须论文明确承认或可推断)")
  36. future_work: FieldSource = Field(description="未来工作")
  37. extracted_at: datetime = Field(default_factory=datetime.now)
  38. # ---------- 步骤4:Gap 识别 ----------
  39. class GapType(str, Enum):
  40. REPEATED_LIMITATION = "repeated_limitation" # 重复局限
  41. METHOD_GAP = "method_gap" # 方法空白
  42. CONTRADICTORY = "contradictory" # 矛盾结论
  43. UNFULFILLED_FUTURE = "unfulfilled_future" # 未兑现的未来工作
  44. class Gap(BaseModel):
  45. """候选研究空白"""
  46. description: str
  47. gap_type: GapType
  48. source_papers: list[str] = Field(description="来源论文标题列表")
  49. evidence: str = Field(description="论证")
  50. confidence: float = Field(ge=0.0, le=1.0, default=1.0)
  51. # ---------- 步骤5:创新点 ----------
  52. class InnovationSource(str, Enum):
  53. METHOD_COMBINATION = "method_combination" # 方法组合
  54. LIMITATION_FIX = "limitation_fix" # 局限改进
  55. CROSS_DOMAIN = "cross_domain" # 跨域迁移
  56. NEW_SCENARIO = "new_scenario" # 新场景应用
  57. class ThreeDScore(BaseModel):
  58. """三维评分:新颖性/可行性/显著性"""
  59. novelty: float = Field(ge=0.0, le=10.0)
  60. feasibility: float = Field(ge=0.0, le=10.0)
  61. significance: float = Field(ge=0.0, le=10.0)
  62. @property
  63. def total(self) -> float:
  64. return round(self.novelty + self.feasibility + self.significance, 2)
  65. class Innovation(BaseModel):
  66. """创新点"""
  67. title: str
  68. idea: str = Field(description="创新思路")
  69. source: InnovationSource
  70. gap_origin: str = Field(description="Gap 来源描述")
  71. score: ThreeDScore
  72. novelty_check: Optional[str] = Field(default=None, description="联网新颖性验证结果")
  73. supporting_evidence: str = Field(default="", description="论据支撑")
  74. # ---------- 步骤6:实验方案 ----------
  75. class ExperimentPlan(BaseModel):
  76. """实验方案"""
  77. innovation_title: str
  78. hypothesis: str = Field(description="研究假设")
  79. datasets: list[str]
  80. baselines: list[str] = Field(description="基线方法")
  81. metrics: list[str]
  82. ablation: list[str] = Field(description="消融实验设计")
  83. steps: list[str] = Field(description="实验步骤")
  84. expected_results: str
  85. risks: str
  86. # ---------- 评估 ----------
  87. class ToolCallRecord(BaseModel):
  88. """工具调用记录"""
  89. tool_name: str
  90. args_summary: str
  91. success: bool
  92. duration_sec: float
  93. error: Optional[str] = None
  94. # 工具返回的结构化结果(dict/list/str),供 ReportAgent 整合使用
  95. result: Optional[Any] = None
  96. timestamp: datetime = Field(default_factory=datetime.now)
  97. class EvaluationReport(BaseModel):
  98. """评估报告"""
  99. innovations_scores: list[ThreeDScore]
  100. tool_call_success_rate: float = Field(description="工具调用成功率 0-1")
  101. total_tool_calls: int
  102. failed_tool_calls: int
  103. avg_confidence: float = Field(description="平均自校验置信度")
  104. tool_calls: list[ToolCallRecord]
  105. # ---------- 最终报告 ----------
  106. class FinalReport(BaseModel):
  107. """步骤7 整合输出"""
  108. background_review: str = Field(description="背景综述")
  109. innovations: list[Innovation]
  110. experiment_plans: list[ExperimentPlan]
  111. references: list[dict] = Field(description="参考文献溯源 {title, path, sections}")
  112. evaluation: Optional[EvaluationReport] = None
  113. markdown: str = Field(default="", description="完整 Markdown 报告")
  114. generated_at: datetime = Field(default_factory=datetime.now)
  115. # ---------- 进度事件 ----------
  116. class StepStage(str, Enum):
  117. PARSE = "step1_parse"
  118. EXTRACT = "step2_extract"
  119. INDEX = "step3_index"
  120. GAP = "step4_gap"
  121. INNOVATION = "step5_innovation"
  122. EXPERIMENT = "step6_experiment"
  123. INTEGRATE = "step7_integrate"
  124. class ProgressEvent(BaseModel):
  125. """前端进度推送事件"""
  126. stage: StepStage
  127. status: str = Field(description="running/done/failed/degraded")
  128. message: str
  129. progress: float = Field(ge=0.0, le=1.0, description="整体进度 0-1")
  130. payload: Optional[dict[str, Any]] = None