-
Notifications
You must be signed in to change notification settings - Fork 103
Expose context from the orchestrator to the components #301
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
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cdcd2fb
Expose context from the orchestrator to the components
stellasia 69b99dc
Rename to match component name + ruff
stellasia 7cb5488
Add tests
stellasia 14bff07
Update changelog and example readme
stellasia 056d699
unused import
stellasia 8d9c97b
mypy
stellasia 6a55af4
Changes so that the `run` method is not required anymore
stellasia b2a750a
Update documentation
stellasia 0e349f8
We still need to raise error if components do not have at least one o…
stellasia e7a0ff0
Update CHANGELOG.md
stellasia e41a738
Improve documentation of future changes
stellasia fe0fe02
Undo make notifier private, not needed
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
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
110 changes: 110 additions & 0 deletions
110
examples/customize/build_graph/pipeline/pipeline_with_component_notifications.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,110 @@ | ||
"""This example demonstrates how to use event callback to receive notifications | ||
about the component progress. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
from typing import Any | ||
|
||
from neo4j_graphrag.experimental.pipeline import Pipeline, Component, DataModel | ||
from neo4j_graphrag.experimental.pipeline.notification import Event, EventType | ||
from neo4j_graphrag.experimental.pipeline.types.context import RunContext | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig() | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
class MultiplyComponentResult(DataModel): | ||
result: list[int] | ||
|
||
|
||
class MultiplicationComponent(Component): | ||
def __init__(self, f: int) -> None: | ||
self.f = f | ||
|
||
async def run(self, numbers: list[int]) -> MultiplyComponentResult: | ||
return MultiplyComponentResult(result=[]) | ||
|
||
async def multiply_number( | ||
self, | ||
context_: RunContext, | ||
number: int, | ||
) -> int: | ||
await context_.notify( | ||
message=f"Processing number {number}", | ||
data={"number_processed": number}, | ||
) | ||
return self.f * number | ||
|
||
async def run_with_context( | ||
self, | ||
context_: RunContext, | ||
numbers: list[int], | ||
**kwargs: Any, | ||
) -> MultiplyComponentResult: | ||
result = await asyncio.gather( | ||
*[ | ||
self.multiply_number( | ||
context_, | ||
number, | ||
) | ||
for number in numbers | ||
] | ||
) | ||
return MultiplyComponentResult(result=result) | ||
|
||
|
||
async def event_handler(event: Event) -> None: | ||
"""Function can do anything about the event, | ||
here we're just logging it if it's a pipeline-level event. | ||
""" | ||
if event.event_type == EventType.TASK_PROGRESS: | ||
logger.warning(event) | ||
else: | ||
logger.info(event) | ||
|
||
|
||
async def main() -> None: | ||
""" """ | ||
pipe = Pipeline( | ||
callback=event_handler, | ||
) | ||
# define the components | ||
pipe.add_component( | ||
MultiplicationComponent(f=2), | ||
"multiply_by_2", | ||
) | ||
pipe.add_component( | ||
MultiplicationComponent(f=10), | ||
"multiply_by_10", | ||
) | ||
# define the execution order of component | ||
# and how the output of previous components must be used | ||
pipe.connect( | ||
"multiply_by_2", | ||
"multiply_by_10", | ||
input_config={"numbers": "multiply_by_2.result"}, | ||
) | ||
# user input: | ||
pipe_inputs_1 = { | ||
"multiply_by_2": { | ||
"numbers": [1, 2, 5, 4], | ||
}, | ||
} | ||
pipe_inputs_2 = { | ||
"multiply_by_2": { | ||
"numbers": [3, 10, 1], | ||
} | ||
} | ||
# run the pipeline | ||
await asyncio.gather( | ||
pipe.run(pipe_inputs_1), | ||
pipe.run(pipe_inputs_2), | ||
) | ||
|
||
|
||
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
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
Oops, something went wrong.
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.