langchain-core 1.0.0a8__py3-none-any.whl → 1.0.0rc1__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 +0 -1
- langchain_core/_api/beta_decorator.py +17 -20
- langchain_core/_api/deprecation.py +30 -35
- langchain_core/_import_utils.py +1 -1
- langchain_core/agents.py +7 -6
- langchain_core/caches.py +4 -10
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +232 -243
- langchain_core/callbacks/file.py +33 -33
- langchain_core/callbacks/manager.py +353 -416
- langchain_core/callbacks/stdout.py +21 -22
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +54 -51
- langchain_core/chat_history.py +42 -57
- langchain_core/document_loaders/langsmith.py +21 -21
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +37 -40
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +46 -52
- langchain_core/exceptions.py +5 -5
- langchain_core/indexing/api.py +11 -11
- langchain_core/indexing/base.py +24 -24
- langchain_core/language_models/__init__.py +0 -2
- langchain_core/language_models/_utils.py +51 -53
- langchain_core/language_models/base.py +23 -24
- langchain_core/language_models/chat_models.py +121 -144
- langchain_core/language_models/fake_chat_models.py +5 -5
- langchain_core/language_models/llms.py +10 -12
- langchain_core/load/dump.py +1 -1
- langchain_core/load/load.py +16 -16
- langchain_core/load/serializable.py +35 -34
- langchain_core/messages/__init__.py +1 -16
- langchain_core/messages/ai.py +105 -104
- langchain_core/messages/base.py +26 -26
- langchain_core/messages/block_translators/__init__.py +17 -17
- langchain_core/messages/block_translators/anthropic.py +2 -2
- langchain_core/messages/block_translators/bedrock_converse.py +2 -2
- langchain_core/messages/block_translators/google_genai.py +2 -2
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +2 -2
- langchain_core/messages/block_translators/openai.py +4 -4
- langchain_core/messages/chat.py +1 -1
- langchain_core/messages/content.py +189 -193
- langchain_core/messages/function.py +5 -5
- langchain_core/messages/human.py +15 -17
- langchain_core/messages/modifier.py +1 -1
- langchain_core/messages/system.py +12 -14
- langchain_core/messages/tool.py +45 -49
- langchain_core/messages/utils.py +384 -396
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +22 -23
- langchain_core/output_parsers/json.py +3 -3
- langchain_core/output_parsers/list.py +1 -1
- langchain_core/output_parsers/openai_functions.py +46 -44
- langchain_core/output_parsers/openai_tools.py +7 -7
- langchain_core/output_parsers/pydantic.py +10 -11
- langchain_core/output_parsers/string.py +1 -1
- langchain_core/output_parsers/transform.py +2 -2
- langchain_core/output_parsers/xml.py +1 -1
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +14 -14
- langchain_core/outputs/generation.py +5 -5
- langchain_core/outputs/llm_result.py +5 -5
- langchain_core/prompt_values.py +5 -5
- langchain_core/prompts/__init__.py +3 -23
- langchain_core/prompts/base.py +32 -37
- langchain_core/prompts/chat.py +216 -222
- langchain_core/prompts/dict.py +2 -2
- langchain_core/prompts/few_shot.py +76 -83
- langchain_core/prompts/few_shot_with_templates.py +6 -8
- langchain_core/prompts/image.py +11 -13
- langchain_core/prompts/loading.py +1 -1
- langchain_core/prompts/message.py +2 -2
- langchain_core/prompts/prompt.py +14 -16
- langchain_core/prompts/string.py +19 -7
- langchain_core/prompts/structured.py +24 -25
- langchain_core/rate_limiters.py +36 -38
- langchain_core/retrievers.py +41 -182
- langchain_core/runnables/base.py +565 -590
- langchain_core/runnables/branch.py +7 -7
- langchain_core/runnables/config.py +37 -44
- langchain_core/runnables/configurable.py +8 -9
- langchain_core/runnables/fallbacks.py +8 -8
- langchain_core/runnables/graph.py +28 -27
- langchain_core/runnables/graph_ascii.py +19 -18
- langchain_core/runnables/graph_mermaid.py +20 -31
- langchain_core/runnables/graph_png.py +7 -7
- langchain_core/runnables/history.py +20 -20
- langchain_core/runnables/passthrough.py +8 -8
- langchain_core/runnables/retry.py +3 -3
- langchain_core/runnables/router.py +1 -1
- langchain_core/runnables/schema.py +33 -33
- langchain_core/runnables/utils.py +30 -34
- langchain_core/stores.py +72 -102
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +63 -63
- langchain_core/tools/convert.py +92 -92
- langchain_core/tools/render.py +9 -9
- langchain_core/tools/retriever.py +1 -1
- langchain_core/tools/simple.py +6 -7
- langchain_core/tools/structured.py +17 -18
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +35 -35
- langchain_core/tracers/context.py +12 -17
- langchain_core/tracers/event_stream.py +3 -3
- langchain_core/tracers/langchain.py +8 -8
- langchain_core/tracers/log_stream.py +17 -18
- langchain_core/tracers/memory_stream.py +2 -2
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/utils/aiter.py +31 -31
- langchain_core/utils/env.py +5 -5
- langchain_core/utils/function_calling.py +48 -120
- langchain_core/utils/html.py +4 -4
- langchain_core/utils/input.py +2 -2
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +19 -19
- langchain_core/utils/json.py +1 -1
- langchain_core/utils/json_schema.py +2 -2
- langchain_core/utils/mustache.py +5 -5
- langchain_core/utils/pydantic.py +17 -17
- langchain_core/utils/strings.py +4 -4
- langchain_core/utils/utils.py +25 -28
- langchain_core/vectorstores/base.py +43 -64
- langchain_core/vectorstores/in_memory.py +83 -85
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc1.dist-info}/METADATA +23 -11
- langchain_core-1.0.0rc1.dist-info/RECORD +172 -0
- langchain_core/memory.py +0 -120
- 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-1.0.0a8.dist-info/RECORD +0 -176
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc1.dist-info}/WHEEL +0 -0
|
@@ -78,39 +78,30 @@ def trace_as_chain_group(
|
|
|
78
78
|
they aren't composed in a single chain.
|
|
79
79
|
|
|
80
80
|
Args:
|
|
81
|
-
group_name
|
|
82
|
-
callback_manager
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
Defaults to None.
|
|
90
|
-
run_id (UUID, optional): The ID of the run.
|
|
91
|
-
tags (list[str], optional): The inheritable tags to apply to all runs.
|
|
92
|
-
Defaults to None.
|
|
93
|
-
metadata (dict[str, Any], optional): The metadata to apply to all runs.
|
|
94
|
-
Defaults to None.
|
|
81
|
+
group_name: The name of the chain group.
|
|
82
|
+
callback_manager: The callback manager to use.
|
|
83
|
+
inputs: The inputs to the chain group.
|
|
84
|
+
project_name: The name of the project.
|
|
85
|
+
example_id: The ID of the example.
|
|
86
|
+
run_id: The ID of the run.
|
|
87
|
+
tags: The inheritable tags to apply to all runs.
|
|
88
|
+
metadata: The metadata to apply to all runs.
|
|
95
89
|
|
|
96
90
|
!!! note
|
|
97
|
-
Must have
|
|
91
|
+
Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
|
|
98
92
|
LangSmith.
|
|
99
93
|
|
|
100
94
|
Yields:
|
|
101
95
|
The callback manager for the chain group.
|
|
102
96
|
|
|
103
97
|
Example:
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
res = llm.invoke(llm_input, {"callbacks": manager})
|
|
112
|
-
manager.on_chain_end({"output": res})
|
|
113
|
-
|
|
98
|
+
```python
|
|
99
|
+
llm_input = "Foo"
|
|
100
|
+
with trace_as_chain_group("group_name", inputs={"input": llm_input}) as manager:
|
|
101
|
+
# Use the callback manager for the chain group
|
|
102
|
+
res = llm.invoke(llm_input, {"callbacks": manager})
|
|
103
|
+
manager.on_chain_end({"output": res})
|
|
104
|
+
```
|
|
114
105
|
"""
|
|
115
106
|
cb = _get_trace_callbacks(
|
|
116
107
|
project_name, example_id, callback_manager=callback_manager
|
|
@@ -162,39 +153,33 @@ async def atrace_as_chain_group(
|
|
|
162
153
|
they aren't composed in a single chain.
|
|
163
154
|
|
|
164
155
|
Args:
|
|
165
|
-
group_name
|
|
166
|
-
callback_manager
|
|
167
|
-
|
|
168
|
-
inputs
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
run_id (UUID, optional): The ID of the run.
|
|
175
|
-
tags (list[str], optional): The inheritable tags to apply to all runs.
|
|
176
|
-
Defaults to None.
|
|
177
|
-
metadata (dict[str, Any], optional): The metadata to apply to all runs.
|
|
178
|
-
Defaults to None.
|
|
156
|
+
group_name: The name of the chain group.
|
|
157
|
+
callback_manager: The async callback manager to use,
|
|
158
|
+
which manages tracing and other callback behavior.
|
|
159
|
+
inputs: The inputs to the chain group.
|
|
160
|
+
project_name: The name of the project.
|
|
161
|
+
example_id: The ID of the example.
|
|
162
|
+
run_id: The ID of the run.
|
|
163
|
+
tags: The inheritable tags to apply to all runs.
|
|
164
|
+
metadata: The metadata to apply to all runs.
|
|
179
165
|
|
|
180
166
|
Yields:
|
|
181
167
|
The async callback manager for the chain group.
|
|
182
168
|
|
|
183
169
|
!!! note
|
|
184
|
-
Must have
|
|
170
|
+
Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
|
|
185
171
|
LangSmith.
|
|
186
172
|
|
|
187
173
|
Example:
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
174
|
+
```python
|
|
175
|
+
llm_input = "Foo"
|
|
176
|
+
async with atrace_as_chain_group(
|
|
177
|
+
"group_name", inputs={"input": llm_input}
|
|
178
|
+
) as manager:
|
|
179
|
+
# Use the async callback manager for the chain group
|
|
180
|
+
res = await llm.ainvoke(llm_input, {"callbacks": manager})
|
|
181
|
+
await manager.on_chain_end({"output": res})
|
|
182
|
+
```
|
|
198
183
|
"""
|
|
199
184
|
cb = _get_trace_callbacks(
|
|
200
185
|
project_name, example_id, callback_manager=callback_manager
|
|
@@ -235,10 +220,10 @@ def shielded(func: Func) -> Func:
|
|
|
235
220
|
"""Makes so an awaitable method is always shielded from cancellation.
|
|
236
221
|
|
|
237
222
|
Args:
|
|
238
|
-
func
|
|
223
|
+
func: The function to shield.
|
|
239
224
|
|
|
240
225
|
Returns:
|
|
241
|
-
|
|
226
|
+
The shielded function
|
|
242
227
|
|
|
243
228
|
"""
|
|
244
229
|
|
|
@@ -259,11 +244,11 @@ def handle_event(
|
|
|
259
244
|
"""Generic event handler for CallbackManager.
|
|
260
245
|
|
|
261
246
|
!!! note
|
|
262
|
-
This function is used by
|
|
247
|
+
This function is used by `LangServe` to handle events.
|
|
263
248
|
|
|
264
249
|
Args:
|
|
265
250
|
handlers: The list of handlers that will handle the event.
|
|
266
|
-
event_name: The name of the event (e.g.,
|
|
251
|
+
event_name: The name of the event (e.g., `'on_llm_start'`).
|
|
267
252
|
ignore_condition_name: Name of the attribute defined on handler
|
|
268
253
|
that if True will cause the handler to be skipped for the given event.
|
|
269
254
|
*args: The arguments to pass to the event handler.
|
|
@@ -423,14 +408,14 @@ async def ahandle_event(
|
|
|
423
408
|
*args: Any,
|
|
424
409
|
**kwargs: Any,
|
|
425
410
|
) -> None:
|
|
426
|
-
"""Async generic event handler for
|
|
411
|
+
"""Async generic event handler for `AsyncCallbackManager`.
|
|
427
412
|
|
|
428
413
|
!!! note
|
|
429
|
-
This function is used by
|
|
414
|
+
This function is used by `LangServe` to handle events.
|
|
430
415
|
|
|
431
416
|
Args:
|
|
432
417
|
handlers: The list of handlers that will handle the event.
|
|
433
|
-
event_name: The name of the event (e.g.,
|
|
418
|
+
event_name: The name of the event (e.g., `'on_llm_start'`).
|
|
434
419
|
ignore_condition_name: Name of the attribute defined on handler
|
|
435
420
|
that if True will cause the handler to be skipped for the given event.
|
|
436
421
|
*args: The arguments to pass to the event handler.
|
|
@@ -474,19 +459,14 @@ class BaseRunManager(RunManagerMixin):
|
|
|
474
459
|
"""Initialize the run manager.
|
|
475
460
|
|
|
476
461
|
Args:
|
|
477
|
-
run_id
|
|
478
|
-
handlers
|
|
479
|
-
inheritable_handlers
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
Defaults to None.
|
|
486
|
-
metadata (Optional[dict[str, Any]]): The metadata.
|
|
487
|
-
Defaults to None.
|
|
488
|
-
inheritable_metadata (Optional[dict[str, Any]]): The inheritable metadata.
|
|
489
|
-
Defaults to None.
|
|
462
|
+
run_id: The ID of the run.
|
|
463
|
+
handlers: The list of handlers.
|
|
464
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
465
|
+
parent_run_id: The ID of the parent run.
|
|
466
|
+
tags: The list of tags.
|
|
467
|
+
inheritable_tags: The list of inheritable tags.
|
|
468
|
+
metadata: The metadata.
|
|
469
|
+
inheritable_metadata: The inheritable metadata.
|
|
490
470
|
|
|
491
471
|
"""
|
|
492
472
|
self.run_id = run_id
|
|
@@ -503,7 +483,7 @@ class BaseRunManager(RunManagerMixin):
|
|
|
503
483
|
"""Return a manager that doesn't perform any operations.
|
|
504
484
|
|
|
505
485
|
Returns:
|
|
506
|
-
|
|
486
|
+
The noop manager.
|
|
507
487
|
|
|
508
488
|
"""
|
|
509
489
|
return cls(
|
|
@@ -528,8 +508,8 @@ class RunManager(BaseRunManager):
|
|
|
528
508
|
"""Run when a text is received.
|
|
529
509
|
|
|
530
510
|
Args:
|
|
531
|
-
text
|
|
532
|
-
**kwargs
|
|
511
|
+
text: The received text.
|
|
512
|
+
**kwargs: Additional keyword arguments.
|
|
533
513
|
"""
|
|
534
514
|
if not self.handlers:
|
|
535
515
|
return
|
|
@@ -552,8 +532,8 @@ class RunManager(BaseRunManager):
|
|
|
552
532
|
"""Run when a retry is received.
|
|
553
533
|
|
|
554
534
|
Args:
|
|
555
|
-
retry_state
|
|
556
|
-
**kwargs
|
|
535
|
+
retry_state: The retry state.
|
|
536
|
+
**kwargs: Additional keyword arguments.
|
|
557
537
|
|
|
558
538
|
"""
|
|
559
539
|
if not self.handlers:
|
|
@@ -577,11 +557,10 @@ class ParentRunManager(RunManager):
|
|
|
577
557
|
"""Get a child callback manager.
|
|
578
558
|
|
|
579
559
|
Args:
|
|
580
|
-
tag
|
|
581
|
-
Defaults to None.
|
|
560
|
+
tag: The tag for the child callback manager.
|
|
582
561
|
|
|
583
562
|
Returns:
|
|
584
|
-
|
|
563
|
+
The child callback manager.
|
|
585
564
|
|
|
586
565
|
"""
|
|
587
566
|
manager = CallbackManager(handlers=[], parent_run_id=self.run_id)
|
|
@@ -601,7 +580,7 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
601
580
|
"""Get the equivalent sync RunManager.
|
|
602
581
|
|
|
603
582
|
Returns:
|
|
604
|
-
|
|
583
|
+
The sync RunManager.
|
|
605
584
|
|
|
606
585
|
"""
|
|
607
586
|
|
|
@@ -613,8 +592,8 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
613
592
|
"""Run when a text is received.
|
|
614
593
|
|
|
615
594
|
Args:
|
|
616
|
-
text
|
|
617
|
-
**kwargs
|
|
595
|
+
text: The received text.
|
|
596
|
+
**kwargs: Additional keyword arguments.
|
|
618
597
|
"""
|
|
619
598
|
if not self.handlers:
|
|
620
599
|
return
|
|
@@ -637,8 +616,8 @@ class AsyncRunManager(BaseRunManager, ABC):
|
|
|
637
616
|
"""Async run when a retry is received.
|
|
638
617
|
|
|
639
618
|
Args:
|
|
640
|
-
retry_state
|
|
641
|
-
**kwargs
|
|
619
|
+
retry_state: The retry state.
|
|
620
|
+
**kwargs: Additional keyword arguments.
|
|
642
621
|
|
|
643
622
|
"""
|
|
644
623
|
if not self.handlers:
|
|
@@ -662,11 +641,10 @@ class AsyncParentRunManager(AsyncRunManager):
|
|
|
662
641
|
"""Get a child callback manager.
|
|
663
642
|
|
|
664
643
|
Args:
|
|
665
|
-
tag
|
|
666
|
-
Defaults to None.
|
|
644
|
+
tag: The tag for the child callback manager.
|
|
667
645
|
|
|
668
646
|
Returns:
|
|
669
|
-
|
|
647
|
+
The child callback manager.
|
|
670
648
|
|
|
671
649
|
"""
|
|
672
650
|
manager = AsyncCallbackManager(handlers=[], parent_run_id=self.run_id)
|
|
@@ -691,10 +669,9 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
691
669
|
"""Run when LLM generates a new token.
|
|
692
670
|
|
|
693
671
|
Args:
|
|
694
|
-
token
|
|
695
|
-
chunk
|
|
696
|
-
|
|
697
|
-
**kwargs (Any): Additional keyword arguments.
|
|
672
|
+
token: The new token.
|
|
673
|
+
chunk: The chunk.
|
|
674
|
+
**kwargs: Additional keyword arguments.
|
|
698
675
|
|
|
699
676
|
"""
|
|
700
677
|
if not self.handlers:
|
|
@@ -715,8 +692,8 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
715
692
|
"""Run when LLM ends running.
|
|
716
693
|
|
|
717
694
|
Args:
|
|
718
|
-
response
|
|
719
|
-
**kwargs
|
|
695
|
+
response: The LLM result.
|
|
696
|
+
**kwargs: Additional keyword arguments.
|
|
720
697
|
|
|
721
698
|
"""
|
|
722
699
|
if not self.handlers:
|
|
@@ -740,8 +717,8 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
|
|
|
740
717
|
"""Run when LLM errors.
|
|
741
718
|
|
|
742
719
|
Args:
|
|
743
|
-
error
|
|
744
|
-
kwargs
|
|
720
|
+
error: The error.
|
|
721
|
+
**kwargs: Additional keyword arguments.
|
|
745
722
|
- response (LLMResult): The response which was generated before
|
|
746
723
|
the error occurred.
|
|
747
724
|
"""
|
|
@@ -766,7 +743,7 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
766
743
|
"""Get the equivalent sync RunManager.
|
|
767
744
|
|
|
768
745
|
Returns:
|
|
769
|
-
|
|
746
|
+
The sync RunManager.
|
|
770
747
|
|
|
771
748
|
"""
|
|
772
749
|
return CallbackManagerForLLMRun(
|
|
@@ -790,10 +767,9 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
790
767
|
"""Run when LLM generates a new token.
|
|
791
768
|
|
|
792
769
|
Args:
|
|
793
|
-
token
|
|
794
|
-
chunk
|
|
795
|
-
|
|
796
|
-
**kwargs (Any): Additional keyword arguments.
|
|
770
|
+
token: The new token.
|
|
771
|
+
chunk: The chunk.
|
|
772
|
+
**kwargs: Additional keyword arguments.
|
|
797
773
|
|
|
798
774
|
"""
|
|
799
775
|
if not self.handlers:
|
|
@@ -815,8 +791,8 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
815
791
|
"""Run when LLM ends running.
|
|
816
792
|
|
|
817
793
|
Args:
|
|
818
|
-
response
|
|
819
|
-
**kwargs
|
|
794
|
+
response: The LLM result.
|
|
795
|
+
**kwargs: Additional keyword arguments.
|
|
820
796
|
|
|
821
797
|
"""
|
|
822
798
|
if not self.handlers:
|
|
@@ -841,8 +817,8 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
|
|
|
841
817
|
"""Run when LLM errors.
|
|
842
818
|
|
|
843
819
|
Args:
|
|
844
|
-
error
|
|
845
|
-
kwargs
|
|
820
|
+
error: The error.
|
|
821
|
+
**kwargs: Additional keyword arguments.
|
|
846
822
|
- response (LLMResult): The response which was generated before
|
|
847
823
|
the error occurred.
|
|
848
824
|
|
|
@@ -870,8 +846,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
870
846
|
"""Run when chain ends running.
|
|
871
847
|
|
|
872
848
|
Args:
|
|
873
|
-
outputs
|
|
874
|
-
**kwargs
|
|
849
|
+
outputs: The outputs of the chain.
|
|
850
|
+
**kwargs: Additional keyword arguments.
|
|
875
851
|
|
|
876
852
|
"""
|
|
877
853
|
if not self.handlers:
|
|
@@ -895,8 +871,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
895
871
|
"""Run when chain errors.
|
|
896
872
|
|
|
897
873
|
Args:
|
|
898
|
-
error
|
|
899
|
-
**kwargs
|
|
874
|
+
error: The error.
|
|
875
|
+
**kwargs: Additional keyword arguments.
|
|
900
876
|
|
|
901
877
|
"""
|
|
902
878
|
if not self.handlers:
|
|
@@ -916,8 +892,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
916
892
|
"""Run when agent action is received.
|
|
917
893
|
|
|
918
894
|
Args:
|
|
919
|
-
action
|
|
920
|
-
**kwargs
|
|
895
|
+
action: The agent action.
|
|
896
|
+
**kwargs: Additional keyword arguments.
|
|
921
897
|
"""
|
|
922
898
|
if not self.handlers:
|
|
923
899
|
return
|
|
@@ -936,8 +912,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
|
|
|
936
912
|
"""Run when agent finish is received.
|
|
937
913
|
|
|
938
914
|
Args:
|
|
939
|
-
finish
|
|
940
|
-
**kwargs
|
|
915
|
+
finish: The agent finish.
|
|
916
|
+
**kwargs: Additional keyword arguments.
|
|
941
917
|
"""
|
|
942
918
|
if not self.handlers:
|
|
943
919
|
return
|
|
@@ -960,7 +936,7 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
960
936
|
"""Get the equivalent sync RunManager.
|
|
961
937
|
|
|
962
938
|
Returns:
|
|
963
|
-
|
|
939
|
+
The sync RunManager.
|
|
964
940
|
"""
|
|
965
941
|
return CallbackManagerForChainRun(
|
|
966
942
|
run_id=self.run_id,
|
|
@@ -978,8 +954,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
978
954
|
"""Run when a chain ends running.
|
|
979
955
|
|
|
980
956
|
Args:
|
|
981
|
-
outputs
|
|
982
|
-
**kwargs
|
|
957
|
+
outputs: The outputs of the chain.
|
|
958
|
+
**kwargs: Additional keyword arguments.
|
|
983
959
|
|
|
984
960
|
"""
|
|
985
961
|
if not self.handlers:
|
|
@@ -1004,8 +980,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1004
980
|
"""Run when chain errors.
|
|
1005
981
|
|
|
1006
982
|
Args:
|
|
1007
|
-
error
|
|
1008
|
-
**kwargs
|
|
983
|
+
error: The error.
|
|
984
|
+
**kwargs: Additional keyword arguments.
|
|
1009
985
|
|
|
1010
986
|
"""
|
|
1011
987
|
if not self.handlers:
|
|
@@ -1025,8 +1001,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1025
1001
|
"""Run when agent action is received.
|
|
1026
1002
|
|
|
1027
1003
|
Args:
|
|
1028
|
-
action
|
|
1029
|
-
**kwargs
|
|
1004
|
+
action: The agent action.
|
|
1005
|
+
**kwargs: Additional keyword arguments.
|
|
1030
1006
|
"""
|
|
1031
1007
|
if not self.handlers:
|
|
1032
1008
|
return
|
|
@@ -1045,8 +1021,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
|
|
|
1045
1021
|
"""Run when agent finish is received.
|
|
1046
1022
|
|
|
1047
1023
|
Args:
|
|
1048
|
-
finish
|
|
1049
|
-
**kwargs
|
|
1024
|
+
finish: The agent finish.
|
|
1025
|
+
**kwargs: Additional keyword arguments.
|
|
1050
1026
|
"""
|
|
1051
1027
|
if not self.handlers:
|
|
1052
1028
|
return
|
|
@@ -1073,8 +1049,8 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
|
|
|
1073
1049
|
"""Run when the tool ends running.
|
|
1074
1050
|
|
|
1075
1051
|
Args:
|
|
1076
|
-
output
|
|
1077
|
-
**kwargs
|
|
1052
|
+
output: The output of the tool.
|
|
1053
|
+
**kwargs: The keyword arguments to pass to the event handler
|
|
1078
1054
|
|
|
1079
1055
|
"""
|
|
1080
1056
|
if not self.handlers:
|
|
@@ -1098,8 +1074,8 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
|
|
|
1098
1074
|
"""Run when tool errors.
|
|
1099
1075
|
|
|
1100
1076
|
Args:
|
|
1101
|
-
error
|
|
1102
|
-
**kwargs
|
|
1077
|
+
error: The error.
|
|
1078
|
+
**kwargs: Additional keyword arguments.
|
|
1103
1079
|
|
|
1104
1080
|
"""
|
|
1105
1081
|
if not self.handlers:
|
|
@@ -1123,7 +1099,7 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1123
1099
|
"""Get the equivalent sync RunManager.
|
|
1124
1100
|
|
|
1125
1101
|
Returns:
|
|
1126
|
-
|
|
1102
|
+
The sync RunManager.
|
|
1127
1103
|
"""
|
|
1128
1104
|
return CallbackManagerForToolRun(
|
|
1129
1105
|
run_id=self.run_id,
|
|
@@ -1140,8 +1116,8 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1140
1116
|
"""Async run when the tool ends running.
|
|
1141
1117
|
|
|
1142
1118
|
Args:
|
|
1143
|
-
output
|
|
1144
|
-
**kwargs
|
|
1119
|
+
output: The output of the tool.
|
|
1120
|
+
**kwargs: Additional keyword arguments.
|
|
1145
1121
|
|
|
1146
1122
|
"""
|
|
1147
1123
|
if not self.handlers:
|
|
@@ -1165,8 +1141,8 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
|
|
|
1165
1141
|
"""Run when tool errors.
|
|
1166
1142
|
|
|
1167
1143
|
Args:
|
|
1168
|
-
error
|
|
1169
|
-
**kwargs
|
|
1144
|
+
error: The error.
|
|
1145
|
+
**kwargs: Additional keyword arguments.
|
|
1170
1146
|
|
|
1171
1147
|
"""
|
|
1172
1148
|
if not self.handlers:
|
|
@@ -1194,8 +1170,8 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
|
|
|
1194
1170
|
"""Run when retriever ends running.
|
|
1195
1171
|
|
|
1196
1172
|
Args:
|
|
1197
|
-
documents
|
|
1198
|
-
**kwargs
|
|
1173
|
+
documents: The retrieved documents.
|
|
1174
|
+
**kwargs: Additional keyword arguments.
|
|
1199
1175
|
|
|
1200
1176
|
"""
|
|
1201
1177
|
if not self.handlers:
|
|
@@ -1219,8 +1195,8 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
|
|
|
1219
1195
|
"""Run when retriever errors.
|
|
1220
1196
|
|
|
1221
1197
|
Args:
|
|
1222
|
-
error
|
|
1223
|
-
**kwargs
|
|
1198
|
+
error: The error.
|
|
1199
|
+
**kwargs: Additional keyword arguments.
|
|
1224
1200
|
|
|
1225
1201
|
"""
|
|
1226
1202
|
if not self.handlers:
|
|
@@ -1247,7 +1223,7 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1247
1223
|
"""Get the equivalent sync RunManager.
|
|
1248
1224
|
|
|
1249
1225
|
Returns:
|
|
1250
|
-
|
|
1226
|
+
The sync RunManager.
|
|
1251
1227
|
|
|
1252
1228
|
"""
|
|
1253
1229
|
return CallbackManagerForRetrieverRun(
|
|
@@ -1268,8 +1244,8 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1268
1244
|
"""Run when the retriever ends running.
|
|
1269
1245
|
|
|
1270
1246
|
Args:
|
|
1271
|
-
documents
|
|
1272
|
-
**kwargs
|
|
1247
|
+
documents: The retrieved documents.
|
|
1248
|
+
**kwargs: Additional keyword arguments.
|
|
1273
1249
|
|
|
1274
1250
|
"""
|
|
1275
1251
|
if not self.handlers:
|
|
@@ -1294,8 +1270,8 @@ class AsyncCallbackManagerForRetrieverRun(
|
|
|
1294
1270
|
"""Run when retriever errors.
|
|
1295
1271
|
|
|
1296
1272
|
Args:
|
|
1297
|
-
error
|
|
1298
|
-
**kwargs
|
|
1273
|
+
error: The error.
|
|
1274
|
+
**kwargs: Additional keyword arguments.
|
|
1299
1275
|
|
|
1300
1276
|
"""
|
|
1301
1277
|
if not self.handlers:
|
|
@@ -1325,14 +1301,13 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1325
1301
|
"""Run when LLM starts running.
|
|
1326
1302
|
|
|
1327
1303
|
Args:
|
|
1328
|
-
serialized
|
|
1329
|
-
prompts
|
|
1330
|
-
run_id
|
|
1331
|
-
**kwargs
|
|
1304
|
+
serialized: The serialized LLM.
|
|
1305
|
+
prompts: The list of prompts.
|
|
1306
|
+
run_id: The ID of the run.
|
|
1307
|
+
**kwargs: Additional keyword arguments.
|
|
1332
1308
|
|
|
1333
1309
|
Returns:
|
|
1334
|
-
|
|
1335
|
-
prompt as an LLM run.
|
|
1310
|
+
A callback manager for each prompt as an LLM run.
|
|
1336
1311
|
|
|
1337
1312
|
"""
|
|
1338
1313
|
managers = []
|
|
@@ -1377,14 +1352,13 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1377
1352
|
"""Run when chat model starts running.
|
|
1378
1353
|
|
|
1379
1354
|
Args:
|
|
1380
|
-
serialized
|
|
1381
|
-
messages
|
|
1382
|
-
run_id
|
|
1383
|
-
**kwargs
|
|
1355
|
+
serialized: The serialized LLM.
|
|
1356
|
+
messages: The list of messages.
|
|
1357
|
+
run_id: The ID of the run.
|
|
1358
|
+
**kwargs: Additional keyword arguments.
|
|
1384
1359
|
|
|
1385
1360
|
Returns:
|
|
1386
|
-
|
|
1387
|
-
list of messages as an LLM run.
|
|
1361
|
+
A callback manager for each list of messages as an LLM run.
|
|
1388
1362
|
|
|
1389
1363
|
"""
|
|
1390
1364
|
managers = []
|
|
@@ -1432,13 +1406,13 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1432
1406
|
"""Run when chain starts running.
|
|
1433
1407
|
|
|
1434
1408
|
Args:
|
|
1435
|
-
serialized
|
|
1436
|
-
inputs
|
|
1437
|
-
run_id
|
|
1438
|
-
**kwargs
|
|
1409
|
+
serialized: The serialized chain.
|
|
1410
|
+
inputs: The inputs to the chain.
|
|
1411
|
+
run_id: The ID of the run.
|
|
1412
|
+
**kwargs: Additional keyword arguments.
|
|
1439
1413
|
|
|
1440
1414
|
Returns:
|
|
1441
|
-
|
|
1415
|
+
The callback manager for the chain run.
|
|
1442
1416
|
|
|
1443
1417
|
"""
|
|
1444
1418
|
if run_id is None:
|
|
@@ -1483,17 +1457,17 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1483
1457
|
serialized: Serialized representation of the tool.
|
|
1484
1458
|
input_str: The input to the tool as a string.
|
|
1485
1459
|
Non-string inputs are cast to strings.
|
|
1486
|
-
run_id: ID for the run.
|
|
1487
|
-
parent_run_id: The ID of the parent run.
|
|
1460
|
+
run_id: ID for the run.
|
|
1461
|
+
parent_run_id: The ID of the parent run.
|
|
1488
1462
|
inputs: The original input to the tool if provided.
|
|
1489
1463
|
Recommended for usage instead of input_str when the original
|
|
1490
1464
|
input is needed.
|
|
1491
1465
|
If provided, the inputs are expected to be formatted as a dict.
|
|
1492
1466
|
The keys will correspond to the named-arguments in the tool.
|
|
1493
|
-
**kwargs
|
|
1467
|
+
**kwargs: The keyword arguments to pass to the event handler
|
|
1494
1468
|
|
|
1495
1469
|
Returns:
|
|
1496
|
-
|
|
1470
|
+
The callback manager for the tool run.
|
|
1497
1471
|
|
|
1498
1472
|
"""
|
|
1499
1473
|
if run_id is None:
|
|
@@ -1536,11 +1510,11 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1536
1510
|
"""Run when the retriever starts running.
|
|
1537
1511
|
|
|
1538
1512
|
Args:
|
|
1539
|
-
serialized
|
|
1540
|
-
query
|
|
1541
|
-
run_id
|
|
1542
|
-
parent_run_id
|
|
1543
|
-
**kwargs
|
|
1513
|
+
serialized: The serialized retriever.
|
|
1514
|
+
query: The query.
|
|
1515
|
+
run_id: The ID of the run.
|
|
1516
|
+
parent_run_id: The ID of the parent run.
|
|
1517
|
+
**kwargs: Additional keyword arguments.
|
|
1544
1518
|
|
|
1545
1519
|
Returns:
|
|
1546
1520
|
The callback manager for the retriever run.
|
|
@@ -1588,7 +1562,7 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1588
1562
|
Args:
|
|
1589
1563
|
name: The name of the adhoc event.
|
|
1590
1564
|
data: The data for the adhoc event.
|
|
1591
|
-
run_id: The ID of the run.
|
|
1565
|
+
run_id: The ID of the run.
|
|
1592
1566
|
|
|
1593
1567
|
Raises:
|
|
1594
1568
|
ValueError: If additional keyword arguments are passed.
|
|
@@ -1633,23 +1607,16 @@ class CallbackManager(BaseCallbackManager):
|
|
|
1633
1607
|
"""Configure the callback manager.
|
|
1634
1608
|
|
|
1635
1609
|
Args:
|
|
1636
|
-
inheritable_callbacks
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
local_tags (Optional[list[str]], optional): The local tags.
|
|
1644
|
-
Defaults to None.
|
|
1645
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
1646
|
-
metadata. Defaults to None.
|
|
1647
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
1648
|
-
Defaults to None.
|
|
1610
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
1611
|
+
local_callbacks: The local callbacks.
|
|
1612
|
+
verbose: Whether to enable verbose mode.
|
|
1613
|
+
inheritable_tags: The inheritable tags.
|
|
1614
|
+
local_tags: The local tags.
|
|
1615
|
+
inheritable_metadata: The inheritable metadata.
|
|
1616
|
+
local_metadata: The local metadata.
|
|
1649
1617
|
|
|
1650
1618
|
Returns:
|
|
1651
|
-
|
|
1652
|
-
|
|
1619
|
+
The configured callback manager.
|
|
1653
1620
|
"""
|
|
1654
1621
|
return _configure(
|
|
1655
1622
|
cls,
|
|
@@ -1678,12 +1645,11 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1678
1645
|
"""Initialize the callback manager.
|
|
1679
1646
|
|
|
1680
1647
|
Args:
|
|
1681
|
-
handlers
|
|
1682
|
-
inheritable_handlers
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
**kwargs (Any): Additional keyword arguments.
|
|
1648
|
+
handlers: The list of handlers.
|
|
1649
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
1650
|
+
parent_run_id: The ID of the parent run.
|
|
1651
|
+
parent_run_manager: The parent run manager.
|
|
1652
|
+
**kwargs: Additional keyword arguments.
|
|
1687
1653
|
|
|
1688
1654
|
"""
|
|
1689
1655
|
super().__init__(
|
|
@@ -1718,38 +1684,33 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1718
1684
|
from the current object.
|
|
1719
1685
|
|
|
1720
1686
|
Returns:
|
|
1721
|
-
|
|
1722
|
-
|
|
1687
|
+
A copy of the current object with the handlers, tags, and other attributes
|
|
1688
|
+
merged from the other object.
|
|
1723
1689
|
|
|
1724
1690
|
Example: Merging two callback managers.
|
|
1725
1691
|
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
)
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
)
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
# ]
|
|
1749
|
-
|
|
1750
|
-
print(merged_manager.tags)
|
|
1751
|
-
# ['tag2', 'tag1']
|
|
1752
|
-
|
|
1692
|
+
```python
|
|
1693
|
+
from langchain_core.callbacks.manager import (
|
|
1694
|
+
CallbackManager,
|
|
1695
|
+
trace_as_chain_group,
|
|
1696
|
+
)
|
|
1697
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
1698
|
+
|
|
1699
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
1700
|
+
with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
|
|
1701
|
+
merged_manager = group_manager.merge(manager)
|
|
1702
|
+
print(type(merged_manager))
|
|
1703
|
+
# <class 'langchain_core.callbacks.manager.CallbackManagerForChainGroup'>
|
|
1704
|
+
|
|
1705
|
+
print(merged_manager.handlers)
|
|
1706
|
+
# [
|
|
1707
|
+
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
1708
|
+
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
1709
|
+
# ]
|
|
1710
|
+
|
|
1711
|
+
print(merged_manager.tags)
|
|
1712
|
+
# ['tag2', 'tag1']
|
|
1713
|
+
```
|
|
1753
1714
|
""" # noqa: E501
|
|
1754
1715
|
manager = self.__class__(
|
|
1755
1716
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -1778,8 +1739,8 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1778
1739
|
"""Run when traced chain group ends.
|
|
1779
1740
|
|
|
1780
1741
|
Args:
|
|
1781
|
-
outputs
|
|
1782
|
-
**kwargs
|
|
1742
|
+
outputs: The outputs of the chain.
|
|
1743
|
+
**kwargs: Additional keyword arguments.
|
|
1783
1744
|
|
|
1784
1745
|
"""
|
|
1785
1746
|
self.ended = True
|
|
@@ -1793,8 +1754,8 @@ class CallbackManagerForChainGroup(CallbackManager):
|
|
|
1793
1754
|
"""Run when chain errors.
|
|
1794
1755
|
|
|
1795
1756
|
Args:
|
|
1796
|
-
error
|
|
1797
|
-
**kwargs
|
|
1757
|
+
error: The error.
|
|
1758
|
+
**kwargs: Additional keyword arguments.
|
|
1798
1759
|
|
|
1799
1760
|
"""
|
|
1800
1761
|
self.ended = True
|
|
@@ -1819,15 +1780,14 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1819
1780
|
"""Run when LLM starts running.
|
|
1820
1781
|
|
|
1821
1782
|
Args:
|
|
1822
|
-
serialized
|
|
1823
|
-
prompts
|
|
1824
|
-
run_id
|
|
1825
|
-
**kwargs
|
|
1783
|
+
serialized: The serialized LLM.
|
|
1784
|
+
prompts: The list of prompts.
|
|
1785
|
+
run_id: The ID of the run.
|
|
1786
|
+
**kwargs: Additional keyword arguments.
|
|
1826
1787
|
|
|
1827
1788
|
Returns:
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
to each prompt.
|
|
1789
|
+
The list of async callback managers, one for each LLM Run corresponding to
|
|
1790
|
+
each prompt.
|
|
1831
1791
|
"""
|
|
1832
1792
|
inline_tasks = []
|
|
1833
1793
|
non_inline_tasks = []
|
|
@@ -1908,15 +1868,14 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1908
1868
|
"""Async run when LLM starts running.
|
|
1909
1869
|
|
|
1910
1870
|
Args:
|
|
1911
|
-
serialized
|
|
1912
|
-
messages
|
|
1913
|
-
run_id
|
|
1914
|
-
**kwargs
|
|
1871
|
+
serialized: The serialized LLM.
|
|
1872
|
+
messages: The list of messages.
|
|
1873
|
+
run_id: The ID of the run.
|
|
1874
|
+
**kwargs: Additional keyword arguments.
|
|
1915
1875
|
|
|
1916
1876
|
Returns:
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
corresponding to each inner message list.
|
|
1877
|
+
The list of async callback managers, one for each LLM Run corresponding to
|
|
1878
|
+
each inner message list.
|
|
1920
1879
|
"""
|
|
1921
1880
|
inline_tasks = []
|
|
1922
1881
|
non_inline_tasks = []
|
|
@@ -1980,14 +1939,13 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
1980
1939
|
"""Async run when chain starts running.
|
|
1981
1940
|
|
|
1982
1941
|
Args:
|
|
1983
|
-
serialized
|
|
1984
|
-
inputs
|
|
1985
|
-
run_id
|
|
1986
|
-
**kwargs
|
|
1942
|
+
serialized: The serialized chain.
|
|
1943
|
+
inputs: The inputs to the chain.
|
|
1944
|
+
run_id: The ID of the run.
|
|
1945
|
+
**kwargs: Additional keyword arguments.
|
|
1987
1946
|
|
|
1988
1947
|
Returns:
|
|
1989
|
-
|
|
1990
|
-
for the chain run.
|
|
1948
|
+
The async callback manager for the chain run.
|
|
1991
1949
|
"""
|
|
1992
1950
|
if run_id is None:
|
|
1993
1951
|
run_id = uuid.uuid4()
|
|
@@ -2028,16 +1986,14 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2028
1986
|
"""Run when the tool starts running.
|
|
2029
1987
|
|
|
2030
1988
|
Args:
|
|
2031
|
-
serialized
|
|
2032
|
-
input_str
|
|
2033
|
-
run_id
|
|
2034
|
-
parent_run_id
|
|
2035
|
-
|
|
2036
|
-
**kwargs (Any): Additional keyword arguments.
|
|
1989
|
+
serialized: The serialized tool.
|
|
1990
|
+
input_str: The input to the tool.
|
|
1991
|
+
run_id: The ID of the run.
|
|
1992
|
+
parent_run_id: The ID of the parent run.
|
|
1993
|
+
**kwargs: Additional keyword arguments.
|
|
2037
1994
|
|
|
2038
1995
|
Returns:
|
|
2039
|
-
|
|
2040
|
-
for the tool run.
|
|
1996
|
+
The async callback manager for the tool run.
|
|
2041
1997
|
"""
|
|
2042
1998
|
if run_id is None:
|
|
2043
1999
|
run_id = uuid.uuid4()
|
|
@@ -2082,7 +2038,7 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2082
2038
|
Args:
|
|
2083
2039
|
name: The name of the adhoc event.
|
|
2084
2040
|
data: The data for the adhoc event.
|
|
2085
|
-
run_id: The ID of the run.
|
|
2041
|
+
run_id: The ID of the run.
|
|
2086
2042
|
|
|
2087
2043
|
Raises:
|
|
2088
2044
|
ValueError: If additional keyword arguments are passed.
|
|
@@ -2124,15 +2080,14 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2124
2080
|
"""Run when the retriever starts running.
|
|
2125
2081
|
|
|
2126
2082
|
Args:
|
|
2127
|
-
serialized
|
|
2128
|
-
query
|
|
2129
|
-
run_id
|
|
2130
|
-
parent_run_id
|
|
2131
|
-
**kwargs
|
|
2083
|
+
serialized: The serialized retriever.
|
|
2084
|
+
query: The query.
|
|
2085
|
+
run_id: The ID of the run.
|
|
2086
|
+
parent_run_id: The ID of the parent run.
|
|
2087
|
+
**kwargs: Additional keyword arguments.
|
|
2132
2088
|
|
|
2133
2089
|
Returns:
|
|
2134
|
-
|
|
2135
|
-
for the retriever run.
|
|
2090
|
+
The async callback manager for the retriever run.
|
|
2136
2091
|
"""
|
|
2137
2092
|
if run_id is None:
|
|
2138
2093
|
run_id = uuid.uuid4()
|
|
@@ -2175,22 +2130,16 @@ class AsyncCallbackManager(BaseCallbackManager):
|
|
|
2175
2130
|
"""Configure the async callback manager.
|
|
2176
2131
|
|
|
2177
2132
|
Args:
|
|
2178
|
-
inheritable_callbacks
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
local_tags (Optional[list[str]], optional): The local tags.
|
|
2186
|
-
Defaults to None.
|
|
2187
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
2188
|
-
metadata. Defaults to None.
|
|
2189
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
2190
|
-
Defaults to None.
|
|
2133
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
2134
|
+
local_callbacks: The local callbacks.
|
|
2135
|
+
verbose: Whether to enable verbose mode.
|
|
2136
|
+
inheritable_tags: The inheritable tags.
|
|
2137
|
+
local_tags: The local tags.
|
|
2138
|
+
inheritable_metadata: The inheritable metadata.
|
|
2139
|
+
local_metadata: The local metadata.
|
|
2191
2140
|
|
|
2192
2141
|
Returns:
|
|
2193
|
-
|
|
2142
|
+
The configured async callback manager.
|
|
2194
2143
|
"""
|
|
2195
2144
|
return _configure(
|
|
2196
2145
|
cls,
|
|
@@ -2219,13 +2168,11 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2219
2168
|
"""Initialize the async callback manager.
|
|
2220
2169
|
|
|
2221
2170
|
Args:
|
|
2222
|
-
handlers
|
|
2223
|
-
inheritable_handlers
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
The parent run manager.
|
|
2228
|
-
**kwargs (Any): Additional keyword arguments.
|
|
2171
|
+
handlers: The list of handlers.
|
|
2172
|
+
inheritable_handlers: The list of inheritable handlers.
|
|
2173
|
+
parent_run_id: The ID of the parent run.
|
|
2174
|
+
parent_run_manager: The parent run manager.
|
|
2175
|
+
**kwargs: Additional keyword arguments.
|
|
2229
2176
|
"""
|
|
2230
2177
|
super().__init__(
|
|
2231
2178
|
handlers,
|
|
@@ -2264,33 +2211,30 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2264
2211
|
|
|
2265
2212
|
Example: Merging two callback managers.
|
|
2266
2213
|
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
2274
|
-
|
|
2275
|
-
manager = CallbackManager(
|
|
2276
|
-
handlers=[StdOutCallbackHandler()], tags=["tag2"]
|
|
2277
|
-
)
|
|
2278
|
-
async with atrace_as_chain_group(
|
|
2279
|
-
"My Group Name", tags=["tag1"]
|
|
2280
|
-
) as group_manager:
|
|
2281
|
-
merged_manager = group_manager.merge(manager)
|
|
2282
|
-
print(type(merged_manager))
|
|
2283
|
-
# <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
|
|
2284
|
-
|
|
2285
|
-
print(merged_manager.handlers)
|
|
2286
|
-
# [
|
|
2287
|
-
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
2288
|
-
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
2289
|
-
# ]
|
|
2290
|
-
|
|
2291
|
-
print(merged_manager.tags)
|
|
2292
|
-
# ['tag2', 'tag1']
|
|
2214
|
+
```python
|
|
2215
|
+
from langchain_core.callbacks.manager import (
|
|
2216
|
+
CallbackManager,
|
|
2217
|
+
atrace_as_chain_group,
|
|
2218
|
+
)
|
|
2219
|
+
from langchain_core.callbacks.stdout import StdOutCallbackHandler
|
|
2293
2220
|
|
|
2221
|
+
manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
|
|
2222
|
+
async with atrace_as_chain_group(
|
|
2223
|
+
"My Group Name", tags=["tag1"]
|
|
2224
|
+
) as group_manager:
|
|
2225
|
+
merged_manager = group_manager.merge(manager)
|
|
2226
|
+
print(type(merged_manager))
|
|
2227
|
+
# <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
|
|
2228
|
+
|
|
2229
|
+
print(merged_manager.handlers)
|
|
2230
|
+
# [
|
|
2231
|
+
# <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
|
|
2232
|
+
# <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
|
|
2233
|
+
# ]
|
|
2234
|
+
|
|
2235
|
+
print(merged_manager.tags)
|
|
2236
|
+
# ['tag2', 'tag1']
|
|
2237
|
+
```
|
|
2294
2238
|
""" # noqa: E501
|
|
2295
2239
|
manager = self.__class__(
|
|
2296
2240
|
parent_run_id=self.parent_run_id or other.parent_run_id,
|
|
@@ -2319,8 +2263,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2319
2263
|
"""Run when traced chain group ends.
|
|
2320
2264
|
|
|
2321
2265
|
Args:
|
|
2322
|
-
outputs
|
|
2323
|
-
**kwargs
|
|
2266
|
+
outputs: The outputs of the chain.
|
|
2267
|
+
**kwargs: Additional keyword arguments.
|
|
2324
2268
|
"""
|
|
2325
2269
|
self.ended = True
|
|
2326
2270
|
await self.parent_run_manager.on_chain_end(outputs, **kwargs)
|
|
@@ -2333,8 +2277,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
|
|
|
2333
2277
|
"""Run when chain errors.
|
|
2334
2278
|
|
|
2335
2279
|
Args:
|
|
2336
|
-
error
|
|
2337
|
-
**kwargs
|
|
2280
|
+
error: The error.
|
|
2281
|
+
**kwargs: Additional keyword arguments.
|
|
2338
2282
|
"""
|
|
2339
2283
|
self.ended = True
|
|
2340
2284
|
await self.parent_run_manager.on_chain_error(error, **kwargs)
|
|
@@ -2357,25 +2301,20 @@ def _configure(
|
|
|
2357
2301
|
"""Configure the callback manager.
|
|
2358
2302
|
|
|
2359
2303
|
Args:
|
|
2360
|
-
callback_manager_cls
|
|
2361
|
-
inheritable_callbacks
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
local_tags (Optional[list[str]], optional): The local tags. Defaults to None.
|
|
2369
|
-
inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
|
|
2370
|
-
metadata. Defaults to None.
|
|
2371
|
-
local_metadata (Optional[dict[str, Any]], optional): The local metadata.
|
|
2372
|
-
Defaults to None.
|
|
2304
|
+
callback_manager_cls: The callback manager class.
|
|
2305
|
+
inheritable_callbacks: The inheritable callbacks.
|
|
2306
|
+
local_callbacks: The local callbacks.
|
|
2307
|
+
inheritable_tags: The inheritable tags.
|
|
2308
|
+
local_tags: The local tags.
|
|
2309
|
+
inheritable_metadata: The inheritable metadata.
|
|
2310
|
+
local_metadata: The local metadata.
|
|
2311
|
+
verbose: Whether to enable verbose mode.
|
|
2373
2312
|
|
|
2374
2313
|
Raises:
|
|
2375
2314
|
RuntimeError: If `LANGCHAIN_TRACING` is set but `LANGCHAIN_TRACING_V2` is not.
|
|
2376
2315
|
|
|
2377
2316
|
Returns:
|
|
2378
|
-
|
|
2317
|
+
The configured callback manager.
|
|
2379
2318
|
"""
|
|
2380
2319
|
tracing_context = get_tracing_context()
|
|
2381
2320
|
tracing_metadata = tracing_context["metadata"]
|
|
@@ -2532,8 +2471,8 @@ async def adispatch_custom_event(
|
|
|
2532
2471
|
Args:
|
|
2533
2472
|
name: The name of the adhoc event.
|
|
2534
2473
|
data: The data for the adhoc event. Free form data. Ideally should be
|
|
2535
|
-
|
|
2536
|
-
|
|
2474
|
+
JSON serializable to avoid serialization issues downstream, but
|
|
2475
|
+
this is not enforced.
|
|
2537
2476
|
config: Optional config object. Mirrors the async API but not strictly needed.
|
|
2538
2477
|
|
|
2539
2478
|
Raises:
|
|
@@ -2541,75 +2480,74 @@ async def adispatch_custom_event(
|
|
|
2541
2480
|
the event with.
|
|
2542
2481
|
|
|
2543
2482
|
Example:
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2483
|
+
```python
|
|
2484
|
+
from langchain_core.callbacks import (
|
|
2485
|
+
AsyncCallbackHandler,
|
|
2486
|
+
adispatch_custom_event
|
|
2487
|
+
)
|
|
2488
|
+
from langchain_core.runnable import RunnableLambda
|
|
2489
|
+
|
|
2490
|
+
class CustomCallbackManager(AsyncCallbackHandler):
|
|
2491
|
+
async def on_custom_event(
|
|
2492
|
+
self,
|
|
2493
|
+
name: str,
|
|
2494
|
+
data: Any,
|
|
2495
|
+
*,
|
|
2496
|
+
run_id: UUID,
|
|
2497
|
+
tags: list[str] | None = None,
|
|
2498
|
+
metadata: dict[str, Any] | None = None,
|
|
2499
|
+
**kwargs: Any,
|
|
2500
|
+
) -> None:
|
|
2501
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2502
|
+
|
|
2503
|
+
callback = CustomCallbackManager()
|
|
2504
|
+
|
|
2505
|
+
async def foo(inputs):
|
|
2506
|
+
await adispatch_custom_event("my_event", {"bar": "buzz})
|
|
2507
|
+
return inputs
|
|
2508
|
+
|
|
2509
|
+
foo_ = RunnableLambda(foo)
|
|
2510
|
+
await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2511
|
+
```
|
|
2574
2512
|
|
|
2575
2513
|
Example: Use with astream events
|
|
2576
2514
|
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
)
|
|
2612
|
-
|
|
2515
|
+
```python
|
|
2516
|
+
from langchain_core.callbacks import (
|
|
2517
|
+
AsyncCallbackHandler,
|
|
2518
|
+
adispatch_custom_event
|
|
2519
|
+
)
|
|
2520
|
+
from langchain_core.runnable import RunnableLambda
|
|
2521
|
+
|
|
2522
|
+
class CustomCallbackManager(AsyncCallbackHandler):
|
|
2523
|
+
async def on_custom_event(
|
|
2524
|
+
self,
|
|
2525
|
+
name: str,
|
|
2526
|
+
data: Any,
|
|
2527
|
+
*,
|
|
2528
|
+
run_id: UUID,
|
|
2529
|
+
tags: list[str] | None = None,
|
|
2530
|
+
metadata: dict[str, Any] | None = None,
|
|
2531
|
+
**kwargs: Any,
|
|
2532
|
+
) -> None:
|
|
2533
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2534
|
+
|
|
2535
|
+
callback = CustomCallbackManager()
|
|
2536
|
+
|
|
2537
|
+
async def foo(inputs):
|
|
2538
|
+
await adispatch_custom_event("event_type_1", {"bar": "buzz})
|
|
2539
|
+
await adispatch_custom_event("event_type_2", 5)
|
|
2540
|
+
return inputs
|
|
2541
|
+
|
|
2542
|
+
foo_ = RunnableLambda(foo)
|
|
2543
|
+
|
|
2544
|
+
async for event in foo_.ainvoke_stream(
|
|
2545
|
+
{"a": "1"},
|
|
2546
|
+
version="v2",
|
|
2547
|
+
config={"callbacks": [CustomCallbackManager()]}
|
|
2548
|
+
):
|
|
2549
|
+
print(event)
|
|
2550
|
+
```
|
|
2613
2551
|
|
|
2614
2552
|
!!! warning
|
|
2615
2553
|
If using python <= 3.10 and async, you MUST
|
|
@@ -2658,8 +2596,8 @@ def dispatch_custom_event(
|
|
|
2658
2596
|
Args:
|
|
2659
2597
|
name: The name of the adhoc event.
|
|
2660
2598
|
data: The data for the adhoc event. Free form data. Ideally should be
|
|
2661
|
-
|
|
2662
|
-
|
|
2599
|
+
JSON serializable to avoid serialization issues downstream, but
|
|
2600
|
+
this is not enforced.
|
|
2663
2601
|
config: Optional config object. Mirrors the async API but not strictly needed.
|
|
2664
2602
|
|
|
2665
2603
|
Raises:
|
|
@@ -2667,32 +2605,31 @@ def dispatch_custom_event(
|
|
|
2667
2605
|
the event with.
|
|
2668
2606
|
|
|
2669
2607
|
Example:
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2608
|
+
```python
|
|
2609
|
+
from langchain_core.callbacks import BaseCallbackHandler
|
|
2610
|
+
from langchain_core.callbacks import dispatch_custom_event
|
|
2611
|
+
from langchain_core.runnable import RunnableLambda
|
|
2612
|
+
|
|
2613
|
+
class CustomCallbackManager(BaseCallbackHandler):
|
|
2614
|
+
def on_custom_event(
|
|
2615
|
+
self,
|
|
2616
|
+
name: str,
|
|
2617
|
+
data: Any,
|
|
2618
|
+
*,
|
|
2619
|
+
run_id: UUID,
|
|
2620
|
+
tags: list[str] | None = None,
|
|
2621
|
+
metadata: dict[str, Any] | None = None,
|
|
2622
|
+
**kwargs: Any,
|
|
2623
|
+
) -> None:
|
|
2624
|
+
print(f"Received custom event: {name} with data: {data}")
|
|
2625
|
+
|
|
2626
|
+
def foo(inputs):
|
|
2627
|
+
dispatch_custom_event("my_event", {"bar": "buzz})
|
|
2628
|
+
return inputs
|
|
2629
|
+
|
|
2630
|
+
foo_ = RunnableLambda(foo)
|
|
2631
|
+
foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
|
|
2632
|
+
```
|
|
2696
2633
|
|
|
2697
2634
|
!!! version-added "Added in version 0.2.15"
|
|
2698
2635
|
|