Add Run Collector Callback (#6133)

Add a callback handler that can collect nested run objects. Useful for
evaluation.
This commit is contained in:
Zander Chase 2023-06-13 22:17:37 -07:00 committed by GitHub
parent 6ac120f299
commit 4555ad5d1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,20 @@
"""A tracer that collects all nested runs in a list."""
from typing import Any, List
from langchain.callbacks.tracers.base import BaseTracer
from langchain.callbacks.tracers.schemas import Run
class RunStackCallbackHandler(BaseTracer):
"""A tracer that collects all nested runs in a list.
Useful for inspection and for evaluation."""
name = "run-collector_callback_handler"
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.traced_runs: List[Run] = []
def _persist_run(self, run: Run) -> None:
self.traced_runs.append(run)