-
Notifications
You must be signed in to change notification settings - Fork 16.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Runnable.with_listeners() (#12549)
- This binds start/end/error listeners to a runnable, which will be called with the Run object
- Loading branch information
Showing
7 changed files
with
295 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
46 changes: 46 additions & 0 deletions
46
libs/langchain/langchain/callbacks/tracers/root_listeners.py
This file contains 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,46 @@ | ||
from typing import Callable, Optional | ||
from uuid import UUID | ||
|
||
from langchain.callbacks.tracers.base import BaseTracer | ||
from langchain.callbacks.tracers.schemas import Run | ||
|
||
|
||
class RootListenersTracer(BaseTracer): | ||
def __init__( | ||
self, | ||
*, | ||
on_start: Optional[Callable[[Run], None]], | ||
on_end: Optional[Callable[[Run], None]], | ||
on_error: Optional[Callable[[Run], None]] | ||
) -> None: | ||
super().__init__() | ||
|
||
self._arg_on_start = on_start | ||
self._arg_on_end = on_end | ||
self._arg_on_error = on_error | ||
self.root_id: Optional[UUID] = None | ||
|
||
def _persist_run(self, run: Run) -> None: | ||
# This is a legacy method only called once for an entire run tree | ||
# therefore not useful here | ||
pass | ||
|
||
def _on_run_create(self, run: Run) -> None: | ||
if self.root_id is not None: | ||
return | ||
|
||
self.root_id = run.id | ||
|
||
if self._arg_on_start is not None: | ||
self._arg_on_start(run) | ||
|
||
def _on_run_update(self, run: Run) -> None: | ||
if run.id != self.root_id: | ||
return | ||
|
||
if run.error is None: | ||
if self._arg_on_end is not None: | ||
self._arg_on_end(run) | ||
else: | ||
if self._arg_on_error is not None: | ||
self._arg_on_error(run) |
This file contains 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 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 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.