Elffuss BraidHub

kikocis/GraphRAG-SDK public

main · 2.5 KiBRawPermalink
1from graphrag_sdk.orchestrator.step import PlanStep, StepBlockType
2from graphrag_sdk.orchestrator.step_result import StepResult
3from graphrag_sdk.orchestrator.orchestrator_runner import OrchestratorRunner
4import logging
5
6logger = logging.getLogger(__name__)
7
8
9class UserInputResult(StepResult):
10
11 def __init__(self, output: str):
12 self._output = output
13
14 def to_json(self) -> dict:
15 return {
16 "output": self._output,
17 }
18
19 @staticmethod
20 def from_json(json: dict) -> "UserInputResult":
21 return UserInputResult(
22 json["output"],
23 )
24
25 def __str__(self) -> str:
26 return f"UserInputResult(output={self._output})"
27
28 def __repr__(self) -> str:
29 return str(self)
30
31 @property
32 def output(self) -> str:
33 return self._output
34
35
36class UserInputProperties:
37
38 def __init__(self, question: str):
39 self.question = question
40
41 @staticmethod
42 def from_json(json: dict) -> "UserInputProperties":
43 return UserInputProperties(
44 json["question"],
45 )
46
47 def to_json(self) -> dict:
48 return {
49 "question": self.question,
50 }
51
52 def __str__(self) -> str:
53 return f"UserInputProperties(question={self.question})"
54
55 def __repr__(self) -> str:
56 return str(self)
57
58
59class UserInputStep(PlanStep):
60
61 def __init__(self, id: str, properties: UserInputProperties):
62 self._id = id
63 self._properties = properties
64
65 @property
66 def id(self) -> str:
67 return self._id
68
69 @property
70 def block(self) -> StepBlockType:
71 return StepBlockType.USER_INPUT
72
73 @property
74 def properties(self) -> UserInputProperties:
75 return self._properties
76
77 def to_json(self) -> dict:
78 return {
79 "id": self.id,
80 "block": self.block,
81 "properties": self.properties.to_json(),
82 }
83
84 @staticmethod
85 def from_json(json: dict) -> "UserInputStep":
86 return UserInputStep(
87 json["id"], UserInputProperties.from_json(json["properties"])
88 )
89
90 def __str__(self) -> str:
91 return f"UserInputStep(id={self.id}, properties={self.properties})"
92
93 def __repr__(self) -> str:
94 return str(self)
95
96 def run(
97 self,
98 runner: OrchestratorRunner,
99 config: dict = None,
100 ) -> UserInputResult:
101 logger.info(f"Running user input step: {self.id}")
102 return UserInputResult(runner.get_user_input(self.properties.question))