| 1 | import graphrag_sdk.orchestrator.step |
| 2 | from graphrag_sdk.orchestrator.step_result import StepResult |
| 3 | from concurrent.futures import ThreadPoolExecutor, wait |
| 4 | from graphrag_sdk.orchestrator.orchestrator_runner import OrchestratorRunner |
| 5 | from graphrag_sdk.fixtures.prompts import ORCHESTRATOR_SUMMARY_PROMPT |
| 6 | |
| 7 | import logging |
| 8 | |
| 9 | logger = logging.getLogger(__name__) |
| 10 | |
| 11 | |
| 12 | class SummaryResult(StepResult): |
| 13 | |
| 14 | def __init__(self, output: str): |
| 15 | self._output = output |
| 16 | |
| 17 | def to_json(self) -> dict: |
| 18 | return { |
| 19 | "output": self._output, |
| 20 | } |
| 21 | |
| 22 | @staticmethod |
| 23 | def from_json(json: dict) -> "SummaryResult": |
| 24 | return SummaryResult( |
| 25 | json["output"], |
| 26 | ) |
| 27 | |
| 28 | def __str__(self) -> str: |
| 29 | return f"SummaryResult(output={self._output})" |
| 30 | |
| 31 | def __repr__(self) -> str: |
| 32 | return str(self) |
| 33 | |
| 34 | @property |
| 35 | def output(self) -> str: |
| 36 | return self._output |
| 37 | |
| 38 | |
| 39 | class SummaryStep(graphrag_sdk.orchestrator.step.PlanStep): |
| 40 | |
| 41 | def __init__(self, id: str, properties: any): |
| 42 | self._id = id |
| 43 | self._properties = {} |
| 44 | |
| 45 | @property |
| 46 | def id(self) -> str: |
| 47 | return self._id |
| 48 | |
| 49 | @property |
| 50 | def block(self) -> graphrag_sdk.orchestrator.step.StepBlockType: |
| 51 | return graphrag_sdk.orchestrator.step.StepBlockType.SUMMARY |
| 52 | |
| 53 | @property |
| 54 | def properties(self) -> any: |
| 55 | return self._properties |
| 56 | |
| 57 | def to_json(self) -> dict: |
| 58 | return { |
| 59 | "id": self.id, |
| 60 | "block": self.block, |
| 61 | "properties": {}, |
| 62 | } |
| 63 | |
| 64 | @staticmethod |
| 65 | def from_json(json: dict) -> "SummaryStep": |
| 66 | return SummaryStep(json["id"], {}) |
| 67 | |
| 68 | def __str__(self) -> str: |
| 69 | return f"SummaryStep(id={self.id}, properties={self.properties})" |
| 70 | |
| 71 | def __repr__(self) -> str: |
| 72 | return str(self) |
| 73 | |
| 74 | def run( |
| 75 | self, |
| 76 | runner: OrchestratorRunner, |
| 77 | config: dict = None, |
| 78 | ) -> SummaryResult: |
| 79 | response = runner.chat.send_message( |
| 80 | ORCHESTRATOR_SUMMARY_PROMPT.replace( |
| 81 | "#USER_QUESTION", str(runner.user_question) |
| 82 | ).replace("#EXECUTION_LOG", str(runner.runner_log)) |
| 83 | ) |
| 84 | |
| 85 | return SummaryResult(response.text) |