Skip to content

Commit 6d3c17c

Browse files
committed
switch from | to Union
1 parent 3f3fe5b commit 6d3c17c

File tree

13 files changed

+68
-56
lines changed

13 files changed

+68
-56
lines changed

graphrag_sdk/attribute.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
import json
33
import logging
4+
from typing import Union
45
from graphrag_sdk.fixtures.regex import *
56

67
logger = logging.getLogger(__name__)
@@ -89,12 +90,12 @@ def __init__(
8990
self.required = required
9091

9192
@staticmethod
92-
def from_json(txt: str | dict):
93+
def from_json(txt: Union[str, dict]):
9394
"""
9495
Creates an Attribute object from a JSON string or dictionary.
9596
9697
Args:
97-
txt (str | dict): The JSON string or dictionary representing the Attribute.
98+
txt (Union[str, dict]): The JSON string or dictionary representing the Attribute.
9899
99100
Returns:
100101
Attribute: The created Attribute object.

graphrag_sdk/entity.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
from typing import Union
34
from .attribute import Attribute
45
from falkordb import Node as GraphNode
56
import re
@@ -20,7 +21,7 @@ class Entity:
2021
2122
Methods:
2223
from_graph(entity: GraphNode) -> Entity: Creates an Entity object from a GraphNode object.
23-
from_json(txt: dict | str) -> Entity: Creates an Entity object from a JSON string or dictionary.
24+
from_json(txt: Union[dict, str]) -> Entity: Creates an Entity object from a JSON string or dictionary.
2425
to_json() -> dict: Converts the Entity object to a JSON dictionary.
2526
merge(entity2: Entity) -> Entity: Overwrites attributes of self with attributes of entity2.
2627
get_unique_attributes() -> list[Attribute]: Returns a list of unique attributes of the entity.
@@ -64,12 +65,12 @@ def from_graph(entity: GraphNode):
6465
)
6566

6667
@staticmethod
67-
def from_json(txt: dict | str):
68+
def from_json(txt: Union[dict, str]):
6869
"""
6970
Create an Entity object from a JSON representation.
7071
7172
Args:
72-
txt (dict | str): The JSON representation of the Entity. It can be either a dictionary or a string.
73+
txt Union[dict, str]: The JSON representation of the Entity. It can be either a dictionary or a string.
7374
7475
Returns:
7576
Entity: The Entity object created from the JSON representation.

graphrag_sdk/helpers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import re
22
import logging
33
import graphrag_sdk
4+
from typing import Union
45
from fix_busted_json import repair_json
56

67
logger = logging.getLogger(__name__)
78

89

9-
def extract_json(text: str | dict, skip_repair=False) -> str:
10+
def extract_json(text: Union[str, dict], skip_repair=False) -> str:
1011
if not isinstance(text, str):
1112
text = str(text)
1213
regex = r"(?:```)?(?:json)?([^`]*)(?:\\n)?(?:```)?"
@@ -79,7 +80,7 @@ def extract_cypher(text: str):
7980

8081
def validate_cypher(
8182
cypher: str, ontology: graphrag_sdk.Ontology
82-
) -> list[str] | None:
83+
) -> Union[list[str], None]:
8384
try:
8485
if not cypher or len(cypher) == 0:
8586
return ["Cypher statement is empty"]
@@ -215,4 +216,4 @@ def validate_cypher_relation_directions(
215216
except Exception:
216217
continue
217218

218-
return errors
219+
return errors

graphrag_sdk/kg.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import warnings
3-
from typing import Optional
43
from falkordb import FalkorDB
4+
from typing import Optional, Union
55
from graphrag_sdk.ontology import Ontology
66
from graphrag_sdk.source import AbstractSource
77
from graphrag_sdk.chat_session import ChatSession
@@ -45,14 +45,14 @@ def __init__(
4545
model (GenerativeModel): The Google GenerativeModel to use.
4646
host (str): FalkorDB hostname.
4747
port (int): FalkorDB port number.
48-
username (str|None): FalkorDB username.
49-
password (str|None): FalkorDB password.
50-
ontology (Ontology|None): Ontology to use.
51-
cypher_system_instruction (str|None): Cypher system instruction. Make sure you have {ontology} in the instruction.
52-
qa_system_instruction (str|None): QA system instruction.
53-
cypher_gen_prompt (str|None): Cypher generation prompt. Make sure you have {question} in the prompt.
54-
qa_prompt (str|None): QA prompt. Make sure you have {question}, {context} and {cypher} in the prompt.
55-
cypher_gen_prompt_history (str|None): Cypher generation prompt with history. Make sure you have {question} and {last_answer} in the prompt.
48+
username (Union[str, None]): FalkorDB username.
49+
password (Union[str, None]): FalkorDB password.
50+
ontology (Union[Ontology, None]): Ontology to use.
51+
cypher_system_instruction (Union[str, None]): Cypher system instruction. Make sure you have {ontology} in the instruction.
52+
qa_system_instruction (Union[str, None]): QA system instruction.
53+
cypher_gen_prompt (Union[str, None]): Cypher generation prompt. Make sure you have {question} in the prompt.
54+
qa_prompt (Union[str, None]): QA prompt. Make sure you have {question}, {context} and {cypher} in the prompt.
55+
cypher_gen_prompt_history (Union[str, None]): Cypher generation prompt with history. Make sure you have {question} and {last_answer} in the prompt.
5656
"""
5757

5858
if not isinstance(name, str) or name == "":
@@ -165,7 +165,7 @@ def process_sources(
165165

166166

167167
def _create_graph_with_sources(
168-
self, sources: list[AbstractSource] | None = None, instructions: str = None, hide_progress: bool = False
168+
self, sources: Union[list[AbstractSource], None] = None, instructions: str = None, hide_progress: bool = False
169169
) -> None:
170170

171171
step = ExtractDataStep(

graphrag_sdk/models/gemini.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
from typing import Union
3+
24
from .model import (
35
OutputMethod,
46
GenerativeModel,
@@ -23,8 +25,8 @@ class GeminiGenerativeModel(GenerativeModel):
2325
def __init__(
2426
self,
2527
model_name: str,
26-
generation_config: GoogleGenerationConfig | None = None,
27-
system_instruction: str | None = None,
28+
generation_config: Union[GoogleGenerationConfig, None] = None,
29+
system_instruction: Union[str, None] = None,
2830
):
2931
self._model_name = model_name
3032
self._generation_config = generation_config
@@ -59,7 +61,7 @@ def with_system_instruction(self, system_instruction: str) -> "GenerativeModel":
5961

6062
return self
6163

62-
def start_chat(self, args: dict | None = None) -> GenerativeModelChatSession:
64+
def start_chat(self, args: Union[dict, None] = None) -> GenerativeModelChatSession:
6365
return GeminiChatSession(self, args)
6466

6567
def parse_generate_content_response(
@@ -99,7 +101,7 @@ def from_json(json: dict) -> "GenerativeModel":
99101

100102
class GeminiChatSession(GenerativeModelChatSession):
101103

102-
def __init__(self, model: GeminiGenerativeModel, args: dict | None = None):
104+
def __init__(self, model: GeminiGenerativeModel, args: Union[dict, None] = None):
103105
self._model = model
104106
self._chat_session = self._model._model.start_chat(
105107
history=args.get("history", []) if args is not None else [],

graphrag_sdk/models/model.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from abc import ABC, abstractmethod
21
from enum import Enum
2+
from typing import Union
3+
from abc import ABC, abstractmethod
34

45

56
class FinishReason:
@@ -30,12 +31,12 @@ class GenerativeModelConfig:
3031

3132
def __init__(
3233
self,
33-
temperature: float | None = None,
34-
top_p: float | None = None,
35-
top_k: int | None = None,
36-
max_output_tokens: int | None = None,
37-
stop_sequences: list[str] | None = None,
38-
response_format: dict | None = None,
34+
temperature: Union[float, None] = None,
35+
top_p: Union[float, None] = None,
36+
top_k: Union[int, None] = None,
37+
max_output_tokens: Union[int, None] = None,
38+
stop_sequences: Union[list[str], None] = None,
39+
response_format: Union[dict, None] = None,
3940
):
4041
self.temperature = temperature
4142
self.top_p = top_p
@@ -103,7 +104,7 @@ def with_system_instruction(self, system_instruction: str) -> "GenerativeModel":
103104
pass
104105

105106
@abstractmethod
106-
def start_chat(self, args: dict | None) -> GenerativeModelChatSession:
107+
def start_chat(self, args: Union[dict, None]) -> GenerativeModelChatSession:
107108
pass
108109

109110
@staticmethod

graphrag_sdk/models/openai.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
FinishReason,
77
GenerativeModelChatSession,
88
)
9+
from typing import Union
910
from openai import OpenAI
1011

1112

@@ -16,8 +17,8 @@ class OpenAiGenerativeModel(GenerativeModel):
1617
def __init__(
1718
self,
1819
model_name: str,
19-
generation_config: GenerativeModelConfig | None = None,
20-
system_instruction: str | None = None,
20+
generation_config: Union[GenerativeModelConfig, None] = None,
21+
system_instruction: Union[str, None] = None,
2122
):
2223
self.model_name = model_name
2324
self.generation_config = generation_config or GenerativeModelConfig()
@@ -36,7 +37,7 @@ def with_system_instruction(self, system_instruction: str) -> "GenerativeModel":
3637

3738
return self
3839

39-
def start_chat(self, args: dict | None = None) -> GenerativeModelChatSession:
40+
def start_chat(self, args: Union[dict, None] = None) -> GenerativeModelChatSession:
4041
return OpenAiChatSession(self, args)
4142

4243
def parse_generate_content_response(self, response: any) -> GenerationResponse:
@@ -75,7 +76,7 @@ class OpenAiChatSession(GenerativeModelChatSession):
7576

7677
_history = []
7778

78-
def __init__(self, model: OpenAiGenerativeModel, args: dict | None = None):
79+
def __init__(self, model: OpenAiGenerativeModel, args: Union[dict, None] = None):
7980
self._model = model
8081
self._args = args
8182
self._history = (

graphrag_sdk/ontology.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import graphrag_sdk
44
from .entity import Entity
55
from falkordb import Graph
6-
from typing import Optional
6+
from typing import Optional, Union
77
from .relation import Relation
88
from graphrag_sdk.source import AbstractSource
99
from graphrag_sdk.models import GenerativeModel
@@ -81,12 +81,12 @@ def from_sources(
8181
return step.run(boundaries=boundaries)
8282

8383
@staticmethod
84-
def from_json(txt: dict | str):
84+
def from_json(txt: Union[dict, str]):
8585
"""
8686
Creates an Ontology object from a JSON representation.
8787
8888
Args:
89-
txt (dict | str): The JSON representation of the ontology. It can be either a dictionary or a string.
89+
txt (Union[dict, str]): The JSON representation of the ontology. It can be either a dictionary or a string.
9090
9191
Returns:
9292
The Ontology object created from the JSON representation.
@@ -401,4 +401,4 @@ def save_to_graph(self, graph: Graph):
401401
for relation in self.relations:
402402
query = relation.to_graph_query()
403403
logger.debug(f"Query: {query}")
404-
graph.query(query)
404+
graph.query(query)

graphrag_sdk/orchestrator/execution_plan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from json import loads
2+
from typing import Union
23
from graphrag_sdk.orchestrator.step import PlanStep, StepBlockType
34

45

@@ -10,7 +11,7 @@ def __init__(self, steps: list[PlanStep]):
1011
self.steps = steps
1112

1213
@staticmethod
13-
def from_json(json: str | dict) -> "ExecutionPlan":
14+
def from_json(json: Union[str, dict]) -> "ExecutionPlan":
1415
if isinstance(json, str):
1516
json = loads(json)
1617
return ExecutionPlan([PlanStep.from_json(step) for step in json])

graphrag_sdk/orchestrator/orchestrator_decision.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from graphrag_sdk.orchestrator.step import PlanStep
21
from json import loads
2+
from typing import Union
3+
from graphrag_sdk.orchestrator.step import PlanStep
34

45

56
class OrchestratorDecisionCode:
@@ -21,7 +22,7 @@ def from_str(code: str) -> str:
2122

2223
class OrchestratorDecision:
2324
def __init__(
24-
self, code: OrchestratorDecisionCode, new_step: PlanStep | None = None
25+
self, code: OrchestratorDecisionCode, new_step: Union[PlanStep, None] = None
2526
):
2627
self.code = code
2728
self.new_step = new_step
@@ -33,7 +34,7 @@ def to_json(self) -> dict:
3334
}
3435

3536
@staticmethod
36-
def from_json(json: dict | str) -> "OrchestratorDecision":
37+
def from_json(json: Union[dict, str]) -> "OrchestratorDecision":
3738
json = json if isinstance(json, dict) else loads(json)
3839
return OrchestratorDecision(
3940
OrchestratorDecisionCode.from_str(json["code"]),

0 commit comments

Comments
 (0)