langchain-core 0.3.79__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +52 -65
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +19 -19
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +323 -334
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +441 -507
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +48 -63
- langchain_core/document_loaders/base.py +23 -23
- langchain_core/document_loaders/langsmith.py +37 -37
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +62 -65
- langchain_core/documents/compressor.py +4 -4
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +10 -11
- langchain_core/globals.py +3 -151
- langchain_core/indexing/api.py +61 -66
- langchain_core/indexing/base.py +58 -58
- langchain_core/indexing/in_memory.py +3 -3
- langchain_core/language_models/__init__.py +14 -27
- langchain_core/language_models/_utils.py +270 -84
- langchain_core/language_models/base.py +55 -162
- langchain_core/language_models/chat_models.py +442 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +61 -39
- langchain_core/language_models/llms.py +123 -231
- langchain_core/load/dump.py +4 -5
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +39 -40
- langchain_core/messages/__init__.py +61 -22
- langchain_core/messages/ai.py +368 -163
- langchain_core/messages/base.py +214 -43
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +41 -38
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +38 -28
- langchain_core/messages/tool.py +96 -103
- langchain_core/messages/utils.py +478 -504
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +58 -61
- langchain_core/output_parsers/json.py +7 -8
- langchain_core/output_parsers/list.py +5 -7
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +14 -19
- langchain_core/output_parsers/pydantic.py +12 -13
- langchain_core/output_parsers/string.py +2 -2
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +8 -10
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +8 -8
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +12 -12
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +45 -55
- langchain_core/prompts/chat.py +254 -313
- langchain_core/prompts/dict.py +5 -5
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +4 -4
- langchain_core/prompts/structured.py +42 -50
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +49 -190
- langchain_core/runnables/base.py +1484 -1709
- langchain_core/runnables/branch.py +45 -61
- langchain_core/runnables/config.py +80 -88
- langchain_core/runnables/configurable.py +117 -134
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +85 -95
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +38 -50
- langchain_core/runnables/graph_png.py +15 -16
- langchain_core/runnables/history.py +135 -148
- langchain_core/runnables/passthrough.py +124 -150
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +25 -30
- langchain_core/runnables/schema.py +79 -74
- langchain_core/runnables/utils.py +62 -68
- langchain_core/stores.py +81 -115
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +179 -187
- langchain_core/tools/convert.py +131 -139
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +11 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +30 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +50 -60
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +115 -70
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +43 -43
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +46 -8
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -191
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +37 -42
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +8 -8
- langchain_core/utils/mustache.py +9 -11
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +80 -54
- langchain_core/vectorstores/base.py +129 -164
- langchain_core/vectorstores/in_memory.py +99 -174
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -447
- langchain_core/memory.py +0 -120
- langchain_core/messages/content_blocks.py +0 -176
- langchain_core/prompts/pipeline.py +0 -138
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-0.3.79.dist-info/RECORD +0 -174
- langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
|
@@ -1,21 +1,21 @@
|
|
|
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
|
|
|
12
16
|
from pydantic import BaseModel, ConfigDict
|
|
13
17
|
from typing_extensions import override
|
|
14
18
|
|
|
15
|
-
from langchain_core.beta.runnables.context import (
|
|
16
|
-
CONTEXT_CONFIG_PREFIX,
|
|
17
|
-
CONTEXT_CONFIG_SUFFIX_SET,
|
|
18
|
-
)
|
|
19
19
|
from langchain_core.runnables.base import (
|
|
20
20
|
Runnable,
|
|
21
21
|
RunnableLike,
|
|
@@ -49,21 +49,19 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
49
49
|
If no condition evaluates to True, the default branch is run on the input.
|
|
50
50
|
|
|
51
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
|
+
)
|
|
52
61
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
branch = RunnableBranch(
|
|
58
|
-
(lambda x: isinstance(x, str), lambda x: x.upper()),
|
|
59
|
-
(lambda x: isinstance(x, int), lambda x: x + 1),
|
|
60
|
-
(lambda x: isinstance(x, float), lambda x: x * 2),
|
|
61
|
-
lambda x: "goodbye",
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
branch.invoke("hello") # "HELLO"
|
|
65
|
-
branch.invoke(None) # "goodbye"
|
|
66
|
-
|
|
62
|
+
branch.invoke("hello") # "HELLO"
|
|
63
|
+
branch.invoke(None) # "goodbye"
|
|
64
|
+
```
|
|
67
65
|
"""
|
|
68
66
|
|
|
69
67
|
branches: Sequence[tuple[Runnable[Input, bool], Runnable[Input, Output]]]
|
|
@@ -73,17 +71,13 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
73
71
|
|
|
74
72
|
def __init__(
|
|
75
73
|
self,
|
|
76
|
-
*branches:
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
RunnableLike,
|
|
84
|
-
],
|
|
85
|
-
RunnableLike, # To accommodate the default branch
|
|
86
|
-
],
|
|
74
|
+
*branches: tuple[
|
|
75
|
+
Runnable[Input, bool]
|
|
76
|
+
| Callable[[Input], bool]
|
|
77
|
+
| Callable[[Input], Awaitable[bool]],
|
|
78
|
+
RunnableLike,
|
|
79
|
+
]
|
|
80
|
+
| RunnableLike,
|
|
87
81
|
) -> None:
|
|
88
82
|
"""A Runnable that runs one of two branches based on a condition.
|
|
89
83
|
|
|
@@ -152,17 +146,15 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
152
146
|
@classmethod
|
|
153
147
|
@override
|
|
154
148
|
def get_lc_namespace(cls) -> list[str]:
|
|
155
|
-
"""Get the namespace of the
|
|
149
|
+
"""Get the namespace of the LangChain object.
|
|
156
150
|
|
|
157
151
|
Returns:
|
|
158
|
-
|
|
152
|
+
`["langchain", "schema", "runnable"]`
|
|
159
153
|
"""
|
|
160
154
|
return ["langchain", "schema", "runnable"]
|
|
161
155
|
|
|
162
156
|
@override
|
|
163
|
-
def get_input_schema(
|
|
164
|
-
self, config: Optional[RunnableConfig] = None
|
|
165
|
-
) -> type[BaseModel]:
|
|
157
|
+
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
166
158
|
runnables = (
|
|
167
159
|
[self.default]
|
|
168
160
|
+ [r for _, r in self.branches]
|
|
@@ -181,7 +173,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
181
173
|
@property
|
|
182
174
|
@override
|
|
183
175
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
184
|
-
|
|
176
|
+
return get_unique_config_specs(
|
|
185
177
|
spec
|
|
186
178
|
for step in (
|
|
187
179
|
[self.default]
|
|
@@ -190,25 +182,17 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
190
182
|
)
|
|
191
183
|
for spec in step.config_specs
|
|
192
184
|
)
|
|
193
|
-
if any(
|
|
194
|
-
s.id.startswith(CONTEXT_CONFIG_PREFIX)
|
|
195
|
-
and s.id.endswith(CONTEXT_CONFIG_SUFFIX_SET)
|
|
196
|
-
for s in specs
|
|
197
|
-
):
|
|
198
|
-
msg = "RunnableBranch cannot contain context setters."
|
|
199
|
-
raise ValueError(msg)
|
|
200
|
-
return specs
|
|
201
185
|
|
|
202
186
|
@override
|
|
203
187
|
def invoke(
|
|
204
|
-
self, input: Input, config:
|
|
188
|
+
self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
|
|
205
189
|
) -> Output:
|
|
206
190
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
207
191
|
|
|
208
192
|
Args:
|
|
209
193
|
input: The input to the Runnable.
|
|
210
|
-
config: The configuration for the Runnable.
|
|
211
|
-
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.
|
|
212
196
|
|
|
213
197
|
Returns:
|
|
214
198
|
The output of the branch that was run.
|
|
@@ -260,7 +244,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
260
244
|
|
|
261
245
|
@override
|
|
262
246
|
async def ainvoke(
|
|
263
|
-
self, input: Input, config:
|
|
247
|
+
self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
|
|
264
248
|
) -> Output:
|
|
265
249
|
config = ensure_config(config)
|
|
266
250
|
callback_manager = get_async_callback_manager_for_config(config)
|
|
@@ -310,15 +294,15 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
310
294
|
def stream(
|
|
311
295
|
self,
|
|
312
296
|
input: Input,
|
|
313
|
-
config:
|
|
314
|
-
**kwargs:
|
|
297
|
+
config: RunnableConfig | None = None,
|
|
298
|
+
**kwargs: Any | None,
|
|
315
299
|
) -> Iterator[Output]:
|
|
316
300
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
317
301
|
|
|
318
302
|
Args:
|
|
319
303
|
input: The input to the Runnable.
|
|
320
|
-
config: The configuration for the Runnable.
|
|
321
|
-
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.
|
|
322
306
|
|
|
323
307
|
Yields:
|
|
324
308
|
The output of the branch that was run.
|
|
@@ -331,7 +315,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
331
315
|
name=config.get("run_name") or self.get_name(),
|
|
332
316
|
run_id=config.pop("run_id", None),
|
|
333
317
|
)
|
|
334
|
-
final_output:
|
|
318
|
+
final_output: Output | None = None
|
|
335
319
|
final_output_supported = True
|
|
336
320
|
|
|
337
321
|
try:
|
|
@@ -394,15 +378,15 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
394
378
|
async def astream(
|
|
395
379
|
self,
|
|
396
380
|
input: Input,
|
|
397
|
-
config:
|
|
398
|
-
**kwargs:
|
|
381
|
+
config: RunnableConfig | None = None,
|
|
382
|
+
**kwargs: Any | None,
|
|
399
383
|
) -> AsyncIterator[Output]:
|
|
400
384
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
401
385
|
|
|
402
386
|
Args:
|
|
403
387
|
input: The input to the Runnable.
|
|
404
|
-
config: The configuration for the Runnable.
|
|
405
|
-
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.
|
|
406
390
|
|
|
407
391
|
Yields:
|
|
408
392
|
The output of the branch that was run.
|
|
@@ -415,7 +399,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
415
399
|
name=config.get("run_name") or self.get_name(),
|
|
416
400
|
run_id=config.pop("run_id", None),
|
|
417
401
|
)
|
|
418
|
-
final_output:
|
|
402
|
+
final_output: Output | None = None
|
|
419
403
|
final_output_supported = True
|
|
420
404
|
|
|
421
405
|
try:
|
|
@@ -5,15 +5,21 @@ 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
|
|
12
12
|
from functools import partial
|
|
13
|
-
from typing import
|
|
13
|
+
from typing import (
|
|
14
|
+
TYPE_CHECKING,
|
|
15
|
+
Any,
|
|
16
|
+
ParamSpec,
|
|
17
|
+
TypeVar,
|
|
18
|
+
cast,
|
|
19
|
+
)
|
|
14
20
|
|
|
15
21
|
from langsmith.run_helpers import _set_tracing_context, get_tracing_context
|
|
16
|
-
from typing_extensions import
|
|
22
|
+
from typing_extensions import TypedDict
|
|
17
23
|
|
|
18
24
|
from langchain_core.callbacks.manager import AsyncCallbackManager, CallbackManager
|
|
19
25
|
from langchain_core.runnables.utils import (
|
|
@@ -33,7 +39,7 @@ if TYPE_CHECKING:
|
|
|
33
39
|
else:
|
|
34
40
|
# Pydantic validates through typed dicts, but
|
|
35
41
|
# the callbacks need forward refs updated
|
|
36
|
-
Callbacks =
|
|
42
|
+
Callbacks = list | Any | None
|
|
37
43
|
|
|
38
44
|
|
|
39
45
|
class EmptyDict(TypedDict, total=False):
|
|
@@ -66,29 +72,29 @@ class RunnableConfig(TypedDict, total=False):
|
|
|
66
72
|
Name for the tracer run for this call. Defaults to the name of the class.
|
|
67
73
|
"""
|
|
68
74
|
|
|
69
|
-
max_concurrency:
|
|
75
|
+
max_concurrency: int | None
|
|
70
76
|
"""
|
|
71
77
|
Maximum number of parallel calls to make. If not provided, defaults to
|
|
72
|
-
ThreadPoolExecutor's default.
|
|
78
|
+
`ThreadPoolExecutor`'s default.
|
|
73
79
|
"""
|
|
74
80
|
|
|
75
81
|
recursion_limit: int
|
|
76
82
|
"""
|
|
77
|
-
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`.
|
|
78
84
|
"""
|
|
79
85
|
|
|
80
86
|
configurable: dict[str, Any]
|
|
81
87
|
"""
|
|
82
|
-
Runtime values for attributes previously made configurable on this Runnable
|
|
83
|
-
or sub-Runnables, through
|
|
84
|
-
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
|
|
85
91
|
configurable.
|
|
86
92
|
"""
|
|
87
93
|
|
|
88
|
-
run_id:
|
|
94
|
+
run_id: uuid.UUID | None
|
|
89
95
|
"""
|
|
90
96
|
Unique identifier for the tracer run for this call. If not provided, a new UUID
|
|
91
|
-
|
|
97
|
+
will be generated.
|
|
92
98
|
"""
|
|
93
99
|
|
|
94
100
|
|
|
@@ -121,11 +127,11 @@ var_child_runnable_config: ContextVar[RunnableConfig | None] = ContextVar(
|
|
|
121
127
|
# This is imported and used in langgraph, so don't break.
|
|
122
128
|
def _set_config_context(
|
|
123
129
|
config: RunnableConfig,
|
|
124
|
-
) -> tuple[Token[
|
|
130
|
+
) -> tuple[Token[RunnableConfig | None], dict[str, Any] | None]:
|
|
125
131
|
"""Set the child Runnable config + tracing context.
|
|
126
132
|
|
|
127
133
|
Args:
|
|
128
|
-
config
|
|
134
|
+
config: The config to set.
|
|
129
135
|
|
|
130
136
|
Returns:
|
|
131
137
|
The token to reset the config and the previous tracing context.
|
|
@@ -159,7 +165,7 @@ def set_config_context(config: RunnableConfig) -> Generator[Context, None, None]
|
|
|
159
165
|
"""Set the child Runnable config + tracing context.
|
|
160
166
|
|
|
161
167
|
Args:
|
|
162
|
-
config
|
|
168
|
+
config: The config to set.
|
|
163
169
|
|
|
164
170
|
Yields:
|
|
165
171
|
The config context.
|
|
@@ -183,15 +189,14 @@ def set_config_context(config: RunnableConfig) -> Generator[Context, None, None]
|
|
|
183
189
|
)
|
|
184
190
|
|
|
185
191
|
|
|
186
|
-
def ensure_config(config:
|
|
192
|
+
def ensure_config(config: RunnableConfig | None = None) -> RunnableConfig:
|
|
187
193
|
"""Ensure that a config is a dict with all keys present.
|
|
188
194
|
|
|
189
195
|
Args:
|
|
190
|
-
config
|
|
191
|
-
Defaults to None.
|
|
196
|
+
config: The config to ensure.
|
|
192
197
|
|
|
193
198
|
Returns:
|
|
194
|
-
|
|
199
|
+
The ensured config.
|
|
195
200
|
"""
|
|
196
201
|
empty = RunnableConfig(
|
|
197
202
|
tags=[],
|
|
@@ -238,19 +243,18 @@ def ensure_config(config: Optional[RunnableConfig] = None) -> RunnableConfig:
|
|
|
238
243
|
|
|
239
244
|
|
|
240
245
|
def get_config_list(
|
|
241
|
-
config:
|
|
246
|
+
config: RunnableConfig | Sequence[RunnableConfig] | None, length: int
|
|
242
247
|
) -> list[RunnableConfig]:
|
|
243
248
|
"""Get a list of configs from a single config or a list of configs.
|
|
244
249
|
|
|
245
250
|
It is useful for subclasses overriding batch() or abatch().
|
|
246
251
|
|
|
247
252
|
Args:
|
|
248
|
-
config
|
|
249
|
-
|
|
250
|
-
length (int): The length of the list.
|
|
253
|
+
config: The config or list of configs.
|
|
254
|
+
length: The length of the list.
|
|
251
255
|
|
|
252
256
|
Returns:
|
|
253
|
-
|
|
257
|
+
The list of configs.
|
|
254
258
|
|
|
255
259
|
Raises:
|
|
256
260
|
ValueError: If the length of the list is not equal to the length of the inputs.
|
|
@@ -285,30 +289,26 @@ def get_config_list(
|
|
|
285
289
|
|
|
286
290
|
|
|
287
291
|
def patch_config(
|
|
288
|
-
config:
|
|
292
|
+
config: RunnableConfig | None,
|
|
289
293
|
*,
|
|
290
|
-
callbacks:
|
|
291
|
-
recursion_limit:
|
|
292
|
-
max_concurrency:
|
|
293
|
-
run_name:
|
|
294
|
-
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,
|
|
295
299
|
) -> RunnableConfig:
|
|
296
300
|
"""Patch a config with new values.
|
|
297
301
|
|
|
298
302
|
Args:
|
|
299
|
-
config
|
|
300
|
-
callbacks
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
Defaults to None.
|
|
306
|
-
run_name (Optional[str], optional): The run name to set. Defaults to None.
|
|
307
|
-
configurable (Optional[dict[str, Any]], optional): The configurable to set.
|
|
308
|
-
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.
|
|
309
309
|
|
|
310
310
|
Returns:
|
|
311
|
-
|
|
311
|
+
The patched config.
|
|
312
312
|
"""
|
|
313
313
|
config = ensure_config(config)
|
|
314
314
|
if callbacks is not None:
|
|
@@ -330,14 +330,14 @@ def patch_config(
|
|
|
330
330
|
return config
|
|
331
331
|
|
|
332
332
|
|
|
333
|
-
def merge_configs(*configs:
|
|
333
|
+
def merge_configs(*configs: RunnableConfig | None) -> RunnableConfig:
|
|
334
334
|
"""Merge multiple configs into one.
|
|
335
335
|
|
|
336
336
|
Args:
|
|
337
|
-
*configs
|
|
337
|
+
*configs: The configs to merge.
|
|
338
338
|
|
|
339
339
|
Returns:
|
|
340
|
-
|
|
340
|
+
The merged config.
|
|
341
341
|
"""
|
|
342
342
|
base: RunnableConfig = {}
|
|
343
343
|
# Even though the keys aren't literals, this is correct
|
|
@@ -397,15 +397,13 @@ def merge_configs(*configs: Optional[RunnableConfig]) -> RunnableConfig:
|
|
|
397
397
|
|
|
398
398
|
|
|
399
399
|
def call_func_with_variable_args(
|
|
400
|
-
func:
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
Callable[[Input, CallbackManagerForChainRun, RunnableConfig], Output],
|
|
405
|
-
],
|
|
400
|
+
func: Callable[[Input], Output]
|
|
401
|
+
| Callable[[Input, RunnableConfig], Output]
|
|
402
|
+
| Callable[[Input, CallbackManagerForChainRun], Output]
|
|
403
|
+
| Callable[[Input, CallbackManagerForChainRun, RunnableConfig], Output],
|
|
406
404
|
input: Input,
|
|
407
405
|
config: RunnableConfig,
|
|
408
|
-
run_manager:
|
|
406
|
+
run_manager: CallbackManagerForChainRun | None = None,
|
|
409
407
|
**kwargs: Any,
|
|
410
408
|
) -> Output:
|
|
411
409
|
"""Call function that may optionally accept a run_manager and/or config.
|
|
@@ -414,7 +412,7 @@ def call_func_with_variable_args(
|
|
|
414
412
|
func: The function to call.
|
|
415
413
|
input: The input to the function.
|
|
416
414
|
config: The config to pass to the function.
|
|
417
|
-
run_manager: The run manager to pass to the function.
|
|
415
|
+
run_manager: The run manager to pass to the function.
|
|
418
416
|
**kwargs: The keyword arguments to pass to the function.
|
|
419
417
|
|
|
420
418
|
Returns:
|
|
@@ -431,18 +429,15 @@ def call_func_with_variable_args(
|
|
|
431
429
|
|
|
432
430
|
|
|
433
431
|
def acall_func_with_variable_args(
|
|
434
|
-
func:
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
[Input, AsyncCallbackManagerForChainRun, RunnableConfig],
|
|
440
|
-
Awaitable[Output],
|
|
441
|
-
],
|
|
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]
|
|
442
437
|
],
|
|
443
438
|
input: Input,
|
|
444
439
|
config: RunnableConfig,
|
|
445
|
-
run_manager:
|
|
440
|
+
run_manager: AsyncCallbackManagerForChainRun | None = None,
|
|
446
441
|
**kwargs: Any,
|
|
447
442
|
) -> Awaitable[Output]:
|
|
448
443
|
"""Async call function that may optionally accept a run_manager and/or config.
|
|
@@ -451,7 +446,7 @@ def acall_func_with_variable_args(
|
|
|
451
446
|
func: The function to call.
|
|
452
447
|
input: The input to the function.
|
|
453
448
|
config: The config to pass to the function.
|
|
454
|
-
run_manager: The run manager to pass to the function.
|
|
449
|
+
run_manager: The run manager to pass to the function.
|
|
455
450
|
**kwargs: The keyword arguments to pass to the function.
|
|
456
451
|
|
|
457
452
|
Returns:
|
|
@@ -471,10 +466,10 @@ def get_callback_manager_for_config(config: RunnableConfig) -> CallbackManager:
|
|
|
471
466
|
"""Get a callback manager for a config.
|
|
472
467
|
|
|
473
468
|
Args:
|
|
474
|
-
config
|
|
469
|
+
config: The config.
|
|
475
470
|
|
|
476
471
|
Returns:
|
|
477
|
-
|
|
472
|
+
The callback manager.
|
|
478
473
|
"""
|
|
479
474
|
return CallbackManager.configure(
|
|
480
475
|
inheritable_callbacks=config.get("callbacks"),
|
|
@@ -489,10 +484,10 @@ def get_async_callback_manager_for_config(
|
|
|
489
484
|
"""Get an async callback manager for a config.
|
|
490
485
|
|
|
491
486
|
Args:
|
|
492
|
-
config
|
|
487
|
+
config: The config.
|
|
493
488
|
|
|
494
489
|
Returns:
|
|
495
|
-
|
|
490
|
+
The async callback manager.
|
|
496
491
|
"""
|
|
497
492
|
return AsyncCallbackManager.configure(
|
|
498
493
|
inheritable_callbacks=config.get("callbacks"),
|
|
@@ -517,12 +512,12 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor):
|
|
|
517
512
|
"""Submit a function to the executor.
|
|
518
513
|
|
|
519
514
|
Args:
|
|
520
|
-
func
|
|
521
|
-
*args
|
|
522
|
-
**kwargs
|
|
515
|
+
func: The function to submit.
|
|
516
|
+
*args: The positional arguments to the function.
|
|
517
|
+
**kwargs: The keyword arguments to the function.
|
|
523
518
|
|
|
524
519
|
Returns:
|
|
525
|
-
|
|
520
|
+
The future for the function.
|
|
526
521
|
"""
|
|
527
522
|
return super().submit(
|
|
528
523
|
cast("Callable[..., T]", partial(copy_context().run, func, *args, **kwargs))
|
|
@@ -532,20 +527,18 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor):
|
|
|
532
527
|
self,
|
|
533
528
|
fn: Callable[..., T],
|
|
534
529
|
*iterables: Iterable[Any],
|
|
535
|
-
|
|
536
|
-
chunksize: int = 1,
|
|
530
|
+
**kwargs: Any,
|
|
537
531
|
) -> Iterator[T]:
|
|
538
532
|
"""Map a function to multiple iterables.
|
|
539
533
|
|
|
540
534
|
Args:
|
|
541
|
-
fn
|
|
542
|
-
*iterables
|
|
543
|
-
timeout
|
|
544
|
-
|
|
545
|
-
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.
|
|
546
539
|
|
|
547
540
|
Returns:
|
|
548
|
-
|
|
541
|
+
The iterator for the mapped function.
|
|
549
542
|
"""
|
|
550
543
|
contexts = [copy_context() for _ in range(len(iterables[0]))] # type: ignore[arg-type]
|
|
551
544
|
|
|
@@ -555,22 +548,21 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor):
|
|
|
555
548
|
return super().map(
|
|
556
549
|
_wrapped_fn,
|
|
557
550
|
*iterables,
|
|
558
|
-
|
|
559
|
-
chunksize=chunksize,
|
|
551
|
+
**kwargs,
|
|
560
552
|
)
|
|
561
553
|
|
|
562
554
|
|
|
563
555
|
@contextmanager
|
|
564
556
|
def get_executor_for_config(
|
|
565
|
-
config:
|
|
557
|
+
config: RunnableConfig | None,
|
|
566
558
|
) -> Generator[Executor, None, None]:
|
|
567
559
|
"""Get an executor for a config.
|
|
568
560
|
|
|
569
561
|
Args:
|
|
570
|
-
config
|
|
562
|
+
config: The config.
|
|
571
563
|
|
|
572
564
|
Yields:
|
|
573
|
-
|
|
565
|
+
The executor.
|
|
574
566
|
"""
|
|
575
567
|
config = config or {}
|
|
576
568
|
with ContextThreadPoolExecutor(
|
|
@@ -580,7 +572,7 @@ def get_executor_for_config(
|
|
|
580
572
|
|
|
581
573
|
|
|
582
574
|
async def run_in_executor(
|
|
583
|
-
executor_or_config:
|
|
575
|
+
executor_or_config: Executor | RunnableConfig | None,
|
|
584
576
|
func: Callable[P, T],
|
|
585
577
|
*args: P.args,
|
|
586
578
|
**kwargs: P.kwargs,
|
|
@@ -589,12 +581,12 @@ async def run_in_executor(
|
|
|
589
581
|
|
|
590
582
|
Args:
|
|
591
583
|
executor_or_config: The executor or config to run in.
|
|
592
|
-
func
|
|
593
|
-
*args
|
|
594
|
-
**kwargs
|
|
584
|
+
func: The function.
|
|
585
|
+
*args: The positional arguments to the function.
|
|
586
|
+
**kwargs: The keyword arguments to the function.
|
|
595
587
|
|
|
596
588
|
Returns:
|
|
597
|
-
|
|
589
|
+
The output of the function.
|
|
598
590
|
"""
|
|
599
591
|
|
|
600
592
|
def wrapper() -> T:
|