Skip to content

Commit e41a738

Browse files
committed
Improve documentation of future changes
1 parent e7a0ff0 commit e41a738

File tree

7 files changed

+21
-15
lines changed

7 files changed

+21
-15
lines changed

CHANGELOG.md

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

55
### Added
66

7-
- Added a `run_with_context` method to `Component`. This method has a `context_` parameter that contains information from the pipeline the component is being run from (e.g. the `run_id`)
7+
- Added the `run_with_context` method to `Component`. This method includes a `context_` parameter, which provides information about the pipeline from which the component is executed (e.g., the `run_id`). It also enables the component to send events to the pipeline's callback function.
88

99

1010
## 1.6.0

docs/source/user_guide_pipeline.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ See :ref:`pipelineevent` and :ref:`taskevent` to see what is sent in each event
179179
Send Events from Components
180180
===========================
181181

182-
Components can send notifications about their progress using the `notify` function from
183-
the `context_`:
182+
Components can send progress notifications using the `notify` function from
183+
`context_` by implementing the `run_from_context` method:
184184

185185
.. code:: python
186186
@@ -200,3 +200,7 @@ the `context_`:
200200
return IntResultModel(result = number1 + number2)
201201
202202
This will send an `TASK_PROGRESS` event to the pipeline callback.
203+
204+
.. note::
205+
206+
In a future release, the `context_` parameter will be added to the `run` method.

examples/customize/build_graph/pipeline/pipeline_with_component_notifications.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ class MultiplicationComponent(Component):
2525
def __init__(self, f: int) -> None:
2626
self.f = f
2727

28-
async def run(self, numbers: list[int]) -> MultiplyComponentResult:
29-
return MultiplyComponentResult(result=[])
30-
3128
async def multiply_number(
3229
self,
3330
context_: RunContext,
@@ -39,6 +36,8 @@ async def multiply_number(
3936
)
4037
return self.f * number
4138

39+
# implementing `run_with_context` to get access to
40+
# the pipeline's RunContext:
4241
async def run_with_context(
4342
self,
4443
context_: RunContext,

src/neo4j_graphrag/experimental/pipeline/component.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ class Component(metaclass=ComponentMeta):
8484
component_outputs: dict[str, dict[str, str | bool | type]]
8585

8686
async def run(self, *args: Any, **kwargs: Any) -> DataModel:
87-
"""This function is planned for deprecation in a future release.
87+
"""Run the component and return its result.
8888
8989
Note: if `run_with_context` is implemented, this method will not be used.
9090
"""
9191
raise NotImplementedError(
9292
"You must implement the `run` or `run_with_context` method. "
93-
"`run` method will be marked for deprecation in a future release."
9493
)
9594

9695
async def run_with_context(
@@ -102,8 +101,10 @@ async def run_with_context(
102101
that can be used to send events from the component to
103102
the pipeline callback.
104103
105-
For now, it defaults to calling the `run` method, but it
106-
is meant to replace the `run` method in a future release.
104+
This feature will be moved to the `run` method in a future
105+
release.
106+
107+
It defaults to calling the `run` method to prevent any breaking change.
107108
"""
108109
# default behavior to prevent a breaking change
109110
return await self.run(*args, **kwargs)

src/neo4j_graphrag/experimental/pipeline/orchestrator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ async def run_task(self, task: TaskPipelineNode, data: dict[str, Any]) -> None:
8585
run_id=self.run_id,
8686
task_name=task.name,
8787
)
88-
context = RunContext(run_id=self.run_id, task_name=task.name, notifier=notifier)
88+
context = RunContext(
89+
run_id=self.run_id, task_name=task.name, _notifier=notifier
90+
)
8991
res = await task.run(context, inputs)
9092
await self.set_task_status(task.name, RunStatus.DONE)
9193
await self.event_notifier.notify_task_finished(self.run_id, task.name, res)

src/neo4j_graphrag/experimental/pipeline/types/context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class RunContext(BaseModel):
2828

2929
run_id: str
3030
task_name: str
31-
notifier: Optional[TaskProgressCallbackProtocol] = None
31+
_notifier: Optional[TaskProgressCallbackProtocol] = None
3232

3333
model_config = ConfigDict(arbitrary_types_allowed=True)
3434

3535
async def notify(self, message: str, data: dict[str, Any]) -> None:
36-
if self.notifier:
37-
await self.notifier(message=message, data=data)
36+
if self._notifier:
37+
await self._notifier(message=message, data=data)

tests/unit/experimental/pipeline/test_component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def test_component_run_with_context() -> None:
6868
c = ComponentMultiplyWithContext()
6969
notifier_mock = AsyncMock()
7070
result = await c.run_with_context(
71-
RunContext(run_id="run_id", task_name="task_name", notifier=notifier_mock),
71+
RunContext(run_id="run_id", task_name="task_name", _notifier=notifier_mock),
7272
number1=1,
7373
number2=2,
7474
)

0 commit comments

Comments
 (0)