langchain-core 1.0.0a6__py3-none-any.whl → 1.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +51 -64
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +55 -48
- langchain_core/caches.py +65 -66
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +321 -336
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +454 -514
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +53 -68
- langchain_core/document_loaders/base.py +27 -25
- langchain_core/document_loaders/blob_loaders.py +1 -1
- langchain_core/document_loaders/langsmith.py +44 -48
- langchain_core/documents/__init__.py +23 -3
- langchain_core/documents/base.py +102 -94
- langchain_core/documents/compressor.py +10 -10
- langchain_core/documents/transformers.py +34 -35
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +2 -2
- langchain_core/example_selectors/semantic_similarity.py +28 -32
- langchain_core/exceptions.py +21 -20
- langchain_core/globals.py +3 -151
- langchain_core/indexing/__init__.py +1 -1
- langchain_core/indexing/api.py +121 -126
- langchain_core/indexing/base.py +73 -75
- langchain_core/indexing/in_memory.py +4 -6
- langchain_core/language_models/__init__.py +14 -29
- langchain_core/language_models/_utils.py +58 -61
- langchain_core/language_models/base.py +82 -172
- langchain_core/language_models/chat_models.py +329 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +42 -36
- langchain_core/language_models/llms.py +189 -269
- langchain_core/load/dump.py +9 -12
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +42 -40
- langchain_core/messages/__init__.py +10 -16
- langchain_core/messages/ai.py +148 -148
- langchain_core/messages/base.py +53 -51
- langchain_core/messages/block_translators/__init__.py +19 -22
- langchain_core/messages/block_translators/anthropic.py +6 -6
- langchain_core/messages/block_translators/bedrock_converse.py +5 -5
- langchain_core/messages/block_translators/google_genai.py +10 -7
- langchain_core/messages/block_translators/google_vertexai.py +4 -32
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +5 -5
- langchain_core/messages/block_translators/openai.py +11 -11
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +339 -330
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +24 -31
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +19 -29
- langchain_core/messages/tool.py +74 -90
- langchain_core/messages/utils.py +484 -510
- langchain_core/output_parsers/__init__.py +13 -10
- langchain_core/output_parsers/base.py +61 -61
- langchain_core/output_parsers/format_instructions.py +9 -4
- langchain_core/output_parsers/json.py +12 -10
- langchain_core/output_parsers/list.py +21 -23
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +30 -23
- langchain_core/output_parsers/pydantic.py +13 -14
- langchain_core/output_parsers/string.py +5 -5
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +35 -34
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +16 -16
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +13 -19
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +81 -86
- langchain_core/prompts/chat.py +308 -351
- langchain_core/prompts/dict.py +6 -6
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +4 -6
- langchain_core/prompts/message.py +7 -7
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +26 -10
- langchain_core/prompts/structured.py +49 -53
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +61 -198
- langchain_core/runnables/base.py +1551 -1656
- langchain_core/runnables/branch.py +68 -70
- langchain_core/runnables/config.py +72 -89
- langchain_core/runnables/configurable.py +145 -161
- langchain_core/runnables/fallbacks.py +102 -96
- langchain_core/runnables/graph.py +91 -97
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +42 -51
- langchain_core/runnables/graph_png.py +43 -16
- langchain_core/runnables/history.py +175 -177
- langchain_core/runnables/passthrough.py +151 -167
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +30 -35
- langchain_core/runnables/schema.py +75 -80
- langchain_core/runnables/utils.py +60 -67
- langchain_core/stores.py +85 -121
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +29 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +306 -245
- langchain_core/tools/convert.py +160 -155
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +32 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +49 -53
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +65 -64
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +45 -45
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +2 -2
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +94 -188
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +9 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +36 -40
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +9 -9
- langchain_core/utils/mustache.py +8 -10
- langchain_core/utils/pydantic.py +35 -37
- langchain_core/utils/strings.py +6 -9
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +66 -62
- langchain_core/vectorstores/base.py +182 -216
- langchain_core/vectorstores/in_memory.py +101 -176
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- langchain_core-1.0.4.dist-info/METADATA +69 -0
- langchain_core-1.0.4.dist-info/RECORD +172 -0
- {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.4.dist-info}/WHEEL +1 -1
- langchain_core/memory.py +0 -120
- langchain_core/messages/block_translators/ollama.py +0 -47
- langchain_core/prompts/pipeline.py +0 -138
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-1.0.0a6.dist-info/METADATA +0 -67
- langchain_core-1.0.0a6.dist-info/RECORD +0 -181
- langchain_core-1.0.0a6.dist-info/entry_points.txt +0 -4
|
@@ -8,10 +8,11 @@ import functools
|
|
|
8
8
|
import logging
|
|
9
9
|
import uuid
|
|
10
10
|
from abc import ABC, abstractmethod
|
|
11
|
+
from collections.abc import Callable
|
|
11
12
|
from concurrent.futures import ThreadPoolExecutor
|
|
12
13
|
from contextlib import asynccontextmanager, contextmanager
|
|
13
14
|
from contextvars import copy_context
|
|
14
|
-
from typing import TYPE_CHECKING, Any,
|
|
15
|
+
from typing import TYPE_CHECKING, Any, TypeVar, cast
|
|
15
16
|
from uuid import UUID
|
|
16
17
|
|
|
17
18
|
from langsmith.run_helpers import get_tracing_context
|
|
@@ -62,14 +63,14 @@ def _get_debug() -> bool:
|
|
|
62
63
|
@contextmanager
|
|
63
64
|
def trace_as_chain_group(
|
|
64
65
|
group_name: str,
|
|
65
|
-
callback_manager:
|
|
66
|
+
callback_manager: CallbackManager | None = None,
|
|
66
67
|
*,
|
|
67
|
-
inputs:
|
|
68
|
-
project_name:
|
|
69
|
-
example_id:
|
|
70
|
-
run_id:
|
|
71
|
-
tags:
|
|
72
|
-
metadata:
|
|
68
|
+
inputs: dict[str, Any] | None = None,
|
|
69
|
+
project_name: str | None = None,
|
|
70
|
+
example_id: str | UUID | None = None,
|
|
71
|
+
run_id: UUID | None = None,
|
|
72
|
+
tags: list[str] | None = None,
|
|
73
|
+
metadata: dict[str, Any] | None = None,
|
|
73
74
|
) -> Generator[CallbackManagerForChainGroup, None, None]:
|
|
74
75
|
"""Get a callback manager for a chain group in a context manager.
|
|
75
76
|
|
|
@@ -77,39 +78,30 @@ def trace_as_chain_group(
|
|
|
77
78
|
they aren't composed in a single chain.
|
|
78
79
|
|
|
79
80
|
Args:
|
|
80
|
-
group_name
|
|
81
|
-
callback_manager
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
Defaults to None.
|
|
92
|
-
metadata (dict[str, Any], optional): The metadata to apply to all runs.
|
|
93
|
-
Defaults to None.
|
|
94
|
-
|
|
95
|
-
.. note::
|
|
96
|
-
Must have ``LANGCHAIN_TRACING_V2`` env var set to true to see the trace in
|
|
81
|
+
group_name: The name of the chain group.
|
|
82
|
+
callback_manager: The callback manager to use.
|
|
83
|
+
inputs: The inputs to the chain group.
|
|
84
|
+
project_name: The name of the project.
|
|
85
|
+
example_id: The ID of the example.
|
|
86
|
+
run_id: The ID of the run.
|
|
87
|
+
tags: The inheritable tags to apply to all runs.
|
|
88
|
+
metadata: The metadata to apply to all runs.
|
|
89
|
+
|
|
90
|
+
!!! note
|
|
91
|
+
Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
|
|
97
92
|
LangSmith.
|
|
98
93
|
|
|
99
94
|
Yields:
|
|
100
95
|
The callback manager for the chain group.
|
|
101
96
|
|
|
102
97
|
Example:
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
res = llm.invoke(llm_input, {"callbacks": manager})
|
|
111
|
-
manager.on_chain_end({"output": res})
|
|
112
|
-
|
|
98
|
+
```python
|
|
99
|
+
llm_input = "Foo"
|
|
100
|
+
with trace_as_chain_group("group_name", inputs={"input": llm_input}) as manager:
|
|
101
|
+
# Use the callback manager for the chain group
|
|
102
|
+
res = llm.invoke(llm_input, {"callbacks": manager})
|
|
103
|
+
manager.on_chain_end({"output": res})
|
|
104
|
+
```
|
|
113
105
|
"""
|
|
114
106
|
cb = _get_trace_callbacks(
|
|
115
107
|
project_name, example_id, callback_manager=callback_manager
|
|
@@ -146,14 +138,14 @@ def trace_as_chain_group(
|
|
|
146
138
|
@asynccontextmanager
|
|
147
139
|
async def atrace_as_chain_group(
|
|
148
140
|
group_name: str,
|
|
149
|
-
callback_manager:
|
|
141
|
+
callback_manager: AsyncCallbackManager | None = None,
|
|
150
142
|
*,
|
|
151
|
-
inputs:
|
|
152
|
-
project_name:
|
|
153
|
-
example_id:
|
|
154
|
-
run_id:
|
|
155
|
-
tags:
|
|
156
|
-
metadata:
|
|
143
|
+
inputs: dict[str, Any] | None = None,
|
|
144
|
+
project_name: str | None = None,
|
|
145
|
+
example_id: str | UUID | None = None,
|
|
146
|
+
run_id: UUID | None = None,
|
|
147
|
+
tags: list[str] | None = None,
|
|
148
|
+
metadata: dict[str, Any] | None = None,
|
|
157
149
|
) -> AsyncGenerator[AsyncCallbackManagerForChainGroup, None]:
|
|
158
150
|
"""Get an async callback manager for a chain group in a context manager.
|
|
159
151
|
|
|
@@ -161,39 +153,33 @@ async def atrace_as_chain_group(
|
|
|
161
153
|
they aren't composed in a single chain.
|
|
162
154
|
|
|
163
155
|
Args:
|
|
164
|
-
group_name
|
|
165
|
-
callback_manager
|
|
166
|
-
|
|
167
|
-
inputs
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
run_id (UUID, optional): The ID of the run.
|
|
174
|
-
tags (list[str], optional): The inheritable tags to apply to all runs.
|
|
175
|
-
Defaults to None.
|
|
176
|
-
metadata (dict[str, Any], optional): The metadata to apply to all runs.
|
|
177
|
-
Defaults to None.
|
|
156
|
+
group_name: The name of the chain group.
|
|
157
|
+
callback_manager: The async callback manager to use,
|
|
158
|
+
which manages tracing and other callback behavior.
|
|
159
|
+
inputs: The inputs to the chain group.
|
|
160
|
+
project_name: The name of the project.
|
|
161
|
+
example_id: The ID of the example.
|
|
162
|
+
run_id: The ID of the run.
|
|
163
|
+
tags: The inheritable tags to apply to all runs.
|
|
164
|
+
metadata: The metadata to apply to all runs.
|
|
178
165
|
|
|
179
166
|
Yields:
|
|
180
167
|
The async callback manager for the chain group.
|
|
181
168
|
|
|
182
|
-
|
|
183
|
-
Must have
|
|
169
|
+
!!! note
|
|
170
|
+
Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
|
|
184
171
|
LangSmith.
|
|
185
172
|
|
|
186
173
|
Example:
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
174
|
+
```python
|
|
175
|
+
llm_input = "Foo"
|
|
176
|
+
async with atrace_as_chain_group(
|
|
177
|
+
"group_name", inputs={"input": llm_input}
|
|
178
|
+
) as manager:
|
|
179
|
+
# Use the async callback manager for the chain group
|
|
180
|
+
res = await llm.ainvoke(llm_input, {"callbacks": manager})
|
|
181
|
+
await manager.on_chain_end({"output": res})
|
|
182
|
+
```
|
|
197
183
|
"""
|
|
198
184
|
cb = _get_trace_callbacks(
|
|
199
185
|
project_name, example_id, callback_manager=callback_manager
|
|
@@ -234,16 +220,33 @@ def shielded(func: Func) -> Func:
|
|
|
234
220
|
"""Makes so an awaitable method is always shielded from cancellation.
|
|
235
221
|
|
|
236
222
|
Args:
|
|
237
|
-
func
|
|
223
|
+
func: The function to shield.
|
|
238
224
|
|
|
239
225
|
Returns:
|
|
240
|
-
|
|
226
|
+
The shielded function
|
|
241
227
|
|
|
242
228
|
"""
|
|
243
229
|
|
|
244
230
|
@functools.wraps(func)
|
|
245
231
|
async def wrapped(*args: Any, **kwargs: Any) -> Any:
|
|
246
|
-
|
|
232
|
+
# Capture the current context to preserve context variables
|
|
233
|
+
ctx = copy_context()
|
|
234
|
+
|
|
235
|
+
# Create the coroutine
|
|
236
|
+
coro = func(*args, **kwargs)
|
|
237
|
+
|
|
238
|
+
# For Python 3.11+, create task with explicit context
|
|
239
|
+
# For older versions, fallback to original behavior
|
|
240
|
+
try:
|
|
241
|
+
# Create a task with the captured context to preserve context variables
|
|
242
|
+
task = asyncio.create_task(coro, context=ctx) # type: ignore[call-arg, unused-ignore]
|
|
243
|
+
# `call-arg` used to not fail 3.9 or 3.10 tests
|
|
244
|
+
return await asyncio.shield(task)
|
|
245
|
+
except TypeError:
|
|
246
|
+
# Python < 3.11 fallback - create task normally then shield
|
|
247
|
+
# This won't preserve context perfectly but is better than nothing
|
|
248
|
+
task = asyncio.create_task(coro)
|
|
249
|
+
return await asyncio.shield(task)
|
|
247
250
|
|
|
248
251
|
return cast("Func", wrapped)
|
|
249
252
|
|
|
@@ -251,18 +254,18 @@ def shielded(func: Func) -> Func:
|
|
|
251
254
|
def handle_event(
|
|
252
255
|
handlers: list[BaseCallbackHandler],
|
|
253
256
|
event_name: str,
|
|
254
|
-
ignore_condition_name:
|
|
257
|
+
ignore_condition_name: str | None,
|
|
255
258
|
*args: Any,
|
|
256
259
|
**kwargs: Any,
|
|
257
260
|
) -> None:
|
|
258
261
|
"""Generic event handler for CallbackManager.
|
|
259
262
|
|
|
260
|
-
|
|
261
|
-
This function is used by
|
|
263
|
+
!!! note
|
|
264
|
+
This function is used by `LangServe` to handle events.
|
|
262
265
|
|
|
263
266
|
Args:
|
|
264
267
|
handlers: The list of handlers that will handle the event.
|
|
265
|
-
event_name: The name of the event (e.g.,
|
|
268
|
+
event_name: The name of the event (e.g., `'on_llm_start'`).
|
|
266
269
|
ignore_condition_name: Name of the attribute defined on handler
|
|
267
270
|
that if True will cause the handler to be skipped for the given event.
|
|
268
271
|
*args: The arguments to pass to the event handler.
|
|
@@ -272,7 +275,7 @@ def handle_event(
|
|
|
272
275
|
coros: list[Coroutine[Any, Any, Any]] = []
|
|
273
276
|
|
|
274
277
|
try:
|
|
275
|
-
message_strings:
|
|
278
|
+
message_strings: list[str] | None = None
|
|
276
279
|
for handler in handlers:
|
|
277
280
|
try:
|
|
278
281
|
if ignore_condition_name is None or not getattr(
|
|
@@ -366,7 +369,7 @@ def _run_coros(coros: list[Coroutine[Any, Any, Any]]) -> None:
|
|
|
366
369
|
async def _ahandle_event_for_handler(
|
|
367
370
|
handler: BaseCallbackHandler,
|
|
368
371
|
event_name: str,
|
|
369
|
-
ignore_condition_name:
|
|
372
|
+
ignore_condition_name: str | None,
|
|
370
373
|
*args: Any,
|
|
371
374
|
**kwargs: Any,
|
|
372
375
|
) -> None:
|
|
@@ -418,18 +421,18 @@ async def _ahandle_event_for_handler(
|
|
|
418
421
|
async def ahandle_event(
|
|
419
422
|
handlers: list[BaseCallbackHandler],
|
|
420
423
|
event_name: str,
|
|
421
|
-
ignore_condition_name:
|
|
424
|
+
ignore_condition_name: str | None,
|
|
422
425
|
*args: Any,
|
|
423
426
|
**kwargs: Any,
|
|
424
427
|
) -> None:
|
|
425
|
-
"""Async generic event handler for
|
|
428
|
+
"""Async generic event handler for `AsyncCallbackManager`.
|
|
426
429
|
|
|
427
|
-
|
|
428
|
-
This function is used by
|
|
430
|
+
!!! note
|
|
431
|
+
This function is used by `LangServe` to handle events.
|
|
429
432
|
|
|
430
433
|
Args:
|
|
431
434
|
handlers: The list of handlers that will handle the event.
|
|
432
|
-
event_name: The name of the event (e.g.,
|
|
435
|
+
event_name: The name of the event (e.g., `'on_llm_start'`).
|
|
433
436
|
ignore_condition_name: Name of the attribute defined on handler
|
|
434
437
|
that if True will cause the handler to be skipped for the given event.
|
|
435
438
|
*args: The arguments to pass to the event handler.
|
|
@@ -464,28 +467,23 @@ class BaseRunManager(RunManagerMixin):
|
|
|
464
467
|
run_id: UUID,
|
|
465
468
|
handlers: list[BaseCallbackHandler],
|
|
466
469
|
inheritable_handlers: list[BaseCallbackHandler],
|
|
467
|
-
parent_run_id:
|
|
468
|
-
tags:
|
|
469
|
-
inheritable_tags:
|
|
470
|
-
metadata:
|
|
471
|
-
inheritable_metadata:
|
|
470
|
+
parent_run_id: UUID | None = None,
|
|
471
|
+
tags: list[str] | None = None,
|
|
472
|
+
inheritable_tags: list[str] | None = None,
|
|
473
|
+
metadata: dict[str, Any] | None = None,
|
|
474
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
472
475
|
) -> None:
|
|
473
476
|
"""Initialize the run manager.
|
|
474
477
|
|
|
475
478
|
Args:
|
|
476
|
-
run_id
|
|
477
|
-
handlers
|
|
478
|
-
inheritable_handlers
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
Defaults to None.
|
|
485
|
-
metadata (Optional[dict[str, Any]]): The metadata.
|
|
486
|
-
Defaults to None.
|
|
487
|
-
inheritable_metadata (Optional[dict[str, Any]]): The inheritable metadata.
|
|
488
|
-
Defaults to None.
|
|
479
|
+
run_id: The ID of the run.
|
|
480
|
+
handlers: The list of handlers.
|
|
481
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
482
|
+
parent_run_id: The ID of the parent run.
|
|
483
|
+
tags: The list of tags.
|
|
484
|
+
inheritable_tags: The list of inheritable tags.
|
|
485
|
+
metadata: The metadata.
|
|
486
|
+
inheritable_metadata: The inheritable metadata.
|
|
489
487
|
|
|
490
488
|
"""
|
|
491
489
|
self.run_id = run_id
|
|
@@ -502,7 +500,7 @@ class BaseRunManager(RunManagerMixin):
|
|
|
502
500
|
"""Return a manager that doesn't perform any operations.
|
|
503
501
|
|
|
504
502
|
Returns:
|
|
505
|
-
|
|
503
|
+
The noop manager.
|
|
506
504
|
|
|
507
505
|
"""
|
|
508
506
|
return cls(
|
|
@@ -527,8 +525,8 @@ class RunManager(BaseRunManager):
|
|
|
527
525
|
"""Run when a text is received.
|
|
528
526
|
|
|
529
527
|
Args:
|
|
530
|
-
text
|
|
531
|
-
**kwargs
|
|
528
|
+
text: The received text.
|
|
529
|
+
**kwargs: Additional keyword arguments.
|
|
532
530
|
"""
|
|
533
531
|
if not self.handlers:
|
|
534
532
|
return
|
|
@@ -551,8 +549,8 @@ class RunManager(BaseRunManager):
|
|
|
551
549
|
"""Run when a retry is received.
|
|
552
550
|
|
|
553
551
|
Args:
|
|
554
|
-
retry_state
|
|
555
|
-
**kwargs
|
|
552
|
+
retry_state: The retry state.
|
|
553
|
+
**kwargs: Additional keyword arguments.
|
|
556
554
|
|
|
557
555
|
"""
|
|
558
556
|
if not self.handlers:
|
|
@@ -572,15 +570,14 @@ class RunManager(BaseRunManager):
|
|
|
572
570
|
class ParentRunManager(RunManager):
|
|
573
571
|
"""Sync Parent Run Manager."""
|
|
574
572
|
|
|
575
|
-
def get_child(self, tag:
|
|
573
|
+
def get_child(self, tag: str | None = None) -> CallbackManager:
|
|
576
574
|
"""Get a child callback manager.
|
|
577
575
|
|
|
578
576
|
Args:
|
|
579
|
-
tag
|
|
580
|
-
Defaults to None.
|
|
577
|
+
tag: The tag for the child callback manager.
|
|
581
578
|
|
|
582
579
|
Returns:
|
|
583
|
-
|
|
580
|
+
The child callback manager.
|
|
584
581
|
|
|
585
582
|
"""
|
|
586
583
|
manager = CallbackManager(handlers=[], parent_run_id=self.run_id)
|
|
@@ -600,7 +597,7 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
600
597
|
"""Get the equivalent sync RunManager.
|
|
601
598
|
|
|
602
599
|
Returns:
|
|
603
|
-
|
|
600
|
+
The sync RunManager.
|
|
604
601
|
|
|
605
602
|
"""
|
|
606
603
|
|
|
@@ -612,8 +609,8 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
612
609
|
"""Run when a text is received.
|
|
613
610
|
|
|
614
611
|
Args:
|
|
615
|
-
text
|
|
616
|
-
**kwargs
|
|
612
|
+
text: The received text.
|
|
613
|
+
**kwargs: Additional keyword arguments.
|
|
617
614
|
"""
|
|
618
615
|
if not self.handlers:
|
|
619
616
|
return
|
|
@@ -636,8 +633,8 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
636
633
|
"""Async run when a retry is received.
|
|
637
634
|
|
|
638
635
|
Args:
|
|
639
|
-
retry_state
|
|
640
|
-
**kwargs
|
|
636
|
+
retry_state: The retry state.
|
|
637
|
+
**kwargs: Additional keyword arguments.
|
|
641
638
|
|
|
642
639
|
"""
|
|
643
640
|
if not self.handlers:
|
|
@@ -657,15 +654,14 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
657
654
|
class AsyncParentRunManager(AsyncRunManager):
|
|
658
655
|
"""Async Parent Run Manager."""
|
|
659
656
|
|
|
660
|
-
def get_child(self, tag:
|
|
657
|
+
def get_child(self, tag: str | None = None) -> AsyncCallbackManager:
|
|
661
658
|
"""Get a child callback manager.
|
|
662
659
|
|
|
663
660
|
Args:
|
|
664
|
-
tag
|
|
665
|
-
Defaults to None.
|
|
661
|
+
tag: The tag for the child callback manager.
|
|
666
662
|
|
|
667
663
|
Returns:
|
|
668
|
-
|
|
664
|
+
The child callback manager.
|
|
669
665
|
|
|
670
666
|
"""
|
|
671
667
|
manager = AsyncCallbackManager(handlers=[], parent_run_id=self.run_id)
|
|
@@ -684,16 +680,15 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
684
680
|
self,
|
|
685
681
|
token: str,
|
|
686
682
|
*,
|
|
687
|
-
chunk:
|
|
683
|
+
chunk: GenerationChunk | ChatGenerationChunk | None = None,
|
|
688
684
|
**kwargs: Any,
|
|
689
685
|
) -> None:
|
|
690
686
|
"""Run when LLM generates a new token.
|
|
691
687
|
|
|
692
688
|
Args:
|
|
693
|
-
token
|
|
694
|
-
chunk
|
|
695
|
-
|
|
696
|
-
**kwargs (Any): Additional keyword arguments.
|
|
689
|
+
token: The new token.
|
|
690
|
+
chunk: The chunk.
|
|
691
|
+
**kwargs: Additional keyword arguments.
|
|
697
692
|
|
|
698
693
|
"""
|
|
699
694
|
if not self.handlers:
|
|
@@ -714,8 +709,8 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
714
709
|
"""Run when LLM ends running.
|
|
715
710
|
|
|
716
711
|
Args:
|
|
717
|
-
response
|
|
718
|
-
**kwargs
|
|
712
|
+
response: The LLM result.
|
|
713
|
+
**kwargs: Additional keyword arguments.
|
|
719
714
|
|
|
720
715
|
"""
|
|
721
716
|
if not self.handlers:
|
|
@@ -739,8 +734,8 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
739
734
|
"""Run when LLM errors.
|
|
740
735
|
|
|
741
736
|
Args:
|
|
742
|
-
error
|
|
743
|
-
kwargs
|
|
737
|
+
error: The error.
|
|
738
|
+
**kwargs: Additional keyword arguments.
|
|
744
739
|
- response (LLMResult): The response which was generated before
|
|
745
740
|
the error occurred.
|
|
746
741
|
"""
|
|
@@ -765,7 +760,7 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
765
760
|
"""Get the equivalent sync RunManager.
|
|
766
761
|
|
|
767
762
|
Returns:
|
|
768
|
-
|
|
763
|
+
The sync RunManager.
|
|
769
764
|
|
|
770
765
|
"""
|
|
771
766
|
return CallbackManagerForLLMRun(
|
|
@@ -783,16 +778,15 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
783
778
|
self,
|
|
784
779
|
token: str,
|
|
785
780
|
*,
|
|
786
|
-
chunk:
|
|
781
|
+
chunk: GenerationChunk | ChatGenerationChunk | None = None,
|
|
787
782
|
**kwargs: Any,
|
|
788
783
|
) -> None:
|
|
789
784
|
"""Run when LLM generates a new token.
|
|
790
785
|
|
|
791
786
|
Args:
|
|
792
|
-
token
|
|
793
|
-
chunk
|
|
794
|
-
|
|
795
|
-
**kwargs (Any): Additional keyword arguments.
|
|
787
|
+
token: The new token.
|
|
788
|
+
chunk: The chunk.
|
|
789
|
+
**kwargs: Additional keyword arguments.
|
|
796
790
|
|
|
797
791
|
"""
|
|
798
792
|
if not self.handlers:
|
|
@@ -814,8 +808,8 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
814
808
|
"""Run when LLM ends running.
|
|
815
809
|
|
|
816
810
|
Args:
|
|
817
|
-
response
|
|
818
|
-
**kwargs
|
|
811
|
+
response: The LLM result.
|
|
812
|
+
**kwargs: Additional keyword arguments.
|
|
819
813
|
|
|
820
814
|
"""
|
|
821
815
|
if not self.handlers:
|
|
@@ -840,8 +834,8 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
840
834
|
"""Run when LLM errors.
|
|
841
835
|
|
|
842
836
|
Args:
|
|
843
|
-
error
|
|
844
|
-
kwargs
|
|
837
|
+
error: The error.
|
|
838
|
+
**kwargs: Additional keyword arguments.
|
|
845
839
|
- response (LLMResult): The response which was generated before
|
|
846
840
|
the error occurred.
|
|
847
841
|
|
|
@@ -865,12 +859,12 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
865
859
|
class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
866
860
|
"""Callback manager for chain run."""
|
|
867
861
|
|
|
868
|
-
def on_chain_end(self, outputs:
|
|
862
|
+
def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
869
863
|
"""Run when chain ends running.
|
|
870
864
|
|
|
871
865
|
Args:
|
|
872
|
-
outputs
|
|
873
|
-
**kwargs
|
|
866
|
+
outputs: The outputs of the chain.
|
|
867
|
+
**kwargs: Additional keyword arguments.
|
|
874
868
|
|
|
875
869
|
"""
|
|
876
870
|
if not self.handlers:
|
|
@@ -894,8 +888,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
894
888
|
"""Run when chain errors.
|
|
895
889
|
|
|
896
890
|
Args:
|
|
897
|
-
error
|
|
898
|
-
**kwargs
|
|
891
|
+
error: The error.
|
|
892
|
+
**kwargs: Additional keyword arguments.
|
|
899
893
|
|
|
900
894
|
"""
|
|
901
895
|
if not self.handlers:
|
|
@@ -915,8 +909,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
915
909
|
"""Run when agent action is received.
|
|
916
910
|
|
|
917
911
|
Args:
|
|
918
|
-
action
|
|
919
|
-
**kwargs
|
|
912
|
+
action: The agent action.
|
|
913
|
+
**kwargs: Additional keyword arguments.
|
|
920
914
|
"""
|
|
921
915
|
if not self.handlers:
|
|
922
916
|
return
|
|
@@ -935,8 +929,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
935
929
|
"""Run when agent finish is received.
|
|
936
930
|
|
|
937
931
|
Args:
|
|
938
|
-
finish
|
|
939
|
-
**kwargs
|
|
932
|
+
finish: The agent finish.
|
|
933
|
+
**kwargs: Additional keyword arguments.
|
|
940
934
|
"""
|
|
941
935
|
if not self.handlers:
|
|
942
936
|
return
|
|
@@ -959,7 +953,7 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
959
953
|
"""Get the equivalent sync RunManager.
|
|
960
954
|
|
|
961
955
|
Returns:
|
|
962
|
-
|
|
956
|
+
The sync RunManager.
|
|
963
957
|
"""
|
|
964
958
|
return CallbackManagerForChainRun(
|
|
965
959
|
run_id=self.run_id,
|
|
@@ -973,14 +967,12 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
973
967
|
)
|
|
974
968
|
|
|
975
969
|
@shielded
|
|
976
|
-
async def on_chain_end(
|
|
977
|
-
self, outputs: Union[dict[str, Any], Any], **kwargs: Any
|
|
978
|
-
) -> None:
|
|
970
|
+
async def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
979
971
|
"""Run when a chain ends running.
|
|
980
972
|
|
|
981
973
|
Args:
|
|
982
|
-
outputs
|
|
983
|
-
**kwargs
|
|
974
|
+
outputs: The outputs of the chain.
|
|
975
|
+
**kwargs: Additional keyword arguments.
|
|
984
976
|
|
|
985
977
|
"""
|
|
986
978
|
if not self.handlers:
|
|
@@ -1005,8 +997,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1005
997
|
"""Run when chain errors.
|
|
1006
998
|
|
|
1007
999
|
Args:
|
|
1008
|
-
error
|
|
1009
|
-
**kwargs
|
|
1000
|
+
error: The error.
|
|
1001
|
+
**kwargs: Additional keyword arguments.
|
|
1010
1002
|
|
|
1011
1003
|
"""
|
|
1012
1004
|
if not self.handlers:
|
|
@@ -1026,8 +1018,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1026
1018
|
"""Run when agent action is received.
|
|
1027
1019
|
|
|
1028
1020
|
Args:
|
|
1029
|
-
action
|
|
1030
|
-
**kwargs
|
|
1021
|
+
action: The agent action.
|
|
1022
|
+
**kwargs: Additional keyword arguments.
|
|
1031
1023
|
"""
|
|
1032
1024
|
if not self.handlers:
|
|
1033
1025
|
return
|
|
@@ -1046,8 +1038,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1046
1038
|
"""Run when agent finish is received.
|
|
1047
1039
|
|
|
1048
1040
|
Args:
|
|
1049
|
-
finish
|
|
1050
|
-
**kwargs
|
|
1041
|
+
finish: The agent finish.
|
|
1042
|
+
**kwargs: Additional keyword arguments.
|
|
1051
1043
|
"""
|
|
1052
1044
|
if not self.handlers:
|
|
1053
1045
|
return
|
|
@@ -1074,8 +1066,8 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
|
|
|
1074
1066
|
"""Run when the tool ends running.
|
|
1075
1067
|
|
|
1076
1068
|
Args:
|
|
1077
|
-
output
|
|
1078
|
-
**kwargs
|
|
1069
|
+
output: The output of the tool.
|
|
1070
|
+
**kwargs: The keyword arguments to pass to the event handler
|
|
1079
1071
|
|
|
1080
1072
|
"""
|
|
1081
1073
|
if not self.handlers:
|
|
@@ -1099,8 +1091,8 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
|
|
|
1099
1091
|
"""Run when tool errors.
|
|
1100
1092
|
|
|
1101
1093
|
Args:
|
|
1102
|
-
error
|
|
1103
|
-
**kwargs
|
|
1094
|
+
error: The error.
|
|
1095
|
+
**kwargs: Additional keyword arguments.
|
|
1104
1096
|
|
|
1105
1097
|
"""
|
|
1106
1098
|
if not self.handlers:
|
|
@@ -1124,7 +1116,7 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1124
1116
|
"""Get the equivalent sync RunManager.
|
|
1125
1117
|
|
|
1126
1118
|
Returns:
|
|
1127
|
-
|
|
1119
|
+
The sync RunManager.
|
|
1128
1120
|
"""
|
|
1129
1121
|
return CallbackManagerForToolRun(
|
|
1130
1122
|
run_id=self.run_id,
|
|
@@ -1141,8 +1133,8 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1141
1133
|
"""Async run when the tool ends running.
|
|
1142
1134
|
|
|
1143
1135
|
Args:
|
|
1144
|
-
output
|
|
1145
|
-
**kwargs
|
|
1136
|
+
output: The output of the tool.
|
|
1137
|
+
**kwargs: Additional keyword arguments.
|
|
1146
1138
|
|
|
1147
1139
|
"""
|
|
1148
1140
|
if not self.handlers:
|
|
@@ -1166,8 +1158,8 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1166
1158
|
"""Run when tool errors.
|
|
1167
1159
|
|
|
1168
1160
|
Args:
|
|
1169
|
-
error
|
|
1170
|
-
**kwargs
|
|
1161
|
+
error: The error.
|
|
1162
|
+
**kwargs: Additional keyword arguments.
|
|
1171
1163
|
|
|
1172
1164
|
"""
|
|
1173
1165
|
if not self.handlers:
|
|
@@ -1195,8 +1187,8 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
|
|
|
1195
1187
|
"""Run when retriever ends running.
|
|
1196
1188
|
|
|
1197
1189
|
Args:
|
|
1198
|
-
documents
|
|
1199
|
-
**kwargs
|
|
1190
|
+
documents: The retrieved documents.
|
|
1191
|
+
**kwargs: Additional keyword arguments.
|
|
1200
1192
|
|
|
1201
1193
|
"""
|
|
1202
1194
|
if not self.handlers:
|
|
@@ -1220,8 +1212,8 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
|
|
|
1220
1212
|
"""Run when retriever errors.
|
|
1221
1213
|
|
|
1222
1214
|
Args:
|
|
1223
|
-
error
|
|
1224
|
-
**kwargs
|
|
1215
|
+
error: The error.
|
|
1216
|
+
**kwargs: Additional keyword arguments.
|
|
1225
1217
|
|
|
1226
1218
|
"""
|
|
1227
1219
|
if not self.handlers:
|
|
@@ -1248,7 +1240,7 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1248
1240
|
"""Get the equivalent sync RunManager.
|
|
1249
1241
|
|
|
1250
1242
|
Returns:
|
|
1251
|
-
|
|
1243
|
+
The sync RunManager.
|
|
1252
1244
|
|
|
1253
1245
|
"""
|
|
1254
1246
|
return CallbackManagerForRetrieverRun(
|
|
@@ -1269,8 +1261,8 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1269
1261
|
"""Run when the retriever ends running.
|
|
1270
1262
|
|
|
1271
1263
|
Args:
|
|
1272
|
-
documents
|
|
1273
|
-
**kwargs
|
|
1264
|
+
documents: The retrieved documents.
|
|
1265
|
+
**kwargs: Additional keyword arguments.
|
|
1274
1266
|
|
|
1275
1267
|
"""
|
|
1276
1268
|
if not self.handlers:
|
|
@@ -1295,8 +1287,8 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1295
1287
|
"""Run when retriever errors.
|
|
1296
1288
|
|
|
1297
1289
|
Args:
|
|
1298
|
-
error
|
|
1299
|
-
**kwargs
|
|
1290
|
+
error: The error.
|
|
1291
|
+
**kwargs: Additional keyword arguments.
|
|
1300
1292
|
|
|
1301
1293
|
"""
|
|
1302
1294
|
if not self.handlers:
|
|
@@ -1320,20 +1312,19 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1320
1312
|
self,
|
|
1321
1313
|
serialized: dict[str, Any],
|
|
1322
1314
|
prompts: list[str],
|
|
1323
|
-
run_id:
|
|
1315
|
+
run_id: UUID | None = None,
|
|
1324
1316
|
**kwargs: Any,
|
|
1325
1317
|
) -> list[CallbackManagerForLLMRun]:
|
|
1326
1318
|
"""Run when LLM starts running.
|
|
1327
1319
|
|
|
1328
1320
|
Args:
|
|
1329
|
-
serialized
|
|
1330
|
-
prompts
|
|
1331
|
-
run_id
|
|
1332
|
-
**kwargs
|
|
1321
|
+
serialized: The serialized LLM.
|
|
1322
|
+
prompts: The list of prompts.
|
|
1323
|
+
run_id: The ID of the run.
|
|
1324
|
+
**kwargs: Additional keyword arguments.
|
|
1333
1325
|
|
|
1334
1326
|
Returns:
|
|
1335
|
-
|
|
1336
|
-
prompt as an LLM run.
|
|
1327
|
+
A callback manager for each prompt as an LLM run.
|
|
1337
1328
|
|
|
1338
1329
|
"""
|
|
1339
1330
|
managers = []
|
|
@@ -1372,20 +1363,19 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1372
1363
|
self,
|
|
1373
1364
|
serialized: dict[str, Any],
|
|
1374
1365
|
messages: list[list[BaseMessage]],
|
|
1375
|
-
run_id:
|
|
1366
|
+
run_id: UUID | None = None,
|
|
1376
1367
|
**kwargs: Any,
|
|
1377
1368
|
) -> list[CallbackManagerForLLMRun]:
|
|
1378
1369
|
"""Run when chat model starts running.
|
|
1379
1370
|
|
|
1380
1371
|
Args:
|
|
1381
|
-
serialized
|
|
1382
|
-
messages
|
|
1383
|
-
run_id
|
|
1384
|
-
**kwargs
|
|
1372
|
+
serialized: The serialized LLM.
|
|
1373
|
+
messages: The list of messages.
|
|
1374
|
+
run_id: The ID of the run.
|
|
1375
|
+
**kwargs: Additional keyword arguments.
|
|
1385
1376
|
|
|
1386
1377
|
Returns:
|
|
1387
|
-
|
|
1388
|
-
list of messages as an LLM run.
|
|
1378
|
+
A callback manager for each list of messages as an LLM run.
|
|
1389
1379
|
|
|
1390
1380
|
"""
|
|
1391
1381
|
managers = []
|
|
@@ -1425,21 +1415,21 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1425
1415
|
|
|
1426
1416
|
def on_chain_start(
|
|
1427
1417
|
self,
|
|
1428
|
-
serialized:
|
|
1429
|
-
inputs:
|
|
1430
|
-
run_id:
|
|
1418
|
+
serialized: dict[str, Any] | None,
|
|
1419
|
+
inputs: dict[str, Any] | Any,
|
|
1420
|
+
run_id: UUID | None = None,
|
|
1431
1421
|
**kwargs: Any,
|
|
1432
1422
|
) -> CallbackManagerForChainRun:
|
|
1433
1423
|
"""Run when chain starts running.
|
|
1434
1424
|
|
|
1435
1425
|
Args:
|
|
1436
|
-
serialized
|
|
1437
|
-
inputs
|
|
1438
|
-
run_id
|
|
1439
|
-
**kwargs
|
|
1426
|
+
serialized: The serialized chain.
|
|
1427
|
+
inputs: The inputs to the chain.
|
|
1428
|
+
run_id: The ID of the run.
|
|
1429
|
+
**kwargs: Additional keyword arguments.
|
|
1440
1430
|
|
|
1441
1431
|
Returns:
|
|
1442
|
-
|
|
1432
|
+
The callback manager for the chain run.
|
|
1443
1433
|
|
|
1444
1434
|
"""
|
|
1445
1435
|
if run_id is None:
|
|
@@ -1471,11 +1461,11 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1471
1461
|
@override
|
|
1472
1462
|
def on_tool_start(
|
|
1473
1463
|
self,
|
|
1474
|
-
serialized:
|
|
1464
|
+
serialized: dict[str, Any] | None,
|
|
1475
1465
|
input_str: str,
|
|
1476
|
-
run_id:
|
|
1477
|
-
parent_run_id:
|
|
1478
|
-
inputs:
|
|
1466
|
+
run_id: UUID | None = None,
|
|
1467
|
+
parent_run_id: UUID | None = None,
|
|
1468
|
+
inputs: dict[str, Any] | None = None,
|
|
1479
1469
|
**kwargs: Any,
|
|
1480
1470
|
) -> CallbackManagerForToolRun:
|
|
1481
1471
|
"""Run when tool starts running.
|
|
@@ -1484,17 +1474,17 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1484
1474
|
serialized: Serialized representation of the tool.
|
|
1485
1475
|
input_str: The input to the tool as a string.
|
|
1486
1476
|
Non-string inputs are cast to strings.
|
|
1487
|
-
run_id: ID for the run.
|
|
1488
|
-
parent_run_id: The ID of the parent run.
|
|
1477
|
+
run_id: ID for the run.
|
|
1478
|
+
parent_run_id: The ID of the parent run.
|
|
1489
1479
|
inputs: The original input to the tool if provided.
|
|
1490
1480
|
Recommended for usage instead of input_str when the original
|
|
1491
1481
|
input is needed.
|
|
1492
1482
|
If provided, the inputs are expected to be formatted as a dict.
|
|
1493
1483
|
The keys will correspond to the named-arguments in the tool.
|
|
1494
|
-
**kwargs
|
|
1484
|
+
**kwargs: The keyword arguments to pass to the event handler
|
|
1495
1485
|
|
|
1496
1486
|
Returns:
|
|
1497
|
-
|
|
1487
|
+
The callback manager for the tool run.
|
|
1498
1488
|
|
|
1499
1489
|
"""
|
|
1500
1490
|
if run_id is None:
|
|
@@ -1528,20 +1518,20 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1528
1518
|
@override
|
|
1529
1519
|
def on_retriever_start(
|
|
1530
1520
|
self,
|
|
1531
|
-
serialized:
|
|
1521
|
+
serialized: dict[str, Any] | None,
|
|
1532
1522
|
query: str,
|
|
1533
|
-
run_id:
|
|
1534
|
-
parent_run_id:
|
|
1523
|
+
run_id: UUID | None = None,
|
|
1524
|
+
parent_run_id: UUID | None = None,
|
|
1535
1525
|
**kwargs: Any,
|
|
1536
1526
|
) -> CallbackManagerForRetrieverRun:
|
|
1537
1527
|
"""Run when the retriever starts running.
|
|
1538
1528
|
|
|
1539
1529
|
Args:
|
|
1540
|
-
serialized
|
|
1541
|
-
query
|
|
1542
|
-
run_id
|
|
1543
|
-
parent_run_id
|
|
1544
|
-
**kwargs
|
|
1530
|
+
serialized: The serialized retriever.
|
|
1531
|
+
query: The query.
|
|
1532
|
+
run_id: The ID of the run.
|
|
1533
|
+
parent_run_id: The ID of the parent run.
|
|
1534
|
+
**kwargs: Additional keyword arguments.
|
|
1545
1535
|
|
|
1546
1536
|
Returns:
|
|
1547
1537
|
The callback manager for the retriever run.
|
|
@@ -1577,7 +1567,7 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1577
1567
|
self,
|
|
1578
1568
|
name: str,
|
|
1579
1569
|
data: Any,
|
|
1580
|
-
run_id:
|
|
1570
|
+
run_id: UUID | None = None,
|
|
1581
1571
|
**kwargs: Any,
|
|
1582
1572
|
) -> None:
|
|
1583
1573
|
"""Dispatch an adhoc event to the handlers (async version).
|
|
@@ -1589,13 +1579,10 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1589
1579
|
Args:
|
|
1590
1580
|
name: The name of the adhoc event.
|
|
1591
1581
|
data: The data for the adhoc event.
|
|
1592
|
-
run_id: The ID of the run.
|
|
1582
|
+
run_id: The ID of the run.
|
|
1593
1583
|
|
|
1594
1584
|
Raises:
|
|
1595
1585
|
ValueError: If additional keyword arguments are passed.
|
|
1596
|
-
|
|
1597
|
-
.. versionadded:: 0.2.14
|
|
1598
|
-
|
|
1599
1586
|
"""
|
|
1600
1587
|
if not self.handlers:
|
|
1601
1588
|
return
|
|
@@ -1626,31 +1613,24 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1626
1613
|
inheritable_callbacks: Callbacks = None,
|
|
1627
1614
|
local_callbacks: Callbacks = None,
|
|
1628
1615
|
verbose: bool = False, # noqa: FBT001,FBT002
|
|
1629
|
-
inheritable_tags:
|
|
1630
|
-
local_tags:
|
|
1631
|
-
inheritable_metadata:
|
|
1632
|
-
local_metadata:
|
|
1616
|
+
inheritable_tags: list[str] | None = None,
|
|
1617
|
+
local_tags: list[str] | None = None,
|
|
1618
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
1619
|
+
local_metadata: dict[str, Any] | None = None,
|
|
1633
1620
|
) -> CallbackManager:
|
|
1634
1621
|
"""Configure the callback manager.
|
|
1635
1622
|
|
|
1636
1623
|
Args:
|
|
1637
|
-
inheritable_callbacks
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
local_tags (Optional[list[str]], optional): The local tags.
|
|
1645
|
-
Defaults to None.
|
|
1646
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
1647
|
-
metadata. Defaults to None.
|
|
1648
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
1649
|
-
Defaults to None.
|
|
1624
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
1625
|
+
local_callbacks: The local callbacks.
|
|
1626
|
+
verbose: Whether to enable verbose mode.
|
|
1627
|
+
inheritable_tags: The inheritable tags.
|
|
1628
|
+
local_tags: The local tags.
|
|
1629
|
+
inheritable_metadata: The inheritable metadata.
|
|
1630
|
+
local_metadata: The local metadata.
|
|
1650
1631
|
|
|
1651
1632
|
Returns:
|
|
1652
|
-
|
|
1653
|
-
|
|
1633
|
+
The configured callback manager.
|
|
1654
1634
|
"""
|
|
1655
1635
|
return _configure(
|
|
1656
1636
|
cls,
|
|
@@ -1670,8 +1650,8 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1670
1650
|
def __init__(
|
|
1671
1651
|
self,
|
|
1672
1652
|
handlers: list[BaseCallbackHandler],
|
|
1673
|
-
inheritable_handlers:
|
|
1674
|
-
parent_run_id:
|
|
1653
|
+
inheritable_handlers: list[BaseCallbackHandler] | None = None,
|
|
1654
|
+
parent_run_id: UUID | None = None,
|
|
1675
1655
|
*,
|
|
1676
1656
|
parent_run_manager: CallbackManagerForChainRun,
|
|
1677
1657
|
**kwargs: Any,
|
|
@@ -1679,12 +1659,11 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1679
1659
|
"""Initialize the callback manager.
|
|
1680
1660
|
|
|
1681
1661
|
Args:
|
|
1682
|
-
handlers
|
|
1683
|
-
inheritable_handlers
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
**kwargs (Any): Additional keyword arguments.
|
|
1662
|
+
handlers: The list of handlers.
|
|
1663
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
1664
|
+
parent_run_id: The ID of the parent run.
|
|
1665
|
+
parent_run_manager: The parent run manager.
|
|
1666
|
+
**kwargs: Additional keyword arguments.
|
|
1688
1667
|
|
|
1689
1668
|
"""
|
|
1690
1669
|
super().__init__(
|
|
@@ -1719,38 +1698,33 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1719
1698
|
from the current object.
|
|
1720
1699
|
|
|
1721
1700
|
Returns:
|
|
1722
|
-
|
|
1723
|
-
|
|
1701
|
+
A copy of the current object with the handlers, tags, and other attributes
|
|
1702
|
+
merged from the other object.
|
|
1724
1703
|
|
|
1725
1704
|
Example: Merging two callback managers.
|
|
1726
1705
|
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
)
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
)
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
# ]
|
|
1750
|
-
|
|
1751
|
-
print(merged_manager.tags)
|
|
1752
|
-
# ['tag2', 'tag1']
|
|
1753
|
-
|
|
1706
|
+
```python
|
|
1707
|
+
from langchain_core.callbacks.manager import (
|
|
1708
|
+
CallbackManager,
|
|
1709
|
+
trace_as_chain_group,
|
|
1710
|
+
)
|
|
1711
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
1712
|
+
|
|
1713
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
1714
|
+
with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
|
|
1715
|
+
merged_manager = group_manager.merge(manager)
|
|
1716
|
+
print(type(merged_manager))
|
|
1717
|
+
# <class 'langchain_core.callbacks.manager.CallbackManagerForChainGroup'>
|
|
1718
|
+
|
|
1719
|
+
print(merged_manager.handlers)
|
|
1720
|
+
# [
|
|
1721
|
+
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
1722
|
+
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
1723
|
+
# ]
|
|
1724
|
+
|
|
1725
|
+
print(merged_manager.tags)
|
|
1726
|
+
# ['tag2', 'tag1']
|
|
1727
|
+
```
|
|
1754
1728
|
""" # noqa: E501
|
|
1755
1729
|
manager = self.__class__(
|
|
1756
1730
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -1775,12 +1749,12 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1775
1749
|
manager.add_handler(handler, inherit=True)
|
|
1776
1750
|
return manager
|
|
1777
1751
|
|
|
1778
|
-
def on_chain_end(self, outputs:
|
|
1752
|
+
def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
1779
1753
|
"""Run when traced chain group ends.
|
|
1780
1754
|
|
|
1781
1755
|
Args:
|
|
1782
|
-
outputs
|
|
1783
|
-
**kwargs
|
|
1756
|
+
outputs: The outputs of the chain.
|
|
1757
|
+
**kwargs: Additional keyword arguments.
|
|
1784
1758
|
|
|
1785
1759
|
"""
|
|
1786
1760
|
self.ended = True
|
|
@@ -1794,8 +1768,8 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1794
1768
|
"""Run when chain errors.
|
|
1795
1769
|
|
|
1796
1770
|
Args:
|
|
1797
|
-
error
|
|
1798
|
-
**kwargs
|
|
1771
|
+
error: The error.
|
|
1772
|
+
**kwargs: Additional keyword arguments.
|
|
1799
1773
|
|
|
1800
1774
|
"""
|
|
1801
1775
|
self.ended = True
|
|
@@ -1814,21 +1788,20 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1814
1788
|
self,
|
|
1815
1789
|
serialized: dict[str, Any],
|
|
1816
1790
|
prompts: list[str],
|
|
1817
|
-
run_id:
|
|
1791
|
+
run_id: UUID | None = None,
|
|
1818
1792
|
**kwargs: Any,
|
|
1819
1793
|
) -> list[AsyncCallbackManagerForLLMRun]:
|
|
1820
1794
|
"""Run when LLM starts running.
|
|
1821
1795
|
|
|
1822
1796
|
Args:
|
|
1823
|
-
serialized
|
|
1824
|
-
prompts
|
|
1825
|
-
run_id
|
|
1826
|
-
**kwargs
|
|
1797
|
+
serialized: The serialized LLM.
|
|
1798
|
+
prompts: The list of prompts.
|
|
1799
|
+
run_id: The ID of the run.
|
|
1800
|
+
**kwargs: Additional keyword arguments.
|
|
1827
1801
|
|
|
1828
1802
|
Returns:
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
to each prompt.
|
|
1803
|
+
The list of async callback managers, one for each LLM Run corresponding to
|
|
1804
|
+
each prompt.
|
|
1832
1805
|
"""
|
|
1833
1806
|
inline_tasks = []
|
|
1834
1807
|
non_inline_tasks = []
|
|
@@ -1903,21 +1876,20 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1903
1876
|
self,
|
|
1904
1877
|
serialized: dict[str, Any],
|
|
1905
1878
|
messages: list[list[BaseMessage]],
|
|
1906
|
-
run_id:
|
|
1879
|
+
run_id: UUID | None = None,
|
|
1907
1880
|
**kwargs: Any,
|
|
1908
1881
|
) -> list[AsyncCallbackManagerForLLMRun]:
|
|
1909
1882
|
"""Async run when LLM starts running.
|
|
1910
1883
|
|
|
1911
1884
|
Args:
|
|
1912
|
-
serialized
|
|
1913
|
-
messages
|
|
1914
|
-
run_id
|
|
1915
|
-
**kwargs
|
|
1885
|
+
serialized: The serialized LLM.
|
|
1886
|
+
messages: The list of messages.
|
|
1887
|
+
run_id: The ID of the run.
|
|
1888
|
+
**kwargs: Additional keyword arguments.
|
|
1916
1889
|
|
|
1917
1890
|
Returns:
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
corresponding to each inner message list.
|
|
1891
|
+
The list of async callback managers, one for each LLM Run corresponding to
|
|
1892
|
+
each inner message list.
|
|
1921
1893
|
"""
|
|
1922
1894
|
inline_tasks = []
|
|
1923
1895
|
non_inline_tasks = []
|
|
@@ -1973,22 +1945,21 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1973
1945
|
|
|
1974
1946
|
async def on_chain_start(
|
|
1975
1947
|
self,
|
|
1976
|
-
serialized:
|
|
1977
|
-
inputs:
|
|
1978
|
-
run_id:
|
|
1948
|
+
serialized: dict[str, Any] | None,
|
|
1949
|
+
inputs: dict[str, Any] | Any,
|
|
1950
|
+
run_id: UUID | None = None,
|
|
1979
1951
|
**kwargs: Any,
|
|
1980
1952
|
) -> AsyncCallbackManagerForChainRun:
|
|
1981
1953
|
"""Async run when chain starts running.
|
|
1982
1954
|
|
|
1983
1955
|
Args:
|
|
1984
|
-
serialized
|
|
1985
|
-
inputs
|
|
1986
|
-
run_id
|
|
1987
|
-
**kwargs
|
|
1956
|
+
serialized: The serialized chain.
|
|
1957
|
+
inputs: The inputs to the chain.
|
|
1958
|
+
run_id: The ID of the run.
|
|
1959
|
+
**kwargs: Additional keyword arguments.
|
|
1988
1960
|
|
|
1989
1961
|
Returns:
|
|
1990
|
-
|
|
1991
|
-
for the chain run.
|
|
1962
|
+
The async callback manager for the chain run.
|
|
1992
1963
|
"""
|
|
1993
1964
|
if run_id is None:
|
|
1994
1965
|
run_id = uuid.uuid4()
|
|
@@ -2020,25 +1991,23 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2020
1991
|
@override
|
|
2021
1992
|
async def on_tool_start(
|
|
2022
1993
|
self,
|
|
2023
|
-
serialized:
|
|
1994
|
+
serialized: dict[str, Any] | None,
|
|
2024
1995
|
input_str: str,
|
|
2025
|
-
run_id:
|
|
2026
|
-
parent_run_id:
|
|
1996
|
+
run_id: UUID | None = None,
|
|
1997
|
+
parent_run_id: UUID | None = None,
|
|
2027
1998
|
**kwargs: Any,
|
|
2028
1999
|
) -> AsyncCallbackManagerForToolRun:
|
|
2029
2000
|
"""Run when the tool starts running.
|
|
2030
2001
|
|
|
2031
2002
|
Args:
|
|
2032
|
-
serialized
|
|
2033
|
-
input_str
|
|
2034
|
-
run_id
|
|
2035
|
-
parent_run_id
|
|
2036
|
-
|
|
2037
|
-
**kwargs (Any): Additional keyword arguments.
|
|
2003
|
+
serialized: The serialized tool.
|
|
2004
|
+
input_str: The input to the tool.
|
|
2005
|
+
run_id: The ID of the run.
|
|
2006
|
+
parent_run_id: The ID of the parent run.
|
|
2007
|
+
**kwargs: Additional keyword arguments.
|
|
2038
2008
|
|
|
2039
2009
|
Returns:
|
|
2040
|
-
|
|
2041
|
-
for the tool run.
|
|
2010
|
+
The async callback manager for the tool run.
|
|
2042
2011
|
"""
|
|
2043
2012
|
if run_id is None:
|
|
2044
2013
|
run_id = uuid.uuid4()
|
|
@@ -2071,7 +2040,7 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2071
2040
|
self,
|
|
2072
2041
|
name: str,
|
|
2073
2042
|
data: Any,
|
|
2074
|
-
run_id:
|
|
2043
|
+
run_id: UUID | None = None,
|
|
2075
2044
|
**kwargs: Any,
|
|
2076
2045
|
) -> None:
|
|
2077
2046
|
"""Dispatch an adhoc event to the handlers (async version).
|
|
@@ -2083,12 +2052,10 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2083
2052
|
Args:
|
|
2084
2053
|
name: The name of the adhoc event.
|
|
2085
2054
|
data: The data for the adhoc event.
|
|
2086
|
-
run_id: The ID of the run.
|
|
2055
|
+
run_id: The ID of the run.
|
|
2087
2056
|
|
|
2088
2057
|
Raises:
|
|
2089
2058
|
ValueError: If additional keyword arguments are passed.
|
|
2090
|
-
|
|
2091
|
-
.. versionadded:: 0.2.14
|
|
2092
2059
|
"""
|
|
2093
2060
|
if not self.handlers:
|
|
2094
2061
|
return
|
|
@@ -2116,24 +2083,23 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2116
2083
|
@override
|
|
2117
2084
|
async def on_retriever_start(
|
|
2118
2085
|
self,
|
|
2119
|
-
serialized:
|
|
2086
|
+
serialized: dict[str, Any] | None,
|
|
2120
2087
|
query: str,
|
|
2121
|
-
run_id:
|
|
2122
|
-
parent_run_id:
|
|
2088
|
+
run_id: UUID | None = None,
|
|
2089
|
+
parent_run_id: UUID | None = None,
|
|
2123
2090
|
**kwargs: Any,
|
|
2124
2091
|
) -> AsyncCallbackManagerForRetrieverRun:
|
|
2125
2092
|
"""Run when the retriever starts running.
|
|
2126
2093
|
|
|
2127
2094
|
Args:
|
|
2128
|
-
serialized
|
|
2129
|
-
query
|
|
2130
|
-
run_id
|
|
2131
|
-
parent_run_id
|
|
2132
|
-
**kwargs
|
|
2095
|
+
serialized: The serialized retriever.
|
|
2096
|
+
query: The query.
|
|
2097
|
+
run_id: The ID of the run.
|
|
2098
|
+
parent_run_id: The ID of the parent run.
|
|
2099
|
+
**kwargs: Additional keyword arguments.
|
|
2133
2100
|
|
|
2134
2101
|
Returns:
|
|
2135
|
-
|
|
2136
|
-
for the retriever run.
|
|
2102
|
+
The async callback manager for the retriever run.
|
|
2137
2103
|
"""
|
|
2138
2104
|
if run_id is None:
|
|
2139
2105
|
run_id = uuid.uuid4()
|
|
@@ -2168,30 +2134,24 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2168
2134
|
inheritable_callbacks: Callbacks = None,
|
|
2169
2135
|
local_callbacks: Callbacks = None,
|
|
2170
2136
|
verbose: bool = False, # noqa: FBT001,FBT002
|
|
2171
|
-
inheritable_tags:
|
|
2172
|
-
local_tags:
|
|
2173
|
-
inheritable_metadata:
|
|
2174
|
-
local_metadata:
|
|
2137
|
+
inheritable_tags: list[str] | None = None,
|
|
2138
|
+
local_tags: list[str] | None = None,
|
|
2139
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
2140
|
+
local_metadata: dict[str, Any] | None = None,
|
|
2175
2141
|
) -> AsyncCallbackManager:
|
|
2176
2142
|
"""Configure the async callback manager.
|
|
2177
2143
|
|
|
2178
2144
|
Args:
|
|
2179
|
-
inheritable_callbacks
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
local_tags (Optional[list[str]], optional): The local tags.
|
|
2187
|
-
Defaults to None.
|
|
2188
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
2189
|
-
metadata. Defaults to None.
|
|
2190
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
2191
|
-
Defaults to None.
|
|
2145
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
2146
|
+
local_callbacks: The local callbacks.
|
|
2147
|
+
verbose: Whether to enable verbose mode.
|
|
2148
|
+
inheritable_tags: The inheritable tags.
|
|
2149
|
+
local_tags: The local tags.
|
|
2150
|
+
inheritable_metadata: The inheritable metadata.
|
|
2151
|
+
local_metadata: The local metadata.
|
|
2192
2152
|
|
|
2193
2153
|
Returns:
|
|
2194
|
-
|
|
2154
|
+
The configured async callback manager.
|
|
2195
2155
|
"""
|
|
2196
2156
|
return _configure(
|
|
2197
2157
|
cls,
|
|
@@ -2211,8 +2171,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2211
2171
|
def __init__(
|
|
2212
2172
|
self,
|
|
2213
2173
|
handlers: list[BaseCallbackHandler],
|
|
2214
|
-
inheritable_handlers:
|
|
2215
|
-
parent_run_id:
|
|
2174
|
+
inheritable_handlers: list[BaseCallbackHandler] | None = None,
|
|
2175
|
+
parent_run_id: UUID | None = None,
|
|
2216
2176
|
*,
|
|
2217
2177
|
parent_run_manager: AsyncCallbackManagerForChainRun,
|
|
2218
2178
|
**kwargs: Any,
|
|
@@ -2220,13 +2180,11 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2220
2180
|
"""Initialize the async callback manager.
|
|
2221
2181
|
|
|
2222
2182
|
Args:
|
|
2223
|
-
handlers
|
|
2224
|
-
inheritable_handlers
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
The parent run manager.
|
|
2229
|
-
**kwargs (Any): Additional keyword arguments.
|
|
2183
|
+
handlers: The list of handlers.
|
|
2184
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
2185
|
+
parent_run_id: The ID of the parent run.
|
|
2186
|
+
parent_run_manager: The parent run manager.
|
|
2187
|
+
**kwargs: Additional keyword arguments.
|
|
2230
2188
|
"""
|
|
2231
2189
|
super().__init__(
|
|
2232
2190
|
handlers,
|
|
@@ -2265,33 +2223,30 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2265
2223
|
|
|
2266
2224
|
Example: Merging two callback managers.
|
|
2267
2225
|
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
2275
|
-
|
|
2276
|
-
manager = CallbackManager(
|
|
2277
|
-
handlers=[StdOutCallbackHandler()], tags=["tag2"]
|
|
2278
|
-
)
|
|
2279
|
-
async with atrace_as_chain_group(
|
|
2280
|
-
"My Group Name", tags=["tag1"]
|
|
2281
|
-
) as group_manager:
|
|
2282
|
-
merged_manager = group_manager.merge(manager)
|
|
2283
|
-
print(type(merged_manager))
|
|
2284
|
-
# <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
|
|
2285
|
-
|
|
2286
|
-
print(merged_manager.handlers)
|
|
2287
|
-
# [
|
|
2288
|
-
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
2289
|
-
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
2290
|
-
# ]
|
|
2291
|
-
|
|
2292
|
-
print(merged_manager.tags)
|
|
2293
|
-
# ['tag2', 'tag1']
|
|
2226
|
+
```python
|
|
2227
|
+
from langchain_core.callbacks.manager import (
|
|
2228
|
+
CallbackManager,
|
|
2229
|
+
atrace_as_chain_group,
|
|
2230
|
+
)
|
|
2231
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
2294
2232
|
|
|
2233
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
2234
|
+
async with atrace_as_chain_group(
|
|
2235
|
+
"My Group Name", tags=["tag1"]
|
|
2236
|
+
) as group_manager:
|
|
2237
|
+
merged_manager = group_manager.merge(manager)
|
|
2238
|
+
print(type(merged_manager))
|
|
2239
|
+
# <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
|
|
2240
|
+
|
|
2241
|
+
print(merged_manager.handlers)
|
|
2242
|
+
# [
|
|
2243
|
+
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
2244
|
+
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
2245
|
+
# ]
|
|
2246
|
+
|
|
2247
|
+
print(merged_manager.tags)
|
|
2248
|
+
# ['tag2', 'tag1']
|
|
2249
|
+
```
|
|
2295
2250
|
""" # noqa: E501
|
|
2296
2251
|
manager = self.__class__(
|
|
2297
2252
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -2316,14 +2271,12 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2316
2271
|
manager.add_handler(handler, inherit=True)
|
|
2317
2272
|
return manager
|
|
2318
2273
|
|
|
2319
|
-
async def on_chain_end(
|
|
2320
|
-
self, outputs: Union[dict[str, Any], Any], **kwargs: Any
|
|
2321
|
-
) -> None:
|
|
2274
|
+
async def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
2322
2275
|
"""Run when traced chain group ends.
|
|
2323
2276
|
|
|
2324
2277
|
Args:
|
|
2325
|
-
outputs
|
|
2326
|
-
**kwargs
|
|
2278
|
+
outputs: The outputs of the chain.
|
|
2279
|
+
**kwargs: Additional keyword arguments.
|
|
2327
2280
|
"""
|
|
2328
2281
|
self.ended = True
|
|
2329
2282
|
await self.parent_run_manager.on_chain_end(outputs, **kwargs)
|
|
@@ -2336,8 +2289,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2336
2289
|
"""Run when chain errors.
|
|
2337
2290
|
|
|
2338
2291
|
Args:
|
|
2339
|
-
error
|
|
2340
|
-
**kwargs
|
|
2292
|
+
error: The error.
|
|
2293
|
+
**kwargs: Additional keyword arguments.
|
|
2341
2294
|
"""
|
|
2342
2295
|
self.ended = True
|
|
2343
2296
|
await self.parent_run_manager.on_chain_error(error, **kwargs)
|
|
@@ -2350,40 +2303,35 @@ def _configure(
|
|
|
2350
2303
|
callback_manager_cls: type[T],
|
|
2351
2304
|
inheritable_callbacks: Callbacks = None,
|
|
2352
2305
|
local_callbacks: Callbacks = None,
|
|
2353
|
-
inheritable_tags:
|
|
2354
|
-
local_tags:
|
|
2355
|
-
inheritable_metadata:
|
|
2356
|
-
local_metadata:
|
|
2306
|
+
inheritable_tags: list[str] | None = None,
|
|
2307
|
+
local_tags: list[str] | None = None,
|
|
2308
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
2309
|
+
local_metadata: dict[str, Any] | None = None,
|
|
2357
2310
|
*,
|
|
2358
2311
|
verbose: bool = False,
|
|
2359
2312
|
) -> T:
|
|
2360
2313
|
"""Configure the callback manager.
|
|
2361
2314
|
|
|
2362
2315
|
Args:
|
|
2363
|
-
callback_manager_cls
|
|
2364
|
-
inheritable_callbacks
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
local_tags (Optional[list[str]], optional): The local tags. Defaults to None.
|
|
2372
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
2373
|
-
metadata. Defaults to None.
|
|
2374
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
2375
|
-
Defaults to None.
|
|
2316
|
+
callback_manager_cls: The callback manager class.
|
|
2317
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
2318
|
+
local_callbacks: The local callbacks.
|
|
2319
|
+
inheritable_tags: The inheritable tags.
|
|
2320
|
+
local_tags: The local tags.
|
|
2321
|
+
inheritable_metadata: The inheritable metadata.
|
|
2322
|
+
local_metadata: The local metadata.
|
|
2323
|
+
verbose: Whether to enable verbose mode.
|
|
2376
2324
|
|
|
2377
2325
|
Raises:
|
|
2378
2326
|
RuntimeError: If `LANGCHAIN_TRACING` is set but `LANGCHAIN_TRACING_V2` is not.
|
|
2379
2327
|
|
|
2380
2328
|
Returns:
|
|
2381
|
-
|
|
2329
|
+
The configured callback manager.
|
|
2382
2330
|
"""
|
|
2383
2331
|
tracing_context = get_tracing_context()
|
|
2384
2332
|
tracing_metadata = tracing_context["metadata"]
|
|
2385
2333
|
tracing_tags = tracing_context["tags"]
|
|
2386
|
-
run_tree:
|
|
2334
|
+
run_tree: Run | None = tracing_context["parent"]
|
|
2387
2335
|
parent_run_id = None if run_tree is None else run_tree.id
|
|
2388
2336
|
callback_manager = callback_manager_cls(
|
|
2389
2337
|
handlers=[],
|
|
@@ -2528,15 +2476,15 @@ def _configure(
|
|
|
2528
2476
|
|
|
2529
2477
|
|
|
2530
2478
|
async def adispatch_custom_event(
|
|
2531
|
-
name: str, data: Any, *, config:
|
|
2479
|
+
name: str, data: Any, *, config: RunnableConfig | None = None
|
|
2532
2480
|
) -> None:
|
|
2533
2481
|
"""Dispatch an adhoc event to the handlers.
|
|
2534
2482
|
|
|
2535
2483
|
Args:
|
|
2536
2484
|
name: The name of the adhoc event.
|
|
2537
2485
|
data: The data for the adhoc event. Free form data. Ideally should be
|
|
2538
|
-
|
|
2539
|
-
|
|
2486
|
+
JSON serializable to avoid serialization issues downstream, but
|
|
2487
|
+
this is not enforced.
|
|
2540
2488
|
config: Optional config object. Mirrors the async API but not strictly needed.
|
|
2541
2489
|
|
|
2542
2490
|
Raises:
|
|
@@ -2544,85 +2492,81 @@ async def adispatch_custom_event(
|
|
|
2544
2492
|
the event with.
|
|
2545
2493
|
|
|
2546
2494
|
Example:
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2495
|
+
```python
|
|
2496
|
+
from langchain_core.callbacks import (
|
|
2497
|
+
AsyncCallbackHandler,
|
|
2498
|
+
adispatch_custom_event
|
|
2499
|
+
)
|
|
2500
|
+
from langchain_core.runnable import RunnableLambda
|
|
2501
|
+
|
|
2502
|
+
class CustomCallbackManager(AsyncCallbackHandler):
|
|
2503
|
+
async def on_custom_event(
|
|
2504
|
+
self,
|
|
2505
|
+
name: str,
|
|
2506
|
+
data: Any,
|
|
2507
|
+
*,
|
|
2508
|
+
run_id: UUID,
|
|
2509
|
+
tags: list[str] | None = None,
|
|
2510
|
+
metadata: dict[str, Any] | None = None,
|
|
2511
|
+
**kwargs: Any,
|
|
2512
|
+
) -> None:
|
|
2513
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2514
|
+
|
|
2515
|
+
callback = CustomCallbackManager()
|
|
2516
|
+
|
|
2517
|
+
async def foo(inputs):
|
|
2518
|
+
await adispatch_custom_event("my_event", {"bar": "buzz})
|
|
2519
|
+
return inputs
|
|
2520
|
+
|
|
2521
|
+
foo_ = RunnableLambda(foo)
|
|
2522
|
+
await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2523
|
+
```
|
|
2577
2524
|
|
|
2578
2525
|
Example: Use with astream events
|
|
2579
2526
|
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
)
|
|
2615
|
-
|
|
2527
|
+
```python
|
|
2528
|
+
from langchain_core.callbacks import (
|
|
2529
|
+
AsyncCallbackHandler,
|
|
2530
|
+
adispatch_custom_event
|
|
2531
|
+
)
|
|
2532
|
+
from langchain_core.runnable import RunnableLambda
|
|
2533
|
+
|
|
2534
|
+
class CustomCallbackManager(AsyncCallbackHandler):
|
|
2535
|
+
async def on_custom_event(
|
|
2536
|
+
self,
|
|
2537
|
+
name: str,
|
|
2538
|
+
data: Any,
|
|
2539
|
+
*,
|
|
2540
|
+
run_id: UUID,
|
|
2541
|
+
tags: list[str] | None = None,
|
|
2542
|
+
metadata: dict[str, Any] | None = None,
|
|
2543
|
+
**kwargs: Any,
|
|
2544
|
+
) -> None:
|
|
2545
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2546
|
+
|
|
2547
|
+
callback = CustomCallbackManager()
|
|
2548
|
+
|
|
2549
|
+
async def foo(inputs):
|
|
2550
|
+
await adispatch_custom_event("event_type_1", {"bar": "buzz})
|
|
2551
|
+
await adispatch_custom_event("event_type_2", 5)
|
|
2552
|
+
return inputs
|
|
2553
|
+
|
|
2554
|
+
foo_ = RunnableLambda(foo)
|
|
2555
|
+
|
|
2556
|
+
async for event in foo_.ainvoke_stream(
|
|
2557
|
+
{"a": "1"},
|
|
2558
|
+
version="v2",
|
|
2559
|
+
config={"callbacks": [CustomCallbackManager()]}
|
|
2560
|
+
):
|
|
2561
|
+
print(event)
|
|
2562
|
+
```
|
|
2616
2563
|
|
|
2617
|
-
|
|
2564
|
+
!!! warning
|
|
2618
2565
|
If using python <= 3.10 and async, you MUST
|
|
2619
2566
|
specify the `config` parameter or the function will raise an error.
|
|
2620
2567
|
This is due to a limitation in asyncio for python <= 3.10 that prevents
|
|
2621
2568
|
LangChain from automatically propagating the config object on the user's
|
|
2622
2569
|
behalf.
|
|
2623
|
-
|
|
2624
|
-
.. versionadded:: 0.2.15
|
|
2625
|
-
|
|
2626
2570
|
"""
|
|
2627
2571
|
# Import locally to prevent circular imports.
|
|
2628
2572
|
from langchain_core.runnables.config import ( # noqa: PLC0415
|
|
@@ -2654,15 +2598,15 @@ async def adispatch_custom_event(
|
|
|
2654
2598
|
|
|
2655
2599
|
|
|
2656
2600
|
def dispatch_custom_event(
|
|
2657
|
-
name: str, data: Any, *, config:
|
|
2601
|
+
name: str, data: Any, *, config: RunnableConfig | None = None
|
|
2658
2602
|
) -> None:
|
|
2659
2603
|
"""Dispatch an adhoc event.
|
|
2660
2604
|
|
|
2661
2605
|
Args:
|
|
2662
2606
|
name: The name of the adhoc event.
|
|
2663
2607
|
data: The data for the adhoc event. Free form data. Ideally should be
|
|
2664
|
-
|
|
2665
|
-
|
|
2608
|
+
JSON serializable to avoid serialization issues downstream, but
|
|
2609
|
+
this is not enforced.
|
|
2666
2610
|
config: Optional config object. Mirrors the async API but not strictly needed.
|
|
2667
2611
|
|
|
2668
2612
|
Raises:
|
|
@@ -2670,35 +2614,31 @@ def dispatch_custom_event(
|
|
|
2670
2614
|
the event with.
|
|
2671
2615
|
|
|
2672
2616
|
Example:
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2699
|
-
|
|
2700
|
-
.. versionadded:: 0.2.15
|
|
2701
|
-
|
|
2617
|
+
```python
|
|
2618
|
+
from langchain_core.callbacks import BaseCallbackHandler
|
|
2619
|
+
from langchain_core.callbacks import dispatch_custom_event
|
|
2620
|
+
from langchain_core.runnable import RunnableLambda
|
|
2621
|
+
|
|
2622
|
+
class CustomCallbackManager(BaseCallbackHandler):
|
|
2623
|
+
def on_custom_event(
|
|
2624
|
+
self,
|
|
2625
|
+
name: str,
|
|
2626
|
+
data: Any,
|
|
2627
|
+
*,
|
|
2628
|
+
run_id: UUID,
|
|
2629
|
+
tags: list[str] | None = None,
|
|
2630
|
+
metadata: dict[str, Any] | None = None,
|
|
2631
|
+
**kwargs: Any,
|
|
2632
|
+
) -> None:
|
|
2633
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2634
|
+
|
|
2635
|
+
def foo(inputs):
|
|
2636
|
+
dispatch_custom_event("my_event", {"bar": "buzz})
|
|
2637
|
+
return inputs
|
|
2638
|
+
|
|
2639
|
+
foo_ = RunnableLambda(foo)
|
|
2640
|
+
foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2641
|
+
```
|
|
2702
2642
|
"""
|
|
2703
2643
|
# Import locally to prevent circular imports.
|
|
2704
2644
|
from langchain_core.runnables.config import ( # noqa: PLC0415
|