langchain-core 1.0.0a5__py3-none-any.whl → 1.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +51 -64
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +20 -22
- langchain_core/caches.py +65 -66
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +321 -336
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +436 -513
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +53 -68
- langchain_core/document_loaders/base.py +27 -25
- langchain_core/document_loaders/blob_loaders.py +1 -1
- langchain_core/document_loaders/langsmith.py +44 -48
- langchain_core/documents/__init__.py +23 -3
- langchain_core/documents/base.py +98 -90
- langchain_core/documents/compressor.py +10 -10
- langchain_core/documents/transformers.py +34 -35
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +28 -32
- langchain_core/exceptions.py +21 -20
- langchain_core/globals.py +3 -151
- langchain_core/indexing/__init__.py +1 -1
- langchain_core/indexing/api.py +121 -126
- langchain_core/indexing/base.py +73 -75
- langchain_core/indexing/in_memory.py +4 -6
- langchain_core/language_models/__init__.py +14 -29
- langchain_core/language_models/_utils.py +58 -61
- langchain_core/language_models/base.py +53 -162
- langchain_core/language_models/chat_models.py +298 -387
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +42 -36
- langchain_core/language_models/llms.py +125 -235
- langchain_core/load/dump.py +9 -12
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +42 -40
- langchain_core/messages/__init__.py +10 -16
- langchain_core/messages/ai.py +148 -148
- langchain_core/messages/base.py +58 -52
- langchain_core/messages/block_translators/__init__.py +27 -17
- langchain_core/messages/block_translators/anthropic.py +6 -6
- langchain_core/messages/block_translators/bedrock_converse.py +5 -5
- langchain_core/messages/block_translators/google_genai.py +505 -20
- langchain_core/messages/block_translators/google_vertexai.py +4 -32
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +5 -5
- langchain_core/messages/block_translators/openai.py +11 -11
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +337 -328
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +24 -31
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +19 -29
- langchain_core/messages/tool.py +74 -90
- langchain_core/messages/utils.py +474 -504
- langchain_core/output_parsers/__init__.py +13 -10
- langchain_core/output_parsers/base.py +61 -61
- langchain_core/output_parsers/format_instructions.py +9 -4
- langchain_core/output_parsers/json.py +12 -10
- langchain_core/output_parsers/list.py +21 -23
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +16 -21
- langchain_core/output_parsers/pydantic.py +13 -14
- langchain_core/output_parsers/string.py +5 -5
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +35 -34
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +10 -11
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +11 -17
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -56
- langchain_core/prompts/chat.py +275 -325
- 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 +4 -6
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +26 -10
- langchain_core/prompts/structured.py +49 -53
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +61 -198
- langchain_core/runnables/base.py +1478 -1630
- langchain_core/runnables/branch.py +53 -57
- langchain_core/runnables/config.py +72 -89
- langchain_core/runnables/configurable.py +120 -137
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +91 -97
- 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 +75 -80
- langchain_core/runnables/utils.py +60 -67
- langchain_core/stores.py +85 -121
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +285 -229
- langchain_core/tools/convert.py +160 -155
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +32 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +49 -53
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +65 -64
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +45 -45
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +2 -2
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -186
- 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 +36 -40
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +9 -9
- langchain_core/utils/mustache.py +8 -10
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +6 -9
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +66 -62
- langchain_core/vectorstores/base.py +182 -216
- langchain_core/vectorstores/in_memory.py +101 -176
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- langchain_core-1.0.3.dist-info/METADATA +69 -0
- langchain_core-1.0.3.dist-info/RECORD +172 -0
- {langchain_core-1.0.0a5.dist-info → langchain_core-1.0.3.dist-info}/WHEEL +1 -1
- langchain_core/memory.py +0 -120
- langchain_core/messages/block_translators/ollama.py +0 -47
- langchain_core/prompts/pipeline.py +0 -138
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-1.0.0a5.dist-info/METADATA +0 -77
- langchain_core-1.0.0a5.dist-info/RECORD +0 -181
- langchain_core-1.0.0a5.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,12 @@ 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
|
-
|
|
425
|
-
.. versionadded:: 0.2.15
|
|
426
423
|
"""
|
|
427
424
|
|
|
428
425
|
|
|
@@ -487,26 +484,26 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
487
484
|
prompts: list[str],
|
|
488
485
|
*,
|
|
489
486
|
run_id: UUID,
|
|
490
|
-
parent_run_id:
|
|
491
|
-
tags:
|
|
492
|
-
metadata:
|
|
487
|
+
parent_run_id: UUID | None = None,
|
|
488
|
+
tags: list[str] | None = None,
|
|
489
|
+
metadata: dict[str, Any] | None = None,
|
|
493
490
|
**kwargs: Any,
|
|
494
491
|
) -> None:
|
|
495
492
|
"""Run when the model starts running.
|
|
496
493
|
|
|
497
|
-
|
|
494
|
+
!!! warning
|
|
498
495
|
This method is called for non-chat models (regular LLMs). If you're
|
|
499
496
|
implementing a handler for a chat model, you should use
|
|
500
|
-
|
|
497
|
+
`on_chat_model_start` instead.
|
|
501
498
|
|
|
502
499
|
Args:
|
|
503
|
-
serialized
|
|
504
|
-
prompts
|
|
505
|
-
run_id
|
|
506
|
-
parent_run_id
|
|
507
|
-
tags
|
|
508
|
-
metadata
|
|
509
|
-
kwargs
|
|
500
|
+
serialized: The serialized LLM.
|
|
501
|
+
prompts: The prompts.
|
|
502
|
+
run_id: The run ID. This is the ID of the current run.
|
|
503
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
504
|
+
tags: The tags.
|
|
505
|
+
metadata: The metadata.
|
|
506
|
+
**kwargs: Additional keyword arguments.
|
|
510
507
|
"""
|
|
511
508
|
|
|
512
509
|
async def on_chat_model_start(
|
|
@@ -515,25 +512,25 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
515
512
|
messages: list[list[BaseMessage]],
|
|
516
513
|
*,
|
|
517
514
|
run_id: UUID,
|
|
518
|
-
parent_run_id:
|
|
519
|
-
tags:
|
|
520
|
-
metadata:
|
|
515
|
+
parent_run_id: UUID | None = None,
|
|
516
|
+
tags: list[str] | None = None,
|
|
517
|
+
metadata: dict[str, Any] | None = None,
|
|
521
518
|
**kwargs: Any,
|
|
522
519
|
) -> Any:
|
|
523
520
|
"""Run when a chat model starts running.
|
|
524
521
|
|
|
525
|
-
|
|
522
|
+
!!! warning
|
|
526
523
|
This method is called for chat models. If you're implementing a handler for
|
|
527
|
-
a non-chat model, you should use
|
|
524
|
+
a non-chat model, you should use `on_llm_start` instead.
|
|
528
525
|
|
|
529
526
|
Args:
|
|
530
|
-
serialized
|
|
531
|
-
messages
|
|
532
|
-
run_id
|
|
533
|
-
parent_run_id
|
|
534
|
-
tags
|
|
535
|
-
metadata
|
|
536
|
-
kwargs
|
|
527
|
+
serialized: The serialized chat model.
|
|
528
|
+
messages: The messages.
|
|
529
|
+
run_id: The run ID. This is the ID of the current run.
|
|
530
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
531
|
+
tags: The tags.
|
|
532
|
+
metadata: The metadata.
|
|
533
|
+
**kwargs: Additional keyword arguments.
|
|
537
534
|
"""
|
|
538
535
|
# NotImplementedError is thrown intentionally
|
|
539
536
|
# Callback handler will fall back to on_llm_start if this is exception is thrown
|
|
@@ -544,10 +541,10 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
544
541
|
self,
|
|
545
542
|
token: str,
|
|
546
543
|
*,
|
|
547
|
-
chunk:
|
|
544
|
+
chunk: GenerationChunk | ChatGenerationChunk | None = None,
|
|
548
545
|
run_id: UUID,
|
|
549
|
-
parent_run_id:
|
|
550
|
-
tags:
|
|
546
|
+
parent_run_id: UUID | None = None,
|
|
547
|
+
tags: list[str] | None = None,
|
|
551
548
|
**kwargs: Any,
|
|
552
549
|
) -> None:
|
|
553
550
|
"""Run on new output token. Only available when streaming is enabled.
|
|
@@ -555,13 +552,12 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
555
552
|
For both chat models and non-chat models (legacy LLMs).
|
|
556
553
|
|
|
557
554
|
Args:
|
|
558
|
-
token
|
|
559
|
-
chunk
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
kwargs (Any): Additional keyword arguments.
|
|
555
|
+
token: The new token.
|
|
556
|
+
chunk: The new generated chunk, containing content and other information.
|
|
557
|
+
run_id: The run ID. This is the ID of the current run.
|
|
558
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
559
|
+
tags: The tags.
|
|
560
|
+
**kwargs: Additional keyword arguments.
|
|
565
561
|
"""
|
|
566
562
|
|
|
567
563
|
async def on_llm_end(
|
|
@@ -569,18 +565,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
569
565
|
response: LLMResult,
|
|
570
566
|
*,
|
|
571
567
|
run_id: UUID,
|
|
572
|
-
parent_run_id:
|
|
573
|
-
tags:
|
|
568
|
+
parent_run_id: UUID | None = None,
|
|
569
|
+
tags: list[str] | None = None,
|
|
574
570
|
**kwargs: Any,
|
|
575
571
|
) -> None:
|
|
576
572
|
"""Run when the model ends running.
|
|
577
573
|
|
|
578
574
|
Args:
|
|
579
|
-
response
|
|
580
|
-
run_id
|
|
581
|
-
parent_run_id
|
|
582
|
-
tags
|
|
583
|
-
kwargs
|
|
575
|
+
response: The response which was generated.
|
|
576
|
+
run_id: The run ID. This is the ID of the current run.
|
|
577
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
578
|
+
tags: The tags.
|
|
579
|
+
**kwargs: Additional keyword arguments.
|
|
584
580
|
"""
|
|
585
581
|
|
|
586
582
|
async def on_llm_error(
|
|
@@ -588,8 +584,8 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
588
584
|
error: BaseException,
|
|
589
585
|
*,
|
|
590
586
|
run_id: UUID,
|
|
591
|
-
parent_run_id:
|
|
592
|
-
tags:
|
|
587
|
+
parent_run_id: UUID | None = None,
|
|
588
|
+
tags: list[str] | None = None,
|
|
593
589
|
**kwargs: Any,
|
|
594
590
|
) -> None:
|
|
595
591
|
"""Run when LLM errors.
|
|
@@ -599,7 +595,7 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
599
595
|
run_id: The run ID. This is the ID of the current run.
|
|
600
596
|
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
601
597
|
tags: The tags.
|
|
602
|
-
kwargs
|
|
598
|
+
**kwargs: Additional keyword arguments.
|
|
603
599
|
- response (LLMResult): The response which was generated before
|
|
604
600
|
the error occurred.
|
|
605
601
|
"""
|
|
@@ -610,21 +606,21 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
610
606
|
inputs: dict[str, Any],
|
|
611
607
|
*,
|
|
612
608
|
run_id: UUID,
|
|
613
|
-
parent_run_id:
|
|
614
|
-
tags:
|
|
615
|
-
metadata:
|
|
609
|
+
parent_run_id: UUID | None = None,
|
|
610
|
+
tags: list[str] | None = None,
|
|
611
|
+
metadata: dict[str, Any] | None = None,
|
|
616
612
|
**kwargs: Any,
|
|
617
613
|
) -> None:
|
|
618
614
|
"""Run when a chain starts running.
|
|
619
615
|
|
|
620
616
|
Args:
|
|
621
|
-
serialized
|
|
622
|
-
inputs
|
|
623
|
-
run_id
|
|
624
|
-
parent_run_id
|
|
625
|
-
tags
|
|
626
|
-
metadata
|
|
627
|
-
kwargs
|
|
617
|
+
serialized: The serialized chain.
|
|
618
|
+
inputs: The inputs.
|
|
619
|
+
run_id: The run ID. This is the ID of the current run.
|
|
620
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
621
|
+
tags: The tags.
|
|
622
|
+
metadata: The metadata.
|
|
623
|
+
**kwargs: Additional keyword arguments.
|
|
628
624
|
"""
|
|
629
625
|
|
|
630
626
|
async def on_chain_end(
|
|
@@ -632,18 +628,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
632
628
|
outputs: dict[str, Any],
|
|
633
629
|
*,
|
|
634
630
|
run_id: UUID,
|
|
635
|
-
parent_run_id:
|
|
636
|
-
tags:
|
|
631
|
+
parent_run_id: UUID | None = None,
|
|
632
|
+
tags: list[str] | None = None,
|
|
637
633
|
**kwargs: Any,
|
|
638
634
|
) -> None:
|
|
639
635
|
"""Run when a chain ends running.
|
|
640
636
|
|
|
641
637
|
Args:
|
|
642
|
-
outputs
|
|
643
|
-
run_id
|
|
644
|
-
parent_run_id
|
|
645
|
-
tags
|
|
646
|
-
kwargs
|
|
638
|
+
outputs: The outputs of the chain.
|
|
639
|
+
run_id: The run ID. This is the ID of the current run.
|
|
640
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
641
|
+
tags: The tags.
|
|
642
|
+
**kwargs: Additional keyword arguments.
|
|
647
643
|
"""
|
|
648
644
|
|
|
649
645
|
async def on_chain_error(
|
|
@@ -651,18 +647,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
651
647
|
error: BaseException,
|
|
652
648
|
*,
|
|
653
649
|
run_id: UUID,
|
|
654
|
-
parent_run_id:
|
|
655
|
-
tags:
|
|
650
|
+
parent_run_id: UUID | None = None,
|
|
651
|
+
tags: list[str] | None = None,
|
|
656
652
|
**kwargs: Any,
|
|
657
653
|
) -> None:
|
|
658
654
|
"""Run when chain errors.
|
|
659
655
|
|
|
660
656
|
Args:
|
|
661
|
-
error
|
|
662
|
-
run_id
|
|
663
|
-
parent_run_id
|
|
664
|
-
tags
|
|
665
|
-
kwargs
|
|
657
|
+
error: The error that occurred.
|
|
658
|
+
run_id: The run ID. This is the ID of the current run.
|
|
659
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
660
|
+
tags: The tags.
|
|
661
|
+
**kwargs: Additional keyword arguments.
|
|
666
662
|
"""
|
|
667
663
|
|
|
668
664
|
async def on_tool_start(
|
|
@@ -671,23 +667,23 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
671
667
|
input_str: str,
|
|
672
668
|
*,
|
|
673
669
|
run_id: UUID,
|
|
674
|
-
parent_run_id:
|
|
675
|
-
tags:
|
|
676
|
-
metadata:
|
|
677
|
-
inputs:
|
|
670
|
+
parent_run_id: UUID | None = None,
|
|
671
|
+
tags: list[str] | None = None,
|
|
672
|
+
metadata: dict[str, Any] | None = None,
|
|
673
|
+
inputs: dict[str, Any] | None = None,
|
|
678
674
|
**kwargs: Any,
|
|
679
675
|
) -> None:
|
|
680
676
|
"""Run when the tool starts running.
|
|
681
677
|
|
|
682
678
|
Args:
|
|
683
|
-
serialized
|
|
684
|
-
input_str
|
|
685
|
-
run_id
|
|
686
|
-
parent_run_id
|
|
687
|
-
tags
|
|
688
|
-
metadata
|
|
689
|
-
inputs
|
|
690
|
-
kwargs
|
|
679
|
+
serialized: The serialized tool.
|
|
680
|
+
input_str: The input string.
|
|
681
|
+
run_id: The run ID. This is the ID of the current run.
|
|
682
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
683
|
+
tags: The tags.
|
|
684
|
+
metadata: The metadata.
|
|
685
|
+
inputs: The inputs.
|
|
686
|
+
**kwargs: Additional keyword arguments.
|
|
691
687
|
"""
|
|
692
688
|
|
|
693
689
|
async def on_tool_end(
|
|
@@ -695,18 +691,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
695
691
|
output: Any,
|
|
696
692
|
*,
|
|
697
693
|
run_id: UUID,
|
|
698
|
-
parent_run_id:
|
|
699
|
-
tags:
|
|
694
|
+
parent_run_id: UUID | None = None,
|
|
695
|
+
tags: list[str] | None = None,
|
|
700
696
|
**kwargs: Any,
|
|
701
697
|
) -> None:
|
|
702
698
|
"""Run when the tool ends running.
|
|
703
699
|
|
|
704
700
|
Args:
|
|
705
|
-
output
|
|
706
|
-
run_id
|
|
707
|
-
parent_run_id
|
|
708
|
-
tags
|
|
709
|
-
kwargs
|
|
701
|
+
output: The output of the tool.
|
|
702
|
+
run_id: The run ID. This is the ID of the current run.
|
|
703
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
704
|
+
tags: The tags.
|
|
705
|
+
**kwargs: Additional keyword arguments.
|
|
710
706
|
"""
|
|
711
707
|
|
|
712
708
|
async def on_tool_error(
|
|
@@ -714,18 +710,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
714
710
|
error: BaseException,
|
|
715
711
|
*,
|
|
716
712
|
run_id: UUID,
|
|
717
|
-
parent_run_id:
|
|
718
|
-
tags:
|
|
713
|
+
parent_run_id: UUID | None = None,
|
|
714
|
+
tags: list[str] | None = None,
|
|
719
715
|
**kwargs: Any,
|
|
720
716
|
) -> None:
|
|
721
717
|
"""Run when tool errors.
|
|
722
718
|
|
|
723
719
|
Args:
|
|
724
|
-
error
|
|
725
|
-
run_id
|
|
726
|
-
parent_run_id
|
|
727
|
-
tags
|
|
728
|
-
kwargs
|
|
720
|
+
error: The error that occurred.
|
|
721
|
+
run_id: The run ID. This is the ID of the current run.
|
|
722
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
723
|
+
tags: The tags.
|
|
724
|
+
**kwargs: Additional keyword arguments.
|
|
729
725
|
"""
|
|
730
726
|
|
|
731
727
|
async def on_text(
|
|
@@ -733,18 +729,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
733
729
|
text: str,
|
|
734
730
|
*,
|
|
735
731
|
run_id: UUID,
|
|
736
|
-
parent_run_id:
|
|
737
|
-
tags:
|
|
732
|
+
parent_run_id: UUID | None = None,
|
|
733
|
+
tags: list[str] | None = None,
|
|
738
734
|
**kwargs: Any,
|
|
739
735
|
) -> None:
|
|
740
736
|
"""Run on an arbitrary text.
|
|
741
737
|
|
|
742
738
|
Args:
|
|
743
|
-
text
|
|
744
|
-
run_id
|
|
745
|
-
parent_run_id
|
|
746
|
-
tags
|
|
747
|
-
kwargs
|
|
739
|
+
text: The text.
|
|
740
|
+
run_id: The run ID. This is the ID of the current run.
|
|
741
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
742
|
+
tags: The tags.
|
|
743
|
+
**kwargs: Additional keyword arguments.
|
|
748
744
|
"""
|
|
749
745
|
|
|
750
746
|
async def on_retry(
|
|
@@ -752,16 +748,16 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
752
748
|
retry_state: RetryCallState,
|
|
753
749
|
*,
|
|
754
750
|
run_id: UUID,
|
|
755
|
-
parent_run_id:
|
|
751
|
+
parent_run_id: UUID | None = None,
|
|
756
752
|
**kwargs: Any,
|
|
757
753
|
) -> Any:
|
|
758
754
|
"""Run on a retry event.
|
|
759
755
|
|
|
760
756
|
Args:
|
|
761
|
-
retry_state
|
|
762
|
-
run_id
|
|
763
|
-
parent_run_id
|
|
764
|
-
kwargs
|
|
757
|
+
retry_state: The retry state.
|
|
758
|
+
run_id: The run ID. This is the ID of the current run.
|
|
759
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
760
|
+
**kwargs: Additional keyword arguments.
|
|
765
761
|
"""
|
|
766
762
|
|
|
767
763
|
async def on_agent_action(
|
|
@@ -769,18 +765,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
769
765
|
action: AgentAction,
|
|
770
766
|
*,
|
|
771
767
|
run_id: UUID,
|
|
772
|
-
parent_run_id:
|
|
773
|
-
tags:
|
|
768
|
+
parent_run_id: UUID | None = None,
|
|
769
|
+
tags: list[str] | None = None,
|
|
774
770
|
**kwargs: Any,
|
|
775
771
|
) -> None:
|
|
776
772
|
"""Run on agent action.
|
|
777
773
|
|
|
778
774
|
Args:
|
|
779
|
-
action
|
|
780
|
-
run_id
|
|
781
|
-
parent_run_id
|
|
782
|
-
tags
|
|
783
|
-
kwargs
|
|
775
|
+
action: The agent action.
|
|
776
|
+
run_id: The run ID. This is the ID of the current run.
|
|
777
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
778
|
+
tags: The tags.
|
|
779
|
+
**kwargs: Additional keyword arguments.
|
|
784
780
|
"""
|
|
785
781
|
|
|
786
782
|
async def on_agent_finish(
|
|
@@ -788,18 +784,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
788
784
|
finish: AgentFinish,
|
|
789
785
|
*,
|
|
790
786
|
run_id: UUID,
|
|
791
|
-
parent_run_id:
|
|
792
|
-
tags:
|
|
787
|
+
parent_run_id: UUID | None = None,
|
|
788
|
+
tags: list[str] | None = None,
|
|
793
789
|
**kwargs: Any,
|
|
794
790
|
) -> None:
|
|
795
791
|
"""Run on the agent end.
|
|
796
792
|
|
|
797
793
|
Args:
|
|
798
|
-
finish
|
|
799
|
-
run_id
|
|
800
|
-
parent_run_id
|
|
801
|
-
tags
|
|
802
|
-
kwargs
|
|
794
|
+
finish: The agent finish.
|
|
795
|
+
run_id: The run ID. This is the ID of the current run.
|
|
796
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
797
|
+
tags: The tags.
|
|
798
|
+
**kwargs: Additional keyword arguments.
|
|
803
799
|
"""
|
|
804
800
|
|
|
805
801
|
async def on_retriever_start(
|
|
@@ -808,21 +804,21 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
808
804
|
query: str,
|
|
809
805
|
*,
|
|
810
806
|
run_id: UUID,
|
|
811
|
-
parent_run_id:
|
|
812
|
-
tags:
|
|
813
|
-
metadata:
|
|
807
|
+
parent_run_id: UUID | None = None,
|
|
808
|
+
tags: list[str] | None = None,
|
|
809
|
+
metadata: dict[str, Any] | None = None,
|
|
814
810
|
**kwargs: Any,
|
|
815
811
|
) -> None:
|
|
816
812
|
"""Run on the retriever start.
|
|
817
813
|
|
|
818
814
|
Args:
|
|
819
|
-
serialized
|
|
820
|
-
query
|
|
821
|
-
run_id
|
|
822
|
-
parent_run_id
|
|
823
|
-
tags
|
|
824
|
-
metadata
|
|
825
|
-
kwargs
|
|
815
|
+
serialized: The serialized retriever.
|
|
816
|
+
query: The query.
|
|
817
|
+
run_id: The run ID. This is the ID of the current run.
|
|
818
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
819
|
+
tags: The tags.
|
|
820
|
+
metadata: The metadata.
|
|
821
|
+
**kwargs: Additional keyword arguments.
|
|
826
822
|
"""
|
|
827
823
|
|
|
828
824
|
async def on_retriever_end(
|
|
@@ -830,18 +826,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
830
826
|
documents: Sequence[Document],
|
|
831
827
|
*,
|
|
832
828
|
run_id: UUID,
|
|
833
|
-
parent_run_id:
|
|
834
|
-
tags:
|
|
829
|
+
parent_run_id: UUID | None = None,
|
|
830
|
+
tags: list[str] | None = None,
|
|
835
831
|
**kwargs: Any,
|
|
836
832
|
) -> None:
|
|
837
833
|
"""Run on the retriever end.
|
|
838
834
|
|
|
839
835
|
Args:
|
|
840
|
-
documents
|
|
841
|
-
run_id
|
|
842
|
-
parent_run_id
|
|
843
|
-
tags
|
|
844
|
-
kwargs
|
|
836
|
+
documents: The documents retrieved.
|
|
837
|
+
run_id: The run ID. This is the ID of the current run.
|
|
838
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
839
|
+
tags: The tags.
|
|
840
|
+
**kwargs: Additional keyword arguments.
|
|
845
841
|
"""
|
|
846
842
|
|
|
847
843
|
async def on_retriever_error(
|
|
@@ -849,18 +845,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
849
845
|
error: BaseException,
|
|
850
846
|
*,
|
|
851
847
|
run_id: UUID,
|
|
852
|
-
parent_run_id:
|
|
853
|
-
tags:
|
|
848
|
+
parent_run_id: UUID | None = None,
|
|
849
|
+
tags: list[str] | None = None,
|
|
854
850
|
**kwargs: Any,
|
|
855
851
|
) -> None:
|
|
856
852
|
"""Run on retriever error.
|
|
857
853
|
|
|
858
854
|
Args:
|
|
859
|
-
error
|
|
860
|
-
run_id
|
|
861
|
-
parent_run_id
|
|
862
|
-
tags
|
|
863
|
-
kwargs
|
|
855
|
+
error: The error that occurred.
|
|
856
|
+
run_id: The run ID. This is the ID of the current run.
|
|
857
|
+
parent_run_id: The parent run ID. This is the ID of the parent run.
|
|
858
|
+
tags: The tags.
|
|
859
|
+
**kwargs: Additional keyword arguments.
|
|
864
860
|
"""
|
|
865
861
|
|
|
866
862
|
async def on_custom_event(
|
|
@@ -869,8 +865,8 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
869
865
|
data: Any,
|
|
870
866
|
*,
|
|
871
867
|
run_id: UUID,
|
|
872
|
-
tags:
|
|
873
|
-
metadata:
|
|
868
|
+
tags: list[str] | None = None,
|
|
869
|
+
metadata: dict[str, Any] | None = None,
|
|
874
870
|
**kwargs: Any,
|
|
875
871
|
) -> None:
|
|
876
872
|
"""Override to define a handler for custom events.
|
|
@@ -878,14 +874,12 @@ class AsyncCallbackHandler(BaseCallbackHandler):
|
|
|
878
874
|
Args:
|
|
879
875
|
name: The name of the custom event.
|
|
880
876
|
data: The data for the custom event. Format will match
|
|
881
|
-
|
|
877
|
+
the format specified by the user.
|
|
882
878
|
run_id: The ID of the run.
|
|
883
879
|
tags: The tags associated with the custom event
|
|
884
880
|
(includes inherited tags).
|
|
885
881
|
metadata: The metadata associated with the custom event
|
|
886
882
|
(includes inherited metadata).
|
|
887
|
-
|
|
888
|
-
.. versionadded:: 0.2.15
|
|
889
883
|
"""
|
|
890
884
|
|
|
891
885
|
|
|
@@ -895,33 +889,30 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
895
889
|
def __init__(
|
|
896
890
|
self,
|
|
897
891
|
handlers: list[BaseCallbackHandler],
|
|
898
|
-
inheritable_handlers:
|
|
899
|
-
parent_run_id:
|
|
892
|
+
inheritable_handlers: list[BaseCallbackHandler] | None = None,
|
|
893
|
+
parent_run_id: UUID | None = None,
|
|
900
894
|
*,
|
|
901
|
-
tags:
|
|
902
|
-
inheritable_tags:
|
|
903
|
-
metadata:
|
|
904
|
-
inheritable_metadata:
|
|
895
|
+
tags: list[str] | None = None,
|
|
896
|
+
inheritable_tags: list[str] | None = None,
|
|
897
|
+
metadata: dict[str, Any] | None = None,
|
|
898
|
+
inheritable_metadata: dict[str, Any] | None = None,
|
|
905
899
|
) -> None:
|
|
906
900
|
"""Initialize callback manager.
|
|
907
901
|
|
|
908
902
|
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.
|
|
903
|
+
handlers: The handlers.
|
|
904
|
+
inheritable_handlers: The inheritable handlers.
|
|
905
|
+
parent_run_id: The parent run ID.
|
|
906
|
+
tags: The tags.
|
|
907
|
+
inheritable_tags: The inheritable tags.
|
|
908
|
+
metadata: The metadata.
|
|
909
|
+
inheritable_metadata: The inheritable metadata.
|
|
919
910
|
"""
|
|
920
911
|
self.handlers: list[BaseCallbackHandler] = handlers
|
|
921
912
|
self.inheritable_handlers: list[BaseCallbackHandler] = (
|
|
922
913
|
inheritable_handlers or []
|
|
923
914
|
)
|
|
924
|
-
self.parent_run_id:
|
|
915
|
+
self.parent_run_id: UUID | None = parent_run_id
|
|
925
916
|
self.tags = tags or []
|
|
926
917
|
self.inheritable_tags = inheritable_tags or []
|
|
927
918
|
self.metadata = metadata or {}
|
|
@@ -946,35 +937,29 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
946
937
|
within merge_configs.
|
|
947
938
|
|
|
948
939
|
Returns:
|
|
949
|
-
|
|
950
|
-
as the current object.
|
|
940
|
+
The merged callback manager of the same type as the current object.
|
|
951
941
|
|
|
952
942
|
Example: Merging two callback managers.
|
|
953
943
|
|
|
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
|
-
|
|
944
|
+
```python
|
|
945
|
+
from langchain_core.callbacks.manager import (
|
|
946
|
+
CallbackManager,
|
|
947
|
+
trace_as_chain_group,
|
|
948
|
+
)
|
|
949
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
950
|
+
|
|
951
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
952
|
+
with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
|
|
953
|
+
merged_manager = group_manager.merge(manager)
|
|
954
|
+
print(merged_manager.handlers)
|
|
955
|
+
# [
|
|
956
|
+
# <langchain_core.callbacks.stdout.StdOutCallbackHandler object at ...>,
|
|
957
|
+
# <langchain_core.callbacks.streaming_stdout.StreamingStdOutCallbackHandler object at ...>,
|
|
958
|
+
# ]
|
|
959
|
+
|
|
960
|
+
print(merged_manager.tags)
|
|
961
|
+
# ['tag2', 'tag1']
|
|
962
|
+
```
|
|
978
963
|
""" # noqa: E501
|
|
979
964
|
manager = self.__class__(
|
|
980
965
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -1011,8 +996,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1011
996
|
"""Add a handler to the callback manager.
|
|
1012
997
|
|
|
1013
998
|
Args:
|
|
1014
|
-
handler
|
|
1015
|
-
inherit
|
|
999
|
+
handler: The handler to add.
|
|
1000
|
+
inherit: Whether to inherit the handler.
|
|
1016
1001
|
"""
|
|
1017
1002
|
if handler not in self.handlers:
|
|
1018
1003
|
self.handlers.append(handler)
|
|
@@ -1023,7 +1008,7 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1023
1008
|
"""Remove a handler from the callback manager.
|
|
1024
1009
|
|
|
1025
1010
|
Args:
|
|
1026
|
-
handler
|
|
1011
|
+
handler: The handler to remove.
|
|
1027
1012
|
"""
|
|
1028
1013
|
if handler in self.handlers:
|
|
1029
1014
|
self.handlers.remove(handler)
|
|
@@ -1038,8 +1023,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1038
1023
|
"""Set handlers as the only handlers on the callback manager.
|
|
1039
1024
|
|
|
1040
1025
|
Args:
|
|
1041
|
-
handlers
|
|
1042
|
-
inherit
|
|
1026
|
+
handlers: The handlers to set.
|
|
1027
|
+
inherit: Whether to inherit the handlers.
|
|
1043
1028
|
"""
|
|
1044
1029
|
self.handlers = []
|
|
1045
1030
|
self.inheritable_handlers = []
|
|
@@ -1054,8 +1039,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1054
1039
|
"""Set handler as the only handler on the callback manager.
|
|
1055
1040
|
|
|
1056
1041
|
Args:
|
|
1057
|
-
handler
|
|
1058
|
-
inherit
|
|
1042
|
+
handler: The handler to set.
|
|
1043
|
+
inherit: Whether to inherit the handler.
|
|
1059
1044
|
"""
|
|
1060
1045
|
self.set_handlers([handler], inherit=inherit)
|
|
1061
1046
|
|
|
@@ -1067,8 +1052,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1067
1052
|
"""Add tags to the callback manager.
|
|
1068
1053
|
|
|
1069
1054
|
Args:
|
|
1070
|
-
tags
|
|
1071
|
-
inherit
|
|
1055
|
+
tags: The tags to add.
|
|
1056
|
+
inherit: Whether to inherit the tags.
|
|
1072
1057
|
"""
|
|
1073
1058
|
for tag in tags:
|
|
1074
1059
|
if tag in self.tags:
|
|
@@ -1081,7 +1066,7 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1081
1066
|
"""Remove tags from the callback manager.
|
|
1082
1067
|
|
|
1083
1068
|
Args:
|
|
1084
|
-
tags
|
|
1069
|
+
tags: The tags to remove.
|
|
1085
1070
|
"""
|
|
1086
1071
|
for tag in tags:
|
|
1087
1072
|
if tag in self.tags:
|
|
@@ -1097,8 +1082,8 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1097
1082
|
"""Add metadata to the callback manager.
|
|
1098
1083
|
|
|
1099
1084
|
Args:
|
|
1100
|
-
metadata
|
|
1101
|
-
inherit
|
|
1085
|
+
metadata: The metadata to add.
|
|
1086
|
+
inherit: Whether to inherit the metadata.
|
|
1102
1087
|
"""
|
|
1103
1088
|
self.metadata.update(metadata)
|
|
1104
1089
|
if inherit:
|
|
@@ -1108,11 +1093,11 @@ class BaseCallbackManager(CallbackManagerMixin):
|
|
|
1108
1093
|
"""Remove metadata from the callback manager.
|
|
1109
1094
|
|
|
1110
1095
|
Args:
|
|
1111
|
-
keys
|
|
1096
|
+
keys: The keys to remove.
|
|
1112
1097
|
"""
|
|
1113
1098
|
for key in keys:
|
|
1114
1099
|
self.metadata.pop(key, None)
|
|
1115
1100
|
self.inheritable_metadata.pop(key, None)
|
|
1116
1101
|
|
|
1117
1102
|
|
|
1118
|
-
Callbacks =
|
|
1103
|
+
Callbacks = list[BaseCallbackHandler] | BaseCallbackManager | None
|