langchain-core 0.4.0.dev0__py3-none-any.whl → 1.0.0__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.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +45 -70
- langchain_core/_api/deprecation.py +80 -80
- langchain_core/_api/path.py +22 -8
- langchain_core/_import_utils.py +10 -4
- langchain_core/agents.py +25 -21
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +341 -348
- langchain_core/callbacks/file.py +55 -44
- langchain_core/callbacks/manager.py +546 -683
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +35 -36
- langchain_core/callbacks/usage.py +65 -70
- langchain_core/chat_history.py +48 -55
- langchain_core/document_loaders/base.py +46 -21
- langchain_core/document_loaders/langsmith.py +39 -36
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +96 -74
- langchain_core/documents/compressor.py +12 -9
- langchain_core/documents/transformers.py +29 -28
- langchain_core/embeddings/fake.py +56 -57
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +15 -9
- langchain_core/globals.py +4 -163
- langchain_core/indexing/api.py +132 -125
- langchain_core/indexing/base.py +64 -67
- langchain_core/indexing/in_memory.py +26 -6
- langchain_core/language_models/__init__.py +15 -27
- langchain_core/language_models/_utils.py +267 -117
- langchain_core/language_models/base.py +92 -177
- langchain_core/language_models/chat_models.py +547 -407
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +72 -118
- langchain_core/language_models/llms.py +168 -242
- langchain_core/load/dump.py +8 -11
- langchain_core/load/load.py +32 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +50 -56
- langchain_core/messages/__init__.py +36 -51
- langchain_core/messages/ai.py +377 -150
- langchain_core/messages/base.py +239 -47
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -3
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +7 -7
- langchain_core/messages/human.py +44 -38
- langchain_core/messages/modifier.py +3 -2
- langchain_core/messages/system.py +40 -27
- langchain_core/messages/tool.py +160 -58
- langchain_core/messages/utils.py +527 -638
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +68 -104
- langchain_core/output_parsers/json.py +13 -17
- langchain_core/output_parsers/list.py +11 -33
- langchain_core/output_parsers/openai_functions.py +56 -74
- langchain_core/output_parsers/openai_tools.py +68 -109
- langchain_core/output_parsers/pydantic.py +15 -13
- langchain_core/output_parsers/string.py +6 -2
- langchain_core/output_parsers/transform.py +17 -60
- langchain_core/output_parsers/xml.py +34 -44
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +26 -11
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +17 -6
- langchain_core/outputs/llm_result.py +15 -8
- langchain_core/prompt_values.py +29 -123
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -63
- langchain_core/prompts/chat.py +259 -288
- langchain_core/prompts/dict.py +19 -11
- langchain_core/prompts/few_shot.py +84 -90
- langchain_core/prompts/few_shot_with_templates.py +14 -12
- langchain_core/prompts/image.py +19 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +7 -8
- langchain_core/prompts/prompt.py +42 -43
- langchain_core/prompts/string.py +37 -16
- langchain_core/prompts/structured.py +43 -46
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +52 -192
- langchain_core/runnables/base.py +1727 -1683
- langchain_core/runnables/branch.py +52 -73
- langchain_core/runnables/config.py +89 -103
- langchain_core/runnables/configurable.py +128 -130
- langchain_core/runnables/fallbacks.py +93 -82
- langchain_core/runnables/graph.py +127 -127
- langchain_core/runnables/graph_ascii.py +63 -41
- langchain_core/runnables/graph_mermaid.py +87 -70
- langchain_core/runnables/graph_png.py +31 -36
- langchain_core/runnables/history.py +145 -161
- langchain_core/runnables/passthrough.py +141 -144
- langchain_core/runnables/retry.py +84 -68
- langchain_core/runnables/router.py +33 -37
- langchain_core/runnables/schema.py +79 -72
- langchain_core/runnables/utils.py +95 -139
- langchain_core/stores.py +85 -131
- langchain_core/structured_query.py +11 -15
- langchain_core/sys_info.py +31 -32
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +221 -247
- langchain_core/tools/convert.py +144 -161
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -19
- langchain_core/tools/simple.py +52 -29
- langchain_core/tools/structured.py +56 -60
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +103 -112
- langchain_core/tracers/context.py +29 -48
- langchain_core/tracers/core.py +142 -105
- langchain_core/tracers/evaluation.py +30 -34
- langchain_core/tracers/event_stream.py +162 -117
- langchain_core/tracers/langchain.py +34 -36
- langchain_core/tracers/log_stream.py +87 -49
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +18 -34
- langchain_core/tracers/run_collector.py +8 -20
- langchain_core/tracers/schemas.py +0 -125
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +47 -9
- langchain_core/utils/aiter.py +70 -66
- langchain_core/utils/env.py +12 -9
- langchain_core/utils/function_calling.py +139 -206
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +48 -45
- langchain_core/utils/json.py +14 -4
- langchain_core/utils/json_schema.py +159 -43
- langchain_core/utils/mustache.py +32 -25
- langchain_core/utils/pydantic.py +67 -40
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +104 -62
- langchain_core/vectorstores/base.py +131 -179
- langchain_core/vectorstores/in_memory.py +113 -182
- langchain_core/vectorstores/utils.py +23 -17
- langchain_core/version.py +1 -1
- langchain_core-1.0.0.dist-info/METADATA +68 -0
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -448
- langchain_core/memory.py +0 -116
- langchain_core/messages/content_blocks.py +0 -1435
- langchain_core/prompts/pipeline.py +0 -133
- 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 -23
- langchain_core/utils/loading.py +0 -31
- langchain_core/v1/__init__.py +0 -1
- langchain_core/v1/chat_models.py +0 -1047
- langchain_core/v1/messages.py +0 -755
- langchain_core-0.4.0.dev0.dist-info/METADATA +0 -108
- langchain_core-0.4.0.dev0.dist-info/RECORD +0 -177
- langchain_core-0.4.0.dev0.dist-info/entry_points.txt +0 -4
|
@@ -8,18 +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
|
|
15
|
-
TYPE_CHECKING,
|
|
16
|
-
Any,
|
|
17
|
-
Callable,
|
|
18
|
-
Optional,
|
|
19
|
-
TypeVar,
|
|
20
|
-
Union,
|
|
21
|
-
cast,
|
|
22
|
-
)
|
|
15
|
+
from typing import TYPE_CHECKING, Any, TypeVar, cast
|
|
23
16
|
from uuid import UUID
|
|
24
17
|
|
|
25
18
|
from langsmith.run_helpers import get_tracing_context
|
|
@@ -36,17 +29,19 @@ from langchain_core.callbacks.base import (
|
|
|
36
29
|
ToolManagerMixin,
|
|
37
30
|
)
|
|
38
31
|
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
32
|
+
from langchain_core.globals import get_debug
|
|
39
33
|
from langchain_core.messages import BaseMessage, get_buffer_string
|
|
40
|
-
from langchain_core.
|
|
41
|
-
|
|
34
|
+
from langchain_core.tracers.context import (
|
|
35
|
+
_configure_hooks,
|
|
36
|
+
_get_trace_callbacks,
|
|
37
|
+
_get_tracer_project,
|
|
38
|
+
_tracing_v2_is_enabled,
|
|
39
|
+
tracing_v2_callback_var,
|
|
40
|
+
)
|
|
41
|
+
from langchain_core.tracers.langchain import LangChainTracer
|
|
42
42
|
from langchain_core.tracers.schemas import Run
|
|
43
|
+
from langchain_core.tracers.stdout import ConsoleCallbackHandler
|
|
43
44
|
from langchain_core.utils.env import env_var_is_set
|
|
44
|
-
from langchain_core.v1.messages import (
|
|
45
|
-
AIMessage,
|
|
46
|
-
AIMessageChunk,
|
|
47
|
-
MessageV1,
|
|
48
|
-
MessageV1Types,
|
|
49
|
-
)
|
|
50
45
|
|
|
51
46
|
if TYPE_CHECKING:
|
|
52
47
|
from collections.abc import AsyncGenerator, Coroutine, Generator, Sequence
|
|
@@ -55,29 +50,27 @@ if TYPE_CHECKING:
|
|
|
55
50
|
|
|
56
51
|
from langchain_core.agents import AgentAction, AgentFinish
|
|
57
52
|
from langchain_core.documents import Document
|
|
58
|
-
from langchain_core.outputs import GenerationChunk
|
|
53
|
+
from langchain_core.outputs import ChatGenerationChunk, GenerationChunk, LLMResult
|
|
59
54
|
from langchain_core.runnables.config import RunnableConfig
|
|
60
55
|
|
|
61
56
|
logger = logging.getLogger(__name__)
|
|
62
57
|
|
|
63
58
|
|
|
64
59
|
def _get_debug() -> bool:
|
|
65
|
-
from langchain_core.globals import get_debug
|
|
66
|
-
|
|
67
60
|
return get_debug()
|
|
68
61
|
|
|
69
62
|
|
|
70
63
|
@contextmanager
|
|
71
64
|
def trace_as_chain_group(
|
|
72
65
|
group_name: str,
|
|
73
|
-
callback_manager:
|
|
66
|
+
callback_manager: CallbackManager | None = None,
|
|
74
67
|
*,
|
|
75
|
-
inputs:
|
|
76
|
-
project_name:
|
|
77
|
-
example_id:
|
|
78
|
-
run_id:
|
|
79
|
-
tags:
|
|
80
|
-
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,
|
|
81
74
|
) -> Generator[CallbackManagerForChainGroup, None, None]:
|
|
82
75
|
"""Get a callback manager for a chain group in a context manager.
|
|
83
76
|
|
|
@@ -85,38 +78,31 @@ def trace_as_chain_group(
|
|
|
85
78
|
they aren't composed in a single chain.
|
|
86
79
|
|
|
87
80
|
Args:
|
|
88
|
-
group_name
|
|
89
|
-
callback_manager
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
Note: must have LANGCHAIN_TRACING_V2 env var set to true to see the trace in LangSmith.
|
|
104
|
-
|
|
105
|
-
Returns:
|
|
106
|
-
CallbackManagerForChainGroup: The callback manager for the chain group.
|
|
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
|
|
92
|
+
LangSmith.
|
|
93
|
+
|
|
94
|
+
Yields:
|
|
95
|
+
The callback manager for the chain group.
|
|
107
96
|
|
|
108
97
|
Example:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
""" # noqa: E501
|
|
118
|
-
from langchain_core.tracers.context import _get_trace_callbacks
|
|
119
|
-
|
|
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
|
+
```
|
|
105
|
+
"""
|
|
120
106
|
cb = _get_trace_callbacks(
|
|
121
107
|
project_name, example_id, callback_manager=callback_manager
|
|
122
108
|
)
|
|
@@ -152,14 +138,14 @@ def trace_as_chain_group(
|
|
|
152
138
|
@asynccontextmanager
|
|
153
139
|
async def atrace_as_chain_group(
|
|
154
140
|
group_name: str,
|
|
155
|
-
callback_manager:
|
|
141
|
+
callback_manager: AsyncCallbackManager | None = None,
|
|
156
142
|
*,
|
|
157
|
-
inputs:
|
|
158
|
-
project_name:
|
|
159
|
-
example_id:
|
|
160
|
-
run_id:
|
|
161
|
-
tags:
|
|
162
|
-
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,
|
|
163
149
|
) -> AsyncGenerator[AsyncCallbackManagerForChainGroup, None]:
|
|
164
150
|
"""Get an async callback manager for a chain group in a context manager.
|
|
165
151
|
|
|
@@ -167,38 +153,34 @@ async def atrace_as_chain_group(
|
|
|
167
153
|
they aren't composed in a single chain.
|
|
168
154
|
|
|
169
155
|
Args:
|
|
170
|
-
group_name
|
|
171
|
-
callback_manager
|
|
172
|
-
which manages tracing and other callback behavior.
|
|
173
|
-
inputs
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
AsyncCallbackManager: The async callback manager for the chain group.
|
|
187
|
-
|
|
188
|
-
Note: must have LANGCHAIN_TRACING_V2 env var set to true to see the trace in LangSmith.
|
|
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.
|
|
165
|
+
|
|
166
|
+
Yields:
|
|
167
|
+
The async callback manager for the chain group.
|
|
168
|
+
|
|
169
|
+
!!! note
|
|
170
|
+
Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
|
|
171
|
+
LangSmith.
|
|
189
172
|
|
|
190
173
|
Example:
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
+
```
|
|
183
|
+
"""
|
|
202
184
|
cb = _get_trace_callbacks(
|
|
203
185
|
project_name, example_id, callback_manager=callback_manager
|
|
204
186
|
)
|
|
@@ -238,10 +220,11 @@ def shielded(func: Func) -> Func:
|
|
|
238
220
|
"""Makes so an awaitable method is always shielded from cancellation.
|
|
239
221
|
|
|
240
222
|
Args:
|
|
241
|
-
func
|
|
223
|
+
func: The function to shield.
|
|
242
224
|
|
|
243
225
|
Returns:
|
|
244
|
-
|
|
226
|
+
The shielded function
|
|
227
|
+
|
|
245
228
|
"""
|
|
246
229
|
|
|
247
230
|
@functools.wraps(func)
|
|
@@ -251,76 +234,36 @@ def shielded(func: Func) -> Func:
|
|
|
251
234
|
return cast("Func", wrapped)
|
|
252
235
|
|
|
253
236
|
|
|
254
|
-
def _convert_llm_events(
|
|
255
|
-
event_name: str, args: tuple[Any, ...], kwargs: dict[str, Any]
|
|
256
|
-
) -> tuple[tuple[Any, ...], dict[str, Any]]:
|
|
257
|
-
args_list = list(args)
|
|
258
|
-
if (
|
|
259
|
-
event_name == "on_chat_model_start"
|
|
260
|
-
and isinstance(args_list[1], list)
|
|
261
|
-
and args_list[1]
|
|
262
|
-
and isinstance(args_list[1][0], MessageV1Types)
|
|
263
|
-
):
|
|
264
|
-
batch = [
|
|
265
|
-
convert_from_v1_message(item)
|
|
266
|
-
for item in args_list[1]
|
|
267
|
-
if isinstance(item, MessageV1Types)
|
|
268
|
-
]
|
|
269
|
-
args_list[1] = [batch]
|
|
270
|
-
elif (
|
|
271
|
-
event_name == "on_llm_new_token"
|
|
272
|
-
and "chunk" in kwargs
|
|
273
|
-
and isinstance(kwargs["chunk"], MessageV1Types)
|
|
274
|
-
):
|
|
275
|
-
chunk = kwargs["chunk"]
|
|
276
|
-
kwargs["chunk"] = ChatGenerationChunk(text=chunk.text, message=chunk)
|
|
277
|
-
elif event_name == "on_llm_end" and isinstance(args_list[0], MessageV1Types):
|
|
278
|
-
args_list[0] = LLMResult(
|
|
279
|
-
generations=[
|
|
280
|
-
[
|
|
281
|
-
ChatGeneration(
|
|
282
|
-
text=args_list[0].text,
|
|
283
|
-
message=convert_from_v1_message(args_list[0]),
|
|
284
|
-
)
|
|
285
|
-
]
|
|
286
|
-
]
|
|
287
|
-
)
|
|
288
|
-
else:
|
|
289
|
-
pass
|
|
290
|
-
|
|
291
|
-
return tuple(args_list), kwargs
|
|
292
|
-
|
|
293
|
-
|
|
294
237
|
def handle_event(
|
|
295
238
|
handlers: list[BaseCallbackHandler],
|
|
296
239
|
event_name: str,
|
|
297
|
-
ignore_condition_name:
|
|
240
|
+
ignore_condition_name: str | None,
|
|
298
241
|
*args: Any,
|
|
299
242
|
**kwargs: Any,
|
|
300
243
|
) -> None:
|
|
301
244
|
"""Generic event handler for CallbackManager.
|
|
302
245
|
|
|
303
|
-
|
|
246
|
+
!!! note
|
|
247
|
+
This function is used by `LangServe` to handle events.
|
|
304
248
|
|
|
305
249
|
Args:
|
|
306
250
|
handlers: The list of handlers that will handle the event.
|
|
307
|
-
event_name: The name of the event (e.g.,
|
|
251
|
+
event_name: The name of the event (e.g., `'on_llm_start'`).
|
|
308
252
|
ignore_condition_name: Name of the attribute defined on handler
|
|
309
253
|
that if True will cause the handler to be skipped for the given event.
|
|
310
254
|
*args: The arguments to pass to the event handler.
|
|
311
255
|
**kwargs: The keyword arguments to pass to the event handler
|
|
256
|
+
|
|
312
257
|
"""
|
|
313
258
|
coros: list[Coroutine[Any, Any, Any]] = []
|
|
314
259
|
|
|
315
260
|
try:
|
|
316
|
-
message_strings:
|
|
261
|
+
message_strings: list[str] | None = None
|
|
317
262
|
for handler in handlers:
|
|
318
263
|
try:
|
|
319
264
|
if ignore_condition_name is None or not getattr(
|
|
320
265
|
handler, ignore_condition_name
|
|
321
266
|
):
|
|
322
|
-
if not handler.accepts_new_messages:
|
|
323
|
-
args, kwargs = _convert_llm_events(event_name, args, kwargs)
|
|
324
267
|
event = getattr(handler, event_name)(*args, **kwargs)
|
|
325
268
|
if asyncio.iscoroutine(event):
|
|
326
269
|
coros.append(event)
|
|
@@ -409,14 +352,12 @@ def _run_coros(coros: list[Coroutine[Any, Any, Any]]) -> None:
|
|
|
409
352
|
async def _ahandle_event_for_handler(
|
|
410
353
|
handler: BaseCallbackHandler,
|
|
411
354
|
event_name: str,
|
|
412
|
-
ignore_condition_name:
|
|
355
|
+
ignore_condition_name: str | None,
|
|
413
356
|
*args: Any,
|
|
414
357
|
**kwargs: Any,
|
|
415
358
|
) -> None:
|
|
416
359
|
try:
|
|
417
360
|
if ignore_condition_name is None or not getattr(handler, ignore_condition_name):
|
|
418
|
-
if not handler.accepts_new_messages:
|
|
419
|
-
args, kwargs = _convert_llm_events(event_name, args, kwargs)
|
|
420
361
|
event = getattr(handler, event_name)
|
|
421
362
|
if asyncio.iscoroutinefunction(event):
|
|
422
363
|
await event(*args, **kwargs)
|
|
@@ -463,21 +404,23 @@ async def _ahandle_event_for_handler(
|
|
|
463
404
|
async def ahandle_event(
|
|
464
405
|
handlers: list[BaseCallbackHandler],
|
|
465
406
|
event_name: str,
|
|
466
|
-
ignore_condition_name:
|
|
407
|
+
ignore_condition_name: str | None,
|
|
467
408
|
*args: Any,
|
|
468
409
|
**kwargs: Any,
|
|
469
410
|
) -> None:
|
|
470
|
-
"""Async generic event handler for AsyncCallbackManager
|
|
411
|
+
"""Async generic event handler for `AsyncCallbackManager`.
|
|
471
412
|
|
|
472
|
-
|
|
413
|
+
!!! note
|
|
414
|
+
This function is used by `LangServe` to handle events.
|
|
473
415
|
|
|
474
416
|
Args:
|
|
475
417
|
handlers: The list of handlers that will handle the event.
|
|
476
|
-
event_name: The name of the event (e.g.,
|
|
418
|
+
event_name: The name of the event (e.g., `'on_llm_start'`).
|
|
477
419
|
ignore_condition_name: Name of the attribute defined on handler
|
|
478
420
|
that if True will cause the handler to be skipped for the given event.
|
|
479
421
|
*args: The arguments to pass to the event handler.
|
|
480
422
|
**kwargs: The keyword arguments to pass to the event handler.
|
|
423
|
+
|
|
481
424
|
"""
|
|
482
425
|
for handler in [h for h in handlers if h.run_inline]:
|
|
483
426
|
await _ahandle_event_for_handler(
|
|
@@ -507,28 +450,24 @@ class BaseRunManager(RunManagerMixin):
|
|
|
507
450
|
run_id: UUID,
|
|
508
451
|
handlers: list[BaseCallbackHandler],
|
|
509
452
|
inheritable_handlers: list[BaseCallbackHandler],
|
|
510
|
-
parent_run_id:
|
|
511
|
-
tags:
|
|
512
|
-
inheritable_tags:
|
|
513
|
-
metadata:
|
|
514
|
-
inheritable_metadata:
|
|
453
|
+
parent_run_id: UUID | None = None,
|
|
454
|
+
tags: list[str] | None = None,
|
|
455
|
+
inheritable_tags: list[str] | None = None,
|
|
456
|
+
metadata: dict[str, Any] | None = None,
|
|
457
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
515
458
|
) -> None:
|
|
516
459
|
"""Initialize the run manager.
|
|
517
460
|
|
|
518
461
|
Args:
|
|
519
|
-
run_id
|
|
520
|
-
handlers
|
|
521
|
-
inheritable_handlers
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
metadata (Optional[dict[str, Any]]): The metadata.
|
|
529
|
-
Defaults to None.
|
|
530
|
-
inheritable_metadata (Optional[dict[str, Any]]): The inheritable metadata.
|
|
531
|
-
Defaults to None.
|
|
462
|
+
run_id: The ID of the run.
|
|
463
|
+
handlers: The list of handlers.
|
|
464
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
465
|
+
parent_run_id: The ID of the parent run.
|
|
466
|
+
tags: The list of tags.
|
|
467
|
+
inheritable_tags: The list of inheritable tags.
|
|
468
|
+
metadata: The metadata.
|
|
469
|
+
inheritable_metadata: The inheritable metadata.
|
|
470
|
+
|
|
532
471
|
"""
|
|
533
472
|
self.run_id = run_id
|
|
534
473
|
self.handlers = handlers
|
|
@@ -544,7 +483,8 @@ class BaseRunManager(RunManagerMixin):
|
|
|
544
483
|
"""Return a manager that doesn't perform any operations.
|
|
545
484
|
|
|
546
485
|
Returns:
|
|
547
|
-
|
|
486
|
+
The noop manager.
|
|
487
|
+
|
|
548
488
|
"""
|
|
549
489
|
return cls(
|
|
550
490
|
run_id=uuid.uuid4(),
|
|
@@ -564,15 +504,12 @@ class RunManager(BaseRunManager):
|
|
|
564
504
|
self,
|
|
565
505
|
text: str,
|
|
566
506
|
**kwargs: Any,
|
|
567
|
-
) ->
|
|
507
|
+
) -> None:
|
|
568
508
|
"""Run when a text is received.
|
|
569
509
|
|
|
570
510
|
Args:
|
|
571
|
-
text
|
|
572
|
-
**kwargs
|
|
573
|
-
|
|
574
|
-
Returns:
|
|
575
|
-
Any: The result of the callback.
|
|
511
|
+
text: The received text.
|
|
512
|
+
**kwargs: Additional keyword arguments.
|
|
576
513
|
"""
|
|
577
514
|
if not self.handlers:
|
|
578
515
|
return
|
|
@@ -595,8 +532,9 @@ class RunManager(BaseRunManager):
|
|
|
595
532
|
"""Run when a retry is received.
|
|
596
533
|
|
|
597
534
|
Args:
|
|
598
|
-
retry_state
|
|
599
|
-
**kwargs
|
|
535
|
+
retry_state: The retry state.
|
|
536
|
+
**kwargs: Additional keyword arguments.
|
|
537
|
+
|
|
600
538
|
"""
|
|
601
539
|
if not self.handlers:
|
|
602
540
|
return
|
|
@@ -615,15 +553,15 @@ class RunManager(BaseRunManager):
|
|
|
615
553
|
class ParentRunManager(RunManager):
|
|
616
554
|
"""Sync Parent Run Manager."""
|
|
617
555
|
|
|
618
|
-
def get_child(self, tag:
|
|
556
|
+
def get_child(self, tag: str | None = None) -> CallbackManager:
|
|
619
557
|
"""Get a child callback manager.
|
|
620
558
|
|
|
621
559
|
Args:
|
|
622
|
-
tag
|
|
623
|
-
Defaults to None.
|
|
560
|
+
tag: The tag for the child callback manager.
|
|
624
561
|
|
|
625
562
|
Returns:
|
|
626
|
-
|
|
563
|
+
The child callback manager.
|
|
564
|
+
|
|
627
565
|
"""
|
|
628
566
|
manager = CallbackManager(handlers=[], parent_run_id=self.run_id)
|
|
629
567
|
manager.set_handlers(self.inheritable_handlers)
|
|
@@ -642,22 +580,20 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
642
580
|
"""Get the equivalent sync RunManager.
|
|
643
581
|
|
|
644
582
|
Returns:
|
|
645
|
-
|
|
583
|
+
The sync RunManager.
|
|
584
|
+
|
|
646
585
|
"""
|
|
647
586
|
|
|
648
587
|
async def on_text(
|
|
649
588
|
self,
|
|
650
589
|
text: str,
|
|
651
590
|
**kwargs: Any,
|
|
652
|
-
) ->
|
|
591
|
+
) -> None:
|
|
653
592
|
"""Run when a text is received.
|
|
654
593
|
|
|
655
594
|
Args:
|
|
656
|
-
text
|
|
657
|
-
**kwargs
|
|
658
|
-
|
|
659
|
-
Returns:
|
|
660
|
-
Any: The result of the callback.
|
|
595
|
+
text: The received text.
|
|
596
|
+
**kwargs: Additional keyword arguments.
|
|
661
597
|
"""
|
|
662
598
|
if not self.handlers:
|
|
663
599
|
return
|
|
@@ -680,8 +616,9 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
680
616
|
"""Async run when a retry is received.
|
|
681
617
|
|
|
682
618
|
Args:
|
|
683
|
-
retry_state
|
|
684
|
-
**kwargs
|
|
619
|
+
retry_state: The retry state.
|
|
620
|
+
**kwargs: Additional keyword arguments.
|
|
621
|
+
|
|
685
622
|
"""
|
|
686
623
|
if not self.handlers:
|
|
687
624
|
return
|
|
@@ -700,15 +637,15 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
700
637
|
class AsyncParentRunManager(AsyncRunManager):
|
|
701
638
|
"""Async Parent Run Manager."""
|
|
702
639
|
|
|
703
|
-
def get_child(self, tag:
|
|
640
|
+
def get_child(self, tag: str | None = None) -> AsyncCallbackManager:
|
|
704
641
|
"""Get a child callback manager.
|
|
705
642
|
|
|
706
643
|
Args:
|
|
707
|
-
tag
|
|
708
|
-
Defaults to None.
|
|
644
|
+
tag: The tag for the child callback manager.
|
|
709
645
|
|
|
710
646
|
Returns:
|
|
711
|
-
|
|
647
|
+
The child callback manager.
|
|
648
|
+
|
|
712
649
|
"""
|
|
713
650
|
manager = AsyncCallbackManager(handlers=[], parent_run_id=self.run_id)
|
|
714
651
|
manager.set_handlers(self.inheritable_handlers)
|
|
@@ -726,18 +663,16 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
726
663
|
self,
|
|
727
664
|
token: str,
|
|
728
665
|
*,
|
|
729
|
-
chunk:
|
|
730
|
-
Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
|
|
731
|
-
] = None,
|
|
666
|
+
chunk: GenerationChunk | ChatGenerationChunk | None = None,
|
|
732
667
|
**kwargs: Any,
|
|
733
668
|
) -> None:
|
|
734
669
|
"""Run when LLM generates a new token.
|
|
735
670
|
|
|
736
671
|
Args:
|
|
737
|
-
token
|
|
738
|
-
chunk
|
|
739
|
-
|
|
740
|
-
|
|
672
|
+
token: The new token.
|
|
673
|
+
chunk: The chunk.
|
|
674
|
+
**kwargs: Additional keyword arguments.
|
|
675
|
+
|
|
741
676
|
"""
|
|
742
677
|
if not self.handlers:
|
|
743
678
|
return
|
|
@@ -753,12 +688,13 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
753
688
|
**kwargs,
|
|
754
689
|
)
|
|
755
690
|
|
|
756
|
-
def on_llm_end(self, response:
|
|
691
|
+
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
|
|
757
692
|
"""Run when LLM ends running.
|
|
758
693
|
|
|
759
694
|
Args:
|
|
760
|
-
response
|
|
761
|
-
**kwargs
|
|
695
|
+
response: The LLM result.
|
|
696
|
+
**kwargs: Additional keyword arguments.
|
|
697
|
+
|
|
762
698
|
"""
|
|
763
699
|
if not self.handlers:
|
|
764
700
|
return
|
|
@@ -781,10 +717,10 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
781
717
|
"""Run when LLM errors.
|
|
782
718
|
|
|
783
719
|
Args:
|
|
784
|
-
error
|
|
785
|
-
kwargs
|
|
786
|
-
- response (LLMResult
|
|
787
|
-
|
|
720
|
+
error: The error.
|
|
721
|
+
**kwargs: Additional keyword arguments.
|
|
722
|
+
- response (LLMResult): The response which was generated before
|
|
723
|
+
the error occurred.
|
|
788
724
|
"""
|
|
789
725
|
if not self.handlers:
|
|
790
726
|
return
|
|
@@ -807,7 +743,8 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
807
743
|
"""Get the equivalent sync RunManager.
|
|
808
744
|
|
|
809
745
|
Returns:
|
|
810
|
-
|
|
746
|
+
The sync RunManager.
|
|
747
|
+
|
|
811
748
|
"""
|
|
812
749
|
return CallbackManagerForLLMRun(
|
|
813
750
|
run_id=self.run_id,
|
|
@@ -824,18 +761,16 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
824
761
|
self,
|
|
825
762
|
token: str,
|
|
826
763
|
*,
|
|
827
|
-
chunk:
|
|
828
|
-
Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
|
|
829
|
-
] = None,
|
|
764
|
+
chunk: GenerationChunk | ChatGenerationChunk | None = None,
|
|
830
765
|
**kwargs: Any,
|
|
831
766
|
) -> None:
|
|
832
767
|
"""Run when LLM generates a new token.
|
|
833
768
|
|
|
834
769
|
Args:
|
|
835
|
-
token
|
|
836
|
-
chunk
|
|
837
|
-
|
|
838
|
-
|
|
770
|
+
token: The new token.
|
|
771
|
+
chunk: The chunk.
|
|
772
|
+
**kwargs: Additional keyword arguments.
|
|
773
|
+
|
|
839
774
|
"""
|
|
840
775
|
if not self.handlers:
|
|
841
776
|
return
|
|
@@ -852,14 +787,13 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
852
787
|
)
|
|
853
788
|
|
|
854
789
|
@shielded
|
|
855
|
-
async def on_llm_end(
|
|
856
|
-
self, response: Union[LLMResult, AIMessage], **kwargs: Any
|
|
857
|
-
) -> None:
|
|
790
|
+
async def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
|
|
858
791
|
"""Run when LLM ends running.
|
|
859
792
|
|
|
860
793
|
Args:
|
|
861
|
-
response
|
|
862
|
-
**kwargs
|
|
794
|
+
response: The LLM result.
|
|
795
|
+
**kwargs: Additional keyword arguments.
|
|
796
|
+
|
|
863
797
|
"""
|
|
864
798
|
if not self.handlers:
|
|
865
799
|
return
|
|
@@ -883,10 +817,13 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
883
817
|
"""Run when LLM errors.
|
|
884
818
|
|
|
885
819
|
Args:
|
|
886
|
-
error
|
|
887
|
-
kwargs
|
|
888
|
-
- response (LLMResult
|
|
889
|
-
|
|
820
|
+
error: The error.
|
|
821
|
+
**kwargs: Additional keyword arguments.
|
|
822
|
+
- response (LLMResult): The response which was generated before
|
|
823
|
+
the error occurred.
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
890
827
|
"""
|
|
891
828
|
if not self.handlers:
|
|
892
829
|
return
|
|
@@ -905,12 +842,13 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
905
842
|
class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
906
843
|
"""Callback manager for chain run."""
|
|
907
844
|
|
|
908
|
-
def on_chain_end(self, outputs:
|
|
845
|
+
def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
909
846
|
"""Run when chain ends running.
|
|
910
847
|
|
|
911
848
|
Args:
|
|
912
|
-
outputs
|
|
913
|
-
**kwargs
|
|
849
|
+
outputs: The outputs of the chain.
|
|
850
|
+
**kwargs: Additional keyword arguments.
|
|
851
|
+
|
|
914
852
|
"""
|
|
915
853
|
if not self.handlers:
|
|
916
854
|
return
|
|
@@ -933,8 +871,9 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
933
871
|
"""Run when chain errors.
|
|
934
872
|
|
|
935
873
|
Args:
|
|
936
|
-
error
|
|
937
|
-
**kwargs
|
|
874
|
+
error: The error.
|
|
875
|
+
**kwargs: Additional keyword arguments.
|
|
876
|
+
|
|
938
877
|
"""
|
|
939
878
|
if not self.handlers:
|
|
940
879
|
return
|
|
@@ -949,15 +888,12 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
949
888
|
**kwargs,
|
|
950
889
|
)
|
|
951
890
|
|
|
952
|
-
def on_agent_action(self, action: AgentAction, **kwargs: Any) ->
|
|
891
|
+
def on_agent_action(self, action: AgentAction, **kwargs: Any) -> None:
|
|
953
892
|
"""Run when agent action is received.
|
|
954
893
|
|
|
955
894
|
Args:
|
|
956
|
-
action
|
|
957
|
-
**kwargs
|
|
958
|
-
|
|
959
|
-
Returns:
|
|
960
|
-
Any: The result of the callback.
|
|
895
|
+
action: The agent action.
|
|
896
|
+
**kwargs: Additional keyword arguments.
|
|
961
897
|
"""
|
|
962
898
|
if not self.handlers:
|
|
963
899
|
return
|
|
@@ -972,15 +908,12 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
972
908
|
**kwargs,
|
|
973
909
|
)
|
|
974
910
|
|
|
975
|
-
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) ->
|
|
911
|
+
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
|
|
976
912
|
"""Run when agent finish is received.
|
|
977
913
|
|
|
978
914
|
Args:
|
|
979
|
-
finish
|
|
980
|
-
**kwargs
|
|
981
|
-
|
|
982
|
-
Returns:
|
|
983
|
-
Any: The result of the callback.
|
|
915
|
+
finish: The agent finish.
|
|
916
|
+
**kwargs: Additional keyword arguments.
|
|
984
917
|
"""
|
|
985
918
|
if not self.handlers:
|
|
986
919
|
return
|
|
@@ -1003,7 +936,7 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1003
936
|
"""Get the equivalent sync RunManager.
|
|
1004
937
|
|
|
1005
938
|
Returns:
|
|
1006
|
-
|
|
939
|
+
The sync RunManager.
|
|
1007
940
|
"""
|
|
1008
941
|
return CallbackManagerForChainRun(
|
|
1009
942
|
run_id=self.run_id,
|
|
@@ -1017,14 +950,13 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1017
950
|
)
|
|
1018
951
|
|
|
1019
952
|
@shielded
|
|
1020
|
-
async def on_chain_end(
|
|
1021
|
-
self, outputs: Union[dict[str, Any], Any], **kwargs: Any
|
|
1022
|
-
) -> None:
|
|
953
|
+
async def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
1023
954
|
"""Run when a chain ends running.
|
|
1024
955
|
|
|
1025
956
|
Args:
|
|
1026
|
-
outputs
|
|
1027
|
-
**kwargs
|
|
957
|
+
outputs: The outputs of the chain.
|
|
958
|
+
**kwargs: Additional keyword arguments.
|
|
959
|
+
|
|
1028
960
|
"""
|
|
1029
961
|
if not self.handlers:
|
|
1030
962
|
return
|
|
@@ -1048,8 +980,9 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1048
980
|
"""Run when chain errors.
|
|
1049
981
|
|
|
1050
982
|
Args:
|
|
1051
|
-
error
|
|
1052
|
-
**kwargs
|
|
983
|
+
error: The error.
|
|
984
|
+
**kwargs: Additional keyword arguments.
|
|
985
|
+
|
|
1053
986
|
"""
|
|
1054
987
|
if not self.handlers:
|
|
1055
988
|
return
|
|
@@ -1064,15 +997,12 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1064
997
|
**kwargs,
|
|
1065
998
|
)
|
|
1066
999
|
|
|
1067
|
-
async def on_agent_action(self, action: AgentAction, **kwargs: Any) ->
|
|
1000
|
+
async def on_agent_action(self, action: AgentAction, **kwargs: Any) -> None:
|
|
1068
1001
|
"""Run when agent action is received.
|
|
1069
1002
|
|
|
1070
1003
|
Args:
|
|
1071
|
-
action
|
|
1072
|
-
**kwargs
|
|
1073
|
-
|
|
1074
|
-
Returns:
|
|
1075
|
-
Any: The result of the callback.
|
|
1004
|
+
action: The agent action.
|
|
1005
|
+
**kwargs: Additional keyword arguments.
|
|
1076
1006
|
"""
|
|
1077
1007
|
if not self.handlers:
|
|
1078
1008
|
return
|
|
@@ -1087,15 +1017,12 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1087
1017
|
**kwargs,
|
|
1088
1018
|
)
|
|
1089
1019
|
|
|
1090
|
-
async def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) ->
|
|
1020
|
+
async def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
|
|
1091
1021
|
"""Run when agent finish is received.
|
|
1092
1022
|
|
|
1093
1023
|
Args:
|
|
1094
|
-
finish
|
|
1095
|
-
**kwargs
|
|
1096
|
-
|
|
1097
|
-
Returns:
|
|
1098
|
-
Any: The result of the callback.
|
|
1024
|
+
finish: The agent finish.
|
|
1025
|
+
**kwargs: Additional keyword arguments.
|
|
1099
1026
|
"""
|
|
1100
1027
|
if not self.handlers:
|
|
1101
1028
|
return
|
|
@@ -1122,8 +1049,9 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
|
|
|
1122
1049
|
"""Run when the tool ends running.
|
|
1123
1050
|
|
|
1124
1051
|
Args:
|
|
1125
|
-
output
|
|
1126
|
-
**kwargs
|
|
1052
|
+
output: The output of the tool.
|
|
1053
|
+
**kwargs: The keyword arguments to pass to the event handler
|
|
1054
|
+
|
|
1127
1055
|
"""
|
|
1128
1056
|
if not self.handlers:
|
|
1129
1057
|
return
|
|
@@ -1146,8 +1074,9 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
|
|
|
1146
1074
|
"""Run when tool errors.
|
|
1147
1075
|
|
|
1148
1076
|
Args:
|
|
1149
|
-
error
|
|
1150
|
-
**kwargs
|
|
1077
|
+
error: The error.
|
|
1078
|
+
**kwargs: Additional keyword arguments.
|
|
1079
|
+
|
|
1151
1080
|
"""
|
|
1152
1081
|
if not self.handlers:
|
|
1153
1082
|
return
|
|
@@ -1170,7 +1099,7 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1170
1099
|
"""Get the equivalent sync RunManager.
|
|
1171
1100
|
|
|
1172
1101
|
Returns:
|
|
1173
|
-
|
|
1102
|
+
The sync RunManager.
|
|
1174
1103
|
"""
|
|
1175
1104
|
return CallbackManagerForToolRun(
|
|
1176
1105
|
run_id=self.run_id,
|
|
@@ -1187,8 +1116,9 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1187
1116
|
"""Async run when the tool ends running.
|
|
1188
1117
|
|
|
1189
1118
|
Args:
|
|
1190
|
-
output
|
|
1191
|
-
**kwargs
|
|
1119
|
+
output: The output of the tool.
|
|
1120
|
+
**kwargs: Additional keyword arguments.
|
|
1121
|
+
|
|
1192
1122
|
"""
|
|
1193
1123
|
if not self.handlers:
|
|
1194
1124
|
return
|
|
@@ -1211,8 +1141,9 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1211
1141
|
"""Run when tool errors.
|
|
1212
1142
|
|
|
1213
1143
|
Args:
|
|
1214
|
-
error
|
|
1215
|
-
**kwargs
|
|
1144
|
+
error: The error.
|
|
1145
|
+
**kwargs: Additional keyword arguments.
|
|
1146
|
+
|
|
1216
1147
|
"""
|
|
1217
1148
|
if not self.handlers:
|
|
1218
1149
|
return
|
|
@@ -1239,8 +1170,9 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
|
|
|
1239
1170
|
"""Run when retriever ends running.
|
|
1240
1171
|
|
|
1241
1172
|
Args:
|
|
1242
|
-
documents
|
|
1243
|
-
**kwargs
|
|
1173
|
+
documents: The retrieved documents.
|
|
1174
|
+
**kwargs: Additional keyword arguments.
|
|
1175
|
+
|
|
1244
1176
|
"""
|
|
1245
1177
|
if not self.handlers:
|
|
1246
1178
|
return
|
|
@@ -1263,8 +1195,9 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
|
|
|
1263
1195
|
"""Run when retriever errors.
|
|
1264
1196
|
|
|
1265
1197
|
Args:
|
|
1266
|
-
error
|
|
1267
|
-
**kwargs
|
|
1198
|
+
error: The error.
|
|
1199
|
+
**kwargs: Additional keyword arguments.
|
|
1200
|
+
|
|
1268
1201
|
"""
|
|
1269
1202
|
if not self.handlers:
|
|
1270
1203
|
return
|
|
@@ -1290,7 +1223,8 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1290
1223
|
"""Get the equivalent sync RunManager.
|
|
1291
1224
|
|
|
1292
1225
|
Returns:
|
|
1293
|
-
|
|
1226
|
+
The sync RunManager.
|
|
1227
|
+
|
|
1294
1228
|
"""
|
|
1295
1229
|
return CallbackManagerForRetrieverRun(
|
|
1296
1230
|
run_id=self.run_id,
|
|
@@ -1310,8 +1244,9 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1310
1244
|
"""Run when the retriever ends running.
|
|
1311
1245
|
|
|
1312
1246
|
Args:
|
|
1313
|
-
documents
|
|
1314
|
-
**kwargs
|
|
1247
|
+
documents: The retrieved documents.
|
|
1248
|
+
**kwargs: Additional keyword arguments.
|
|
1249
|
+
|
|
1315
1250
|
"""
|
|
1316
1251
|
if not self.handlers:
|
|
1317
1252
|
return
|
|
@@ -1335,8 +1270,9 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1335
1270
|
"""Run when retriever errors.
|
|
1336
1271
|
|
|
1337
1272
|
Args:
|
|
1338
|
-
error
|
|
1339
|
-
**kwargs
|
|
1273
|
+
error: The error.
|
|
1274
|
+
**kwargs: Additional keyword arguments.
|
|
1275
|
+
|
|
1340
1276
|
"""
|
|
1341
1277
|
if not self.handlers:
|
|
1342
1278
|
return
|
|
@@ -1359,20 +1295,20 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1359
1295
|
self,
|
|
1360
1296
|
serialized: dict[str, Any],
|
|
1361
1297
|
prompts: list[str],
|
|
1362
|
-
run_id:
|
|
1298
|
+
run_id: UUID | None = None,
|
|
1363
1299
|
**kwargs: Any,
|
|
1364
1300
|
) -> list[CallbackManagerForLLMRun]:
|
|
1365
1301
|
"""Run when LLM starts running.
|
|
1366
1302
|
|
|
1367
1303
|
Args:
|
|
1368
|
-
serialized
|
|
1369
|
-
prompts
|
|
1370
|
-
run_id
|
|
1371
|
-
**kwargs
|
|
1304
|
+
serialized: The serialized LLM.
|
|
1305
|
+
prompts: The list of prompts.
|
|
1306
|
+
run_id: The ID of the run.
|
|
1307
|
+
**kwargs: Additional keyword arguments.
|
|
1372
1308
|
|
|
1373
1309
|
Returns:
|
|
1374
|
-
|
|
1375
|
-
|
|
1310
|
+
A callback manager for each prompt as an LLM run.
|
|
1311
|
+
|
|
1376
1312
|
"""
|
|
1377
1313
|
managers = []
|
|
1378
1314
|
for i, prompt in enumerate(prompts):
|
|
@@ -1409,48 +1345,22 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1409
1345
|
def on_chat_model_start(
|
|
1410
1346
|
self,
|
|
1411
1347
|
serialized: dict[str, Any],
|
|
1412
|
-
messages:
|
|
1413
|
-
run_id:
|
|
1348
|
+
messages: list[list[BaseMessage]],
|
|
1349
|
+
run_id: UUID | None = None,
|
|
1414
1350
|
**kwargs: Any,
|
|
1415
1351
|
) -> list[CallbackManagerForLLMRun]:
|
|
1416
1352
|
"""Run when chat model starts running.
|
|
1417
1353
|
|
|
1418
1354
|
Args:
|
|
1419
|
-
serialized
|
|
1420
|
-
messages
|
|
1421
|
-
run_id
|
|
1422
|
-
**kwargs
|
|
1355
|
+
serialized: The serialized LLM.
|
|
1356
|
+
messages: The list of messages.
|
|
1357
|
+
run_id: The ID of the run.
|
|
1358
|
+
**kwargs: Additional keyword arguments.
|
|
1423
1359
|
|
|
1424
1360
|
Returns:
|
|
1425
|
-
|
|
1426
|
-
|
|
1361
|
+
A callback manager for each list of messages as an LLM run.
|
|
1362
|
+
|
|
1427
1363
|
"""
|
|
1428
|
-
if messages and isinstance(messages[0], MessageV1Types):
|
|
1429
|
-
run_id_ = run_id if run_id is not None else uuid.uuid4()
|
|
1430
|
-
handle_event(
|
|
1431
|
-
self.handlers,
|
|
1432
|
-
"on_chat_model_start",
|
|
1433
|
-
"ignore_chat_model",
|
|
1434
|
-
serialized,
|
|
1435
|
-
messages,
|
|
1436
|
-
run_id=run_id_,
|
|
1437
|
-
parent_run_id=self.parent_run_id,
|
|
1438
|
-
tags=self.tags,
|
|
1439
|
-
metadata=self.metadata,
|
|
1440
|
-
**kwargs,
|
|
1441
|
-
)
|
|
1442
|
-
return [
|
|
1443
|
-
CallbackManagerForLLMRun(
|
|
1444
|
-
run_id=run_id_,
|
|
1445
|
-
handlers=self.handlers,
|
|
1446
|
-
inheritable_handlers=self.inheritable_handlers,
|
|
1447
|
-
parent_run_id=self.parent_run_id,
|
|
1448
|
-
tags=self.tags,
|
|
1449
|
-
inheritable_tags=self.inheritable_tags,
|
|
1450
|
-
metadata=self.metadata,
|
|
1451
|
-
inheritable_metadata=self.inheritable_metadata,
|
|
1452
|
-
)
|
|
1453
|
-
]
|
|
1454
1364
|
managers = []
|
|
1455
1365
|
for message_list in messages:
|
|
1456
1366
|
if run_id is not None:
|
|
@@ -1488,21 +1398,22 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1488
1398
|
|
|
1489
1399
|
def on_chain_start(
|
|
1490
1400
|
self,
|
|
1491
|
-
serialized:
|
|
1492
|
-
inputs:
|
|
1493
|
-
run_id:
|
|
1401
|
+
serialized: dict[str, Any] | None,
|
|
1402
|
+
inputs: dict[str, Any] | Any,
|
|
1403
|
+
run_id: UUID | None = None,
|
|
1494
1404
|
**kwargs: Any,
|
|
1495
1405
|
) -> CallbackManagerForChainRun:
|
|
1496
1406
|
"""Run when chain starts running.
|
|
1497
1407
|
|
|
1498
1408
|
Args:
|
|
1499
|
-
serialized
|
|
1500
|
-
inputs
|
|
1501
|
-
run_id
|
|
1502
|
-
**kwargs
|
|
1409
|
+
serialized: The serialized chain.
|
|
1410
|
+
inputs: The inputs to the chain.
|
|
1411
|
+
run_id: The ID of the run.
|
|
1412
|
+
**kwargs: Additional keyword arguments.
|
|
1503
1413
|
|
|
1504
1414
|
Returns:
|
|
1505
|
-
|
|
1415
|
+
The callback manager for the chain run.
|
|
1416
|
+
|
|
1506
1417
|
"""
|
|
1507
1418
|
if run_id is None:
|
|
1508
1419
|
run_id = uuid.uuid4()
|
|
@@ -1533,11 +1444,11 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1533
1444
|
@override
|
|
1534
1445
|
def on_tool_start(
|
|
1535
1446
|
self,
|
|
1536
|
-
serialized:
|
|
1447
|
+
serialized: dict[str, Any] | None,
|
|
1537
1448
|
input_str: str,
|
|
1538
|
-
run_id:
|
|
1539
|
-
parent_run_id:
|
|
1540
|
-
inputs:
|
|
1449
|
+
run_id: UUID | None = None,
|
|
1450
|
+
parent_run_id: UUID | None = None,
|
|
1451
|
+
inputs: dict[str, Any] | None = None,
|
|
1541
1452
|
**kwargs: Any,
|
|
1542
1453
|
) -> CallbackManagerForToolRun:
|
|
1543
1454
|
"""Run when tool starts running.
|
|
@@ -1546,17 +1457,18 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1546
1457
|
serialized: Serialized representation of the tool.
|
|
1547
1458
|
input_str: The input to the tool as a string.
|
|
1548
1459
|
Non-string inputs are cast to strings.
|
|
1549
|
-
run_id: ID for the run.
|
|
1550
|
-
parent_run_id: The ID of the parent run.
|
|
1460
|
+
run_id: ID for the run.
|
|
1461
|
+
parent_run_id: The ID of the parent run.
|
|
1551
1462
|
inputs: The original input to the tool if provided.
|
|
1552
1463
|
Recommended for usage instead of input_str when the original
|
|
1553
1464
|
input is needed.
|
|
1554
1465
|
If provided, the inputs are expected to be formatted as a dict.
|
|
1555
1466
|
The keys will correspond to the named-arguments in the tool.
|
|
1556
|
-
**kwargs
|
|
1467
|
+
**kwargs: The keyword arguments to pass to the event handler
|
|
1557
1468
|
|
|
1558
1469
|
Returns:
|
|
1559
|
-
|
|
1470
|
+
The callback manager for the tool run.
|
|
1471
|
+
|
|
1560
1472
|
"""
|
|
1561
1473
|
if run_id is None:
|
|
1562
1474
|
run_id = uuid.uuid4()
|
|
@@ -1589,20 +1501,23 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1589
1501
|
@override
|
|
1590
1502
|
def on_retriever_start(
|
|
1591
1503
|
self,
|
|
1592
|
-
serialized:
|
|
1504
|
+
serialized: dict[str, Any] | None,
|
|
1593
1505
|
query: str,
|
|
1594
|
-
run_id:
|
|
1595
|
-
parent_run_id:
|
|
1506
|
+
run_id: UUID | None = None,
|
|
1507
|
+
parent_run_id: UUID | None = None,
|
|
1596
1508
|
**kwargs: Any,
|
|
1597
1509
|
) -> CallbackManagerForRetrieverRun:
|
|
1598
1510
|
"""Run when the retriever starts running.
|
|
1599
1511
|
|
|
1600
1512
|
Args:
|
|
1601
|
-
serialized
|
|
1602
|
-
query
|
|
1603
|
-
run_id
|
|
1604
|
-
parent_run_id
|
|
1605
|
-
**kwargs
|
|
1513
|
+
serialized: The serialized retriever.
|
|
1514
|
+
query: The query.
|
|
1515
|
+
run_id: The ID of the run.
|
|
1516
|
+
parent_run_id: The ID of the parent run.
|
|
1517
|
+
**kwargs: Additional keyword arguments.
|
|
1518
|
+
|
|
1519
|
+
Returns:
|
|
1520
|
+
The callback manager for the retriever run.
|
|
1606
1521
|
"""
|
|
1607
1522
|
if run_id is None:
|
|
1608
1523
|
run_id = uuid.uuid4()
|
|
@@ -1635,7 +1550,7 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1635
1550
|
self,
|
|
1636
1551
|
name: str,
|
|
1637
1552
|
data: Any,
|
|
1638
|
-
run_id:
|
|
1553
|
+
run_id: UUID | None = None,
|
|
1639
1554
|
**kwargs: Any,
|
|
1640
1555
|
) -> None:
|
|
1641
1556
|
"""Dispatch an adhoc event to the handlers (async version).
|
|
@@ -1647,9 +1562,13 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1647
1562
|
Args:
|
|
1648
1563
|
name: The name of the adhoc event.
|
|
1649
1564
|
data: The data for the adhoc event.
|
|
1650
|
-
run_id: The ID of the run.
|
|
1565
|
+
run_id: The ID of the run.
|
|
1566
|
+
|
|
1567
|
+
Raises:
|
|
1568
|
+
ValueError: If additional keyword arguments are passed.
|
|
1569
|
+
|
|
1570
|
+
!!! version-added "Added in version 0.2.14"
|
|
1651
1571
|
|
|
1652
|
-
.. versionadded:: 0.2.14
|
|
1653
1572
|
"""
|
|
1654
1573
|
if not self.handlers:
|
|
1655
1574
|
return
|
|
@@ -1680,30 +1599,24 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1680
1599
|
inheritable_callbacks: Callbacks = None,
|
|
1681
1600
|
local_callbacks: Callbacks = None,
|
|
1682
1601
|
verbose: bool = False, # noqa: FBT001,FBT002
|
|
1683
|
-
inheritable_tags:
|
|
1684
|
-
local_tags:
|
|
1685
|
-
inheritable_metadata:
|
|
1686
|
-
local_metadata:
|
|
1602
|
+
inheritable_tags: list[str] | None = None,
|
|
1603
|
+
local_tags: list[str] | None = None,
|
|
1604
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
1605
|
+
local_metadata: dict[str, Any] | None = None,
|
|
1687
1606
|
) -> CallbackManager:
|
|
1688
1607
|
"""Configure the callback manager.
|
|
1689
1608
|
|
|
1690
1609
|
Args:
|
|
1691
|
-
inheritable_callbacks
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
local_tags (Optional[list[str]], optional): The local tags.
|
|
1699
|
-
Defaults to None.
|
|
1700
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
1701
|
-
metadata. Defaults to None.
|
|
1702
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
1703
|
-
Defaults to None.
|
|
1610
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
1611
|
+
local_callbacks: The local callbacks.
|
|
1612
|
+
verbose: Whether to enable verbose mode.
|
|
1613
|
+
inheritable_tags: The inheritable tags.
|
|
1614
|
+
local_tags: The local tags.
|
|
1615
|
+
inheritable_metadata: The inheritable metadata.
|
|
1616
|
+
local_metadata: The local metadata.
|
|
1704
1617
|
|
|
1705
1618
|
Returns:
|
|
1706
|
-
|
|
1619
|
+
The configured callback manager.
|
|
1707
1620
|
"""
|
|
1708
1621
|
return _configure(
|
|
1709
1622
|
cls,
|
|
@@ -1723,8 +1636,8 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1723
1636
|
def __init__(
|
|
1724
1637
|
self,
|
|
1725
1638
|
handlers: list[BaseCallbackHandler],
|
|
1726
|
-
inheritable_handlers:
|
|
1727
|
-
parent_run_id:
|
|
1639
|
+
inheritable_handlers: list[BaseCallbackHandler] | None = None,
|
|
1640
|
+
parent_run_id: UUID | None = None,
|
|
1728
1641
|
*,
|
|
1729
1642
|
parent_run_manager: CallbackManagerForChainRun,
|
|
1730
1643
|
**kwargs: Any,
|
|
@@ -1732,12 +1645,12 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1732
1645
|
"""Initialize the callback manager.
|
|
1733
1646
|
|
|
1734
1647
|
Args:
|
|
1735
|
-
handlers
|
|
1736
|
-
inheritable_handlers
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1648
|
+
handlers: The list of handlers.
|
|
1649
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
1650
|
+
parent_run_id: The ID of the parent run.
|
|
1651
|
+
parent_run_manager: The parent run manager.
|
|
1652
|
+
**kwargs: Additional keyword arguments.
|
|
1653
|
+
|
|
1741
1654
|
"""
|
|
1742
1655
|
super().__init__(
|
|
1743
1656
|
handlers,
|
|
@@ -1748,8 +1661,8 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1748
1661
|
self.parent_run_manager = parent_run_manager
|
|
1749
1662
|
self.ended = False
|
|
1750
1663
|
|
|
1664
|
+
@override
|
|
1751
1665
|
def copy(self) -> CallbackManagerForChainGroup:
|
|
1752
|
-
"""Copy the callback manager."""
|
|
1753
1666
|
return self.__class__(
|
|
1754
1667
|
handlers=self.handlers.copy(),
|
|
1755
1668
|
inheritable_handlers=self.inheritable_handlers.copy(),
|
|
@@ -1771,31 +1684,33 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1771
1684
|
from the current object.
|
|
1772
1685
|
|
|
1773
1686
|
Returns:
|
|
1774
|
-
|
|
1775
|
-
|
|
1687
|
+
A copy of the current object with the handlers, tags, and other attributes
|
|
1688
|
+
merged from the other object.
|
|
1776
1689
|
|
|
1777
1690
|
Example: Merging two callback managers.
|
|
1778
1691
|
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1692
|
+
```python
|
|
1693
|
+
from langchain_core.callbacks.manager import (
|
|
1694
|
+
CallbackManager,
|
|
1695
|
+
trace_as_chain_group,
|
|
1696
|
+
)
|
|
1697
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
1698
|
+
|
|
1699
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
1700
|
+
with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
|
|
1701
|
+
merged_manager = group_manager.merge(manager)
|
|
1702
|
+
print(type(merged_manager))
|
|
1703
|
+
# <class 'langchain_core.callbacks.manager.CallbackManagerForChainGroup'>
|
|
1704
|
+
|
|
1705
|
+
print(merged_manager.handlers)
|
|
1706
|
+
# [
|
|
1707
|
+
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
1708
|
+
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
1709
|
+
# ]
|
|
1710
|
+
|
|
1711
|
+
print(merged_manager.tags)
|
|
1712
|
+
# ['tag2', 'tag1']
|
|
1713
|
+
```
|
|
1799
1714
|
""" # noqa: E501
|
|
1800
1715
|
manager = self.__class__(
|
|
1801
1716
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -1820,12 +1735,13 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1820
1735
|
manager.add_handler(handler, inherit=True)
|
|
1821
1736
|
return manager
|
|
1822
1737
|
|
|
1823
|
-
def on_chain_end(self, outputs:
|
|
1738
|
+
def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
1824
1739
|
"""Run when traced chain group ends.
|
|
1825
1740
|
|
|
1826
1741
|
Args:
|
|
1827
|
-
outputs
|
|
1828
|
-
**kwargs
|
|
1742
|
+
outputs: The outputs of the chain.
|
|
1743
|
+
**kwargs: Additional keyword arguments.
|
|
1744
|
+
|
|
1829
1745
|
"""
|
|
1830
1746
|
self.ended = True
|
|
1831
1747
|
return self.parent_run_manager.on_chain_end(outputs, **kwargs)
|
|
@@ -1838,8 +1754,9 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1838
1754
|
"""Run when chain errors.
|
|
1839
1755
|
|
|
1840
1756
|
Args:
|
|
1841
|
-
error
|
|
1842
|
-
**kwargs
|
|
1757
|
+
error: The error.
|
|
1758
|
+
**kwargs: Additional keyword arguments.
|
|
1759
|
+
|
|
1843
1760
|
"""
|
|
1844
1761
|
self.ended = True
|
|
1845
1762
|
return self.parent_run_manager.on_chain_error(error, **kwargs)
|
|
@@ -1857,21 +1774,20 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1857
1774
|
self,
|
|
1858
1775
|
serialized: dict[str, Any],
|
|
1859
1776
|
prompts: list[str],
|
|
1860
|
-
run_id:
|
|
1777
|
+
run_id: UUID | None = None,
|
|
1861
1778
|
**kwargs: Any,
|
|
1862
1779
|
) -> list[AsyncCallbackManagerForLLMRun]:
|
|
1863
1780
|
"""Run when LLM starts running.
|
|
1864
1781
|
|
|
1865
1782
|
Args:
|
|
1866
|
-
serialized
|
|
1867
|
-
prompts
|
|
1868
|
-
run_id
|
|
1869
|
-
**kwargs
|
|
1783
|
+
serialized: The serialized LLM.
|
|
1784
|
+
prompts: The list of prompts.
|
|
1785
|
+
run_id: The ID of the run.
|
|
1786
|
+
**kwargs: Additional keyword arguments.
|
|
1870
1787
|
|
|
1871
1788
|
Returns:
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
to each prompt.
|
|
1789
|
+
The list of async callback managers, one for each LLM Run corresponding to
|
|
1790
|
+
each prompt.
|
|
1875
1791
|
"""
|
|
1876
1792
|
inline_tasks = []
|
|
1877
1793
|
non_inline_tasks = []
|
|
@@ -1945,68 +1861,26 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1945
1861
|
async def on_chat_model_start(
|
|
1946
1862
|
self,
|
|
1947
1863
|
serialized: dict[str, Any],
|
|
1948
|
-
messages:
|
|
1949
|
-
run_id:
|
|
1864
|
+
messages: list[list[BaseMessage]],
|
|
1865
|
+
run_id: UUID | None = None,
|
|
1950
1866
|
**kwargs: Any,
|
|
1951
1867
|
) -> list[AsyncCallbackManagerForLLMRun]:
|
|
1952
1868
|
"""Async run when LLM starts running.
|
|
1953
1869
|
|
|
1954
1870
|
Args:
|
|
1955
|
-
serialized
|
|
1956
|
-
messages
|
|
1957
|
-
run_id
|
|
1958
|
-
**kwargs
|
|
1871
|
+
serialized: The serialized LLM.
|
|
1872
|
+
messages: The list of messages.
|
|
1873
|
+
run_id: The ID of the run.
|
|
1874
|
+
**kwargs: Additional keyword arguments.
|
|
1959
1875
|
|
|
1960
1876
|
Returns:
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
corresponding to each inner message list.
|
|
1877
|
+
The list of async callback managers, one for each LLM Run corresponding to
|
|
1878
|
+
each inner message list.
|
|
1964
1879
|
"""
|
|
1965
|
-
if messages and isinstance(messages[0], MessageV1Types):
|
|
1966
|
-
run_id_ = run_id if run_id is not None else uuid.uuid4()
|
|
1967
|
-
inline_tasks = []
|
|
1968
|
-
non_inline_tasks = []
|
|
1969
|
-
for handler in self.handlers:
|
|
1970
|
-
task = ahandle_event(
|
|
1971
|
-
[handler],
|
|
1972
|
-
"on_chat_model_start",
|
|
1973
|
-
"ignore_chat_model",
|
|
1974
|
-
serialized,
|
|
1975
|
-
messages,
|
|
1976
|
-
run_id=run_id_,
|
|
1977
|
-
parent_run_id=self.parent_run_id,
|
|
1978
|
-
tags=self.tags,
|
|
1979
|
-
metadata=self.metadata,
|
|
1980
|
-
**kwargs,
|
|
1981
|
-
)
|
|
1982
|
-
if handler.run_inline:
|
|
1983
|
-
inline_tasks.append(task)
|
|
1984
|
-
else:
|
|
1985
|
-
non_inline_tasks.append(task)
|
|
1986
|
-
managers = [
|
|
1987
|
-
AsyncCallbackManagerForLLMRun(
|
|
1988
|
-
run_id=run_id_,
|
|
1989
|
-
handlers=self.handlers,
|
|
1990
|
-
inheritable_handlers=self.inheritable_handlers,
|
|
1991
|
-
parent_run_id=self.parent_run_id,
|
|
1992
|
-
tags=self.tags,
|
|
1993
|
-
inheritable_tags=self.inheritable_tags,
|
|
1994
|
-
metadata=self.metadata,
|
|
1995
|
-
inheritable_metadata=self.inheritable_metadata,
|
|
1996
|
-
)
|
|
1997
|
-
]
|
|
1998
|
-
# Run inline tasks sequentially
|
|
1999
|
-
for task in inline_tasks:
|
|
2000
|
-
await task
|
|
2001
|
-
|
|
2002
|
-
# Run non-inline tasks concurrently
|
|
2003
|
-
if non_inline_tasks:
|
|
2004
|
-
await asyncio.gather(*non_inline_tasks)
|
|
2005
|
-
|
|
2006
|
-
return managers
|
|
2007
1880
|
inline_tasks = []
|
|
2008
1881
|
non_inline_tasks = []
|
|
2009
1882
|
managers = []
|
|
1883
|
+
|
|
2010
1884
|
for message_list in messages:
|
|
2011
1885
|
if run_id is not None:
|
|
2012
1886
|
run_id_ = run_id
|
|
@@ -2057,22 +1931,21 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2057
1931
|
|
|
2058
1932
|
async def on_chain_start(
|
|
2059
1933
|
self,
|
|
2060
|
-
serialized:
|
|
2061
|
-
inputs:
|
|
2062
|
-
run_id:
|
|
1934
|
+
serialized: dict[str, Any] | None,
|
|
1935
|
+
inputs: dict[str, Any] | Any,
|
|
1936
|
+
run_id: UUID | None = None,
|
|
2063
1937
|
**kwargs: Any,
|
|
2064
1938
|
) -> AsyncCallbackManagerForChainRun:
|
|
2065
1939
|
"""Async run when chain starts running.
|
|
2066
1940
|
|
|
2067
1941
|
Args:
|
|
2068
|
-
serialized
|
|
2069
|
-
inputs
|
|
2070
|
-
run_id
|
|
2071
|
-
**kwargs
|
|
1942
|
+
serialized: The serialized chain.
|
|
1943
|
+
inputs: The inputs to the chain.
|
|
1944
|
+
run_id: The ID of the run.
|
|
1945
|
+
**kwargs: Additional keyword arguments.
|
|
2072
1946
|
|
|
2073
1947
|
Returns:
|
|
2074
|
-
|
|
2075
|
-
for the chain run.
|
|
1948
|
+
The async callback manager for the chain run.
|
|
2076
1949
|
"""
|
|
2077
1950
|
if run_id is None:
|
|
2078
1951
|
run_id = uuid.uuid4()
|
|
@@ -2104,25 +1977,23 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2104
1977
|
@override
|
|
2105
1978
|
async def on_tool_start(
|
|
2106
1979
|
self,
|
|
2107
|
-
serialized:
|
|
1980
|
+
serialized: dict[str, Any] | None,
|
|
2108
1981
|
input_str: str,
|
|
2109
|
-
run_id:
|
|
2110
|
-
parent_run_id:
|
|
1982
|
+
run_id: UUID | None = None,
|
|
1983
|
+
parent_run_id: UUID | None = None,
|
|
2111
1984
|
**kwargs: Any,
|
|
2112
1985
|
) -> AsyncCallbackManagerForToolRun:
|
|
2113
1986
|
"""Run when the tool starts running.
|
|
2114
1987
|
|
|
2115
1988
|
Args:
|
|
2116
|
-
serialized
|
|
2117
|
-
input_str
|
|
2118
|
-
run_id
|
|
2119
|
-
parent_run_id
|
|
2120
|
-
|
|
2121
|
-
**kwargs (Any): Additional keyword arguments.
|
|
1989
|
+
serialized: The serialized tool.
|
|
1990
|
+
input_str: The input to the tool.
|
|
1991
|
+
run_id: The ID of the run.
|
|
1992
|
+
parent_run_id: The ID of the parent run.
|
|
1993
|
+
**kwargs: Additional keyword arguments.
|
|
2122
1994
|
|
|
2123
1995
|
Returns:
|
|
2124
|
-
|
|
2125
|
-
for the tool run.
|
|
1996
|
+
The async callback manager for the tool run.
|
|
2126
1997
|
"""
|
|
2127
1998
|
if run_id is None:
|
|
2128
1999
|
run_id = uuid.uuid4()
|
|
@@ -2155,7 +2026,7 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2155
2026
|
self,
|
|
2156
2027
|
name: str,
|
|
2157
2028
|
data: Any,
|
|
2158
|
-
run_id:
|
|
2029
|
+
run_id: UUID | None = None,
|
|
2159
2030
|
**kwargs: Any,
|
|
2160
2031
|
) -> None:
|
|
2161
2032
|
"""Dispatch an adhoc event to the handlers (async version).
|
|
@@ -2167,9 +2038,12 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2167
2038
|
Args:
|
|
2168
2039
|
name: The name of the adhoc event.
|
|
2169
2040
|
data: The data for the adhoc event.
|
|
2170
|
-
run_id: The ID of the run.
|
|
2041
|
+
run_id: The ID of the run.
|
|
2042
|
+
|
|
2043
|
+
Raises:
|
|
2044
|
+
ValueError: If additional keyword arguments are passed.
|
|
2171
2045
|
|
|
2172
|
-
|
|
2046
|
+
!!! version-added "Added in version 0.2.14"
|
|
2173
2047
|
"""
|
|
2174
2048
|
if not self.handlers:
|
|
2175
2049
|
return
|
|
@@ -2197,24 +2071,23 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2197
2071
|
@override
|
|
2198
2072
|
async def on_retriever_start(
|
|
2199
2073
|
self,
|
|
2200
|
-
serialized:
|
|
2074
|
+
serialized: dict[str, Any] | None,
|
|
2201
2075
|
query: str,
|
|
2202
|
-
run_id:
|
|
2203
|
-
parent_run_id:
|
|
2076
|
+
run_id: UUID | None = None,
|
|
2077
|
+
parent_run_id: UUID | None = None,
|
|
2204
2078
|
**kwargs: Any,
|
|
2205
2079
|
) -> AsyncCallbackManagerForRetrieverRun:
|
|
2206
2080
|
"""Run when the retriever starts running.
|
|
2207
2081
|
|
|
2208
2082
|
Args:
|
|
2209
|
-
serialized
|
|
2210
|
-
query
|
|
2211
|
-
run_id
|
|
2212
|
-
parent_run_id
|
|
2213
|
-
**kwargs
|
|
2083
|
+
serialized: The serialized retriever.
|
|
2084
|
+
query: The query.
|
|
2085
|
+
run_id: The ID of the run.
|
|
2086
|
+
parent_run_id: The ID of the parent run.
|
|
2087
|
+
**kwargs: Additional keyword arguments.
|
|
2214
2088
|
|
|
2215
2089
|
Returns:
|
|
2216
|
-
|
|
2217
|
-
for the retriever run.
|
|
2090
|
+
The async callback manager for the retriever run.
|
|
2218
2091
|
"""
|
|
2219
2092
|
if run_id is None:
|
|
2220
2093
|
run_id = uuid.uuid4()
|
|
@@ -2249,30 +2122,24 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2249
2122
|
inheritable_callbacks: Callbacks = None,
|
|
2250
2123
|
local_callbacks: Callbacks = None,
|
|
2251
2124
|
verbose: bool = False, # noqa: FBT001,FBT002
|
|
2252
|
-
inheritable_tags:
|
|
2253
|
-
local_tags:
|
|
2254
|
-
inheritable_metadata:
|
|
2255
|
-
local_metadata:
|
|
2125
|
+
inheritable_tags: list[str] | None = None,
|
|
2126
|
+
local_tags: list[str] | None = None,
|
|
2127
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
2128
|
+
local_metadata: dict[str, Any] | None = None,
|
|
2256
2129
|
) -> AsyncCallbackManager:
|
|
2257
2130
|
"""Configure the async callback manager.
|
|
2258
2131
|
|
|
2259
2132
|
Args:
|
|
2260
|
-
inheritable_callbacks
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
local_tags (Optional[list[str]], optional): The local tags.
|
|
2268
|
-
Defaults to None.
|
|
2269
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
2270
|
-
metadata. Defaults to None.
|
|
2271
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
2272
|
-
Defaults to None.
|
|
2133
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
2134
|
+
local_callbacks: The local callbacks.
|
|
2135
|
+
verbose: Whether to enable verbose mode.
|
|
2136
|
+
inheritable_tags: The inheritable tags.
|
|
2137
|
+
local_tags: The local tags.
|
|
2138
|
+
inheritable_metadata: The inheritable metadata.
|
|
2139
|
+
local_metadata: The local metadata.
|
|
2273
2140
|
|
|
2274
2141
|
Returns:
|
|
2275
|
-
|
|
2142
|
+
The configured async callback manager.
|
|
2276
2143
|
"""
|
|
2277
2144
|
return _configure(
|
|
2278
2145
|
cls,
|
|
@@ -2292,8 +2159,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2292
2159
|
def __init__(
|
|
2293
2160
|
self,
|
|
2294
2161
|
handlers: list[BaseCallbackHandler],
|
|
2295
|
-
inheritable_handlers:
|
|
2296
|
-
parent_run_id:
|
|
2162
|
+
inheritable_handlers: list[BaseCallbackHandler] | None = None,
|
|
2163
|
+
parent_run_id: UUID | None = None,
|
|
2297
2164
|
*,
|
|
2298
2165
|
parent_run_manager: AsyncCallbackManagerForChainRun,
|
|
2299
2166
|
**kwargs: Any,
|
|
@@ -2301,13 +2168,11 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2301
2168
|
"""Initialize the async callback manager.
|
|
2302
2169
|
|
|
2303
2170
|
Args:
|
|
2304
|
-
handlers
|
|
2305
|
-
inheritable_handlers
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
The parent run manager.
|
|
2310
|
-
**kwargs (Any): Additional keyword arguments.
|
|
2171
|
+
handlers: The list of handlers.
|
|
2172
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
2173
|
+
parent_run_id: The ID of the parent run.
|
|
2174
|
+
parent_run_manager: The parent run manager.
|
|
2175
|
+
**kwargs: Additional keyword arguments.
|
|
2311
2176
|
"""
|
|
2312
2177
|
super().__init__(
|
|
2313
2178
|
handlers,
|
|
@@ -2319,7 +2184,7 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2319
2184
|
self.ended = False
|
|
2320
2185
|
|
|
2321
2186
|
def copy(self) -> AsyncCallbackManagerForChainGroup:
|
|
2322
|
-
"""
|
|
2187
|
+
"""Return a copy the async callback manager."""
|
|
2323
2188
|
return self.__class__(
|
|
2324
2189
|
handlers=self.handlers.copy(),
|
|
2325
2190
|
inheritable_handlers=self.inheritable_handlers.copy(),
|
|
@@ -2341,31 +2206,35 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2341
2206
|
from the current object.
|
|
2342
2207
|
|
|
2343
2208
|
Returns:
|
|
2344
|
-
|
|
2345
|
-
|
|
2209
|
+
A copy of the current AsyncCallbackManagerForChainGroup
|
|
2210
|
+
with the handlers, tags, etc. of the other callback manager merged in.
|
|
2346
2211
|
|
|
2347
2212
|
Example: Merging two callback managers.
|
|
2348
2213
|
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2214
|
+
```python
|
|
2215
|
+
from langchain_core.callbacks.manager import (
|
|
2216
|
+
CallbackManager,
|
|
2217
|
+
atrace_as_chain_group,
|
|
2218
|
+
)
|
|
2219
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
2220
|
+
|
|
2221
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
2222
|
+
async with atrace_as_chain_group(
|
|
2223
|
+
"My Group Name", tags=["tag1"]
|
|
2224
|
+
) as group_manager:
|
|
2225
|
+
merged_manager = group_manager.merge(manager)
|
|
2226
|
+
print(type(merged_manager))
|
|
2227
|
+
# <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
|
|
2228
|
+
|
|
2229
|
+
print(merged_manager.handlers)
|
|
2230
|
+
# [
|
|
2231
|
+
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
2232
|
+
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
2233
|
+
# ]
|
|
2234
|
+
|
|
2235
|
+
print(merged_manager.tags)
|
|
2236
|
+
# ['tag2', 'tag1']
|
|
2237
|
+
```
|
|
2369
2238
|
""" # noqa: E501
|
|
2370
2239
|
manager = self.__class__(
|
|
2371
2240
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -2390,14 +2259,12 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2390
2259
|
manager.add_handler(handler, inherit=True)
|
|
2391
2260
|
return manager
|
|
2392
2261
|
|
|
2393
|
-
async def on_chain_end(
|
|
2394
|
-
self, outputs: Union[dict[str, Any], Any], **kwargs: Any
|
|
2395
|
-
) -> None:
|
|
2262
|
+
async def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
|
|
2396
2263
|
"""Run when traced chain group ends.
|
|
2397
2264
|
|
|
2398
2265
|
Args:
|
|
2399
|
-
outputs
|
|
2400
|
-
**kwargs
|
|
2266
|
+
outputs: The outputs of the chain.
|
|
2267
|
+
**kwargs: Additional keyword arguments.
|
|
2401
2268
|
"""
|
|
2402
2269
|
self.ended = True
|
|
2403
2270
|
await self.parent_run_manager.on_chain_end(outputs, **kwargs)
|
|
@@ -2410,8 +2277,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2410
2277
|
"""Run when chain errors.
|
|
2411
2278
|
|
|
2412
2279
|
Args:
|
|
2413
|
-
error
|
|
2414
|
-
**kwargs
|
|
2280
|
+
error: The error.
|
|
2281
|
+
**kwargs: Additional keyword arguments.
|
|
2415
2282
|
"""
|
|
2416
2283
|
self.ended = True
|
|
2417
2284
|
await self.parent_run_manager.on_chain_error(error, **kwargs)
|
|
@@ -2424,44 +2291,35 @@ def _configure(
|
|
|
2424
2291
|
callback_manager_cls: type[T],
|
|
2425
2292
|
inheritable_callbacks: Callbacks = None,
|
|
2426
2293
|
local_callbacks: Callbacks = None,
|
|
2427
|
-
inheritable_tags:
|
|
2428
|
-
local_tags:
|
|
2429
|
-
inheritable_metadata:
|
|
2430
|
-
local_metadata:
|
|
2294
|
+
inheritable_tags: list[str] | None = None,
|
|
2295
|
+
local_tags: list[str] | None = None,
|
|
2296
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
2297
|
+
local_metadata: dict[str, Any] | None = None,
|
|
2431
2298
|
*,
|
|
2432
2299
|
verbose: bool = False,
|
|
2433
2300
|
) -> T:
|
|
2434
2301
|
"""Configure the callback manager.
|
|
2435
2302
|
|
|
2436
2303
|
Args:
|
|
2437
|
-
callback_manager_cls
|
|
2438
|
-
inheritable_callbacks
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
2449
|
-
Defaults to None.
|
|
2304
|
+
callback_manager_cls: The callback manager class.
|
|
2305
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
2306
|
+
local_callbacks: The local callbacks.
|
|
2307
|
+
inheritable_tags: The inheritable tags.
|
|
2308
|
+
local_tags: The local tags.
|
|
2309
|
+
inheritable_metadata: The inheritable metadata.
|
|
2310
|
+
local_metadata: The local metadata.
|
|
2311
|
+
verbose: Whether to enable verbose mode.
|
|
2312
|
+
|
|
2313
|
+
Raises:
|
|
2314
|
+
RuntimeError: If `LANGCHAIN_TRACING` is set but `LANGCHAIN_TRACING_V2` is not.
|
|
2450
2315
|
|
|
2451
2316
|
Returns:
|
|
2452
|
-
|
|
2317
|
+
The configured callback manager.
|
|
2453
2318
|
"""
|
|
2454
|
-
from langchain_core.tracers.context import (
|
|
2455
|
-
_configure_hooks,
|
|
2456
|
-
_get_tracer_project,
|
|
2457
|
-
_tracing_v2_is_enabled,
|
|
2458
|
-
tracing_v2_callback_var,
|
|
2459
|
-
)
|
|
2460
|
-
|
|
2461
2319
|
tracing_context = get_tracing_context()
|
|
2462
2320
|
tracing_metadata = tracing_context["metadata"]
|
|
2463
2321
|
tracing_tags = tracing_context["tags"]
|
|
2464
|
-
run_tree:
|
|
2322
|
+
run_tree: Run | None = tracing_context["parent"]
|
|
2465
2323
|
parent_run_id = None if run_tree is None else run_tree.id
|
|
2466
2324
|
callback_manager = callback_manager_cls(
|
|
2467
2325
|
handlers=[],
|
|
@@ -2534,9 +2392,6 @@ def _configure(
|
|
|
2534
2392
|
tracer_project = _get_tracer_project()
|
|
2535
2393
|
debug = _get_debug()
|
|
2536
2394
|
if verbose or debug or tracing_v2_enabled_:
|
|
2537
|
-
from langchain_core.tracers.langchain import LangChainTracer
|
|
2538
|
-
from langchain_core.tracers.stdout import ConsoleCallbackHandler
|
|
2539
|
-
|
|
2540
2395
|
if verbose and not any(
|
|
2541
2396
|
isinstance(handler, StdOutCallbackHandler)
|
|
2542
2397
|
for handler in callback_manager.handlers
|
|
@@ -2609,99 +2464,103 @@ def _configure(
|
|
|
2609
2464
|
|
|
2610
2465
|
|
|
2611
2466
|
async def adispatch_custom_event(
|
|
2612
|
-
name: str, data: Any, *, config:
|
|
2467
|
+
name: str, data: Any, *, config: RunnableConfig | None = None
|
|
2613
2468
|
) -> None:
|
|
2614
2469
|
"""Dispatch an adhoc event to the handlers.
|
|
2615
2470
|
|
|
2616
2471
|
Args:
|
|
2617
2472
|
name: The name of the adhoc event.
|
|
2618
2473
|
data: The data for the adhoc event. Free form data. Ideally should be
|
|
2619
|
-
|
|
2620
|
-
|
|
2474
|
+
JSON serializable to avoid serialization issues downstream, but
|
|
2475
|
+
this is not enforced.
|
|
2621
2476
|
config: Optional config object. Mirrors the async API but not strictly needed.
|
|
2622
2477
|
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2478
|
+
Raises:
|
|
2479
|
+
RuntimeError: If there is no parent run ID available to associate
|
|
2480
|
+
the event with.
|
|
2626
2481
|
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2482
|
+
Example:
|
|
2483
|
+
```python
|
|
2484
|
+
from langchain_core.callbacks import (
|
|
2485
|
+
AsyncCallbackHandler,
|
|
2486
|
+
adispatch_custom_event
|
|
2487
|
+
)
|
|
2488
|
+
from langchain_core.runnable import RunnableLambda
|
|
2489
|
+
|
|
2490
|
+
class CustomCallbackManager(AsyncCallbackHandler):
|
|
2491
|
+
async def on_custom_event(
|
|
2492
|
+
self,
|
|
2493
|
+
name: str,
|
|
2494
|
+
data: Any,
|
|
2495
|
+
*,
|
|
2496
|
+
run_id: UUID,
|
|
2497
|
+
tags: list[str] | None = None,
|
|
2498
|
+
metadata: dict[str, Any] | None = None,
|
|
2499
|
+
**kwargs: Any,
|
|
2500
|
+
) -> None:
|
|
2501
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2502
|
+
|
|
2503
|
+
callback = CustomCallbackManager()
|
|
2504
|
+
|
|
2505
|
+
async def foo(inputs):
|
|
2506
|
+
await adispatch_custom_event("my_event", {"bar": "buzz})
|
|
2507
|
+
return inputs
|
|
2508
|
+
|
|
2509
|
+
foo_ = RunnableLambda(foo)
|
|
2510
|
+
await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2511
|
+
```
|
|
2654
2512
|
|
|
2655
2513
|
Example: Use with astream events
|
|
2656
2514
|
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
)
|
|
2692
|
-
|
|
2515
|
+
```python
|
|
2516
|
+
from langchain_core.callbacks import (
|
|
2517
|
+
AsyncCallbackHandler,
|
|
2518
|
+
adispatch_custom_event
|
|
2519
|
+
)
|
|
2520
|
+
from langchain_core.runnable import RunnableLambda
|
|
2521
|
+
|
|
2522
|
+
class CustomCallbackManager(AsyncCallbackHandler):
|
|
2523
|
+
async def on_custom_event(
|
|
2524
|
+
self,
|
|
2525
|
+
name: str,
|
|
2526
|
+
data: Any,
|
|
2527
|
+
*,
|
|
2528
|
+
run_id: UUID,
|
|
2529
|
+
tags: list[str] | None = None,
|
|
2530
|
+
metadata: dict[str, Any] | None = None,
|
|
2531
|
+
**kwargs: Any,
|
|
2532
|
+
) -> None:
|
|
2533
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2534
|
+
|
|
2535
|
+
callback = CustomCallbackManager()
|
|
2536
|
+
|
|
2537
|
+
async def foo(inputs):
|
|
2538
|
+
await adispatch_custom_event("event_type_1", {"bar": "buzz})
|
|
2539
|
+
await adispatch_custom_event("event_type_2", 5)
|
|
2540
|
+
return inputs
|
|
2541
|
+
|
|
2542
|
+
foo_ = RunnableLambda(foo)
|
|
2543
|
+
|
|
2544
|
+
async for event in foo_.ainvoke_stream(
|
|
2545
|
+
{"a": "1"},
|
|
2546
|
+
version="v2",
|
|
2547
|
+
config={"callbacks": [CustomCallbackManager()]}
|
|
2548
|
+
):
|
|
2549
|
+
print(event)
|
|
2550
|
+
```
|
|
2693
2551
|
|
|
2694
|
-
|
|
2552
|
+
!!! warning
|
|
2695
2553
|
If using python <= 3.10 and async, you MUST
|
|
2696
2554
|
specify the `config` parameter or the function will raise an error.
|
|
2697
2555
|
This is due to a limitation in asyncio for python <= 3.10 that prevents
|
|
2698
2556
|
LangChain from automatically propagating the config object on the user's
|
|
2699
2557
|
behalf.
|
|
2700
2558
|
|
|
2701
|
-
|
|
2559
|
+
!!! version-added "Added in version 0.2.15"
|
|
2702
2560
|
|
|
2703
2561
|
"""
|
|
2704
|
-
|
|
2562
|
+
# Import locally to prevent circular imports.
|
|
2563
|
+
from langchain_core.runnables.config import ( # noqa: PLC0415
|
|
2705
2564
|
ensure_config,
|
|
2706
2565
|
get_async_callback_manager_for_config,
|
|
2707
2566
|
)
|
|
@@ -2730,49 +2589,53 @@ async def adispatch_custom_event(
|
|
|
2730
2589
|
|
|
2731
2590
|
|
|
2732
2591
|
def dispatch_custom_event(
|
|
2733
|
-
name: str, data: Any, *, config:
|
|
2592
|
+
name: str, data: Any, *, config: RunnableConfig | None = None
|
|
2734
2593
|
) -> None:
|
|
2735
2594
|
"""Dispatch an adhoc event.
|
|
2736
2595
|
|
|
2737
2596
|
Args:
|
|
2738
2597
|
name: The name of the adhoc event.
|
|
2739
2598
|
data: The data for the adhoc event. Free form data. Ideally should be
|
|
2740
|
-
|
|
2741
|
-
|
|
2599
|
+
JSON serializable to avoid serialization issues downstream, but
|
|
2600
|
+
this is not enforced.
|
|
2742
2601
|
config: Optional config object. Mirrors the async API but not strictly needed.
|
|
2743
2602
|
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2603
|
+
Raises:
|
|
2604
|
+
RuntimeError: If there is no parent run ID available to associate
|
|
2605
|
+
the event with.
|
|
2747
2606
|
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2607
|
+
Example:
|
|
2608
|
+
```python
|
|
2609
|
+
from langchain_core.callbacks import BaseCallbackHandler
|
|
2610
|
+
from langchain_core.callbacks import dispatch_custom_event
|
|
2611
|
+
from langchain_core.runnable import RunnableLambda
|
|
2612
|
+
|
|
2613
|
+
class CustomCallbackManager(BaseCallbackHandler):
|
|
2614
|
+
def on_custom_event(
|
|
2615
|
+
self,
|
|
2616
|
+
name: str,
|
|
2617
|
+
data: Any,
|
|
2618
|
+
*,
|
|
2619
|
+
run_id: UUID,
|
|
2620
|
+
tags: list[str] | None = None,
|
|
2621
|
+
metadata: dict[str, Any] | None = None,
|
|
2622
|
+
**kwargs: Any,
|
|
2623
|
+
) -> None:
|
|
2624
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2625
|
+
|
|
2626
|
+
def foo(inputs):
|
|
2627
|
+
dispatch_custom_event("my_event", {"bar": "buzz})
|
|
2628
|
+
return inputs
|
|
2629
|
+
|
|
2630
|
+
foo_ = RunnableLambda(foo)
|
|
2631
|
+
foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2632
|
+
```
|
|
2633
|
+
|
|
2634
|
+
!!! version-added "Added in version 0.2.15"
|
|
2773
2635
|
|
|
2774
2636
|
"""
|
|
2775
|
-
|
|
2637
|
+
# Import locally to prevent circular imports.
|
|
2638
|
+
from langchain_core.runnables.config import ( # noqa: PLC0415
|
|
2776
2639
|
ensure_config,
|
|
2777
2640
|
get_callback_manager_for_config,
|
|
2778
2641
|
)
|