|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from collections.abc import Sequence |
| 5 | + |
| 6 | +from temporalio import workflow |
| 7 | + |
| 8 | +from agents import RunConfig, Runner, RunResult, custom_span, trace |
| 9 | + |
| 10 | +from openai_agents.financial_research_agent.agents.financials_agent import ( |
| 11 | + new_financials_agent, |
| 12 | +) |
| 13 | +from openai_agents.financial_research_agent.agents.planner_agent import ( |
| 14 | + FinancialSearchItem, |
| 15 | + FinancialSearchPlan, |
| 16 | + new_planner_agent, |
| 17 | +) |
| 18 | +from openai_agents.financial_research_agent.agents.risk_agent import new_risk_agent |
| 19 | +from openai_agents.financial_research_agent.agents.search_agent import ( |
| 20 | + new_search_agent, |
| 21 | +) |
| 22 | +from openai_agents.financial_research_agent.agents.verifier_agent import ( |
| 23 | + VerificationResult, |
| 24 | + new_verifier_agent, |
| 25 | +) |
| 26 | +from openai_agents.financial_research_agent.agents.writer_agent import ( |
| 27 | + FinancialReportData, |
| 28 | + new_writer_agent, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +async def _summary_extractor(run_result: RunResult) -> str: |
| 33 | + """Custom output extractor for sub-agents that return an AnalysisSummary.""" |
| 34 | + # The financial/risk analyst agents emit an AnalysisSummary with a `summary` field. |
| 35 | + # We want the tool call to return just that summary text so the writer can drop it inline. |
| 36 | + return str(run_result.final_output.summary) |
| 37 | + |
| 38 | + |
| 39 | +class FinancialResearchManager: |
| 40 | + """ |
| 41 | + Orchestrates the full flow: planning, searching, sub-analysis, writing, and verification. |
| 42 | + """ |
| 43 | + |
| 44 | + def __init__(self) -> None: |
| 45 | + self.run_config = RunConfig() |
| 46 | + self.planner_agent = new_planner_agent() |
| 47 | + self.search_agent = new_search_agent() |
| 48 | + self.financials_agent = new_financials_agent() |
| 49 | + self.risk_agent = new_risk_agent() |
| 50 | + self.writer_agent = new_writer_agent() |
| 51 | + self.verifier_agent = new_verifier_agent() |
| 52 | + |
| 53 | + async def run(self, query: str) -> str: |
| 54 | + with trace("Financial research trace"): |
| 55 | + search_plan = await self._plan_searches(query) |
| 56 | + search_results = await self._perform_searches(search_plan) |
| 57 | + report = await self._write_report(query, search_results) |
| 58 | + verification = await self._verify_report(report) |
| 59 | + |
| 60 | + # Return formatted output |
| 61 | + result = f"""=====REPORT===== |
| 62 | +
|
| 63 | +{report.markdown_report} |
| 64 | +
|
| 65 | +=====FOLLOW UP QUESTIONS===== |
| 66 | +
|
| 67 | +{chr(10).join(report.follow_up_questions)} |
| 68 | +
|
| 69 | +=====VERIFICATION===== |
| 70 | +
|
| 71 | +Verified: {verification.verified} |
| 72 | +Issues: {verification.issues}""" |
| 73 | + |
| 74 | + return result |
| 75 | + |
| 76 | + async def _plan_searches(self, query: str) -> FinancialSearchPlan: |
| 77 | + result = await Runner.run( |
| 78 | + self.planner_agent, |
| 79 | + f"Query: {query}", |
| 80 | + run_config=self.run_config, |
| 81 | + ) |
| 82 | + return result.final_output_as(FinancialSearchPlan) |
| 83 | + |
| 84 | + async def _perform_searches( |
| 85 | + self, search_plan: FinancialSearchPlan |
| 86 | + ) -> Sequence[str]: |
| 87 | + with custom_span("Search the web"): |
| 88 | + tasks = [ |
| 89 | + asyncio.create_task(self._search(item)) for item in search_plan.searches |
| 90 | + ] |
| 91 | + results: list[str] = [] |
| 92 | + for task in workflow.as_completed(tasks): |
| 93 | + result = await task |
| 94 | + if result is not None: |
| 95 | + results.append(result) |
| 96 | + return results |
| 97 | + |
| 98 | + async def _search(self, item: FinancialSearchItem) -> str | None: |
| 99 | + input_data = f"Search term: {item.query}\nReason: {item.reason}" |
| 100 | + try: |
| 101 | + result = await Runner.run( |
| 102 | + self.search_agent, |
| 103 | + input_data, |
| 104 | + run_config=self.run_config, |
| 105 | + ) |
| 106 | + return str(result.final_output) |
| 107 | + except Exception: |
| 108 | + return None |
| 109 | + |
| 110 | + async def _write_report( |
| 111 | + self, query: str, search_results: Sequence[str] |
| 112 | + ) -> FinancialReportData: |
| 113 | + # Expose the specialist analysts as tools so the writer can invoke them inline |
| 114 | + # and still produce the final FinancialReportData output. |
| 115 | + fundamentals_tool = self.financials_agent.as_tool( |
| 116 | + tool_name="fundamentals_analysis", |
| 117 | + tool_description="Use to get a short write-up of key financial metrics", |
| 118 | + custom_output_extractor=_summary_extractor, |
| 119 | + ) |
| 120 | + risk_tool = self.risk_agent.as_tool( |
| 121 | + tool_name="risk_analysis", |
| 122 | + tool_description="Use to get a short write-up of potential red flags", |
| 123 | + custom_output_extractor=_summary_extractor, |
| 124 | + ) |
| 125 | + writer_with_tools = self.writer_agent.clone( |
| 126 | + tools=[fundamentals_tool, risk_tool] |
| 127 | + ) |
| 128 | + |
| 129 | + input_data = ( |
| 130 | + f"Original query: {query}\nSummarized search results: {search_results}" |
| 131 | + ) |
| 132 | + result = await Runner.run( |
| 133 | + writer_with_tools, |
| 134 | + input_data, |
| 135 | + run_config=self.run_config, |
| 136 | + ) |
| 137 | + return result.final_output_as(FinancialReportData) |
| 138 | + |
| 139 | + async def _verify_report(self, report: FinancialReportData) -> VerificationResult: |
| 140 | + result = await Runner.run( |
| 141 | + self.verifier_agent, |
| 142 | + report.markdown_report, |
| 143 | + run_config=self.run_config, |
| 144 | + ) |
| 145 | + return result.final_output_as(VerificationResult) |
0 commit comments