| 1 | from graphrag_sdk.agents import Agent |
| 2 | from graphrag_sdk.models import GenerativeModelChatSession |
| 3 | from .execution_plan import ExecutionPlan |
| 4 | from .step import StepResult, PlanStep, StepBlockType |
| 5 | from .orchestrator_decision import OrchestratorDecision, OrchestratorDecisionCode |
| 6 | from graphrag_sdk.fixtures.prompts import ORCHESTRATOR_DECISION_PROMPT |
| 7 | from graphrag_sdk.helpers import extract_json |
| 8 | import logging |
| 9 | |
| 10 | logger = logging.getLogger(__name__) |
| 11 | |
| 12 | |
| 13 | class OrchestratorResult(StepResult): |
| 14 | |
| 15 | def __init__(self, output: str): |
| 16 | self._output = output |
| 17 | |
| 18 | def to_json(self) -> dict: |
| 19 | return { |
| 20 | "output": self._output, |
| 21 | } |
| 22 | |
| 23 | @staticmethod |
| 24 | def from_json(json: dict) -> "OrchestratorResult": |
| 25 | return OrchestratorResult( |
| 26 | json["output"], |
| 27 | ) |
| 28 | |
| 29 | def __str__(self) -> str: |
| 30 | return f"OrchestratorResult(output={self._output})" |
| 31 | |
| 32 | def __repr__(self) -> str: |
| 33 | return str(self) |
| 34 | |
| 35 | @property |
| 36 | def output(self) -> str: |
| 37 | return self._output |
| 38 | |
| 39 | |
| 40 | class OrchestratorRunner: |
| 41 | _runner_log: list[tuple[PlanStep, StepResult]] = [] |
| 42 | _agent_sessions: dict[str, GenerativeModelChatSession] = {} |
| 43 | |
| 44 | def __init__( |
| 45 | self, |
| 46 | chat: GenerativeModelChatSession, |
| 47 | agents: list[Agent], |
| 48 | plan: ExecutionPlan, |
| 49 | user_question: str = "", |
| 50 | config: dict = None, |
| 51 | ): |
| 52 | self._chat = chat |
| 53 | self._agents = agents |
| 54 | self._plan = plan |
| 55 | self._user_question = user_question |
| 56 | self._config = config or {"parallel_max_workers": 16} |
| 57 | self._runner_log = [] |
| 58 | self._agent_sessions = {} |
| 59 | |
| 60 | @property |
| 61 | def plan(self) -> ExecutionPlan: |
| 62 | return self._plan |
| 63 | |
| 64 | @property |
| 65 | def chat(self) -> GenerativeModelChatSession: |
| 66 | return self._chat |
| 67 | |
| 68 | @property |
| 69 | def runner_log(self) -> list[tuple[PlanStep, StepResult]]: |
| 70 | return self._runner_log |
| 71 | |
| 72 | @property |
| 73 | def user_question(self) -> str: |
| 74 | return self._user_question |
| 75 | |
| 76 | def get_agent(self, agent_id: str) -> Agent: |
| 77 | return next(agent for agent in self._agents if agent.agent_id == agent_id) |
| 78 | |
| 79 | def get_session(self, session_id: str) -> GenerativeModelChatSession | None: |
| 80 | return self._agent_sessions.get(session_id, None) |
| 81 | |
| 82 | def set_session(self, session_id: str, session: GenerativeModelChatSession): |
| 83 | self._agent_sessions[session_id] = session |
| 84 | |
| 85 | def get_user_input(self, question: str) -> str: |
| 86 | return input(question) |
| 87 | |
| 88 | def run(self) -> OrchestratorResult: |
| 89 | |
| 90 | first_step = self._plan.steps[0] if len(self._plan.steps) > 0 else None |
| 91 | |
| 92 | if first_step is None: |
| 93 | return OrchestratorResult("No steps to run") |
| 94 | |
| 95 | first_step_result = first_step.run(self, self._config) |
| 96 | |
| 97 | self._runner_log.append((first_step, first_step_result)) |
| 98 | |
| 99 | loop_response = self._run_loop(self._plan.steps[1:]) |
| 100 | |
| 101 | logger.info(f"Execution log: {self._runner_log}") |
| 102 | logger.info(f"Execution result: {loop_response}") |
| 103 | |
| 104 | return loop_response |
| 105 | |
| 106 | def _run_loop(self, steps: list[PlanStep]) -> StepResult: |
| 107 | decision = self._get_orchestrator_decision( |
| 108 | steps[0] if len(steps) > 0 else None, |
| 109 | ) |
| 110 | |
| 111 | if decision.code == OrchestratorDecisionCode.END: |
| 112 | return self._handle_end_decision() |
| 113 | |
| 114 | if decision.code == OrchestratorDecisionCode.CONTINUE: |
| 115 | return self._handle_continue_decision(steps) |
| 116 | |
| 117 | if decision.code == OrchestratorDecisionCode.UPDATE_STEP: |
| 118 | return self._handle_update_step_decision(decision.new_step) |
| 119 | |
| 120 | def _handle_end_decision(self) -> StepResult: |
| 121 | last_step = self._runner_log[-1][0] if len(self._runner_log) > 0 else None |
| 122 | if last_step is None: |
| 123 | return OrchestratorResult("No steps to run") |
| 124 | if last_step.block != StepBlockType.SUMMARY: |
| 125 | return self._call_summary_step() |
| 126 | last_result = self._runner_log[-1][1] if len(self._runner_log) > 0 else None |
| 127 | |
| 128 | return ( |
| 129 | OrchestratorResult(last_result.output) |
| 130 | if last_result |
| 131 | else OrchestratorResult("No steps to run") |
| 132 | ) |
| 133 | |
| 134 | def _handle_continue_decision(self, steps: list[PlanStep]) -> StepResult: |
| 135 | if len(steps) == 0: |
| 136 | return self._handle_end_decision() |
| 137 | |
| 138 | next_step = steps[0] |
| 139 | next_step_result = next_step.run(self, self._config) |
| 140 | self._runner_log.append((next_step, next_step_result)) |
| 141 | return self._run_loop(steps[1:]) |
| 142 | |
| 143 | def _handle_update_step_decision(self, new_step: PlanStep) -> StepResult: |
| 144 | next_step_result = new_step.run(self, self._config) |
| 145 | self._runner_log.append((new_step, next_step_result)) |
| 146 | return self._run_loop([]) |
| 147 | |
| 148 | def _call_summary_step(self): |
| 149 | return self._run_loop( |
| 150 | [ |
| 151 | PlanStep.from_json( |
| 152 | { |
| 153 | "block": StepBlockType.SUMMARY, |
| 154 | "id": "summary", |
| 155 | "properties": {}, |
| 156 | } |
| 157 | ) |
| 158 | ] |
| 159 | ) |
| 160 | |
| 161 | def _get_orchestrator_decision( |
| 162 | self, |
| 163 | next_step: PlanStep | None = None, |
| 164 | ) -> OrchestratorDecision: |
| 165 | |
| 166 | response = self.chat.send_message( |
| 167 | ORCHESTRATOR_DECISION_PROMPT.replace( |
| 168 | "#LOG_HISTORY", |
| 169 | str(self._runner_log), |
| 170 | ).replace( |
| 171 | "#NEXT_STEP", |
| 172 | str(next_step), |
| 173 | ) |
| 174 | ) |
| 175 | |
| 176 | logger.debug(f"Orchestrator decision response: {response.text}") |
| 177 | |
| 178 | return OrchestratorDecision.from_json(extract_json(response.text)) |