| 1 | import graphrag_sdk.orchestrator.step |
| 2 | from concurrent.futures import ThreadPoolExecutor, wait |
| 3 | from graphrag_sdk.orchestrator.step_result import StepResult |
| 4 | from graphrag_sdk.orchestrator.orchestrator_runner import OrchestratorRunner |
| 5 | |
| 6 | |
| 7 | class ParallelStepResult(StepResult): |
| 8 | results: list[StepResult] |
| 9 | |
| 10 | def __init__(self, results: list[StepResult]): |
| 11 | self.results = results |
| 12 | |
| 13 | def to_json(self) -> dict: |
| 14 | return {"results": [result.to_json() for result in self.results]} |
| 15 | |
| 16 | @staticmethod |
| 17 | def from_json(json: dict) -> "ParallelStepResult": |
| 18 | return ParallelStepResult( |
| 19 | [ |
| 20 | StepResult.from_json(result) |
| 21 | for result in json["results"] |
| 22 | ] |
| 23 | ) |
| 24 | |
| 25 | def __str__(self) -> str: |
| 26 | return f"ParallelStepResult(results={self.results})" |
| 27 | |
| 28 | def __repr__(self) -> str: |
| 29 | return str(self) |
| 30 | |
| 31 | @property |
| 32 | def output(self) -> str: |
| 33 | return "\n".join([result.output for result in self.results]) |
| 34 | |
| 35 | |
| 36 | class ParallelProperties: |
| 37 | steps: list["PlanStep"] |
| 38 | |
| 39 | def __init__(self, steps: list["PlanStep"]): |
| 40 | self.steps = steps |
| 41 | |
| 42 | @staticmethod |
| 43 | def from_json(json: dict) -> "ParallelProperties": |
| 44 | return ParallelProperties( |
| 45 | [ |
| 46 | graphrag_sdk.orchestrator.step.PlanStep.from_json(step) |
| 47 | for step in (json if isinstance(json, list) else json["steps"]) |
| 48 | ] |
| 49 | ) |
| 50 | |
| 51 | def to_json(self) -> dict: |
| 52 | return {"steps": [step.to_json() for step in self.steps]} |
| 53 | |
| 54 | def __str__(self) -> str: |
| 55 | return f"ParallelProperties(steps={self.steps})" |
| 56 | |
| 57 | def __repr__(self) -> str: |
| 58 | return str(self) |
| 59 | |
| 60 | |
| 61 | class ParallelStep(graphrag_sdk.orchestrator.step.PlanStep): |
| 62 | |
| 63 | def __init__(self, id: str, properties: ParallelProperties): |
| 64 | self._id = id |
| 65 | self._properties = properties |
| 66 | |
| 67 | @property |
| 68 | def id(self) -> str: |
| 69 | return self._id |
| 70 | |
| 71 | @property |
| 72 | def block(self) -> graphrag_sdk.orchestrator.step.StepBlockType: |
| 73 | return graphrag_sdk.orchestrator.step.StepBlockType.PARALLEL |
| 74 | |
| 75 | @property |
| 76 | def properties(self) -> ParallelProperties: |
| 77 | return self._properties |
| 78 | |
| 79 | def to_json(self) -> dict: |
| 80 | return { |
| 81 | "id": self.id, |
| 82 | "block": self.block, |
| 83 | "properties": self.properties.to_json(), |
| 84 | } |
| 85 | |
| 86 | @staticmethod |
| 87 | def from_json(json: dict) -> "ParallelStep": |
| 88 | return ParallelStep( |
| 89 | json["id"], ParallelProperties.from_json(json["properties"]) |
| 90 | ) |
| 91 | |
| 92 | def __str__(self) -> str: |
| 93 | return f"ParallelStep(id={self.id}, properties={self.properties})" |
| 94 | |
| 95 | def __repr__(self) -> str: |
| 96 | return str(self) |
| 97 | |
| 98 | def run( |
| 99 | self, runner: OrchestratorRunner, config: dict = None |
| 100 | ) -> ParallelStepResult: |
| 101 | config = config or {"parallel_max_workers": 16} |
| 102 | tasks = [] |
| 103 | with ThreadPoolExecutor( |
| 104 | max_workers=min(config["parallel_max_workers"], len(self.properties.steps)) |
| 105 | ) as executor: |
| 106 | for sub_step in self.properties.steps: |
| 107 | tasks.append(executor.submit(sub_step.run, runner)) |
| 108 | |
| 109 | wait(tasks) |
| 110 | |
| 111 | return ParallelStepResult([task.result() for task in tasks]) |