-
Notifications
You must be signed in to change notification settings - Fork 103
Pipeline streaming #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stellasia
merged 13 commits into
neo4j:main
from
stellasia:feature/actual-pipeline-streaming
Apr 2, 2025
Merged
Pipeline streaming #304
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
545f1dd
Expose context from the orchestrator to the components
stellasia 8996889
mypy
stellasia d146adf
Changes so that the `run` method is not required anymore
stellasia 567a387
Update documentation
stellasia a4b043f
Improve documentation of future changes
stellasia 19d032d
Undo make notifier private, not needed
stellasia 6e4261b
Pipeline streaming
stellasia 6a7c168
Add tests
stellasia bc70e56
Fix "Task destroyed but is pending" warning
stellasia 8cafb31
Fix rebase
stellasia 2645692
Update CHANGELOG
stellasia 6eebfab
Add PIPELINE_FAILED event and option not to raise exception
stellasia 81aebf7
Mypy
stellasia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
examples/customize/build_graph/pipeline/pipeline_streaming.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import asyncio | ||
|
||
from neo4j_graphrag.experimental.pipeline.component import Component, DataModel | ||
from neo4j_graphrag.experimental.pipeline.pipeline import Pipeline | ||
from neo4j_graphrag.experimental.pipeline.notification import EventType, Event | ||
from neo4j_graphrag.experimental.pipeline.types.context import RunContext | ||
|
||
|
||
# Define some example components with progress notifications | ||
class OutputModel(DataModel): | ||
result: int | ||
|
||
|
||
class SlowAdder(Component): | ||
"""A component that slowly adds numbers and reports progress""" | ||
|
||
def __init__(self, number: int) -> None: | ||
self.number = number | ||
|
||
async def run_with_context(self, context_: RunContext, value: int) -> OutputModel: | ||
# Simulate work with progress updates | ||
for i in range(value): | ||
await asyncio.sleep(0.5) # Simulate work | ||
await context_.notify( | ||
message=f"Added {i+1}/{value}", data={"current": i + 1, "total": value} | ||
) | ||
return OutputModel(result=value + self.number) | ||
|
||
|
||
class SlowMultiplier(Component): | ||
"""A component that slowly multiplies numbers and reports progress""" | ||
|
||
def __init__(self, multiplier: int) -> None: | ||
self.multiplier = multiplier | ||
|
||
async def run_with_context(self, context_: RunContext, value: int) -> OutputModel: | ||
# Simulate work with progress updates | ||
for i in range(3): # Always do 3 steps | ||
await asyncio.sleep(0.7) # Simulate work | ||
await context_.notify( | ||
message=f"Multiplication step {i+1}/3", data={"step": i + 1, "total": 3} | ||
) | ||
return OutputModel(result=value * self.multiplier) | ||
|
||
|
||
async def callback(event: Event) -> None: | ||
await asyncio.sleep(0.1) | ||
|
||
|
||
async def main() -> None: | ||
# Create pipeline | ||
pipeline = Pipeline(callback=callback) | ||
|
||
# Add components | ||
pipeline.add_component(SlowAdder(number=3), "adder") | ||
pipeline.add_component(SlowMultiplier(multiplier=2), "multiplier") | ||
|
||
# Connect components | ||
pipeline.connect("adder", "multiplier", {"value": "adder.result"}) | ||
|
||
print("\n=== Running pipeline with streaming ===") | ||
# Run pipeline with streaming - see events as they happen | ||
async for event in pipeline.stream({"adder": {"value": 2}}): | ||
if event.event_type == EventType.PIPELINE_STARTED: | ||
print("Stream: Pipeline started!") | ||
elif event.event_type == EventType.PIPELINE_FINISHED: | ||
print(f"Stream: Pipeline finished! Final results: {event.payload}") | ||
elif event.event_type == EventType.TASK_STARTED: | ||
print( | ||
f"Stream: Task {event.task_name} started with inputs: {event.payload}" # type: ignore | ||
) | ||
elif event.event_type == EventType.TASK_PROGRESS: | ||
print(f"Stream: Task {event.task_name} progress - {event.message}") # type: ignore | ||
elif event.event_type == EventType.TASK_FINISHED: | ||
print( | ||
f"Stream: Task {event.task_name} finished with result: {event.payload}" # type: ignore | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.