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
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"""Runnable that selects which branch to run based on a condition."""
|
|
2
2
|
|
|
3
|
-
from collections.abc import
|
|
3
|
+
from collections.abc import (
|
|
4
|
+
AsyncIterator,
|
|
5
|
+
Awaitable,
|
|
6
|
+
Callable,
|
|
7
|
+
Iterator,
|
|
8
|
+
Mapping,
|
|
9
|
+
Sequence,
|
|
10
|
+
)
|
|
4
11
|
from typing import (
|
|
5
12
|
Any,
|
|
6
|
-
Callable,
|
|
7
|
-
Optional,
|
|
8
|
-
Union,
|
|
9
13
|
cast,
|
|
10
14
|
)
|
|
11
15
|
|
|
@@ -36,60 +40,54 @@ from langchain_core.runnables.utils import (
|
|
|
36
40
|
class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
37
41
|
"""Runnable that selects which branch to run based on a condition.
|
|
38
42
|
|
|
39
|
-
The Runnable is initialized with a list of (condition, Runnable) pairs and
|
|
43
|
+
The Runnable is initialized with a list of `(condition, Runnable)` pairs and
|
|
40
44
|
a default branch.
|
|
41
45
|
|
|
42
46
|
When operating on an input, the first condition that evaluates to True is
|
|
43
|
-
selected, and the corresponding Runnable is run on the input.
|
|
47
|
+
selected, and the corresponding `Runnable` is run on the input.
|
|
44
48
|
|
|
45
|
-
If no condition evaluates to True
|
|
49
|
+
If no condition evaluates to `True`, the default branch is run on the input.
|
|
46
50
|
|
|
47
51
|
Examples:
|
|
52
|
+
```python
|
|
53
|
+
from langchain_core.runnables import RunnableBranch
|
|
54
|
+
|
|
55
|
+
branch = RunnableBranch(
|
|
56
|
+
(lambda x: isinstance(x, str), lambda x: x.upper()),
|
|
57
|
+
(lambda x: isinstance(x, int), lambda x: x + 1),
|
|
58
|
+
(lambda x: isinstance(x, float), lambda x: x * 2),
|
|
59
|
+
lambda x: "goodbye",
|
|
60
|
+
)
|
|
48
61
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
branch = RunnableBranch(
|
|
54
|
-
(lambda x: isinstance(x, str), lambda x: x.upper()),
|
|
55
|
-
(lambda x: isinstance(x, int), lambda x: x + 1),
|
|
56
|
-
(lambda x: isinstance(x, float), lambda x: x * 2),
|
|
57
|
-
lambda x: "goodbye",
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
branch.invoke("hello") # "HELLO"
|
|
61
|
-
branch.invoke(None) # "goodbye"
|
|
62
|
-
|
|
62
|
+
branch.invoke("hello") # "HELLO"
|
|
63
|
+
branch.invoke(None) # "goodbye"
|
|
64
|
+
```
|
|
63
65
|
"""
|
|
64
66
|
|
|
65
67
|
branches: Sequence[tuple[Runnable[Input, bool], Runnable[Input, Output]]]
|
|
66
|
-
"""A list of (condition, Runnable) pairs."""
|
|
68
|
+
"""A list of `(condition, Runnable)` pairs."""
|
|
67
69
|
default: Runnable[Input, Output]
|
|
68
|
-
"""A Runnable to run if no condition is met."""
|
|
70
|
+
"""A `Runnable` to run if no condition is met."""
|
|
69
71
|
|
|
70
72
|
def __init__(
|
|
71
73
|
self,
|
|
72
|
-
*branches:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
RunnableLike,
|
|
80
|
-
],
|
|
81
|
-
RunnableLike, # To accommodate the default branch
|
|
82
|
-
],
|
|
74
|
+
*branches: tuple[
|
|
75
|
+
Runnable[Input, bool]
|
|
76
|
+
| Callable[[Input], bool]
|
|
77
|
+
| Callable[[Input], Awaitable[bool]],
|
|
78
|
+
RunnableLike,
|
|
79
|
+
]
|
|
80
|
+
| RunnableLike,
|
|
83
81
|
) -> None:
|
|
84
|
-
"""A Runnable that runs one of two branches based on a condition.
|
|
82
|
+
"""A `Runnable` that runs one of two branches based on a condition.
|
|
85
83
|
|
|
86
84
|
Args:
|
|
87
|
-
*branches: A list of (condition, Runnable) pairs.
|
|
88
|
-
Defaults a Runnable to run if no condition is met.
|
|
85
|
+
*branches: A list of `(condition, Runnable)` pairs.
|
|
86
|
+
Defaults a `Runnable` to run if no condition is met.
|
|
89
87
|
|
|
90
88
|
Raises:
|
|
91
89
|
ValueError: If the number of branches is less than 2.
|
|
92
|
-
TypeError: If the default branch is not Runnable
|
|
90
|
+
TypeError: If the default branch is not `Runnable`, `Callable` or `Mapping`.
|
|
93
91
|
TypeError: If a branch is not a tuple or list.
|
|
94
92
|
ValueError: If a branch is not of length 2.
|
|
95
93
|
"""
|
|
@@ -148,17 +146,15 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
148
146
|
@classmethod
|
|
149
147
|
@override
|
|
150
148
|
def get_lc_namespace(cls) -> list[str]:
|
|
151
|
-
"""Get the namespace of the
|
|
149
|
+
"""Get the namespace of the LangChain object.
|
|
152
150
|
|
|
153
151
|
Returns:
|
|
154
|
-
|
|
152
|
+
`["langchain", "schema", "runnable"]`
|
|
155
153
|
"""
|
|
156
154
|
return ["langchain", "schema", "runnable"]
|
|
157
155
|
|
|
158
156
|
@override
|
|
159
|
-
def get_input_schema(
|
|
160
|
-
self, config: Optional[RunnableConfig] = None
|
|
161
|
-
) -> type[BaseModel]:
|
|
157
|
+
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
162
158
|
runnables = (
|
|
163
159
|
[self.default]
|
|
164
160
|
+ [r for _, r in self.branches]
|
|
@@ -189,14 +185,14 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
189
185
|
|
|
190
186
|
@override
|
|
191
187
|
def invoke(
|
|
192
|
-
self, input: Input, config:
|
|
188
|
+
self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
|
|
193
189
|
) -> Output:
|
|
194
190
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
195
191
|
|
|
196
192
|
Args:
|
|
197
193
|
input: The input to the Runnable.
|
|
198
|
-
config: The configuration for the Runnable.
|
|
199
|
-
kwargs: Additional keyword arguments to pass to the Runnable.
|
|
194
|
+
config: The configuration for the Runnable.
|
|
195
|
+
**kwargs: Additional keyword arguments to pass to the Runnable.
|
|
200
196
|
|
|
201
197
|
Returns:
|
|
202
198
|
The output of the branch that was run.
|
|
@@ -248,7 +244,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
248
244
|
|
|
249
245
|
@override
|
|
250
246
|
async def ainvoke(
|
|
251
|
-
self, input: Input, config:
|
|
247
|
+
self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
|
|
252
248
|
) -> Output:
|
|
253
249
|
config = ensure_config(config)
|
|
254
250
|
callback_manager = get_async_callback_manager_for_config(config)
|
|
@@ -298,15 +294,15 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
298
294
|
def stream(
|
|
299
295
|
self,
|
|
300
296
|
input: Input,
|
|
301
|
-
config:
|
|
302
|
-
**kwargs:
|
|
297
|
+
config: RunnableConfig | None = None,
|
|
298
|
+
**kwargs: Any | None,
|
|
303
299
|
) -> Iterator[Output]:
|
|
304
300
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
305
301
|
|
|
306
302
|
Args:
|
|
307
303
|
input: The input to the Runnable.
|
|
308
|
-
config: The configuration for the Runnable.
|
|
309
|
-
kwargs: Additional keyword arguments to pass to the Runnable.
|
|
304
|
+
config: The configuration for the Runnable.
|
|
305
|
+
**kwargs: Additional keyword arguments to pass to the Runnable.
|
|
310
306
|
|
|
311
307
|
Yields:
|
|
312
308
|
The output of the branch that was run.
|
|
@@ -319,7 +315,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
319
315
|
name=config.get("run_name") or self.get_name(),
|
|
320
316
|
run_id=config.pop("run_id", None),
|
|
321
317
|
)
|
|
322
|
-
final_output:
|
|
318
|
+
final_output: Output | None = None
|
|
323
319
|
final_output_supported = True
|
|
324
320
|
|
|
325
321
|
try:
|
|
@@ -382,15 +378,15 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
382
378
|
async def astream(
|
|
383
379
|
self,
|
|
384
380
|
input: Input,
|
|
385
|
-
config:
|
|
386
|
-
**kwargs:
|
|
381
|
+
config: RunnableConfig | None = None,
|
|
382
|
+
**kwargs: Any | None,
|
|
387
383
|
) -> AsyncIterator[Output]:
|
|
388
384
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
389
385
|
|
|
390
386
|
Args:
|
|
391
387
|
input: The input to the Runnable.
|
|
392
|
-
config: The configuration for the Runnable.
|
|
393
|
-
kwargs: Additional keyword arguments to pass to the Runnable.
|
|
388
|
+
config: The configuration for the Runnable.
|
|
389
|
+
**kwargs: Additional keyword arguments to pass to the Runnable.
|
|
394
390
|
|
|
395
391
|
Yields:
|
|
396
392
|
The output of the branch that was run.
|
|
@@ -403,7 +399,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
403
399
|
name=config.get("run_name") or self.get_name(),
|
|
404
400
|
run_id=config.pop("run_id", None),
|
|
405
401
|
)
|
|
406
|
-
final_output:
|
|
402
|
+
final_output: Output | None = None
|
|
407
403
|
final_output_supported = True
|
|
408
404
|
|
|
409
405
|
try:
|
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import asyncio
|
|
6
6
|
import uuid
|
|
7
7
|
import warnings
|
|
8
|
-
from collections.abc import Awaitable, Generator, Iterable, Iterator, Sequence
|
|
8
|
+
from collections.abc import Awaitable, Callable, Generator, Iterable, Iterator, Sequence
|
|
9
9
|
from concurrent.futures import Executor, Future, ThreadPoolExecutor
|
|
10
10
|
from contextlib import contextmanager
|
|
11
11
|
from contextvars import Context, ContextVar, Token, copy_context
|
|
@@ -13,11 +13,8 @@ from functools import partial
|
|
|
13
13
|
from typing import (
|
|
14
14
|
TYPE_CHECKING,
|
|
15
15
|
Any,
|
|
16
|
-
Callable,
|
|
17
|
-
Optional,
|
|
18
16
|
ParamSpec,
|
|
19
17
|
TypeVar,
|
|
20
|
-
Union,
|
|
21
18
|
cast,
|
|
22
19
|
)
|
|
23
20
|
|
|
@@ -42,7 +39,7 @@ if TYPE_CHECKING:
|
|
|
42
39
|
else:
|
|
43
40
|
# Pydantic validates through typed dicts, but
|
|
44
41
|
# the callbacks need forward refs updated
|
|
45
|
-
Callbacks =
|
|
42
|
+
Callbacks = list | Any | None
|
|
46
43
|
|
|
47
44
|
|
|
48
45
|
class EmptyDict(TypedDict, total=False):
|
|
@@ -75,29 +72,29 @@ class RunnableConfig(TypedDict, total=False):
|
|
|
75
72
|
Name for the tracer run for this call. Defaults to the name of the class.
|
|
76
73
|
"""
|
|
77
74
|
|
|
78
|
-
max_concurrency:
|
|
75
|
+
max_concurrency: int | None
|
|
79
76
|
"""
|
|
80
77
|
Maximum number of parallel calls to make. If not provided, defaults to
|
|
81
|
-
ThreadPoolExecutor's default.
|
|
78
|
+
`ThreadPoolExecutor`'s default.
|
|
82
79
|
"""
|
|
83
80
|
|
|
84
81
|
recursion_limit: int
|
|
85
82
|
"""
|
|
86
|
-
Maximum number of times a call can recurse. If not provided, defaults to 25
|
|
83
|
+
Maximum number of times a call can recurse. If not provided, defaults to `25`.
|
|
87
84
|
"""
|
|
88
85
|
|
|
89
86
|
configurable: dict[str, Any]
|
|
90
87
|
"""
|
|
91
|
-
Runtime values for attributes previously made configurable on this Runnable
|
|
92
|
-
or sub-Runnables, through
|
|
93
|
-
Check
|
|
88
|
+
Runtime values for attributes previously made configurable on this `Runnable`,
|
|
89
|
+
or sub-Runnables, through `configurable_fields` or `configurable_alternatives`.
|
|
90
|
+
Check `output_schema` for a description of the attributes that have been made
|
|
94
91
|
configurable.
|
|
95
92
|
"""
|
|
96
93
|
|
|
97
|
-
run_id:
|
|
94
|
+
run_id: uuid.UUID | None
|
|
98
95
|
"""
|
|
99
96
|
Unique identifier for the tracer run for this call. If not provided, a new UUID
|
|
100
|
-
|
|
97
|
+
will be generated.
|
|
101
98
|
"""
|
|
102
99
|
|
|
103
100
|
|
|
@@ -130,11 +127,11 @@ var_child_runnable_config: ContextVar[RunnableConfig | None] = ContextVar(
|
|
|
130
127
|
# This is imported and used in langgraph, so don't break.
|
|
131
128
|
def _set_config_context(
|
|
132
129
|
config: RunnableConfig,
|
|
133
|
-
) -> tuple[Token[
|
|
130
|
+
) -> tuple[Token[RunnableConfig | None], dict[str, Any] | None]:
|
|
134
131
|
"""Set the child Runnable config + tracing context.
|
|
135
132
|
|
|
136
133
|
Args:
|
|
137
|
-
config
|
|
134
|
+
config: The config to set.
|
|
138
135
|
|
|
139
136
|
Returns:
|
|
140
137
|
The token to reset the config and the previous tracing context.
|
|
@@ -168,7 +165,7 @@ def set_config_context(config: RunnableConfig) -> Generator[Context, None, None]
|
|
|
168
165
|
"""Set the child Runnable config + tracing context.
|
|
169
166
|
|
|
170
167
|
Args:
|
|
171
|
-
config
|
|
168
|
+
config: The config to set.
|
|
172
169
|
|
|
173
170
|
Yields:
|
|
174
171
|
The config context.
|
|
@@ -192,15 +189,14 @@ def set_config_context(config: RunnableConfig) -> Generator[Context, None, None]
|
|
|
192
189
|
)
|
|
193
190
|
|
|
194
191
|
|
|
195
|
-
def ensure_config(config:
|
|
192
|
+
def ensure_config(config: RunnableConfig | None = None) -> RunnableConfig:
|
|
196
193
|
"""Ensure that a config is a dict with all keys present.
|
|
197
194
|
|
|
198
195
|
Args:
|
|
199
|
-
config
|
|
200
|
-
Defaults to None.
|
|
196
|
+
config: The config to ensure.
|
|
201
197
|
|
|
202
198
|
Returns:
|
|
203
|
-
|
|
199
|
+
The ensured config.
|
|
204
200
|
"""
|
|
205
201
|
empty = RunnableConfig(
|
|
206
202
|
tags=[],
|
|
@@ -247,19 +243,18 @@ def ensure_config(config: Optional[RunnableConfig] = None) -> RunnableConfig:
|
|
|
247
243
|
|
|
248
244
|
|
|
249
245
|
def get_config_list(
|
|
250
|
-
config:
|
|
246
|
+
config: RunnableConfig | Sequence[RunnableConfig] | None, length: int
|
|
251
247
|
) -> list[RunnableConfig]:
|
|
252
248
|
"""Get a list of configs from a single config or a list of configs.
|
|
253
249
|
|
|
254
250
|
It is useful for subclasses overriding batch() or abatch().
|
|
255
251
|
|
|
256
252
|
Args:
|
|
257
|
-
config
|
|
258
|
-
|
|
259
|
-
length (int): The length of the list.
|
|
253
|
+
config: The config or list of configs.
|
|
254
|
+
length: The length of the list.
|
|
260
255
|
|
|
261
256
|
Returns:
|
|
262
|
-
|
|
257
|
+
The list of configs.
|
|
263
258
|
|
|
264
259
|
Raises:
|
|
265
260
|
ValueError: If the length of the list is not equal to the length of the inputs.
|
|
@@ -294,30 +289,26 @@ def get_config_list(
|
|
|
294
289
|
|
|
295
290
|
|
|
296
291
|
def patch_config(
|
|
297
|
-
config:
|
|
292
|
+
config: RunnableConfig | None,
|
|
298
293
|
*,
|
|
299
|
-
callbacks:
|
|
300
|
-
recursion_limit:
|
|
301
|
-
max_concurrency:
|
|
302
|
-
run_name:
|
|
303
|
-
configurable:
|
|
294
|
+
callbacks: BaseCallbackManager | None = None,
|
|
295
|
+
recursion_limit: int | None = None,
|
|
296
|
+
max_concurrency: int | None = None,
|
|
297
|
+
run_name: str | None = None,
|
|
298
|
+
configurable: dict[str, Any] | None = None,
|
|
304
299
|
) -> RunnableConfig:
|
|
305
300
|
"""Patch a config with new values.
|
|
306
301
|
|
|
307
302
|
Args:
|
|
308
|
-
config
|
|
309
|
-
callbacks
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
Defaults to None.
|
|
315
|
-
run_name (Optional[str], optional): The run name to set. Defaults to None.
|
|
316
|
-
configurable (Optional[dict[str, Any]], optional): The configurable to set.
|
|
317
|
-
Defaults to None.
|
|
303
|
+
config: The config to patch.
|
|
304
|
+
callbacks: The callbacks to set.
|
|
305
|
+
recursion_limit: The recursion limit to set.
|
|
306
|
+
max_concurrency: The max concurrency to set.
|
|
307
|
+
run_name: The run name to set.
|
|
308
|
+
configurable: The configurable to set.
|
|
318
309
|
|
|
319
310
|
Returns:
|
|
320
|
-
|
|
311
|
+
The patched config.
|
|
321
312
|
"""
|
|
322
313
|
config = ensure_config(config)
|
|
323
314
|
if callbacks is not None:
|
|
@@ -339,14 +330,14 @@ def patch_config(
|
|
|
339
330
|
return config
|
|
340
331
|
|
|
341
332
|
|
|
342
|
-
def merge_configs(*configs:
|
|
333
|
+
def merge_configs(*configs: RunnableConfig | None) -> RunnableConfig:
|
|
343
334
|
"""Merge multiple configs into one.
|
|
344
335
|
|
|
345
336
|
Args:
|
|
346
|
-
*configs
|
|
337
|
+
*configs: The configs to merge.
|
|
347
338
|
|
|
348
339
|
Returns:
|
|
349
|
-
|
|
340
|
+
The merged config.
|
|
350
341
|
"""
|
|
351
342
|
base: RunnableConfig = {}
|
|
352
343
|
# Even though the keys aren't literals, this is correct
|
|
@@ -406,15 +397,13 @@ def merge_configs(*configs: Optional[RunnableConfig]) -> RunnableConfig:
|
|
|
406
397
|
|
|
407
398
|
|
|
408
399
|
def call_func_with_variable_args(
|
|
409
|
-
func:
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
Callable[[Input, CallbackManagerForChainRun, RunnableConfig], Output],
|
|
414
|
-
],
|
|
400
|
+
func: Callable[[Input], Output]
|
|
401
|
+
| Callable[[Input, RunnableConfig], Output]
|
|
402
|
+
| Callable[[Input, CallbackManagerForChainRun], Output]
|
|
403
|
+
| Callable[[Input, CallbackManagerForChainRun, RunnableConfig], Output],
|
|
415
404
|
input: Input,
|
|
416
405
|
config: RunnableConfig,
|
|
417
|
-
run_manager:
|
|
406
|
+
run_manager: CallbackManagerForChainRun | None = None,
|
|
418
407
|
**kwargs: Any,
|
|
419
408
|
) -> Output:
|
|
420
409
|
"""Call function that may optionally accept a run_manager and/or config.
|
|
@@ -423,7 +412,7 @@ def call_func_with_variable_args(
|
|
|
423
412
|
func: The function to call.
|
|
424
413
|
input: The input to the function.
|
|
425
414
|
config: The config to pass to the function.
|
|
426
|
-
run_manager: The run manager to pass to the function.
|
|
415
|
+
run_manager: The run manager to pass to the function.
|
|
427
416
|
**kwargs: The keyword arguments to pass to the function.
|
|
428
417
|
|
|
429
418
|
Returns:
|
|
@@ -440,18 +429,15 @@ def call_func_with_variable_args(
|
|
|
440
429
|
|
|
441
430
|
|
|
442
431
|
def acall_func_with_variable_args(
|
|
443
|
-
func:
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
[Input, AsyncCallbackManagerForChainRun, RunnableConfig],
|
|
449
|
-
Awaitable[Output],
|
|
450
|
-
],
|
|
432
|
+
func: Callable[[Input], Awaitable[Output]]
|
|
433
|
+
| Callable[[Input, RunnableConfig], Awaitable[Output]]
|
|
434
|
+
| Callable[[Input, AsyncCallbackManagerForChainRun], Awaitable[Output]]
|
|
435
|
+
| Callable[
|
|
436
|
+
[Input, AsyncCallbackManagerForChainRun, RunnableConfig], Awaitable[Output]
|
|
451
437
|
],
|
|
452
438
|
input: Input,
|
|
453
439
|
config: RunnableConfig,
|
|
454
|
-
run_manager:
|
|
440
|
+
run_manager: AsyncCallbackManagerForChainRun | None = None,
|
|
455
441
|
**kwargs: Any,
|
|
456
442
|
) -> Awaitable[Output]:
|
|
457
443
|
"""Async call function that may optionally accept a run_manager and/or config.
|
|
@@ -460,7 +446,7 @@ def acall_func_with_variable_args(
|
|
|
460
446
|
func: The function to call.
|
|
461
447
|
input: The input to the function.
|
|
462
448
|
config: The config to pass to the function.
|
|
463
|
-
run_manager: The run manager to pass to the function.
|
|
449
|
+
run_manager: The run manager to pass to the function.
|
|
464
450
|
**kwargs: The keyword arguments to pass to the function.
|
|
465
451
|
|
|
466
452
|
Returns:
|
|
@@ -480,10 +466,10 @@ def get_callback_manager_for_config(config: RunnableConfig) -> CallbackManager:
|
|
|
480
466
|
"""Get a callback manager for a config.
|
|
481
467
|
|
|
482
468
|
Args:
|
|
483
|
-
config
|
|
469
|
+
config: The config.
|
|
484
470
|
|
|
485
471
|
Returns:
|
|
486
|
-
|
|
472
|
+
The callback manager.
|
|
487
473
|
"""
|
|
488
474
|
return CallbackManager.configure(
|
|
489
475
|
inheritable_callbacks=config.get("callbacks"),
|
|
@@ -498,10 +484,10 @@ def get_async_callback_manager_for_config(
|
|
|
498
484
|
"""Get an async callback manager for a config.
|
|
499
485
|
|
|
500
486
|
Args:
|
|
501
|
-
config
|
|
487
|
+
config: The config.
|
|
502
488
|
|
|
503
489
|
Returns:
|
|
504
|
-
|
|
490
|
+
The async callback manager.
|
|
505
491
|
"""
|
|
506
492
|
return AsyncCallbackManager.configure(
|
|
507
493
|
inheritable_callbacks=config.get("callbacks"),
|
|
@@ -526,12 +512,12 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor):
|
|
|
526
512
|
"""Submit a function to the executor.
|
|
527
513
|
|
|
528
514
|
Args:
|
|
529
|
-
func
|
|
530
|
-
*args
|
|
531
|
-
**kwargs
|
|
515
|
+
func: The function to submit.
|
|
516
|
+
*args: The positional arguments to the function.
|
|
517
|
+
**kwargs: The keyword arguments to the function.
|
|
532
518
|
|
|
533
519
|
Returns:
|
|
534
|
-
|
|
520
|
+
The future for the function.
|
|
535
521
|
"""
|
|
536
522
|
return super().submit(
|
|
537
523
|
cast("Callable[..., T]", partial(copy_context().run, func, *args, **kwargs))
|
|
@@ -541,20 +527,18 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor):
|
|
|
541
527
|
self,
|
|
542
528
|
fn: Callable[..., T],
|
|
543
529
|
*iterables: Iterable[Any],
|
|
544
|
-
|
|
545
|
-
chunksize: int = 1,
|
|
530
|
+
**kwargs: Any,
|
|
546
531
|
) -> Iterator[T]:
|
|
547
532
|
"""Map a function to multiple iterables.
|
|
548
533
|
|
|
549
534
|
Args:
|
|
550
|
-
fn
|
|
551
|
-
*iterables
|
|
552
|
-
timeout
|
|
553
|
-
|
|
554
|
-
chunksize (int, optional): The chunksize for the map. Defaults to 1.
|
|
535
|
+
fn: The function to map.
|
|
536
|
+
*iterables: The iterables to map over.
|
|
537
|
+
timeout: The timeout for the map.
|
|
538
|
+
chunksize: The chunksize for the map.
|
|
555
539
|
|
|
556
540
|
Returns:
|
|
557
|
-
|
|
541
|
+
The iterator for the mapped function.
|
|
558
542
|
"""
|
|
559
543
|
contexts = [copy_context() for _ in range(len(iterables[0]))] # type: ignore[arg-type]
|
|
560
544
|
|
|
@@ -564,22 +548,21 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor):
|
|
|
564
548
|
return super().map(
|
|
565
549
|
_wrapped_fn,
|
|
566
550
|
*iterables,
|
|
567
|
-
|
|
568
|
-
chunksize=chunksize,
|
|
551
|
+
**kwargs,
|
|
569
552
|
)
|
|
570
553
|
|
|
571
554
|
|
|
572
555
|
@contextmanager
|
|
573
556
|
def get_executor_for_config(
|
|
574
|
-
config:
|
|
557
|
+
config: RunnableConfig | None,
|
|
575
558
|
) -> Generator[Executor, None, None]:
|
|
576
559
|
"""Get an executor for a config.
|
|
577
560
|
|
|
578
561
|
Args:
|
|
579
|
-
config
|
|
562
|
+
config: The config.
|
|
580
563
|
|
|
581
564
|
Yields:
|
|
582
|
-
|
|
565
|
+
The executor.
|
|
583
566
|
"""
|
|
584
567
|
config = config or {}
|
|
585
568
|
with ContextThreadPoolExecutor(
|
|
@@ -589,7 +572,7 @@ def get_executor_for_config(
|
|
|
589
572
|
|
|
590
573
|
|
|
591
574
|
async def run_in_executor(
|
|
592
|
-
executor_or_config:
|
|
575
|
+
executor_or_config: Executor | RunnableConfig | None,
|
|
593
576
|
func: Callable[P, T],
|
|
594
577
|
*args: P.args,
|
|
595
578
|
**kwargs: P.kwargs,
|
|
@@ -598,12 +581,12 @@ async def run_in_executor(
|
|
|
598
581
|
|
|
599
582
|
Args:
|
|
600
583
|
executor_or_config: The executor or config to run in.
|
|
601
|
-
func
|
|
602
|
-
*args
|
|
603
|
-
**kwargs
|
|
584
|
+
func: The function.
|
|
585
|
+
*args: The positional arguments to the function.
|
|
586
|
+
**kwargs: The keyword arguments to the function.
|
|
604
587
|
|
|
605
588
|
Returns:
|
|
606
|
-
|
|
589
|
+
The output of the function.
|
|
607
590
|
"""
|
|
608
591
|
|
|
609
592
|
def wrapper() -> T:
|