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