forked from neo4j/neo4j-graphrag-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
58 lines (46 loc) · 2.03 KB
/
visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This example illustrates how to visualize a Pipeline"""
from neo4j_graphrag.experimental.pipeline import Component, Pipeline
from neo4j_graphrag.experimental.pipeline.component import DataModel
from pydantic import validate_call
class IntDataModel(DataModel):
value: int
message: str
class Addition(Component):
async def run(self, a: int, b: int) -> IntDataModel:
return IntDataModel(value=a + b, message="addition complete")
class Duplicate(Component):
def __init__(self, factor: int = 2) -> None:
self.factor = factor
async def run(self, number: int) -> IntDataModel:
return IntDataModel(
value=number * self.factor, message=f"multiplication by {self.factor} done"
)
class Save(Component):
@validate_call
async def run(self, number: IntDataModel) -> IntDataModel:
return IntDataModel(value=number.value, message="saved")
if __name__ == "__main__":
pipe = Pipeline()
pipe.add_component(Duplicate(), "times_two")
pipe.add_component(Duplicate(factor=10), "times_ten")
pipe.add_component(Addition(), "addition")
pipe.add_component(Save(), "save")
pipe.connect("times_two", "addition", {"a": "times_two.value"})
pipe.connect("times_ten", "addition", {"b": "times_ten.value"})
pipe.connect("addition", "save", {"number": "addition"})
pipe.draw("graph.html")
pipe.draw("graph_full.html", hide_unused_outputs=False)