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
langchain_core/callbacks/base.py
CHANGED
|
@@ -3,12 +3,10 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import logging
|
|
6
|
-
from typing import TYPE_CHECKING, Any
|
|
6
|
+
from typing import TYPE_CHECKING, Any
|
|
7
7
|
|
|
8
8
|
from typing_extensions import Self
|
|
9
9
|
|
|
10
|
-
from langchain_core.v1.messages import AIMessage, AIMessageChunk, MessageV1
|
|
11
|
-
|
|
12
10
|
if TYPE_CHECKING:
|
|
13
11
|
from collections.abc import Sequence
|
|
14
12
|
from uuid import UUID
|
|
@@ -31,16 +29,16 @@ class RetrieverManagerMixin:
|
|
|
31
29
|
error: BaseException,
|
|
32
30
|
*,
|
|
33
31
|
run_id: UUID,
|
|
34
|
-
parent_run_id:
|
|
32
|
+
parent_run_id: UUID | None = None,
|
|
35
33
|
**kwargs: Any,
|
|
36
34
|
) -> Any:
|
|
37
35
|
"""Run when Retriever errors.
|
|
38
36
|
|
|
39
37
|
Args:
|
|
40
|
-
error
|
|
41
|
-
run_id
|
|
42
|
-
parent_run_id
|
|
43
|
-
kwargs
|
|
38
|
+
error: The error that occurred.
|
|
39
|
+
run_id: The run ID. This is the ID of the current run.
|
|
40
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
41
|
+
**kwargs: Additional keyword arguments.
|
|
44
42
|
"""
|
|
45
43
|
|
|
46
44
|
def on_retriever_end(
|
|
@@ -48,16 +46,16 @@ class RetrieverManagerMixin:
|
|
|
48
46
|
documents: Sequence[Document],
|
|
49
47
|
*,
|
|
50
48
|
run_id: UUID,
|
|
51
|
-
parent_run_id:
|
|
49
|
+
parent_run_id: UUID | None = None,
|
|
52
50
|
**kwargs: Any,
|
|
53
51
|
) -> Any:
|
|
54
52
|
"""Run when Retriever ends running.
|
|
55
53
|
|
|
56
54
|
Args:
|
|
57
|
-
documents
|
|
58
|
-
run_id
|
|
59
|
-
parent_run_id
|
|
60
|
-
kwargs
|
|
55
|
+
documents: The documents retrieved.
|
|
56
|
+
run_id: The run ID. This is the ID of the current run.
|
|
57
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
58
|
+
**kwargs: Additional keyword arguments.
|
|
61
59
|
"""
|
|
62
60
|
|
|
63
61
|
|
|
@@ -68,39 +66,38 @@ class LLMManagerMixin:
|
|
|
68
66
|
self,
|
|
69
67
|
token: str,
|
|
70
68
|
*,
|
|
71
|
-
chunk:
|
|
72
|
-
Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
|
|
73
|
-
] = None,
|
|
69
|
+
chunk: GenerationChunk | ChatGenerationChunk | None = None,
|
|
74
70
|
run_id: UUID,
|
|
75
|
-
parent_run_id:
|
|
71
|
+
parent_run_id: UUID | None = None,
|
|
76
72
|
**kwargs: Any,
|
|
77
73
|
) -> Any:
|
|
78
|
-
"""Run on new
|
|
74
|
+
"""Run on new output token. Only available when streaming is enabled.
|
|
75
|
+
|
|
76
|
+
For both chat models and non-chat models (legacy LLMs).
|
|
79
77
|
|
|
80
78
|
Args:
|
|
81
|
-
token
|
|
82
|
-
chunk
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
kwargs (Any): Additional keyword arguments.
|
|
79
|
+
token: The new token.
|
|
80
|
+
chunk: The new generated chunk, containing content and other information.
|
|
81
|
+
run_id: The run ID. This is the ID of the current run.
|
|
82
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
83
|
+
**kwargs: Additional keyword arguments.
|
|
87
84
|
"""
|
|
88
85
|
|
|
89
86
|
def on_llm_end(
|
|
90
87
|
self,
|
|
91
|
-
response:
|
|
88
|
+
response: LLMResult,
|
|
92
89
|
*,
|
|
93
90
|
run_id: UUID,
|
|
94
|
-
parent_run_id:
|
|
91
|
+
parent_run_id: UUID | None = None,
|
|
95
92
|
**kwargs: Any,
|
|
96
93
|
) -> Any:
|
|
97
94
|
"""Run when LLM ends running.
|
|
98
95
|
|
|
99
96
|
Args:
|
|
100
|
-
response
|
|
101
|
-
run_id
|
|
102
|
-
parent_run_id
|
|
103
|
-
kwargs
|
|
97
|
+
response: The response which was generated.
|
|
98
|
+
run_id: The run ID. This is the ID of the current run.
|
|
99
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
100
|
+
**kwargs: Additional keyword arguments.
|
|
104
101
|
"""
|
|
105
102
|
|
|
106
103
|
def on_llm_error(
|
|
@@ -108,16 +105,16 @@ class LLMManagerMixin:
|
|
|
108
105
|
error: BaseException,
|
|
109
106
|
*,
|
|
110
107
|
run_id: UUID,
|
|
111
|
-
parent_run_id:
|
|
108
|
+
parent_run_id: UUID | None = None,
|
|
112
109
|
**kwargs: Any,
|
|
113
110
|
) -> Any:
|
|
114
111
|
"""Run when LLM errors.
|
|
115
112
|
|
|
116
113
|
Args:
|
|
117
|
-
error
|
|
118
|
-
run_id
|
|
119
|
-
parent_run_id
|
|
120
|
-
kwargs
|
|
114
|
+
error: The error that occurred.
|
|
115
|
+
run_id: The run ID. This is the ID of the current run.
|
|
116
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
117
|
+
**kwargs: Additional keyword arguments.
|
|
121
118
|
"""
|
|
122
119
|
|
|
123
120
|
|
|
@@ -129,16 +126,16 @@ class ChainManagerMixin:
|
|
|
129
126
|
outputs: dict[str, Any],
|
|
130
127
|
*,
|
|
131
128
|
run_id: UUID,
|
|
132
|
-
parent_run_id:
|
|
129
|
+
parent_run_id: UUID | None = None,
|
|
133
130
|
**kwargs: Any,
|
|
134
131
|
) -> Any:
|
|
135
132
|
"""Run when chain ends running.
|
|
136
133
|
|
|
137
134
|
Args:
|
|
138
|
-
outputs
|
|
139
|
-
run_id
|
|
140
|
-
parent_run_id
|
|
141
|
-
kwargs
|
|
135
|
+
outputs: The outputs of the chain.
|
|
136
|
+
run_id: The run ID. This is the ID of the current run.
|
|
137
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
138
|
+
**kwargs: Additional keyword arguments.
|
|
142
139
|
"""
|
|
143
140
|
|
|
144
141
|
def on_chain_error(
|
|
@@ -146,16 +143,16 @@ class ChainManagerMixin:
|
|
|
146
143
|
error: BaseException,
|
|
147
144
|
*,
|
|
148
145
|
run_id: UUID,
|
|
149
|
-
parent_run_id:
|
|
146
|
+
parent_run_id: UUID | None = None,
|
|
150
147
|
**kwargs: Any,
|
|
151
148
|
) -> Any:
|
|
152
149
|
"""Run when chain errors.
|
|
153
150
|
|
|
154
151
|
Args:
|
|
155
|
-
error
|
|
156
|
-
run_id
|
|
157
|
-
parent_run_id
|
|
158
|
-
kwargs
|
|
152
|
+
error: The error that occurred.
|
|
153
|
+
run_id: The run ID. This is the ID of the current run.
|
|
154
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
155
|
+
**kwargs: Additional keyword arguments.
|
|
159
156
|
"""
|
|
160
157
|
|
|
161
158
|
def on_agent_action(
|
|
@@ -163,16 +160,16 @@ class ChainManagerMixin:
|
|
|
163
160
|
action: AgentAction,
|
|
164
161
|
*,
|
|
165
162
|
run_id: UUID,
|
|
166
|
-
parent_run_id:
|
|
163
|
+
parent_run_id: UUID | None = None,
|
|
167
164
|
**kwargs: Any,
|
|
168
165
|
) -> Any:
|
|
169
166
|
"""Run on agent action.
|
|
170
167
|
|
|
171
168
|
Args:
|
|
172
|
-
action
|
|
173
|
-
run_id
|
|
174
|
-
parent_run_id
|
|
175
|
-
kwargs
|
|
169
|
+
action: The agent action.
|
|
170
|
+
run_id: The run ID. This is the ID of the current run.
|
|
171
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
172
|
+
**kwargs: Additional keyword arguments.
|
|
176
173
|
"""
|
|
177
174
|
|
|
178
175
|
def on_agent_finish(
|
|
@@ -180,16 +177,16 @@ class ChainManagerMixin:
|
|
|
180
177
|
finish: AgentFinish,
|
|
181
178
|
*,
|
|
182
179
|
run_id: UUID,
|
|
183
|
-
parent_run_id:
|
|
180
|
+
parent_run_id: UUID | None = None,
|
|
184
181
|
**kwargs: Any,
|
|
185
182
|
) -> Any:
|
|
186
183
|
"""Run on the agent end.
|
|
187
184
|
|
|
188
185
|
Args:
|
|
189
|
-
finish
|
|
190
|
-
run_id
|
|
191
|
-
parent_run_id
|
|
192
|
-
kwargs
|
|
186
|
+
finish: The agent finish.
|
|
187
|
+
run_id: The run ID. This is the ID of the current run.
|
|
188
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
189
|
+
**kwargs: Additional keyword arguments.
|
|
193
190
|
"""
|
|
194
191
|
|
|
195
192
|
|
|
@@ -201,16 +198,16 @@ class ToolManagerMixin:
|
|
|
201
198
|
output: Any,
|
|
202
199
|
*,
|
|
203
200
|
run_id: UUID,
|
|
204
|
-
parent_run_id:
|
|
201
|
+
parent_run_id: UUID | None = None,
|
|
205
202
|
**kwargs: Any,
|
|
206
203
|
) -> Any:
|
|
207
204
|
"""Run when the tool ends running.
|
|
208
205
|
|
|
209
206
|
Args:
|
|
210
|
-
output
|
|
211
|
-
run_id
|
|
212
|
-
parent_run_id
|
|
213
|
-
kwargs
|
|
207
|
+
output: The output of the tool.
|
|
208
|
+
run_id: The run ID. This is the ID of the current run.
|
|
209
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
210
|
+
**kwargs: Additional keyword arguments.
|
|
214
211
|
"""
|
|
215
212
|
|
|
216
213
|
def on_tool_error(
|
|
@@ -218,16 +215,16 @@ class ToolManagerMixin:
|
|
|
218
215
|
error: BaseException,
|
|
219
216
|
*,
|
|
220
217
|
run_id: UUID,
|
|
221
|
-
parent_run_id:
|
|
218
|
+
parent_run_id: UUID | None = None,
|
|
222
219
|
**kwargs: Any,
|
|
223
220
|
) -> Any:
|
|
224
221
|
"""Run when tool errors.
|
|
225
222
|
|
|
226
223
|
Args:
|
|
227
|
-
error
|
|
228
|
-
run_id
|
|
229
|
-
parent_run_id
|
|
230
|
-
kwargs
|
|
224
|
+
error: The error that occurred.
|
|
225
|
+
run_id: The run ID. This is the ID of the current run.
|
|
226
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
227
|
+
**kwargs: Additional keyword arguments.
|
|
231
228
|
"""
|
|
232
229
|
|
|
233
230
|
|
|
@@ -240,52 +237,53 @@ class CallbackManagerMixin:
|
|
|
240
237
|
prompts: list[str],
|
|
241
238
|
*,
|
|
242
239
|
run_id: UUID,
|
|
243
|
-
parent_run_id:
|
|
244
|
-
tags:
|
|
245
|
-
metadata:
|
|
240
|
+
parent_run_id: UUID | None = None,
|
|
241
|
+
tags: list[str] | None = None,
|
|
242
|
+
metadata: dict[str, Any] | None = None,
|
|
246
243
|
**kwargs: Any,
|
|
247
244
|
) -> Any:
|
|
248
245
|
"""Run when LLM starts running.
|
|
249
246
|
|
|
250
|
-
|
|
247
|
+
!!! warning
|
|
251
248
|
This method is called for non-chat models (regular LLMs). If you're
|
|
252
249
|
implementing a handler for a chat model, you should use
|
|
253
|
-
|
|
250
|
+
`on_chat_model_start` instead.
|
|
254
251
|
|
|
255
252
|
Args:
|
|
256
|
-
serialized
|
|
257
|
-
prompts
|
|
258
|
-
run_id
|
|
259
|
-
parent_run_id
|
|
260
|
-
tags
|
|
261
|
-
metadata
|
|
262
|
-
kwargs
|
|
253
|
+
serialized: The serialized LLM.
|
|
254
|
+
prompts: The prompts.
|
|
255
|
+
run_id: The run ID. This is the ID of the current run.
|
|
256
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
257
|
+
tags: The tags.
|
|
258
|
+
metadata: The metadata.
|
|
259
|
+
**kwargs: Additional keyword arguments.
|
|
263
260
|
"""
|
|
264
261
|
|
|
265
262
|
def on_chat_model_start(
|
|
266
263
|
self,
|
|
267
264
|
serialized: dict[str, Any],
|
|
268
|
-
messages:
|
|
265
|
+
messages: list[list[BaseMessage]],
|
|
269
266
|
*,
|
|
270
267
|
run_id: UUID,
|
|
271
|
-
parent_run_id:
|
|
272
|
-
tags:
|
|
273
|
-
metadata:
|
|
268
|
+
parent_run_id: UUID | None = None,
|
|
269
|
+
tags: list[str] | None = None,
|
|
270
|
+
metadata: dict[str, Any] | None = None,
|
|
274
271
|
**kwargs: Any,
|
|
275
272
|
) -> Any:
|
|
276
273
|
"""Run when a chat model starts running.
|
|
277
274
|
|
|
278
|
-
|
|
279
|
-
|
|
275
|
+
!!! warning
|
|
276
|
+
This method is called for chat models. If you're implementing a handler for
|
|
277
|
+
a non-chat model, you should use `on_llm_start` instead.
|
|
280
278
|
|
|
281
279
|
Args:
|
|
282
|
-
serialized
|
|
283
|
-
messages
|
|
284
|
-
run_id
|
|
285
|
-
parent_run_id
|
|
286
|
-
tags
|
|
287
|
-
metadata
|
|
288
|
-
kwargs
|
|
280
|
+
serialized: The serialized chat model.
|
|
281
|
+
messages: The messages.
|
|
282
|
+
run_id: The run ID. This is the ID of the current run.
|
|
283
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
284
|
+
tags: The tags.
|
|
285
|
+
metadata: The metadata.
|
|
286
|
+
**kwargs: Additional keyword arguments.
|
|
289
287
|
"""
|
|
290
288
|
# NotImplementedError is thrown intentionally
|
|
291
289
|
# Callback handler will fall back to on_llm_start if this is exception is thrown
|
|
@@ -298,21 +296,21 @@ class CallbackManagerMixin:
|
|
|
298
296
|
query: str,
|
|
299
297
|
*,
|
|
300
298
|
run_id: UUID,
|
|
301
|
-
parent_run_id:
|
|
302
|
-
tags:
|
|
303
|
-
metadata:
|
|
299
|
+
parent_run_id: UUID | None = None,
|
|
300
|
+
tags: list[str] | None = None,
|
|
301
|
+
metadata: dict[str, Any] | None = None,
|
|
304
302
|
**kwargs: Any,
|
|
305
303
|
) -> Any:
|
|
306
304
|
"""Run when the Retriever starts running.
|
|
307
305
|
|
|
308
306
|
Args:
|
|
309
|
-
serialized
|
|
310
|
-
query
|
|
311
|
-
run_id
|
|
312
|
-
parent_run_id
|
|
313
|
-
tags
|
|
314
|
-
metadata
|
|
315
|
-
kwargs
|
|
307
|
+
serialized: The serialized Retriever.
|
|
308
|
+
query: The query.
|
|
309
|
+
run_id: The run ID. This is the ID of the current run.
|
|
310
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
311
|
+
tags: The tags.
|
|
312
|
+
metadata: The metadata.
|
|
313
|
+
**kwargs: Additional keyword arguments.
|
|
316
314
|
"""
|
|
317
315
|
|
|
318
316
|
def on_chain_start(
|
|
@@ -321,21 +319,21 @@ class CallbackManagerMixin:
|
|
|
321
319
|
inputs: dict[str, Any],
|
|
322
320
|
*,
|
|
323
321
|
run_id: UUID,
|
|
324
|
-
parent_run_id:
|
|
325
|
-
tags:
|
|
326
|
-
metadata:
|
|
322
|
+
parent_run_id: UUID | None = None,
|
|
323
|
+
tags: list[str] | None = None,
|
|
324
|
+
metadata: dict[str, Any] | None = None,
|
|
327
325
|
**kwargs: Any,
|
|
328
326
|
) -> Any:
|
|
329
327
|
"""Run when a chain starts running.
|
|
330
328
|
|
|
331
329
|
Args:
|
|
332
|
-
serialized
|
|
333
|
-
inputs
|
|
334
|
-
run_id
|
|
335
|
-
parent_run_id
|
|
336
|
-
tags
|
|
337
|
-
metadata
|
|
338
|
-
kwargs
|
|
330
|
+
serialized: The serialized chain.
|
|
331
|
+
inputs: The inputs.
|
|
332
|
+
run_id: The run ID. This is the ID of the current run.
|
|
333
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
334
|
+
tags: The tags.
|
|
335
|
+
metadata: The metadata.
|
|
336
|
+
**kwargs: Additional keyword arguments.
|
|
339
337
|
"""
|
|
340
338
|
|
|
341
339
|
def on_tool_start(
|
|
@@ -344,23 +342,23 @@ class CallbackManagerMixin:
|
|
|
344
342
|
input_str: str,
|
|
345
343
|
*,
|
|
346
344
|
run_id: UUID,
|
|
347
|
-
parent_run_id:
|
|
348
|
-
tags:
|
|
349
|
-
metadata:
|
|
350
|
-
inputs:
|
|
345
|
+
parent_run_id: UUID | None = None,
|
|
346
|
+
tags: list[str] | None = None,
|
|
347
|
+
metadata: dict[str, Any] | None = None,
|
|
348
|
+
inputs: dict[str, Any] | None = None,
|
|
351
349
|
**kwargs: Any,
|
|
352
350
|
) -> Any:
|
|
353
351
|
"""Run when the tool starts running.
|
|
354
352
|
|
|
355
353
|
Args:
|
|
356
|
-
serialized
|
|
357
|
-
input_str
|
|
358
|
-
run_id
|
|
359
|
-
parent_run_id
|
|
360
|
-
tags
|
|
361
|
-
metadata
|
|
362
|
-
inputs
|
|
363
|
-
kwargs
|
|
354
|
+
serialized: The serialized chain.
|
|
355
|
+
input_str: The input string.
|
|
356
|
+
run_id: The run ID. This is the ID of the current run.
|
|
357
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
358
|
+
tags: The tags.
|
|
359
|
+
metadata: The metadata.
|
|
360
|
+
inputs: The inputs.
|
|
361
|
+
**kwargs: Additional keyword arguments.
|
|
364
362
|
"""
|
|
365
363
|
|
|
366
364
|
|
|
@@ -372,16 +370,16 @@ class RunManagerMixin:
|
|
|
372
370
|
text: str,
|
|
373
371
|
*,
|
|
374
372
|
run_id: UUID,
|
|
375
|
-
parent_run_id:
|
|
373
|
+
parent_run_id: UUID | None = None,
|
|
376
374
|
**kwargs: Any,
|
|
377
375
|
) -> Any:
|
|
378
376
|
"""Run on an arbitrary text.
|
|
379
377
|
|
|
380
378
|
Args:
|
|
381
|
-
text
|
|
382
|
-
run_id
|
|
383
|
-
parent_run_id
|
|
384
|
-
kwargs
|
|
379
|
+
text: The text.
|
|
380
|
+
run_id: The run ID. This is the ID of the current run.
|
|
381
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
382
|
+
**kwargs: Additional keyword arguments.
|
|
385
383
|
"""
|
|
386
384
|
|
|
387
385
|
def on_retry(
|
|
@@ -389,16 +387,16 @@ class RunManagerMixin:
|
|
|
389
387
|
retry_state: RetryCallState,
|
|
390
388
|
*,
|
|
391
389
|
run_id: UUID,
|
|
392
|
-
parent_run_id:
|
|
390
|
+
parent_run_id: UUID | None = None,
|
|
393
391
|
**kwargs: Any,
|
|
394
392
|
) -> Any:
|
|
395
393
|
"""Run on a retry event.
|
|
396
394
|
|
|
397
395
|
Args:
|
|
398
|
-
retry_state
|
|
399
|
-
run_id
|
|
400
|
-
parent_run_id
|
|
401
|
-
kwargs
|
|
396
|
+
retry_state: The retry state.
|
|
397
|
+
run_id: The run ID. This is the ID of the current run.
|
|
398
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
399
|
+
**kwargs: Additional keyword arguments.
|
|
402
400
|
"""
|
|
403
401
|
|
|
404
402
|
def on_custom_event(
|
|
@@ -407,8 +405,8 @@ class RunManagerMixin:
|
|
|
407
405
|
data: Any,
|
|
408
406
|
*,
|
|
409
407
|
run_id: UUID,
|
|
410
|
-
tags:
|
|
411
|
-
metadata:
|
|
408
|
+
tags: list[str] | None = None,
|
|
409
|
+
metadata: dict[str, Any] | None = None,
|
|
412
410
|
**kwargs: Any,
|
|
413
411
|
) -> Any:
|
|
414
412
|
"""Override to define a handler for a custom event.
|
|
@@ -416,14 +414,14 @@ class RunManagerMixin:
|
|
|
416
414
|
Args:
|
|
417
415
|
name: The name of the custom event.
|
|
418
416
|
data: The data for the custom event. Format will match
|
|
419
|
-
|
|
417
|
+
the format specified by the user.
|
|
420
418
|
run_id: The ID of the run.
|
|
421
419
|
tags: The tags associated with the custom event
|
|
422
420
|
(includes inherited tags).
|
|
423
421
|
metadata: The metadata associated with the custom event
|
|
424
422
|
(includes inherited metadata).
|
|
425
423
|
|
|
426
|
-
|
|
424
|
+
!!! version-added "Added in version 0.2.15"
|
|
427
425
|
"""
|
|
428
426
|
|
|
429
427
|
|
|
@@ -443,9 +441,6 @@ class BaseCallbackHandler(
|
|
|
443
441
|
run_inline: bool = False
|
|
444
442
|
"""Whether to run the callback inline."""
|
|
445
443
|
|
|
446
|
-
accepts_new_messages: bool = False
|
|
447
|
-
"""Whether the callback accepts new message format."""
|
|
448
|
-
|
|
449
444
|
@property
|
|
450
445
|
def ignore_llm(self) -> bool:
|
|
451
446
|
"""Whether to ignore LLM callbacks."""
|
|
@@ -491,52 +486,53 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
491
486
|
prompts: list[str],
|
|
492
487
|
*,
|
|
493
488
|
run_id: UUID,
|
|
494
|
-
parent_run_id:
|
|
495
|
-
tags:
|
|
496
|
-
metadata:
|
|
489
|
+
parent_run_id: UUID | None = None,
|
|
490
|
+
tags: list[str] | None = None,
|
|
491
|
+
metadata: dict[str, Any] | None = None,
|
|
497
492
|
**kwargs: Any,
|
|
498
493
|
) -> None:
|
|
499
|
-
"""Run when
|
|
494
|
+
"""Run when the model starts running.
|
|
500
495
|
|
|
501
|
-
|
|
496
|
+
!!! warning
|
|
502
497
|
This method is called for non-chat models (regular LLMs). If you're
|
|
503
498
|
implementing a handler for a chat model, you should use
|
|
504
|
-
|
|
499
|
+
`on_chat_model_start` instead.
|
|
505
500
|
|
|
506
501
|
Args:
|
|
507
|
-
serialized
|
|
508
|
-
prompts
|
|
509
|
-
run_id
|
|
510
|
-
parent_run_id
|
|
511
|
-
tags
|
|
512
|
-
metadata
|
|
513
|
-
kwargs
|
|
502
|
+
serialized: The serialized LLM.
|
|
503
|
+
prompts: The prompts.
|
|
504
|
+
run_id: The run ID. This is the ID of the current run.
|
|
505
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
506
|
+
tags: The tags.
|
|
507
|
+
metadata: The metadata.
|
|
508
|
+
**kwargs: Additional keyword arguments.
|
|
514
509
|
"""
|
|
515
510
|
|
|
516
511
|
async def on_chat_model_start(
|
|
517
512
|
self,
|
|
518
513
|
serialized: dict[str, Any],
|
|
519
|
-
messages:
|
|
514
|
+
messages: list[list[BaseMessage]],
|
|
520
515
|
*,
|
|
521
516
|
run_id: UUID,
|
|
522
|
-
parent_run_id:
|
|
523
|
-
tags:
|
|
524
|
-
metadata:
|
|
517
|
+
parent_run_id: UUID | None = None,
|
|
518
|
+
tags: list[str] | None = None,
|
|
519
|
+
metadata: dict[str, Any] | None = None,
|
|
525
520
|
**kwargs: Any,
|
|
526
521
|
) -> Any:
|
|
527
522
|
"""Run when a chat model starts running.
|
|
528
523
|
|
|
529
|
-
|
|
530
|
-
|
|
524
|
+
!!! warning
|
|
525
|
+
This method is called for chat models. If you're implementing a handler for
|
|
526
|
+
a non-chat model, you should use `on_llm_start` instead.
|
|
531
527
|
|
|
532
528
|
Args:
|
|
533
|
-
serialized
|
|
534
|
-
messages
|
|
535
|
-
run_id
|
|
536
|
-
parent_run_id
|
|
537
|
-
tags
|
|
538
|
-
metadata
|
|
539
|
-
kwargs
|
|
529
|
+
serialized: The serialized chat model.
|
|
530
|
+
messages: The messages.
|
|
531
|
+
run_id: The run ID. This is the ID of the current run.
|
|
532
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
533
|
+
tags: The tags.
|
|
534
|
+
metadata: The metadata.
|
|
535
|
+
**kwargs: Additional keyword arguments.
|
|
540
536
|
"""
|
|
541
537
|
# NotImplementedError is thrown intentionally
|
|
542
538
|
# Callback handler will fall back to on_llm_start if this is exception is thrown
|
|
@@ -547,43 +543,42 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
547
543
|
self,
|
|
548
544
|
token: str,
|
|
549
545
|
*,
|
|
550
|
-
chunk:
|
|
551
|
-
Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
|
|
552
|
-
] = None,
|
|
546
|
+
chunk: GenerationChunk | ChatGenerationChunk | None = None,
|
|
553
547
|
run_id: UUID,
|
|
554
|
-
parent_run_id:
|
|
555
|
-
tags:
|
|
548
|
+
parent_run_id: UUID | None = None,
|
|
549
|
+
tags: list[str] | None = None,
|
|
556
550
|
**kwargs: Any,
|
|
557
551
|
) -> None:
|
|
558
|
-
"""Run on new
|
|
552
|
+
"""Run on new output token. Only available when streaming is enabled.
|
|
553
|
+
|
|
554
|
+
For both chat models and non-chat models (legacy LLMs).
|
|
559
555
|
|
|
560
556
|
Args:
|
|
561
|
-
token
|
|
562
|
-
chunk
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
kwargs (Any): Additional keyword arguments.
|
|
557
|
+
token: The new token.
|
|
558
|
+
chunk: The new generated chunk, containing content and other information.
|
|
559
|
+
run_id: The run ID. This is the ID of the current run.
|
|
560
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
561
|
+
tags: The tags.
|
|
562
|
+
**kwargs: Additional keyword arguments.
|
|
568
563
|
"""
|
|
569
564
|
|
|
570
565
|
async def on_llm_end(
|
|
571
566
|
self,
|
|
572
|
-
response:
|
|
567
|
+
response: LLMResult,
|
|
573
568
|
*,
|
|
574
569
|
run_id: UUID,
|
|
575
|
-
parent_run_id:
|
|
576
|
-
tags:
|
|
570
|
+
parent_run_id: UUID | None = None,
|
|
571
|
+
tags: list[str] | None = None,
|
|
577
572
|
**kwargs: Any,
|
|
578
573
|
) -> None:
|
|
579
|
-
"""Run when
|
|
574
|
+
"""Run when the model ends running.
|
|
580
575
|
|
|
581
576
|
Args:
|
|
582
|
-
response
|
|
583
|
-
run_id
|
|
584
|
-
parent_run_id
|
|
585
|
-
tags
|
|
586
|
-
kwargs
|
|
577
|
+
response: The response which was generated.
|
|
578
|
+
run_id: The run ID. This is the ID of the current run.
|
|
579
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
580
|
+
tags: The tags.
|
|
581
|
+
**kwargs: Additional keyword arguments.
|
|
587
582
|
"""
|
|
588
583
|
|
|
589
584
|
async def on_llm_error(
|
|
@@ -591,8 +586,8 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
591
586
|
error: BaseException,
|
|
592
587
|
*,
|
|
593
588
|
run_id: UUID,
|
|
594
|
-
parent_run_id:
|
|
595
|
-
tags:
|
|
589
|
+
parent_run_id: UUID | None = None,
|
|
590
|
+
tags: list[str] | None = None,
|
|
596
591
|
**kwargs: Any,
|
|
597
592
|
) -> None:
|
|
598
593
|
"""Run when LLM errors.
|
|
@@ -602,9 +597,9 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
602
597
|
run_id: The run ID. This is the ID of the current run.
|
|
603
598
|
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
604
599
|
tags: The tags.
|
|
605
|
-
kwargs
|
|
606
|
-
- response (LLMResult
|
|
607
|
-
|
|
600
|
+
**kwargs: Additional keyword arguments.
|
|
601
|
+
- response (LLMResult): The response which was generated before
|
|
602
|
+
the error occurred.
|
|
608
603
|
"""
|
|
609
604
|
|
|
610
605
|
async def on_chain_start(
|
|
@@ -613,21 +608,21 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
613
608
|
inputs: dict[str, Any],
|
|
614
609
|
*,
|
|
615
610
|
run_id: UUID,
|
|
616
|
-
parent_run_id:
|
|
617
|
-
tags:
|
|
618
|
-
metadata:
|
|
611
|
+
parent_run_id: UUID | None = None,
|
|
612
|
+
tags: list[str] | None = None,
|
|
613
|
+
metadata: dict[str, Any] | None = None,
|
|
619
614
|
**kwargs: Any,
|
|
620
615
|
) -> None:
|
|
621
616
|
"""Run when a chain starts running.
|
|
622
617
|
|
|
623
618
|
Args:
|
|
624
|
-
serialized
|
|
625
|
-
inputs
|
|
626
|
-
run_id
|
|
627
|
-
parent_run_id
|
|
628
|
-
tags
|
|
629
|
-
metadata
|
|
630
|
-
kwargs
|
|
619
|
+
serialized: The serialized chain.
|
|
620
|
+
inputs: The inputs.
|
|
621
|
+
run_id: The run ID. This is the ID of the current run.
|
|
622
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
623
|
+
tags: The tags.
|
|
624
|
+
metadata: The metadata.
|
|
625
|
+
**kwargs: Additional keyword arguments.
|
|
631
626
|
"""
|
|
632
627
|
|
|
633
628
|
async def on_chain_end(
|
|
@@ -635,18 +630,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
635
630
|
outputs: dict[str, Any],
|
|
636
631
|
*,
|
|
637
632
|
run_id: UUID,
|
|
638
|
-
parent_run_id:
|
|
639
|
-
tags:
|
|
633
|
+
parent_run_id: UUID | None = None,
|
|
634
|
+
tags: list[str] | None = None,
|
|
640
635
|
**kwargs: Any,
|
|
641
636
|
) -> None:
|
|
642
637
|
"""Run when a chain ends running.
|
|
643
638
|
|
|
644
639
|
Args:
|
|
645
|
-
outputs
|
|
646
|
-
run_id
|
|
647
|
-
parent_run_id
|
|
648
|
-
tags
|
|
649
|
-
kwargs
|
|
640
|
+
outputs: The outputs of the chain.
|
|
641
|
+
run_id: The run ID. This is the ID of the current run.
|
|
642
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
643
|
+
tags: The tags.
|
|
644
|
+
**kwargs: Additional keyword arguments.
|
|
650
645
|
"""
|
|
651
646
|
|
|
652
647
|
async def on_chain_error(
|
|
@@ -654,18 +649,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
654
649
|
error: BaseException,
|
|
655
650
|
*,
|
|
656
651
|
run_id: UUID,
|
|
657
|
-
parent_run_id:
|
|
658
|
-
tags:
|
|
652
|
+
parent_run_id: UUID | None = None,
|
|
653
|
+
tags: list[str] | None = None,
|
|
659
654
|
**kwargs: Any,
|
|
660
655
|
) -> None:
|
|
661
656
|
"""Run when chain errors.
|
|
662
657
|
|
|
663
658
|
Args:
|
|
664
|
-
error
|
|
665
|
-
run_id
|
|
666
|
-
parent_run_id
|
|
667
|
-
tags
|
|
668
|
-
kwargs
|
|
659
|
+
error: The error that occurred.
|
|
660
|
+
run_id: The run ID. This is the ID of the current run.
|
|
661
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
662
|
+
tags: The tags.
|
|
663
|
+
**kwargs: Additional keyword arguments.
|
|
669
664
|
"""
|
|
670
665
|
|
|
671
666
|
async def on_tool_start(
|
|
@@ -674,23 +669,23 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
674
669
|
input_str: str,
|
|
675
670
|
*,
|
|
676
671
|
run_id: UUID,
|
|
677
|
-
parent_run_id:
|
|
678
|
-
tags:
|
|
679
|
-
metadata:
|
|
680
|
-
inputs:
|
|
672
|
+
parent_run_id: UUID | None = None,
|
|
673
|
+
tags: list[str] | None = None,
|
|
674
|
+
metadata: dict[str, Any] | None = None,
|
|
675
|
+
inputs: dict[str, Any] | None = None,
|
|
681
676
|
**kwargs: Any,
|
|
682
677
|
) -> None:
|
|
683
678
|
"""Run when the tool starts running.
|
|
684
679
|
|
|
685
680
|
Args:
|
|
686
|
-
serialized
|
|
687
|
-
input_str
|
|
688
|
-
run_id
|
|
689
|
-
parent_run_id
|
|
690
|
-
tags
|
|
691
|
-
metadata
|
|
692
|
-
inputs
|
|
693
|
-
kwargs
|
|
681
|
+
serialized: The serialized tool.
|
|
682
|
+
input_str: The input string.
|
|
683
|
+
run_id: The run ID. This is the ID of the current run.
|
|
684
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
685
|
+
tags: The tags.
|
|
686
|
+
metadata: The metadata.
|
|
687
|
+
inputs: The inputs.
|
|
688
|
+
**kwargs: Additional keyword arguments.
|
|
694
689
|
"""
|
|
695
690
|
|
|
696
691
|
async def on_tool_end(
|
|
@@ -698,18 +693,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
698
693
|
output: Any,
|
|
699
694
|
*,
|
|
700
695
|
run_id: UUID,
|
|
701
|
-
parent_run_id:
|
|
702
|
-
tags:
|
|
696
|
+
parent_run_id: UUID | None = None,
|
|
697
|
+
tags: list[str] | None = None,
|
|
703
698
|
**kwargs: Any,
|
|
704
699
|
) -> None:
|
|
705
700
|
"""Run when the tool ends running.
|
|
706
701
|
|
|
707
702
|
Args:
|
|
708
|
-
output
|
|
709
|
-
run_id
|
|
710
|
-
parent_run_id
|
|
711
|
-
tags
|
|
712
|
-
kwargs
|
|
703
|
+
output: The output of the tool.
|
|
704
|
+
run_id: The run ID. This is the ID of the current run.
|
|
705
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
706
|
+
tags: The tags.
|
|
707
|
+
**kwargs: Additional keyword arguments.
|
|
713
708
|
"""
|
|
714
709
|
|
|
715
710
|
async def on_tool_error(
|
|
@@ -717,18 +712,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
717
712
|
error: BaseException,
|
|
718
713
|
*,
|
|
719
714
|
run_id: UUID,
|
|
720
|
-
parent_run_id:
|
|
721
|
-
tags:
|
|
715
|
+
parent_run_id: UUID | None = None,
|
|
716
|
+
tags: list[str] | None = None,
|
|
722
717
|
**kwargs: Any,
|
|
723
718
|
) -> None:
|
|
724
719
|
"""Run when tool errors.
|
|
725
720
|
|
|
726
721
|
Args:
|
|
727
|
-
error
|
|
728
|
-
run_id
|
|
729
|
-
parent_run_id
|
|
730
|
-
tags
|
|
731
|
-
kwargs
|
|
722
|
+
error: The error that occurred.
|
|
723
|
+
run_id: The run ID. This is the ID of the current run.
|
|
724
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
725
|
+
tags: The tags.
|
|
726
|
+
**kwargs: Additional keyword arguments.
|
|
732
727
|
"""
|
|
733
728
|
|
|
734
729
|
async def on_text(
|
|
@@ -736,18 +731,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
736
731
|
text: str,
|
|
737
732
|
*,
|
|
738
733
|
run_id: UUID,
|
|
739
|
-
parent_run_id:
|
|
740
|
-
tags:
|
|
734
|
+
parent_run_id: UUID | None = None,
|
|
735
|
+
tags: list[str] | None = None,
|
|
741
736
|
**kwargs: Any,
|
|
742
737
|
) -> None:
|
|
743
738
|
"""Run on an arbitrary text.
|
|
744
739
|
|
|
745
740
|
Args:
|
|
746
|
-
text
|
|
747
|
-
run_id
|
|
748
|
-
parent_run_id
|
|
749
|
-
tags
|
|
750
|
-
kwargs
|
|
741
|
+
text: The text.
|
|
742
|
+
run_id: The run ID. This is the ID of the current run.
|
|
743
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
744
|
+
tags: The tags.
|
|
745
|
+
**kwargs: Additional keyword arguments.
|
|
751
746
|
"""
|
|
752
747
|
|
|
753
748
|
async def on_retry(
|
|
@@ -755,16 +750,16 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
755
750
|
retry_state: RetryCallState,
|
|
756
751
|
*,
|
|
757
752
|
run_id: UUID,
|
|
758
|
-
parent_run_id:
|
|
753
|
+
parent_run_id: UUID | None = None,
|
|
759
754
|
**kwargs: Any,
|
|
760
755
|
) -> Any:
|
|
761
756
|
"""Run on a retry event.
|
|
762
757
|
|
|
763
758
|
Args:
|
|
764
|
-
retry_state
|
|
765
|
-
run_id
|
|
766
|
-
parent_run_id
|
|
767
|
-
kwargs
|
|
759
|
+
retry_state: The retry state.
|
|
760
|
+
run_id: The run ID. This is the ID of the current run.
|
|
761
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
762
|
+
**kwargs: Additional keyword arguments.
|
|
768
763
|
"""
|
|
769
764
|
|
|
770
765
|
async def on_agent_action(
|
|
@@ -772,18 +767,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
772
767
|
action: AgentAction,
|
|
773
768
|
*,
|
|
774
769
|
run_id: UUID,
|
|
775
|
-
parent_run_id:
|
|
776
|
-
tags:
|
|
770
|
+
parent_run_id: UUID | None = None,
|
|
771
|
+
tags: list[str] | None = None,
|
|
777
772
|
**kwargs: Any,
|
|
778
773
|
) -> None:
|
|
779
774
|
"""Run on agent action.
|
|
780
775
|
|
|
781
776
|
Args:
|
|
782
|
-
action
|
|
783
|
-
run_id
|
|
784
|
-
parent_run_id
|
|
785
|
-
tags
|
|
786
|
-
kwargs
|
|
777
|
+
action: The agent action.
|
|
778
|
+
run_id: The run ID. This is the ID of the current run.
|
|
779
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
780
|
+
tags: The tags.
|
|
781
|
+
**kwargs: Additional keyword arguments.
|
|
787
782
|
"""
|
|
788
783
|
|
|
789
784
|
async def on_agent_finish(
|
|
@@ -791,18 +786,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
791
786
|
finish: AgentFinish,
|
|
792
787
|
*,
|
|
793
788
|
run_id: UUID,
|
|
794
|
-
parent_run_id:
|
|
795
|
-
tags:
|
|
789
|
+
parent_run_id: UUID | None = None,
|
|
790
|
+
tags: list[str] | None = None,
|
|
796
791
|
**kwargs: Any,
|
|
797
792
|
) -> None:
|
|
798
793
|
"""Run on the agent end.
|
|
799
794
|
|
|
800
795
|
Args:
|
|
801
|
-
finish
|
|
802
|
-
run_id
|
|
803
|
-
parent_run_id
|
|
804
|
-
tags
|
|
805
|
-
kwargs
|
|
796
|
+
finish: The agent finish.
|
|
797
|
+
run_id: The run ID. This is the ID of the current run.
|
|
798
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
799
|
+
tags: The tags.
|
|
800
|
+
**kwargs: Additional keyword arguments.
|
|
806
801
|
"""
|
|
807
802
|
|
|
808
803
|
async def on_retriever_start(
|
|
@@ -811,21 +806,21 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
811
806
|
query: str,
|
|
812
807
|
*,
|
|
813
808
|
run_id: UUID,
|
|
814
|
-
parent_run_id:
|
|
815
|
-
tags:
|
|
816
|
-
metadata:
|
|
809
|
+
parent_run_id: UUID | None = None,
|
|
810
|
+
tags: list[str] | None = None,
|
|
811
|
+
metadata: dict[str, Any] | None = None,
|
|
817
812
|
**kwargs: Any,
|
|
818
813
|
) -> None:
|
|
819
814
|
"""Run on the retriever start.
|
|
820
815
|
|
|
821
816
|
Args:
|
|
822
|
-
serialized
|
|
823
|
-
query
|
|
824
|
-
run_id
|
|
825
|
-
parent_run_id
|
|
826
|
-
tags
|
|
827
|
-
metadata
|
|
828
|
-
kwargs
|
|
817
|
+
serialized: The serialized retriever.
|
|
818
|
+
query: The query.
|
|
819
|
+
run_id: The run ID. This is the ID of the current run.
|
|
820
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
821
|
+
tags: The tags.
|
|
822
|
+
metadata: The metadata.
|
|
823
|
+
**kwargs: Additional keyword arguments.
|
|
829
824
|
"""
|
|
830
825
|
|
|
831
826
|
async def on_retriever_end(
|
|
@@ -833,18 +828,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
833
828
|
documents: Sequence[Document],
|
|
834
829
|
*,
|
|
835
830
|
run_id: UUID,
|
|
836
|
-
parent_run_id:
|
|
837
|
-
tags:
|
|
831
|
+
parent_run_id: UUID | None = None,
|
|
832
|
+
tags: list[str] | None = None,
|
|
838
833
|
**kwargs: Any,
|
|
839
834
|
) -> None:
|
|
840
835
|
"""Run on the retriever end.
|
|
841
836
|
|
|
842
837
|
Args:
|
|
843
|
-
documents
|
|
844
|
-
run_id
|
|
845
|
-
parent_run_id
|
|
846
|
-
tags
|
|
847
|
-
kwargs
|
|
838
|
+
documents: The documents retrieved.
|
|
839
|
+
run_id: The run ID. This is the ID of the current run.
|
|
840
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
841
|
+
tags: The tags.
|
|
842
|
+
**kwargs: Additional keyword arguments.
|
|
848
843
|
"""
|
|
849
844
|
|
|
850
845
|
async def on_retriever_error(
|
|
@@ -852,18 +847,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
852
847
|
error: BaseException,
|
|
853
848
|
*,
|
|
854
849
|
run_id: UUID,
|
|
855
|
-
parent_run_id:
|
|
856
|
-
tags:
|
|
850
|
+
parent_run_id: UUID | None = None,
|
|
851
|
+
tags: list[str] | None = None,
|
|
857
852
|
**kwargs: Any,
|
|
858
853
|
) -> None:
|
|
859
854
|
"""Run on retriever error.
|
|
860
855
|
|
|
861
856
|
Args:
|
|
862
|
-
error
|
|
863
|
-
run_id
|
|
864
|
-
parent_run_id
|
|
865
|
-
tags
|
|
866
|
-
kwargs
|
|
857
|
+
error: The error that occurred.
|
|
858
|
+
run_id: The run ID. This is the ID of the current run.
|
|
859
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
860
|
+
tags: The tags.
|
|
861
|
+
**kwargs: Additional keyword arguments.
|
|
867
862
|
"""
|
|
868
863
|
|
|
869
864
|
async def on_custom_event(
|
|
@@ -872,23 +867,23 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
872
867
|
data: Any,
|
|
873
868
|
*,
|
|
874
869
|
run_id: UUID,
|
|
875
|
-
tags:
|
|
876
|
-
metadata:
|
|
870
|
+
tags: list[str] | None = None,
|
|
871
|
+
metadata: dict[str, Any] | None = None,
|
|
877
872
|
**kwargs: Any,
|
|
878
873
|
) -> None:
|
|
879
|
-
"""Override to define a handler for
|
|
874
|
+
"""Override to define a handler for custom events.
|
|
880
875
|
|
|
881
876
|
Args:
|
|
882
877
|
name: The name of the custom event.
|
|
883
878
|
data: The data for the custom event. Format will match
|
|
884
|
-
|
|
879
|
+
the format specified by the user.
|
|
885
880
|
run_id: The ID of the run.
|
|
886
881
|
tags: The tags associated with the custom event
|
|
887
882
|
(includes inherited tags).
|
|
888
883
|
metadata: The metadata associated with the custom event
|
|
889
884
|
(includes inherited metadata).
|
|
890
885
|
|
|
891
|
-
|
|
886
|
+
!!! version-added "Added in version 0.2.15"
|
|
892
887
|
"""
|
|
893
888
|
|
|
894
889
|
|
|
@@ -898,40 +893,37 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
898
893
|
def __init__(
|
|
899
894
|
self,
|
|
900
895
|
handlers: list[BaseCallbackHandler],
|
|
901
|
-
inheritable_handlers:
|
|
902
|
-
parent_run_id:
|
|
896
|
+
inheritable_handlers: list[BaseCallbackHandler] | None = None,
|
|
897
|
+
parent_run_id: UUID | None = None,
|
|
903
898
|
*,
|
|
904
|
-
tags:
|
|
905
|
-
inheritable_tags:
|
|
906
|
-
metadata:
|
|
907
|
-
inheritable_metadata:
|
|
899
|
+
tags: list[str] | None = None,
|
|
900
|
+
inheritable_tags: list[str] | None = None,
|
|
901
|
+
metadata: dict[str, Any] | None = None,
|
|
902
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
908
903
|
) -> None:
|
|
909
904
|
"""Initialize callback manager.
|
|
910
905
|
|
|
911
906
|
Args:
|
|
912
|
-
handlers
|
|
913
|
-
inheritable_handlers
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
metadata (Optional[dict[str, Any]]): The metadata. Default is None.
|
|
920
|
-
inheritable_metadata (Optional[dict[str, Any]]): The inheritable metadata.
|
|
921
|
-
Default is None.
|
|
907
|
+
handlers: The handlers.
|
|
908
|
+
inheritable_handlers: The inheritable handlers.
|
|
909
|
+
parent_run_id: The parent run ID.
|
|
910
|
+
tags: The tags.
|
|
911
|
+
inheritable_tags: The inheritable tags.
|
|
912
|
+
metadata: The metadata.
|
|
913
|
+
inheritable_metadata: The inheritable metadata.
|
|
922
914
|
"""
|
|
923
915
|
self.handlers: list[BaseCallbackHandler] = handlers
|
|
924
916
|
self.inheritable_handlers: list[BaseCallbackHandler] = (
|
|
925
917
|
inheritable_handlers or []
|
|
926
918
|
)
|
|
927
|
-
self.parent_run_id:
|
|
919
|
+
self.parent_run_id: UUID | None = parent_run_id
|
|
928
920
|
self.tags = tags or []
|
|
929
921
|
self.inheritable_tags = inheritable_tags or []
|
|
930
922
|
self.metadata = metadata or {}
|
|
931
923
|
self.inheritable_metadata = inheritable_metadata or {}
|
|
932
924
|
|
|
933
925
|
def copy(self) -> Self:
|
|
934
|
-
"""
|
|
926
|
+
"""Return a copy of the callback manager."""
|
|
935
927
|
return self.__class__(
|
|
936
928
|
handlers=self.handlers.copy(),
|
|
937
929
|
inheritable_handlers=self.inheritable_handlers.copy(),
|
|
@@ -949,28 +941,29 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
949
941
|
within merge_configs.
|
|
950
942
|
|
|
951
943
|
Returns:
|
|
952
|
-
|
|
953
|
-
as the current object.
|
|
944
|
+
The merged callback manager of the same type as the current object.
|
|
954
945
|
|
|
955
946
|
Example: Merging two callback managers.
|
|
956
947
|
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
948
|
+
```python
|
|
949
|
+
from langchain_core.callbacks.manager import (
|
|
950
|
+
CallbackManager,
|
|
951
|
+
trace_as_chain_group,
|
|
952
|
+
)
|
|
953
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
954
|
+
|
|
955
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
956
|
+
with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
|
|
957
|
+
merged_manager = group_manager.merge(manager)
|
|
958
|
+
print(merged_manager.handlers)
|
|
959
|
+
# [
|
|
960
|
+
# <langchain_core.callbacks.stdout.StdOutCallbackHandler object at ...>,
|
|
961
|
+
# <langchain_core.callbacks.streaming_stdout.StreamingStdOutCallbackHandler object at ...>,
|
|
962
|
+
# ]
|
|
963
|
+
|
|
964
|
+
print(merged_manager.tags)
|
|
965
|
+
# ['tag2', 'tag1']
|
|
966
|
+
```
|
|
974
967
|
""" # noqa: E501
|
|
975
968
|
manager = self.__class__(
|
|
976
969
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -1007,8 +1000,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1007
1000
|
"""Add a handler to the callback manager.
|
|
1008
1001
|
|
|
1009
1002
|
Args:
|
|
1010
|
-
handler
|
|
1011
|
-
inherit
|
|
1003
|
+
handler: The handler to add.
|
|
1004
|
+
inherit: Whether to inherit the handler.
|
|
1012
1005
|
"""
|
|
1013
1006
|
if handler not in self.handlers:
|
|
1014
1007
|
self.handlers.append(handler)
|
|
@@ -1019,7 +1012,7 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1019
1012
|
"""Remove a handler from the callback manager.
|
|
1020
1013
|
|
|
1021
1014
|
Args:
|
|
1022
|
-
handler
|
|
1015
|
+
handler: The handler to remove.
|
|
1023
1016
|
"""
|
|
1024
1017
|
if handler in self.handlers:
|
|
1025
1018
|
self.handlers.remove(handler)
|
|
@@ -1034,8 +1027,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1034
1027
|
"""Set handlers as the only handlers on the callback manager.
|
|
1035
1028
|
|
|
1036
1029
|
Args:
|
|
1037
|
-
handlers
|
|
1038
|
-
inherit
|
|
1030
|
+
handlers: The handlers to set.
|
|
1031
|
+
inherit: Whether to inherit the handlers.
|
|
1039
1032
|
"""
|
|
1040
1033
|
self.handlers = []
|
|
1041
1034
|
self.inheritable_handlers = []
|
|
@@ -1050,8 +1043,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1050
1043
|
"""Set handler as the only handler on the callback manager.
|
|
1051
1044
|
|
|
1052
1045
|
Args:
|
|
1053
|
-
handler
|
|
1054
|
-
inherit
|
|
1046
|
+
handler: The handler to set.
|
|
1047
|
+
inherit: Whether to inherit the handler.
|
|
1055
1048
|
"""
|
|
1056
1049
|
self.set_handlers([handler], inherit=inherit)
|
|
1057
1050
|
|
|
@@ -1063,8 +1056,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1063
1056
|
"""Add tags to the callback manager.
|
|
1064
1057
|
|
|
1065
1058
|
Args:
|
|
1066
|
-
tags
|
|
1067
|
-
inherit
|
|
1059
|
+
tags: The tags to add.
|
|
1060
|
+
inherit: Whether to inherit the tags.
|
|
1068
1061
|
"""
|
|
1069
1062
|
for tag in tags:
|
|
1070
1063
|
if tag in self.tags:
|
|
@@ -1077,7 +1070,7 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1077
1070
|
"""Remove tags from the callback manager.
|
|
1078
1071
|
|
|
1079
1072
|
Args:
|
|
1080
|
-
tags
|
|
1073
|
+
tags: The tags to remove.
|
|
1081
1074
|
"""
|
|
1082
1075
|
for tag in tags:
|
|
1083
1076
|
if tag in self.tags:
|
|
@@ -1093,8 +1086,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1093
1086
|
"""Add metadata to the callback manager.
|
|
1094
1087
|
|
|
1095
1088
|
Args:
|
|
1096
|
-
metadata
|
|
1097
|
-
inherit
|
|
1089
|
+
metadata: The metadata to add.
|
|
1090
|
+
inherit: Whether to inherit the metadata.
|
|
1098
1091
|
"""
|
|
1099
1092
|
self.metadata.update(metadata)
|
|
1100
1093
|
if inherit:
|
|
@@ -1104,11 +1097,11 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1104
1097
|
"""Remove metadata from the callback manager.
|
|
1105
1098
|
|
|
1106
1099
|
Args:
|
|
1107
|
-
keys
|
|
1100
|
+
keys: The keys to remove.
|
|
1108
1101
|
"""
|
|
1109
1102
|
for key in keys:
|
|
1110
1103
|
self.metadata.pop(key, None)
|
|
1111
1104
|
self.inheritable_metadata.pop(key, None)
|
|
1112
1105
|
|
|
1113
1106
|
|
|
1114
|
-
Callbacks =
|
|
1107
|
+
Callbacks = list[BaseCallbackHandler] | BaseCallbackManager | None
|