|
24 | 24 | from neo4j_graphrag.utils.logging import prettify
|
25 | 25 |
|
26 | 26 | try:
|
27 |
| - import pygraphviz as pgv |
| 27 | + from neo4j_viz import Node, Relationship, VisualizationGraph |
| 28 | + |
| 29 | + neo4j_viz_available = True |
28 | 30 | except ImportError:
|
29 |
| - pgv = None |
| 31 | + neo4j_viz_available = False |
30 | 32 |
|
31 | 33 | from pydantic import BaseModel
|
32 | 34 |
|
@@ -198,53 +200,126 @@ def show_as_dict(self) -> dict[str, Any]:
|
198 | 200 | def draw(
|
199 | 201 | self, path: str, layout: str = "dot", hide_unused_outputs: bool = True
|
200 | 202 | ) -> Any:
|
201 |
| - G = self.get_pygraphviz_graph(hide_unused_outputs) |
202 |
| - G.layout(layout) |
203 |
| - G.draw(path) |
| 203 | + """Render the pipeline graph to an HTML file at the specified path""" |
| 204 | + G = self._get_neo4j_viz_graph(hide_unused_outputs) |
| 205 | + |
| 206 | + # Write the visualization to an HTML file |
| 207 | + with open(path, "w") as f: |
| 208 | + f.write(G.render().data) |
| 209 | + |
| 210 | + return G |
204 | 211 |
|
205 |
| - def get_pygraphviz_graph(self, hide_unused_outputs: bool = True) -> pgv.AGraph: |
206 |
| - if pgv is None: |
| 212 | + def _get_neo4j_viz_graph( |
| 213 | + self, hide_unused_outputs: bool = True |
| 214 | + ) -> VisualizationGraph: |
| 215 | + """Generate a neo4j-viz visualization of the pipeline graph""" |
| 216 | + if not neo4j_viz_available: |
207 | 217 | raise ImportError(
|
208 |
| - "Could not import pygraphviz. " |
209 |
| - "Follow installation instruction in pygraphviz documentation " |
210 |
| - "to get it up and running on your system." |
| 218 | + "Could not import neo4j-viz. Install it with 'pip install \"neo4j-graphrag[experimental]\"'" |
211 | 219 | )
|
| 220 | + |
212 | 221 | self.validate_parameter_mapping()
|
213 |
| - G = pgv.AGraph(strict=False, directed=True) |
214 |
| - # create a node for each component |
215 |
| - for n, node in self._nodes.items(): |
216 |
| - comp_inputs = ",".join( |
| 222 | + |
| 223 | + nodes = [] |
| 224 | + relationships = [] |
| 225 | + node_ids = {} # Map node names to their numeric IDs |
| 226 | + next_id = 0 |
| 227 | + |
| 228 | + # Create nodes for each component |
| 229 | + for n, pipeline_node in self._nodes.items(): |
| 230 | + comp_inputs = ", ".join( |
217 | 231 | f"{i}: {d['annotation']}"
|
218 |
| - for i, d in node.component.component_inputs.items() |
| 232 | + for i, d in pipeline_node.component.component_inputs.items() |
219 | 233 | )
|
220 |
| - G.add_node( |
221 |
| - n, |
222 |
| - node_type="component", |
223 |
| - shape="rectangle", |
224 |
| - label=f"{node.component.__class__.__name__}: {n}({comp_inputs})", |
| 234 | + |
| 235 | + node_ids[n] = next_id |
| 236 | + label = f"{pipeline_node.component.__class__.__name__}: {n}({comp_inputs})" |
| 237 | + |
| 238 | + # Create Node with properties parameter |
| 239 | + viz_node = Node( # type: ignore |
| 240 | + id=next_id, |
| 241 | + caption=label, |
| 242 | + size=20, |
| 243 | + properties={"node_type": "component"}, |
225 | 244 | )
|
226 |
| - # create a node for each output field and connect them it to its component |
227 |
| - for o in node.component.component_outputs: |
| 245 | + nodes.append(viz_node) |
| 246 | + next_id += 1 |
| 247 | + |
| 248 | + # Create nodes for each output field |
| 249 | + for o in pipeline_node.component.component_outputs: |
228 | 250 | param_node_name = f"{n}.{o}"
|
229 |
| - G.add_node(param_node_name, label=o, node_type="output") |
230 |
| - G.add_edge(n, param_node_name) |
231 |
| - # then we create the edges between a component output |
232 |
| - # and the component it gets added to |
| 251 | + |
| 252 | + # Skip if we're hiding unused outputs and it's not used |
| 253 | + if hide_unused_outputs: |
| 254 | + # Check if this output is used as a source in any parameter mapping |
| 255 | + is_used = False |
| 256 | + for params in self.param_mapping.values(): |
| 257 | + for mapping in params.values(): |
| 258 | + source_component = mapping["component"] |
| 259 | + source_param_name = mapping.get("param") |
| 260 | + if source_component == n and source_param_name == o: |
| 261 | + is_used = True |
| 262 | + break |
| 263 | + if is_used: |
| 264 | + break |
| 265 | + |
| 266 | + if not is_used: |
| 267 | + continue |
| 268 | + |
| 269 | + node_ids[param_node_name] = next_id |
| 270 | + # Create Node with properties parameter |
| 271 | + output_node = Node( # type: ignore |
| 272 | + id=next_id, |
| 273 | + caption=o, |
| 274 | + size=15, |
| 275 | + properties={"node_type": "output"}, |
| 276 | + ) |
| 277 | + nodes.append(output_node) |
| 278 | + |
| 279 | + # Connect component to its output |
| 280 | + # Connect component to its output |
| 281 | + rel = Relationship( # type: ignore |
| 282 | + source=node_ids[n], |
| 283 | + target=node_ids[param_node_name], |
| 284 | + properties={"type": "HAS_OUTPUT"}, |
| 285 | + ) |
| 286 | + relationships.append(rel) |
| 287 | + next_id += 1 |
| 288 | + |
| 289 | + # Create edges between components based on parameter mapping |
233 | 290 | for component_name, params in self.param_mapping.items():
|
234 | 291 | for param, mapping in params.items():
|
235 | 292 | source_component = mapping["component"]
|
236 | 293 | source_param_name = mapping.get("param")
|
| 294 | + |
237 | 295 | if source_param_name:
|
238 | 296 | source_output_node = f"{source_component}.{source_param_name}"
|
239 | 297 | else:
|
240 | 298 | source_output_node = source_component
|
241 |
| - G.add_edge(source_output_node, component_name, label=param) |
242 |
| - # remove outputs that are not mapped |
243 |
| - if hide_unused_outputs: |
244 |
| - for n in G.nodes(): |
245 |
| - if n.attr["node_type"] == "output" and G.out_degree(n) == 0: # type: ignore |
246 |
| - G.remove_node(n) |
247 |
| - return G |
| 299 | + |
| 300 | + if source_output_node in node_ids and component_name in node_ids: |
| 301 | + rel = Relationship( # type: ignore |
| 302 | + source=node_ids[source_output_node], |
| 303 | + target=node_ids[component_name], |
| 304 | + caption=param, |
| 305 | + properties={"type": "CONNECTS_TO"}, |
| 306 | + ) |
| 307 | + relationships.append(rel) |
| 308 | + |
| 309 | + # Create the visualization graph |
| 310 | + viz_graph = VisualizationGraph(nodes=nodes, relationships=relationships) |
| 311 | + return viz_graph |
| 312 | + |
| 313 | + def get_pygraphviz_graph(self, hide_unused_outputs: bool = True) -> Any: |
| 314 | + """Legacy method for backward compatibility. |
| 315 | + Uses neo4j-viz instead of pygraphviz. |
| 316 | + """ |
| 317 | + warnings.warn( |
| 318 | + "get_pygraphviz_graph is deprecated, use draw instead", |
| 319 | + DeprecationWarning, |
| 320 | + stacklevel=2, |
| 321 | + ) |
| 322 | + return self._get_neo4j_viz_graph(hide_unused_outputs) |
248 | 323 |
|
249 | 324 | def add_component(self, component: Component, name: str) -> None:
|
250 | 325 | """Add a new component. Components are uniquely identified
|
|
0 commit comments