assembler.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """论文汇总服务"""
  2. from typing import Callable, List
  3. from hello_agents import ToolAwareSimpleAgent
  4. from config import Configuration
  5. from models import PaperSection, PaperState
  6. from utils import strip_thinking_tokens, strip_tool_calls
  7. class ReportAssemblyService:
  8. """调用汇总 Agent 生成最终论文"""
  9. def __init__(
  10. self,
  11. agent_factory: Callable[[], ToolAwareSimpleAgent],
  12. config: Configuration,
  13. ):
  14. self._agent_factory = agent_factory
  15. self._config = config
  16. def assemble(
  17. self,
  18. state: PaperState,
  19. sections: List[PaperSection],
  20. review_notes: str,
  21. ) -> str:
  22. section_blocks = []
  23. for s in sections:
  24. section_blocks.append(f"## {s.title}\n\n{s.content or '(暂无内容)'}\n")
  25. all_refs = [r for s in sections for r in s.references]
  26. ref_lines = [
  27. f"[{i}] {ref.get('citation') or ref.get('title', '')}"
  28. for i, ref in enumerate(all_refs, start=1)
  29. ]
  30. prompt = (
  31. f"论文题目:{state.paper_title or state.research_topic}\n"
  32. f"研究主题:{state.research_topic}\n"
  33. f"引用格式:{state.citation_style}\n\n"
  34. f"章节草稿:\n{''.join(section_blocks)}\n\n"
  35. f"参考文献条目:\n" + "\n".join(ref_lines) + "\n\n"
  36. f"审校意见:\n{review_notes}\n\n"
  37. f"请整合为一份完整论文(标题、摘要、正文、参考文献)。"
  38. )
  39. agent = self._agent_factory()
  40. try:
  41. response = agent.run(prompt)
  42. finally:
  43. agent.clear_history()
  44. paper = response.strip()
  45. if self._config.strip_thinking_tokens:
  46. paper = strip_thinking_tokens(paper)
  47. return strip_tool_calls(paper).strip()