Skip to content

Commit f870d37

Browse files
authored
Make pygraphviz really optional (#137)
* Make pygraphviz optional in code * Remove unused import * Update CHANGELOG.md * Fix broken import * Add link to pygraphviz installation page in README
1 parent 0932e48 commit f870d37

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
### Added
99
- Introduction page to the documentation content tree.
10-
11-
### Added
1210
- Introduced a new Vertex AI embeddings class for generating text embeddings using Vertex AI.
1311
- Updated documentation to include OpenAI and Vertex AI embeddings classes.
1412
- Added google-cloud-aiplatform as an optional dependency for Vertex AI embeddings.
1513

14+
### Fixed
15+
- Make `pygraphviz` an optional dependency - it is now only required when calling `pipeline.draw`.
16+
17+
1618
## 0.6.2
1719

1820
### Fixed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ To install the latest stable version, use:
2828
pip install neo4j-graphrag
2929
```
3030

31+
### Optional dependencies
32+
33+
#### pygraphviz
34+
35+
`pygraphviz` is used for visualizing pipelines.
36+
Follow installation instructions [here](https://pygraphviz.github.io/documentation/stable/install.html).
37+
3138
## Examples
3239

3340
### Creating a vector index

src/neo4j_graphrag/embeddings/vertexai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from typing import Any
1919

20-
from neo4j_genai.embedder import Embedder
20+
from neo4j_graphrag.embedder import Embedder
2121

2222
try:
2323
from vertexai.language_models import TextEmbeddingInput, TextEmbeddingModel

src/neo4j_graphrag/experimental/pipeline/pipeline.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
from timeit import default_timer
2525
from typing import Any, AsyncGenerator, Optional
2626

27-
import pygraphviz as pgv
27+
try:
28+
import pygraphviz as pgv
29+
except ImportError:
30+
pyg = None
31+
2832
from pydantic import BaseModel, Field
2933

3034
from neo4j_graphrag.experimental.pipeline.component import Component, DataModel
@@ -386,6 +390,12 @@ def draw(
386390
G.draw(path)
387391

388392
def get_pygraphviz_graph(self, hide_unused_outputs: bool = True) -> pgv.AGraph:
393+
if pgv is None:
394+
raise ImportError(
395+
"Could not import pygraphviz. "
396+
"Follow installation instruction in pygraphviz documentation "
397+
"to get it up and running on your system."
398+
)
389399
self.validate_parameter_mapping()
390400
G = pgv.AGraph(strict=False, directed=True)
391401
# create a node for each component

tests/unit/experimental/pipeline/test_pipeline.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import asyncio
1818
import tempfile
1919
from unittest import mock
20-
from unittest.mock import AsyncMock, call
20+
from unittest.mock import AsyncMock, call, patch
2121

2222
import pytest
2323
from neo4j_graphrag.experimental.pipeline import Component, Pipeline
@@ -395,3 +395,12 @@ def test_pipeline_draw() -> None:
395395
pipe.draw(t.name)
396396
content = t.file.read()
397397
assert len(content) > 0
398+
399+
400+
@patch("neo4j_graphrag.experimental.pipeline.pipeline.pgv", None)
401+
def test_pipeline_draw_missing_pygraphviz_dep() -> None:
402+
pipe = Pipeline()
403+
pipe.add_component(ComponentAdd(), "add")
404+
t = tempfile.NamedTemporaryFile()
405+
with pytest.raises(ImportError):
406+
pipe.draw(t.name)

0 commit comments

Comments
 (0)