langchain-core 1.0.0a8__py3-none-any.whl → 1.0.0rc2__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 +10 -9
- langchain_core/caches.py +46 -56
- 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 +43 -58
- langchain_core/document_loaders/base.py +21 -21
- langchain_core/document_loaders/langsmith.py +22 -22
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +46 -49
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/semantic_similarity.py +4 -6
- langchain_core/exceptions.py +7 -8
- langchain_core/indexing/api.py +19 -25
- langchain_core/indexing/base.py +24 -24
- langchain_core/language_models/__init__.py +11 -27
- langchain_core/language_models/_utils.py +53 -54
- langchain_core/language_models/base.py +30 -24
- langchain_core/language_models/chat_models.py +123 -148
- langchain_core/language_models/fake_chat_models.py +7 -7
- langchain_core/language_models/llms.py +14 -16
- langchain_core/load/dump.py +3 -4
- langchain_core/load/load.py +7 -16
- langchain_core/load/serializable.py +37 -36
- langchain_core/messages/__init__.py +1 -16
- langchain_core/messages/ai.py +122 -123
- langchain_core/messages/base.py +31 -31
- langchain_core/messages/block_translators/__init__.py +17 -17
- langchain_core/messages/block_translators/anthropic.py +3 -3
- langchain_core/messages/block_translators/bedrock_converse.py +3 -3
- langchain_core/messages/block_translators/google_genai.py +5 -4
- 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 +3 -3
- langchain_core/messages/block_translators/openai.py +5 -5
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +222 -209
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +17 -24
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +12 -22
- langchain_core/messages/tool.py +53 -69
- langchain_core/messages/utils.py +399 -417
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +46 -47
- langchain_core/output_parsers/json.py +3 -4
- langchain_core/output_parsers/list.py +2 -2
- langchain_core/output_parsers/openai_functions.py +46 -44
- langchain_core/output_parsers/openai_tools.py +11 -16
- langchain_core/output_parsers/pydantic.py +10 -11
- langchain_core/output_parsers/string.py +2 -2
- 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 +6 -6
- langchain_core/outputs/llm_result.py +5 -5
- langchain_core/prompt_values.py +11 -11
- langchain_core/prompts/__init__.py +3 -23
- langchain_core/prompts/base.py +33 -38
- langchain_core/prompts/chat.py +222 -229
- langchain_core/prompts/dict.py +3 -3
- langchain_core/prompts/few_shot.py +76 -83
- langchain_core/prompts/few_shot_with_templates.py +7 -9
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +1 -1
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +20 -23
- langchain_core/prompts/string.py +20 -8
- langchain_core/prompts/structured.py +26 -27
- langchain_core/rate_limiters.py +50 -58
- langchain_core/retrievers.py +41 -182
- langchain_core/runnables/base.py +565 -597
- langchain_core/runnables/branch.py +8 -8
- langchain_core/runnables/config.py +37 -44
- langchain_core/runnables/configurable.py +9 -10
- langchain_core/runnables/fallbacks.py +9 -9
- langchain_core/runnables/graph.py +46 -50
- 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 +22 -22
- langchain_core/runnables/passthrough.py +11 -11
- langchain_core/runnables/retry.py +3 -3
- langchain_core/runnables/router.py +2 -2
- 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 +70 -71
- langchain_core/tools/convert.py +100 -104
- langchain_core/tools/render.py +9 -9
- langchain_core/tools/retriever.py +7 -7
- langchain_core/tools/simple.py +6 -7
- langchain_core/tools/structured.py +18 -24
- 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 +3 -3
- langchain_core/tracers/root_listeners.py +2 -2
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +1 -2
- langchain_core/utils/__init__.py +1 -1
- langchain_core/utils/aiter.py +32 -32
- langchain_core/utils/env.py +5 -5
- langchain_core/utils/function_calling.py +59 -154
- langchain_core/utils/html.py +4 -4
- langchain_core/utils/input.py +3 -3
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +20 -20
- 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 +5 -5
- langchain_core/utils/utils.py +25 -28
- langchain_core/vectorstores/base.py +55 -87
- langchain_core/vectorstores/in_memory.py +83 -85
- langchain_core/vectorstores/utils.py +2 -2
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc2.dist-info}/METADATA +23 -11
- langchain_core-1.0.0rc2.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.0rc2.dist-info}/WHEEL +0 -0
langchain_core/runnables/base.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Base classes and utilities for
|
|
1
|
+
"""Base classes and utilities for `Runnable`s."""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
@@ -125,45 +125,45 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
125
125
|
Key Methods
|
|
126
126
|
===========
|
|
127
127
|
|
|
128
|
-
-
|
|
129
|
-
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
132
|
-
|
|
128
|
+
- **`invoke`/`ainvoke`**: Transforms a single input into an output.
|
|
129
|
+
- **`batch`/`abatch`**: Efficiently transforms multiple inputs into outputs.
|
|
130
|
+
- **`stream`/`astream`**: Streams output from a single input as it's produced.
|
|
131
|
+
- **`astream_log`**: Streams output and selected intermediate results from an
|
|
132
|
+
input.
|
|
133
133
|
|
|
134
134
|
Built-in optimizations:
|
|
135
135
|
|
|
136
136
|
- **Batch**: By default, batch runs invoke() in parallel using a thread pool
|
|
137
|
-
|
|
137
|
+
executor. Override to optimize batching.
|
|
138
138
|
|
|
139
|
-
- **Async**: Methods with
|
|
140
|
-
|
|
141
|
-
|
|
139
|
+
- **Async**: Methods with `'a'` suffix are asynchronous. By default, they execute
|
|
140
|
+
the sync counterpart using asyncio's thread pool.
|
|
141
|
+
Override for native async.
|
|
142
142
|
|
|
143
143
|
All methods accept an optional config argument, which can be used to configure
|
|
144
144
|
execution, add tags and metadata for tracing and debugging etc.
|
|
145
145
|
|
|
146
146
|
Runnables expose schematic information about their input, output and config via
|
|
147
|
-
the
|
|
147
|
+
the `input_schema` property, the `output_schema` property and `config_schema`
|
|
148
148
|
method.
|
|
149
149
|
|
|
150
150
|
LCEL and Composition
|
|
151
151
|
====================
|
|
152
152
|
|
|
153
153
|
The LangChain Expression Language (LCEL) is a declarative way to compose
|
|
154
|
-
|
|
154
|
+
`Runnable` objectsinto chains.
|
|
155
155
|
Any chain constructed this way will automatically have sync, async, batch, and
|
|
156
156
|
streaming support.
|
|
157
157
|
|
|
158
|
-
The main composition primitives are
|
|
158
|
+
The main composition primitives are `RunnableSequence` and `RunnableParallel`.
|
|
159
159
|
|
|
160
|
-
|
|
160
|
+
**`RunnableSequence`** invokes a series of runnables sequentially, with
|
|
161
161
|
one Runnable's output serving as the next's input. Construct using
|
|
162
|
-
the
|
|
162
|
+
the `|` operator or by passing a list of runnables to `RunnableSequence`.
|
|
163
163
|
|
|
164
|
-
|
|
164
|
+
**`RunnableParallel`** invokes runnables concurrently, providing the same input
|
|
165
165
|
to each. Construct it using a dict literal within a sequence or by passing a
|
|
166
|
-
dict to
|
|
166
|
+
dict to `RunnableParallel`.
|
|
167
167
|
|
|
168
168
|
|
|
169
169
|
For example,
|
|
@@ -188,12 +188,12 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
188
188
|
Standard Methods
|
|
189
189
|
================
|
|
190
190
|
|
|
191
|
-
All
|
|
191
|
+
All `Runnable`s expose additional methods that can be used to modify their
|
|
192
192
|
behavior (e.g., add a retry policy, add lifecycle listeners, make them
|
|
193
193
|
configurable, etc.).
|
|
194
194
|
|
|
195
|
-
These methods will work on any
|
|
196
|
-
constructed by composing other
|
|
195
|
+
These methods will work on any `Runnable`, including `Runnable` chains
|
|
196
|
+
constructed by composing other `Runnable`s.
|
|
197
197
|
See the individual methods for details.
|
|
198
198
|
|
|
199
199
|
For example,
|
|
@@ -249,22 +249,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
249
249
|
chain.invoke(..., config={"callbacks": [ConsoleCallbackHandler()]})
|
|
250
250
|
```
|
|
251
251
|
|
|
252
|
-
For a UI (and much more) checkout
|
|
252
|
+
For a UI (and much more) checkout [LangSmith](https://docs.smith.langchain.com/).
|
|
253
253
|
|
|
254
254
|
"""
|
|
255
255
|
|
|
256
256
|
name: str | None
|
|
257
|
-
"""The name of the
|
|
257
|
+
"""The name of the `Runnable`. Used for debugging and tracing."""
|
|
258
258
|
|
|
259
259
|
def get_name(self, suffix: str | None = None, *, name: str | None = None) -> str:
|
|
260
|
-
"""Get the name of the
|
|
260
|
+
"""Get the name of the `Runnable`.
|
|
261
261
|
|
|
262
262
|
Args:
|
|
263
263
|
suffix: An optional suffix to append to the name.
|
|
264
|
-
name: An optional name to use instead of the
|
|
264
|
+
name: An optional name to use instead of the `Runnable`'s name.
|
|
265
265
|
|
|
266
266
|
Returns:
|
|
267
|
-
The name of the
|
|
267
|
+
The name of the `Runnable`.
|
|
268
268
|
"""
|
|
269
269
|
if name:
|
|
270
270
|
name_ = name
|
|
@@ -298,13 +298,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
298
298
|
def InputType(self) -> type[Input]: # noqa: N802
|
|
299
299
|
"""Input type.
|
|
300
300
|
|
|
301
|
-
The type of input this
|
|
301
|
+
The type of input this `Runnable` accepts specified as a type annotation.
|
|
302
302
|
|
|
303
303
|
Raises:
|
|
304
304
|
TypeError: If the input type cannot be inferred.
|
|
305
305
|
"""
|
|
306
306
|
# First loop through all parent classes and if any of them is
|
|
307
|
-
# a
|
|
307
|
+
# a Pydantic model, we will pick up the generic parameterization
|
|
308
308
|
# from that model via the __pydantic_generic_metadata__ attribute.
|
|
309
309
|
for base in self.__class__.mro():
|
|
310
310
|
if hasattr(base, "__pydantic_generic_metadata__"):
|
|
@@ -312,7 +312,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
312
312
|
if "args" in metadata and len(metadata["args"]) == 2:
|
|
313
313
|
return metadata["args"][0]
|
|
314
314
|
|
|
315
|
-
# If we didn't find a
|
|
315
|
+
# If we didn't find a Pydantic model in the parent classes,
|
|
316
316
|
# then loop through __orig_bases__. This corresponds to
|
|
317
317
|
# Runnables that are not pydantic models.
|
|
318
318
|
for cls in self.__class__.__orig_bases__: # type: ignore[attr-defined]
|
|
@@ -330,7 +330,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
330
330
|
def OutputType(self) -> type[Output]: # noqa: N802
|
|
331
331
|
"""Output Type.
|
|
332
332
|
|
|
333
|
-
The type of output this
|
|
333
|
+
The type of output this `Runnable` produces specified as a type annotation.
|
|
334
334
|
|
|
335
335
|
Raises:
|
|
336
336
|
TypeError: If the output type cannot be inferred.
|
|
@@ -356,18 +356,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
356
356
|
|
|
357
357
|
@property
|
|
358
358
|
def input_schema(self) -> type[BaseModel]:
|
|
359
|
-
"""The type of input this
|
|
359
|
+
"""The type of input this `Runnable` accepts specified as a Pydantic model."""
|
|
360
360
|
return self.get_input_schema()
|
|
361
361
|
|
|
362
362
|
def get_input_schema(
|
|
363
363
|
self,
|
|
364
364
|
config: RunnableConfig | None = None, # noqa: ARG002
|
|
365
365
|
) -> type[BaseModel]:
|
|
366
|
-
"""Get a
|
|
366
|
+
"""Get a Pydantic model that can be used to validate input to the `Runnable`.
|
|
367
367
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
depends on which configuration the
|
|
368
|
+
`Runnable` objects that leverage the `configurable_fields` and
|
|
369
|
+
`configurable_alternatives` methods will have a dynamic input schema that
|
|
370
|
+
depends on which configuration the `Runnable` is invoked with.
|
|
371
371
|
|
|
372
372
|
This method allows to get an input schema for a specific configuration.
|
|
373
373
|
|
|
@@ -375,7 +375,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
375
375
|
config: A config to use when generating the schema.
|
|
376
376
|
|
|
377
377
|
Returns:
|
|
378
|
-
A
|
|
378
|
+
A Pydantic model that can be used to validate input.
|
|
379
379
|
"""
|
|
380
380
|
root_type = self.InputType
|
|
381
381
|
|
|
@@ -390,7 +390,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
390
390
|
self.get_name("Input"),
|
|
391
391
|
root=root_type,
|
|
392
392
|
# create model needs access to appropriate type annotations to be
|
|
393
|
-
# able to construct the
|
|
393
|
+
# able to construct the Pydantic model.
|
|
394
394
|
# When we create the model, we pass information about the namespace
|
|
395
395
|
# where the model is being created, so the type annotations can
|
|
396
396
|
# be resolved correctly as well.
|
|
@@ -402,13 +402,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
402
402
|
def get_input_jsonschema(
|
|
403
403
|
self, config: RunnableConfig | None = None
|
|
404
404
|
) -> dict[str, Any]:
|
|
405
|
-
"""Get a JSON schema that represents the input to the
|
|
405
|
+
"""Get a JSON schema that represents the input to the `Runnable`.
|
|
406
406
|
|
|
407
407
|
Args:
|
|
408
408
|
config: A config to use when generating the schema.
|
|
409
409
|
|
|
410
410
|
Returns:
|
|
411
|
-
A JSON schema that represents the input to the
|
|
411
|
+
A JSON schema that represents the input to the `Runnable`.
|
|
412
412
|
|
|
413
413
|
Example:
|
|
414
414
|
```python
|
|
@@ -433,7 +433,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
433
433
|
def output_schema(self) -> type[BaseModel]:
|
|
434
434
|
"""Output schema.
|
|
435
435
|
|
|
436
|
-
The type of output this
|
|
436
|
+
The type of output this `Runnable` produces specified as a Pydantic model.
|
|
437
437
|
"""
|
|
438
438
|
return self.get_output_schema()
|
|
439
439
|
|
|
@@ -441,11 +441,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
441
441
|
self,
|
|
442
442
|
config: RunnableConfig | None = None, # noqa: ARG002
|
|
443
443
|
) -> type[BaseModel]:
|
|
444
|
-
"""Get a
|
|
444
|
+
"""Get a Pydantic model that can be used to validate output to the `Runnable`.
|
|
445
445
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
depends on which configuration the
|
|
446
|
+
`Runnable` objects that leverage the `configurable_fields` and
|
|
447
|
+
`configurable_alternatives` methods will have a dynamic output schema that
|
|
448
|
+
depends on which configuration the `Runnable` is invoked with.
|
|
449
449
|
|
|
450
450
|
This method allows to get an output schema for a specific configuration.
|
|
451
451
|
|
|
@@ -453,7 +453,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
453
453
|
config: A config to use when generating the schema.
|
|
454
454
|
|
|
455
455
|
Returns:
|
|
456
|
-
A
|
|
456
|
+
A Pydantic model that can be used to validate output.
|
|
457
457
|
"""
|
|
458
458
|
root_type = self.OutputType
|
|
459
459
|
|
|
@@ -468,7 +468,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
468
468
|
self.get_name("Output"),
|
|
469
469
|
root=root_type,
|
|
470
470
|
# create model needs access to appropriate type annotations to be
|
|
471
|
-
# able to construct the
|
|
471
|
+
# able to construct the Pydantic model.
|
|
472
472
|
# When we create the model, we pass information about the namespace
|
|
473
473
|
# where the model is being created, so the type annotations can
|
|
474
474
|
# be resolved correctly as well.
|
|
@@ -480,13 +480,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
480
480
|
def get_output_jsonschema(
|
|
481
481
|
self, config: RunnableConfig | None = None
|
|
482
482
|
) -> dict[str, Any]:
|
|
483
|
-
"""Get a JSON schema that represents the output of the
|
|
483
|
+
"""Get a JSON schema that represents the output of the `Runnable`.
|
|
484
484
|
|
|
485
485
|
Args:
|
|
486
486
|
config: A config to use when generating the schema.
|
|
487
487
|
|
|
488
488
|
Returns:
|
|
489
|
-
A JSON schema that represents the output of the
|
|
489
|
+
A JSON schema that represents the output of the `Runnable`.
|
|
490
490
|
|
|
491
491
|
Example:
|
|
492
492
|
```python
|
|
@@ -509,20 +509,20 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
509
509
|
|
|
510
510
|
@property
|
|
511
511
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
512
|
-
"""List configurable fields for this
|
|
512
|
+
"""List configurable fields for this `Runnable`."""
|
|
513
513
|
return []
|
|
514
514
|
|
|
515
515
|
def config_schema(self, *, include: Sequence[str] | None = None) -> type[BaseModel]:
|
|
516
|
-
"""The type of config this
|
|
516
|
+
"""The type of config this `Runnable` accepts specified as a Pydantic model.
|
|
517
517
|
|
|
518
|
-
To mark a field as configurable, see the
|
|
519
|
-
and
|
|
518
|
+
To mark a field as configurable, see the `configurable_fields`
|
|
519
|
+
and `configurable_alternatives` methods.
|
|
520
520
|
|
|
521
521
|
Args:
|
|
522
522
|
include: A list of fields to include in the config schema.
|
|
523
523
|
|
|
524
524
|
Returns:
|
|
525
|
-
A
|
|
525
|
+
A Pydantic model that can be used to validate config.
|
|
526
526
|
|
|
527
527
|
"""
|
|
528
528
|
include = include or []
|
|
@@ -558,13 +558,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
558
558
|
def get_config_jsonschema(
|
|
559
559
|
self, *, include: Sequence[str] | None = None
|
|
560
560
|
) -> dict[str, Any]:
|
|
561
|
-
"""Get a JSON schema that represents the config of the
|
|
561
|
+
"""Get a JSON schema that represents the config of the `Runnable`.
|
|
562
562
|
|
|
563
563
|
Args:
|
|
564
564
|
include: A list of fields to include in the config schema.
|
|
565
565
|
|
|
566
566
|
Returns:
|
|
567
|
-
A JSON schema that represents the config of the
|
|
567
|
+
A JSON schema that represents the config of the `Runnable`.
|
|
568
568
|
|
|
569
569
|
!!! version-added "Added in version 0.3.0"
|
|
570
570
|
|
|
@@ -572,7 +572,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
572
572
|
return self.config_schema(include=include).model_json_schema()
|
|
573
573
|
|
|
574
574
|
def get_graph(self, config: RunnableConfig | None = None) -> Graph:
|
|
575
|
-
"""Return a graph representation of this
|
|
575
|
+
"""Return a graph representation of this `Runnable`."""
|
|
576
576
|
# Import locally to prevent circular import
|
|
577
577
|
from langchain_core.runnables.graph import Graph # noqa: PLC0415
|
|
578
578
|
|
|
@@ -595,7 +595,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
595
595
|
def get_prompts(
|
|
596
596
|
self, config: RunnableConfig | None = None
|
|
597
597
|
) -> list[BasePromptTemplate]:
|
|
598
|
-
"""Return a list of prompts used by this
|
|
598
|
+
"""Return a list of prompts used by this `Runnable`."""
|
|
599
599
|
# Import locally to prevent circular import
|
|
600
600
|
from langchain_core.prompts.base import BasePromptTemplate # noqa: PLC0415
|
|
601
601
|
|
|
@@ -615,14 +615,14 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
615
615
|
) -> RunnableSerializable[Input, Other]:
|
|
616
616
|
"""Runnable "or" operator.
|
|
617
617
|
|
|
618
|
-
Compose this
|
|
619
|
-
|
|
618
|
+
Compose this `Runnable` with another object to create a
|
|
619
|
+
`RunnableSequence`.
|
|
620
620
|
|
|
621
621
|
Args:
|
|
622
|
-
other: Another
|
|
622
|
+
other: Another `Runnable` or a `Runnable`-like object.
|
|
623
623
|
|
|
624
624
|
Returns:
|
|
625
|
-
A new
|
|
625
|
+
A new `Runnable`.
|
|
626
626
|
"""
|
|
627
627
|
return RunnableSequence(self, coerce_to_runnable(other))
|
|
628
628
|
|
|
@@ -636,14 +636,14 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
636
636
|
) -> RunnableSerializable[Other, Output]:
|
|
637
637
|
"""Runnable "reverse-or" operator.
|
|
638
638
|
|
|
639
|
-
Compose this
|
|
640
|
-
|
|
639
|
+
Compose this `Runnable` with another object to create a
|
|
640
|
+
`RunnableSequence`.
|
|
641
641
|
|
|
642
642
|
Args:
|
|
643
|
-
other: Another
|
|
643
|
+
other: Another `Runnable` or a `Runnable`-like object.
|
|
644
644
|
|
|
645
645
|
Returns:
|
|
646
|
-
A new
|
|
646
|
+
A new `Runnable`.
|
|
647
647
|
"""
|
|
648
648
|
return RunnableSequence(coerce_to_runnable(other), self)
|
|
649
649
|
|
|
@@ -652,12 +652,12 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
652
652
|
*others: Runnable[Any, Other] | Callable[[Any], Other],
|
|
653
653
|
name: str | None = None,
|
|
654
654
|
) -> RunnableSerializable[Input, Other]:
|
|
655
|
-
"""Pipe
|
|
655
|
+
"""Pipe `Runnable` objects.
|
|
656
656
|
|
|
657
|
-
Compose this
|
|
658
|
-
|
|
657
|
+
Compose this `Runnable` with `Runnable`-like objects to make a
|
|
658
|
+
`RunnableSequence`.
|
|
659
659
|
|
|
660
|
-
Equivalent to
|
|
660
|
+
Equivalent to `RunnableSequence(self, *others)` or `self | others[0] | ...`
|
|
661
661
|
|
|
662
662
|
Example:
|
|
663
663
|
```python
|
|
@@ -688,70 +688,68 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
688
688
|
```
|
|
689
689
|
|
|
690
690
|
Args:
|
|
691
|
-
*others: Other
|
|
692
|
-
name: An optional name for the resulting
|
|
691
|
+
*others: Other `Runnable` or `Runnable`-like objects to compose
|
|
692
|
+
name: An optional name for the resulting `RunnableSequence`.
|
|
693
693
|
|
|
694
694
|
Returns:
|
|
695
|
-
A new
|
|
695
|
+
A new `Runnable`.
|
|
696
696
|
"""
|
|
697
697
|
return RunnableSequence(self, *others, name=name)
|
|
698
698
|
|
|
699
699
|
def pick(self, keys: str | list[str]) -> RunnableSerializable[Any, Any]:
|
|
700
|
-
"""Pick keys from the output dict of this
|
|
700
|
+
"""Pick keys from the output `dict` of this `Runnable`.
|
|
701
701
|
|
|
702
|
-
Pick single key:
|
|
702
|
+
Pick a single key:
|
|
703
703
|
|
|
704
|
-
|
|
705
|
-
|
|
704
|
+
```python
|
|
705
|
+
import json
|
|
706
706
|
|
|
707
|
-
|
|
707
|
+
from langchain_core.runnables import RunnableLambda, RunnableMap
|
|
708
708
|
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
709
|
+
as_str = RunnableLambda(str)
|
|
710
|
+
as_json = RunnableLambda(json.loads)
|
|
711
|
+
chain = RunnableMap(str=as_str, json=as_json)
|
|
712
712
|
|
|
713
|
-
|
|
714
|
-
|
|
713
|
+
chain.invoke("[1, 2, 3]")
|
|
714
|
+
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}
|
|
715
715
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
716
|
+
json_only_chain = chain.pick("json")
|
|
717
|
+
json_only_chain.invoke("[1, 2, 3]")
|
|
718
|
+
# -> [1, 2, 3]
|
|
719
|
+
```
|
|
720
720
|
|
|
721
|
-
Pick list of keys:
|
|
721
|
+
Pick a list of keys:
|
|
722
722
|
|
|
723
|
-
|
|
724
|
-
|
|
723
|
+
```python
|
|
724
|
+
from typing import Any
|
|
725
725
|
|
|
726
|
-
|
|
726
|
+
import json
|
|
727
727
|
|
|
728
|
-
|
|
728
|
+
from langchain_core.runnables import RunnableLambda, RunnableMap
|
|
729
729
|
|
|
730
|
-
|
|
731
|
-
|
|
730
|
+
as_str = RunnableLambda(str)
|
|
731
|
+
as_json = RunnableLambda(json.loads)
|
|
732
732
|
|
|
733
733
|
|
|
734
|
-
|
|
735
|
-
|
|
734
|
+
def as_bytes(x: Any) -> bytes:
|
|
735
|
+
return bytes(x, "utf-8")
|
|
736
736
|
|
|
737
737
|
|
|
738
|
-
|
|
739
|
-
str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
|
|
740
|
-
)
|
|
738
|
+
chain = RunnableMap(str=as_str, json=as_json, bytes=RunnableLambda(as_bytes))
|
|
741
739
|
|
|
742
|
-
|
|
743
|
-
|
|
740
|
+
chain.invoke("[1, 2, 3]")
|
|
741
|
+
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
|
|
744
742
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
743
|
+
json_and_bytes_chain = chain.pick(["json", "bytes"])
|
|
744
|
+
json_and_bytes_chain.invoke("[1, 2, 3]")
|
|
745
|
+
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
|
|
746
|
+
```
|
|
749
747
|
|
|
750
748
|
Args:
|
|
751
749
|
keys: A key or list of keys to pick from the output dict.
|
|
752
750
|
|
|
753
751
|
Returns:
|
|
754
|
-
a new
|
|
752
|
+
a new `Runnable`.
|
|
755
753
|
|
|
756
754
|
"""
|
|
757
755
|
# Import locally to prevent circular import
|
|
@@ -765,7 +763,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
765
763
|
| Callable[[dict[str, Any]], Any]
|
|
766
764
|
| Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]],
|
|
767
765
|
) -> RunnableSerializable[Any, Any]:
|
|
768
|
-
"""Assigns new fields to the dict output of this
|
|
766
|
+
"""Assigns new fields to the `dict` output of this `Runnable`.
|
|
769
767
|
|
|
770
768
|
```python
|
|
771
769
|
from langchain_community.llms.fake import FakeStreamingListLLM
|
|
@@ -778,11 +776,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
778
776
|
SystemMessagePromptTemplate.from_template("You are a nice assistant.")
|
|
779
777
|
+ "{question}"
|
|
780
778
|
)
|
|
781
|
-
|
|
779
|
+
model = FakeStreamingListLLM(responses=["foo-lish"])
|
|
782
780
|
|
|
783
|
-
chain: Runnable = prompt |
|
|
781
|
+
chain: Runnable = prompt | model | {"str": StrOutputParser()}
|
|
784
782
|
|
|
785
|
-
chain_with_assign = chain.assign(hello=itemgetter("str") |
|
|
783
|
+
chain_with_assign = chain.assign(hello=itemgetter("str") | model)
|
|
786
784
|
|
|
787
785
|
print(chain_with_assign.input_schema.model_json_schema())
|
|
788
786
|
# {'title': 'PromptInput', 'type': 'object', 'properties':
|
|
@@ -794,11 +792,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
794
792
|
```
|
|
795
793
|
|
|
796
794
|
Args:
|
|
797
|
-
**kwargs: A mapping of keys to
|
|
798
|
-
that will be invoked with the entire output dict of this
|
|
795
|
+
**kwargs: A mapping of keys to `Runnable` or `Runnable`-like objects
|
|
796
|
+
that will be invoked with the entire output dict of this `Runnable`.
|
|
799
797
|
|
|
800
798
|
Returns:
|
|
801
|
-
A new
|
|
799
|
+
A new `Runnable`.
|
|
802
800
|
|
|
803
801
|
"""
|
|
804
802
|
# Import locally to prevent circular import
|
|
@@ -818,15 +816,15 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
818
816
|
"""Transform a single input into an output.
|
|
819
817
|
|
|
820
818
|
Args:
|
|
821
|
-
input: The input to the
|
|
822
|
-
config: A config to use when invoking the
|
|
823
|
-
The config supports standard keys like
|
|
824
|
-
tracing purposes,
|
|
825
|
-
do in parallel, and other keys. Please refer to the
|
|
826
|
-
for more details.
|
|
819
|
+
input: The input to the `Runnable`.
|
|
820
|
+
config: A config to use when invoking the `Runnable`.
|
|
821
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
822
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
823
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
824
|
+
for more details.
|
|
827
825
|
|
|
828
826
|
Returns:
|
|
829
|
-
The output of the
|
|
827
|
+
The output of the `Runnable`.
|
|
830
828
|
"""
|
|
831
829
|
|
|
832
830
|
async def ainvoke(
|
|
@@ -838,15 +836,15 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
838
836
|
"""Transform a single input into an output.
|
|
839
837
|
|
|
840
838
|
Args:
|
|
841
|
-
input: The input to the
|
|
842
|
-
config: A config to use when invoking the
|
|
843
|
-
The config supports standard keys like
|
|
844
|
-
tracing purposes,
|
|
845
|
-
do in parallel, and other keys. Please refer to the
|
|
846
|
-
for more details.
|
|
839
|
+
input: The input to the `Runnable`.
|
|
840
|
+
config: A config to use when invoking the `Runnable`.
|
|
841
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
842
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
843
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
844
|
+
for more details.
|
|
847
845
|
|
|
848
846
|
Returns:
|
|
849
|
-
The output of the
|
|
847
|
+
The output of the `Runnable`.
|
|
850
848
|
"""
|
|
851
849
|
return await run_in_executor(config, self.invoke, input, config, **kwargs)
|
|
852
850
|
|
|
@@ -863,21 +861,20 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
863
861
|
The default implementation of batch works well for IO bound runnables.
|
|
864
862
|
|
|
865
863
|
Subclasses should override this method if they can batch more efficiently;
|
|
866
|
-
e.g., if the underlying
|
|
864
|
+
e.g., if the underlying `Runnable` uses an API which supports a batch mode.
|
|
867
865
|
|
|
868
866
|
Args:
|
|
869
|
-
inputs: A list of inputs to the
|
|
870
|
-
config: A config to use when invoking the
|
|
871
|
-
standard keys like
|
|
872
|
-
tracing purposes,
|
|
867
|
+
inputs: A list of inputs to the `Runnable`.
|
|
868
|
+
config: A config to use when invoking the `Runnable`. The config supports
|
|
869
|
+
standard keys like `'tags'`, `'metadata'` for
|
|
870
|
+
tracing purposes, `'max_concurrency'` for controlling how much work
|
|
873
871
|
to do in parallel, and other keys. Please refer to the
|
|
874
|
-
|
|
872
|
+
`RunnableConfig` for more details.
|
|
875
873
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
876
|
-
|
|
877
|
-
**kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
874
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
878
875
|
|
|
879
876
|
Returns:
|
|
880
|
-
A list of outputs from the
|
|
877
|
+
A list of outputs from the `Runnable`.
|
|
881
878
|
"""
|
|
882
879
|
if not inputs:
|
|
883
880
|
return []
|
|
@@ -928,23 +925,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
928
925
|
return_exceptions: bool = False,
|
|
929
926
|
**kwargs: Any | None,
|
|
930
927
|
) -> Iterator[tuple[int, Output | Exception]]:
|
|
931
|
-
"""Run
|
|
928
|
+
"""Run `invoke` in parallel on a list of inputs.
|
|
932
929
|
|
|
933
930
|
Yields results as they complete.
|
|
934
931
|
|
|
935
932
|
Args:
|
|
936
|
-
inputs: A list of inputs to the
|
|
937
|
-
config: A config to use when invoking the
|
|
938
|
-
The config supports standard keys like
|
|
939
|
-
tracing purposes,
|
|
940
|
-
do in parallel, and other keys. Please refer to the
|
|
941
|
-
for more details.
|
|
933
|
+
inputs: A list of inputs to the `Runnable`.
|
|
934
|
+
config: A config to use when invoking the `Runnable`.
|
|
935
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
936
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
937
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
938
|
+
for more details.
|
|
942
939
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
943
|
-
|
|
944
|
-
**kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
940
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
945
941
|
|
|
946
942
|
Yields:
|
|
947
|
-
Tuples of the index of the input and the output from the
|
|
943
|
+
Tuples of the index of the input and the output from the `Runnable`.
|
|
948
944
|
|
|
949
945
|
"""
|
|
950
946
|
if not inputs:
|
|
@@ -992,26 +988,25 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
992
988
|
return_exceptions: bool = False,
|
|
993
989
|
**kwargs: Any | None,
|
|
994
990
|
) -> list[Output]:
|
|
995
|
-
"""Default implementation runs
|
|
991
|
+
"""Default implementation runs `ainvoke` in parallel using `asyncio.gather`.
|
|
996
992
|
|
|
997
|
-
The default implementation of
|
|
993
|
+
The default implementation of `batch` works well for IO bound runnables.
|
|
998
994
|
|
|
999
995
|
Subclasses should override this method if they can batch more efficiently;
|
|
1000
|
-
e.g., if the underlying
|
|
996
|
+
e.g., if the underlying `Runnable` uses an API which supports a batch mode.
|
|
1001
997
|
|
|
1002
998
|
Args:
|
|
1003
|
-
inputs: A list of inputs to the
|
|
1004
|
-
config: A config to use when invoking the
|
|
1005
|
-
The config supports standard keys like
|
|
1006
|
-
tracing purposes,
|
|
1007
|
-
do in parallel, and other keys. Please refer to the
|
|
1008
|
-
for more details.
|
|
999
|
+
inputs: A list of inputs to the `Runnable`.
|
|
1000
|
+
config: A config to use when invoking the `Runnable`.
|
|
1001
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
1002
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
1003
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
1004
|
+
for more details.
|
|
1009
1005
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
1010
|
-
|
|
1011
|
-
**kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1006
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1012
1007
|
|
|
1013
1008
|
Returns:
|
|
1014
|
-
A list of outputs from the
|
|
1009
|
+
A list of outputs from the `Runnable`.
|
|
1015
1010
|
|
|
1016
1011
|
"""
|
|
1017
1012
|
if not inputs:
|
|
@@ -1059,23 +1054,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1059
1054
|
return_exceptions: bool = False,
|
|
1060
1055
|
**kwargs: Any | None,
|
|
1061
1056
|
) -> AsyncIterator[tuple[int, Output | Exception]]:
|
|
1062
|
-
"""Run
|
|
1057
|
+
"""Run `ainvoke` in parallel on a list of inputs.
|
|
1063
1058
|
|
|
1064
1059
|
Yields results as they complete.
|
|
1065
1060
|
|
|
1066
1061
|
Args:
|
|
1067
|
-
inputs: A list of inputs to the
|
|
1068
|
-
config: A config to use when invoking the
|
|
1069
|
-
The config supports standard keys like
|
|
1070
|
-
tracing purposes,
|
|
1071
|
-
do in parallel, and other keys. Please refer to the
|
|
1072
|
-
for more details.
|
|
1062
|
+
inputs: A list of inputs to the `Runnable`.
|
|
1063
|
+
config: A config to use when invoking the `Runnable`.
|
|
1064
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
1065
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
1066
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
1067
|
+
for more details.
|
|
1073
1068
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
1074
|
-
|
|
1075
|
-
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1069
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1076
1070
|
|
|
1077
1071
|
Yields:
|
|
1078
|
-
A tuple of the index of the input and the output from the
|
|
1072
|
+
A tuple of the index of the input and the output from the `Runnable`.
|
|
1079
1073
|
|
|
1080
1074
|
"""
|
|
1081
1075
|
if not inputs:
|
|
@@ -1116,17 +1110,17 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1116
1110
|
config: RunnableConfig | None = None,
|
|
1117
1111
|
**kwargs: Any | None,
|
|
1118
1112
|
) -> Iterator[Output]:
|
|
1119
|
-
"""Default implementation of
|
|
1113
|
+
"""Default implementation of `stream`, which calls `invoke`.
|
|
1120
1114
|
|
|
1121
1115
|
Subclasses should override this method if they support streaming output.
|
|
1122
1116
|
|
|
1123
1117
|
Args:
|
|
1124
|
-
input: The input to the
|
|
1125
|
-
config: The config to use for the
|
|
1126
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1118
|
+
input: The input to the `Runnable`.
|
|
1119
|
+
config: The config to use for the `Runnable`.
|
|
1120
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1127
1121
|
|
|
1128
1122
|
Yields:
|
|
1129
|
-
The output of the
|
|
1123
|
+
The output of the `Runnable`.
|
|
1130
1124
|
|
|
1131
1125
|
"""
|
|
1132
1126
|
yield self.invoke(input, config, **kwargs)
|
|
@@ -1137,17 +1131,17 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1137
1131
|
config: RunnableConfig | None = None,
|
|
1138
1132
|
**kwargs: Any | None,
|
|
1139
1133
|
) -> AsyncIterator[Output]:
|
|
1140
|
-
"""Default implementation of
|
|
1134
|
+
"""Default implementation of `astream`, which calls `ainvoke`.
|
|
1141
1135
|
|
|
1142
1136
|
Subclasses should override this method if they support streaming output.
|
|
1143
1137
|
|
|
1144
1138
|
Args:
|
|
1145
|
-
input: The input to the
|
|
1146
|
-
config: The config to use for the
|
|
1147
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1139
|
+
input: The input to the `Runnable`.
|
|
1140
|
+
config: The config to use for the `Runnable`.
|
|
1141
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1148
1142
|
|
|
1149
1143
|
Yields:
|
|
1150
|
-
The output of the
|
|
1144
|
+
The output of the `Runnable`.
|
|
1151
1145
|
|
|
1152
1146
|
"""
|
|
1153
1147
|
yield await self.ainvoke(input, config, **kwargs)
|
|
@@ -1201,7 +1195,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1201
1195
|
exclude_tags: Sequence[str] | None = None,
|
|
1202
1196
|
**kwargs: Any,
|
|
1203
1197
|
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]:
|
|
1204
|
-
"""Stream all output from a
|
|
1198
|
+
"""Stream all output from a `Runnable`, as reported to the callback system.
|
|
1205
1199
|
|
|
1206
1200
|
This includes all inner runs of LLMs, Retrievers, Tools, etc.
|
|
1207
1201
|
|
|
@@ -1212,20 +1206,20 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1212
1206
|
The Jsonpatch ops can be applied in order to construct state.
|
|
1213
1207
|
|
|
1214
1208
|
Args:
|
|
1215
|
-
input: The input to the
|
|
1216
|
-
config: The config to use for the
|
|
1209
|
+
input: The input to the `Runnable`.
|
|
1210
|
+
config: The config to use for the `Runnable`.
|
|
1217
1211
|
diff: Whether to yield diffs between each step or the current state.
|
|
1218
|
-
with_streamed_output_list: Whether to yield the
|
|
1212
|
+
with_streamed_output_list: Whether to yield the `streamed_output` list.
|
|
1219
1213
|
include_names: Only include logs with these names.
|
|
1220
1214
|
include_types: Only include logs with these types.
|
|
1221
1215
|
include_tags: Only include logs with these tags.
|
|
1222
1216
|
exclude_names: Exclude logs with these names.
|
|
1223
1217
|
exclude_types: Exclude logs with these types.
|
|
1224
1218
|
exclude_tags: Exclude logs with these tags.
|
|
1225
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1219
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1226
1220
|
|
|
1227
1221
|
Yields:
|
|
1228
|
-
A
|
|
1222
|
+
A `RunLogPatch` or `RunLog` object.
|
|
1229
1223
|
|
|
1230
1224
|
"""
|
|
1231
1225
|
stream = LogStreamCallbackHandler(
|
|
@@ -1269,28 +1263,26 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1269
1263
|
) -> AsyncIterator[StreamEvent]:
|
|
1270
1264
|
"""Generate a stream of events.
|
|
1271
1265
|
|
|
1272
|
-
Use to create an iterator over
|
|
1273
|
-
about the progress of the
|
|
1266
|
+
Use to create an iterator over `StreamEvent` that provide real-time information
|
|
1267
|
+
about the progress of the `Runnable`, including `StreamEvent` from intermediate
|
|
1274
1268
|
results.
|
|
1275
1269
|
|
|
1276
|
-
A
|
|
1277
|
-
|
|
1278
|
-
-
|
|
1279
|
-
|
|
1280
|
-
-
|
|
1281
|
-
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
generated the event.
|
|
1293
|
-
- ``data``: **dict[str, Any]**
|
|
1270
|
+
A `StreamEvent` is a dictionary with the following schema:
|
|
1271
|
+
|
|
1272
|
+
- `event`: Event names are of the format:
|
|
1273
|
+
`on_[runnable_type]_(start|stream|end)`.
|
|
1274
|
+
- `name`: The name of the `Runnable` that generated the event.
|
|
1275
|
+
- `run_id`: Randomly generated ID associated with the given execution of the
|
|
1276
|
+
`Runnable` that emitted the event. A child `Runnable` that gets invoked as
|
|
1277
|
+
part of the execution of a parent `Runnable` is assigned its own unique ID.
|
|
1278
|
+
- `parent_ids`: The IDs of the parent runnables that generated the event. The
|
|
1279
|
+
root `Runnable` will have an empty list. The order of the parent IDs is from
|
|
1280
|
+
the root to the immediate parent. Only available for v2 version of the API.
|
|
1281
|
+
The v1 version of the API will return an empty list.
|
|
1282
|
+
- `tags`: The tags of the `Runnable` that generated the event.
|
|
1283
|
+
- `metadata`: The metadata of the `Runnable` that generated the event.
|
|
1284
|
+
- `data`: The data associated with the event. The contents of this field
|
|
1285
|
+
depend on the type of event. See the table below for more details.
|
|
1294
1286
|
|
|
1295
1287
|
Below is a table that illustrates some events that might be emitted by various
|
|
1296
1288
|
chains. Metadata fields have been omitted from the table for brevity.
|
|
@@ -1299,39 +1291,23 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1299
1291
|
!!! note
|
|
1300
1292
|
This reference table is for the v2 version of the schema.
|
|
1301
1293
|
|
|
1302
|
-
|
|
1303
|
-
|
|
|
1304
|
-
|
|
1305
|
-
|
|
|
1306
|
-
|
|
1307
|
-
|
|
|
1308
|
-
|
|
1309
|
-
|
|
|
1310
|
-
|
|
1311
|
-
|
|
|
1312
|
-
|
|
1313
|
-
|
|
|
1314
|
-
|
|
1315
|
-
|
|
|
1316
|
-
|
|
1317
|
-
|
|
|
1318
|
-
|
|
1319
|
-
| ``on_chain_stream`` | format_docs | ``'hello world!, goodbye world!'`` | | |
|
|
1320
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1321
|
-
| ``on_chain_end`` | format_docs | | ``[Document(...)]`` | ``'hello world!, goodbye world!'`` |
|
|
1322
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1323
|
-
| ``on_tool_start`` | some_tool | | ``{"x": 1, "y": "2"}`` | |
|
|
1324
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1325
|
-
| ``on_tool_end`` | some_tool | | | ``{"x": 1, "y": "2"}`` |
|
|
1326
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1327
|
-
| ``on_retriever_start`` | [retriever name] | | ``{"query": "hello"}`` | |
|
|
1328
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1329
|
-
| ``on_retriever_end`` | [retriever name] | | ``{"query": "hello"}`` | ``[Document(...), ..]`` |
|
|
1330
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1331
|
-
| ``on_prompt_start`` | [template_name] | | ``{"question": "hello"}`` | |
|
|
1332
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1333
|
-
| ``on_prompt_end`` | [template_name] | | ``{"question": "hello"}`` | ``ChatPromptValue(messages: [SystemMessage, ...])`` |
|
|
1334
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1294
|
+
| event | name | chunk | input | output |
|
|
1295
|
+
| ---------------------- | -------------------- | ----------------------------------- | ------------------------------------------------- | --------------------------------------------------- |
|
|
1296
|
+
| `on_chat_model_start` | `'[model name]'` | | `{"messages": [[SystemMessage, HumanMessage]]}` | |
|
|
1297
|
+
| `on_chat_model_stream` | `'[model name]'` | `AIMessageChunk(content="hello")` | | |
|
|
1298
|
+
| `on_chat_model_end` | `'[model name]'` | | `{"messages": [[SystemMessage, HumanMessage]]}` | `AIMessageChunk(content="hello world")` |
|
|
1299
|
+
| `on_llm_start` | `'[model name]'` | | `{'input': 'hello'}` | |
|
|
1300
|
+
| `on_llm_stream` | `'[model name]'` | `'Hello' ` | | |
|
|
1301
|
+
| `on_llm_end` | `'[model name]'` | | `'Hello human!'` | |
|
|
1302
|
+
| `on_chain_start` | `'format_docs'` | | | |
|
|
1303
|
+
| `on_chain_stream` | `'format_docs'` | `'hello world!, goodbye world!'` | | |
|
|
1304
|
+
| `on_chain_end` | `'format_docs'` | | `[Document(...)]` | `'hello world!, goodbye world!'` |
|
|
1305
|
+
| `on_tool_start` | `'some_tool'` | | `{"x": 1, "y": "2"}` | |
|
|
1306
|
+
| `on_tool_end` | `'some_tool'` | | | `{"x": 1, "y": "2"}` |
|
|
1307
|
+
| `on_retriever_start` | `'[retriever name]'` | | `{"query": "hello"}` | |
|
|
1308
|
+
| `on_retriever_end` | `'[retriever name]'` | | `{"query": "hello"}` | `[Document(...), ..]` |
|
|
1309
|
+
| `on_prompt_start` | `'[template_name]'` | | `{"question": "hello"}` | |
|
|
1310
|
+
| `on_prompt_end` | `'[template_name]'` | | `{"question": "hello"}` | `ChatPromptValue(messages: [SystemMessage, ...])` |
|
|
1335
1311
|
|
|
1336
1312
|
In addition to the standard events, users can also dispatch custom events (see example below).
|
|
1337
1313
|
|
|
@@ -1339,17 +1315,14 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1339
1315
|
|
|
1340
1316
|
A custom event has following format:
|
|
1341
1317
|
|
|
1342
|
-
|
|
1343
|
-
|
|
|
1344
|
-
|
|
1345
|
-
|
|
|
1346
|
-
+-----------+------+-----------------------------------------------------------------------------------------------------------+
|
|
1347
|
-
| data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. |
|
|
1348
|
-
+-----------+------+-----------------------------------------------------------------------------------------------------------+
|
|
1318
|
+
| Attribute | Type | Description |
|
|
1319
|
+
| ----------- | ------ | --------------------------------------------------------------------------------------------------------- |
|
|
1320
|
+
| `name` | `str` | A user defined name for the event. |
|
|
1321
|
+
| `data` | `Any` | The data associated with the event. This can be anything, though we suggest making it JSON serializable. |
|
|
1349
1322
|
|
|
1350
1323
|
Here are declarations associated with the standard events shown above:
|
|
1351
1324
|
|
|
1352
|
-
|
|
1325
|
+
`format_docs`:
|
|
1353
1326
|
|
|
1354
1327
|
```python
|
|
1355
1328
|
def format_docs(docs: list[Document]) -> str:
|
|
@@ -1360,7 +1333,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1360
1333
|
format_docs = RunnableLambda(format_docs)
|
|
1361
1334
|
```
|
|
1362
1335
|
|
|
1363
|
-
|
|
1336
|
+
`some_tool`:
|
|
1364
1337
|
|
|
1365
1338
|
```python
|
|
1366
1339
|
@tool
|
|
@@ -1369,7 +1342,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1369
1342
|
return {"x": x, "y": y}
|
|
1370
1343
|
```
|
|
1371
1344
|
|
|
1372
|
-
|
|
1345
|
+
`prompt`:
|
|
1373
1346
|
|
|
1374
1347
|
```python
|
|
1375
1348
|
template = ChatPromptTemplate.from_messages(
|
|
@@ -1379,7 +1352,8 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1379
1352
|
]
|
|
1380
1353
|
).with_config({"run_name": "my_template", "tags": ["my_template"]})
|
|
1381
1354
|
```
|
|
1382
|
-
|
|
1355
|
+
|
|
1356
|
+
For instance:
|
|
1383
1357
|
|
|
1384
1358
|
```python
|
|
1385
1359
|
from langchain_core.runnables import RunnableLambda
|
|
@@ -1420,9 +1394,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1420
1394
|
]
|
|
1421
1395
|
```
|
|
1422
1396
|
|
|
1423
|
-
Example: Dispatch Custom Event
|
|
1424
|
-
|
|
1425
|
-
```python
|
|
1397
|
+
```python title="Example: Dispatch Custom Event"
|
|
1426
1398
|
from langchain_core.callbacks.manager import (
|
|
1427
1399
|
adispatch_custom_event,
|
|
1428
1400
|
)
|
|
@@ -1451,32 +1423,32 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1451
1423
|
|
|
1452
1424
|
async for event in slow_thing.astream_events("some_input", version="v2"):
|
|
1453
1425
|
print(event)
|
|
1454
|
-
|
|
1426
|
+
``
|
|
1455
1427
|
|
|
1456
1428
|
Args:
|
|
1457
|
-
input: The input to the
|
|
1458
|
-
config: The config to use for the
|
|
1459
|
-
version: The version of the schema to use either
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
include_names: Only include events from
|
|
1466
|
-
include_types: Only include events from
|
|
1467
|
-
include_tags: Only include events from
|
|
1468
|
-
exclude_names: Exclude events from
|
|
1469
|
-
exclude_types: Exclude events from
|
|
1470
|
-
exclude_tags: Exclude events from
|
|
1471
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1472
|
-
These will be passed to
|
|
1473
|
-
of
|
|
1429
|
+
input: The input to the `Runnable`.
|
|
1430
|
+
config: The config to use for the `Runnable`.
|
|
1431
|
+
version: The version of the schema to use either `'v2'` or `'v1'`.
|
|
1432
|
+
Users should use `'v2'`.
|
|
1433
|
+
`'v1'` is for backwards compatibility and will be deprecated
|
|
1434
|
+
in `0.4.0`.
|
|
1435
|
+
No default will be assigned until the API is stabilized.
|
|
1436
|
+
custom events will only be surfaced in `'v2'`.
|
|
1437
|
+
include_names: Only include events from `Runnable` objects with matching names.
|
|
1438
|
+
include_types: Only include events from `Runnable` objects with matching types.
|
|
1439
|
+
include_tags: Only include events from `Runnable` objects with matching tags.
|
|
1440
|
+
exclude_names: Exclude events from `Runnable` objects with matching names.
|
|
1441
|
+
exclude_types: Exclude events from `Runnable` objects with matching types.
|
|
1442
|
+
exclude_tags: Exclude events from `Runnable` objects with matching tags.
|
|
1443
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1444
|
+
These will be passed to `astream_log` as this implementation
|
|
1445
|
+
of `astream_events` is built on top of `astream_log`.
|
|
1474
1446
|
|
|
1475
1447
|
Yields:
|
|
1476
|
-
An async stream of
|
|
1448
|
+
An async stream of `StreamEvent`.
|
|
1477
1449
|
|
|
1478
1450
|
Raises:
|
|
1479
|
-
NotImplementedError: If the version is not
|
|
1451
|
+
NotImplementedError: If the version is not `'v1'` or `'v2'`.
|
|
1480
1452
|
|
|
1481
1453
|
""" # noqa: E501
|
|
1482
1454
|
if version == "v2":
|
|
@@ -1523,18 +1495,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1523
1495
|
) -> Iterator[Output]:
|
|
1524
1496
|
"""Transform inputs to outputs.
|
|
1525
1497
|
|
|
1526
|
-
Default implementation of transform, which buffers input and calls
|
|
1498
|
+
Default implementation of transform, which buffers input and calls `astream`.
|
|
1527
1499
|
|
|
1528
1500
|
Subclasses should override this method if they can start producing output while
|
|
1529
1501
|
input is still being generated.
|
|
1530
1502
|
|
|
1531
1503
|
Args:
|
|
1532
|
-
input: An iterator of inputs to the
|
|
1533
|
-
config: The config to use for the
|
|
1534
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1504
|
+
input: An iterator of inputs to the `Runnable`.
|
|
1505
|
+
config: The config to use for the `Runnable`.
|
|
1506
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1535
1507
|
|
|
1536
1508
|
Yields:
|
|
1537
|
-
The output of the
|
|
1509
|
+
The output of the `Runnable`.
|
|
1538
1510
|
|
|
1539
1511
|
"""
|
|
1540
1512
|
final: Input
|
|
@@ -1568,18 +1540,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1568
1540
|
) -> AsyncIterator[Output]:
|
|
1569
1541
|
"""Transform inputs to outputs.
|
|
1570
1542
|
|
|
1571
|
-
Default implementation of atransform, which buffers input and calls
|
|
1543
|
+
Default implementation of atransform, which buffers input and calls `astream`.
|
|
1572
1544
|
|
|
1573
1545
|
Subclasses should override this method if they can start producing output while
|
|
1574
1546
|
input is still being generated.
|
|
1575
1547
|
|
|
1576
1548
|
Args:
|
|
1577
|
-
input: An async iterator of inputs to the
|
|
1578
|
-
config: The config to use for the
|
|
1579
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1549
|
+
input: An async iterator of inputs to the `Runnable`.
|
|
1550
|
+
config: The config to use for the `Runnable`.
|
|
1551
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1580
1552
|
|
|
1581
1553
|
Yields:
|
|
1582
|
-
The output of the
|
|
1554
|
+
The output of the `Runnable`.
|
|
1583
1555
|
|
|
1584
1556
|
"""
|
|
1585
1557
|
final: Input
|
|
@@ -1607,32 +1579,32 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1607
1579
|
yield output
|
|
1608
1580
|
|
|
1609
1581
|
def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
|
|
1610
|
-
"""Bind arguments to a
|
|
1582
|
+
"""Bind arguments to a `Runnable`, returning a new `Runnable`.
|
|
1611
1583
|
|
|
1612
|
-
Useful when a
|
|
1613
|
-
in the output of the previous
|
|
1584
|
+
Useful when a `Runnable` in a chain requires an argument that is not
|
|
1585
|
+
in the output of the previous `Runnable` or included in the user input.
|
|
1614
1586
|
|
|
1615
1587
|
Args:
|
|
1616
|
-
kwargs: The arguments to bind to the
|
|
1588
|
+
**kwargs: The arguments to bind to the `Runnable`.
|
|
1617
1589
|
|
|
1618
1590
|
Returns:
|
|
1619
|
-
A new
|
|
1591
|
+
A new `Runnable` with the arguments bound.
|
|
1620
1592
|
|
|
1621
1593
|
Example:
|
|
1622
1594
|
```python
|
|
1623
1595
|
from langchain_ollama import ChatOllama
|
|
1624
1596
|
from langchain_core.output_parsers import StrOutputParser
|
|
1625
1597
|
|
|
1626
|
-
|
|
1598
|
+
model = ChatOllama(model="llama3.1")
|
|
1627
1599
|
|
|
1628
|
-
# Without bind
|
|
1629
|
-
chain =
|
|
1600
|
+
# Without bind
|
|
1601
|
+
chain = model | StrOutputParser()
|
|
1630
1602
|
|
|
1631
1603
|
chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
|
|
1632
1604
|
# Output is 'One two three four five.'
|
|
1633
1605
|
|
|
1634
|
-
# With bind
|
|
1635
|
-
chain =
|
|
1606
|
+
# With bind
|
|
1607
|
+
chain = model.bind(stop=["three"]) | StrOutputParser()
|
|
1636
1608
|
|
|
1637
1609
|
chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
|
|
1638
1610
|
# Output is 'One two'
|
|
@@ -1646,14 +1618,14 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1646
1618
|
# Sadly Unpack is not well-supported by mypy so this will have to be untyped
|
|
1647
1619
|
**kwargs: Any,
|
|
1648
1620
|
) -> Runnable[Input, Output]:
|
|
1649
|
-
"""Bind config to a
|
|
1621
|
+
"""Bind config to a `Runnable`, returning a new `Runnable`.
|
|
1650
1622
|
|
|
1651
1623
|
Args:
|
|
1652
|
-
config: The config to bind to the
|
|
1653
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1624
|
+
config: The config to bind to the `Runnable`.
|
|
1625
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1654
1626
|
|
|
1655
1627
|
Returns:
|
|
1656
|
-
A new
|
|
1628
|
+
A new `Runnable` with the config bound.
|
|
1657
1629
|
|
|
1658
1630
|
"""
|
|
1659
1631
|
return RunnableBinding(
|
|
@@ -1678,22 +1650,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1678
1650
|
| Callable[[Run, RunnableConfig], None]
|
|
1679
1651
|
| None = None,
|
|
1680
1652
|
) -> Runnable[Input, Output]:
|
|
1681
|
-
"""Bind lifecycle listeners to a
|
|
1653
|
+
"""Bind lifecycle listeners to a `Runnable`, returning a new `Runnable`.
|
|
1682
1654
|
|
|
1683
|
-
The Run object contains information about the run, including its
|
|
1684
|
-
|
|
1655
|
+
The Run object contains information about the run, including its `id`,
|
|
1656
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
1685
1657
|
any tags or metadata added to the run.
|
|
1686
1658
|
|
|
1687
1659
|
Args:
|
|
1688
|
-
on_start: Called before the
|
|
1689
|
-
object.
|
|
1690
|
-
on_end: Called after the
|
|
1691
|
-
object.
|
|
1692
|
-
on_error: Called if the
|
|
1693
|
-
object.
|
|
1660
|
+
on_start: Called before the `Runnable` starts running, with the `Run`
|
|
1661
|
+
object.
|
|
1662
|
+
on_end: Called after the `Runnable` finishes running, with the `Run`
|
|
1663
|
+
object.
|
|
1664
|
+
on_error: Called if the `Runnable` throws an error, with the `Run`
|
|
1665
|
+
object.
|
|
1694
1666
|
|
|
1695
1667
|
Returns:
|
|
1696
|
-
A new
|
|
1668
|
+
A new `Runnable` with the listeners bound.
|
|
1697
1669
|
|
|
1698
1670
|
Example:
|
|
1699
1671
|
```python
|
|
@@ -1744,24 +1716,24 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1744
1716
|
on_end: AsyncListener | None = None,
|
|
1745
1717
|
on_error: AsyncListener | None = None,
|
|
1746
1718
|
) -> Runnable[Input, Output]:
|
|
1747
|
-
"""Bind async lifecycle listeners to a
|
|
1719
|
+
"""Bind async lifecycle listeners to a `Runnable`.
|
|
1748
1720
|
|
|
1749
|
-
Returns a new
|
|
1721
|
+
Returns a new `Runnable`.
|
|
1750
1722
|
|
|
1751
|
-
The Run object contains information about the run, including its
|
|
1752
|
-
|
|
1723
|
+
The Run object contains information about the run, including its `id`,
|
|
1724
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
1753
1725
|
any tags or metadata added to the run.
|
|
1754
1726
|
|
|
1755
1727
|
Args:
|
|
1756
|
-
on_start: Called asynchronously before the
|
|
1757
|
-
with the
|
|
1758
|
-
on_end: Called asynchronously after the
|
|
1759
|
-
with the
|
|
1760
|
-
on_error: Called asynchronously if the
|
|
1761
|
-
with the
|
|
1728
|
+
on_start: Called asynchronously before the `Runnable` starts running,
|
|
1729
|
+
with the `Run` object.
|
|
1730
|
+
on_end: Called asynchronously after the `Runnable` finishes running,
|
|
1731
|
+
with the `Run` object.
|
|
1732
|
+
on_error: Called asynchronously if the `Runnable` throws an error,
|
|
1733
|
+
with the `Run` object.
|
|
1762
1734
|
|
|
1763
1735
|
Returns:
|
|
1764
|
-
A new
|
|
1736
|
+
A new `Runnable` with the listeners bound.
|
|
1765
1737
|
|
|
1766
1738
|
Example:
|
|
1767
1739
|
```python
|
|
@@ -1834,11 +1806,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1834
1806
|
input_type: type[Input] | None = None,
|
|
1835
1807
|
output_type: type[Output] | None = None,
|
|
1836
1808
|
) -> Runnable[Input, Output]:
|
|
1837
|
-
"""Bind input and output types to a
|
|
1809
|
+
"""Bind input and output types to a `Runnable`, returning a new `Runnable`.
|
|
1838
1810
|
|
|
1839
1811
|
Args:
|
|
1840
|
-
input_type: The input type to bind to the
|
|
1841
|
-
output_type: The output type to bind to the
|
|
1812
|
+
input_type: The input type to bind to the `Runnable`.
|
|
1813
|
+
output_type: The output type to bind to the `Runnable`.
|
|
1842
1814
|
|
|
1843
1815
|
Returns:
|
|
1844
1816
|
A new Runnable with the types bound.
|
|
@@ -1858,18 +1830,17 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1858
1830
|
exponential_jitter_params: ExponentialJitterParams | None = None,
|
|
1859
1831
|
stop_after_attempt: int = 3,
|
|
1860
1832
|
) -> Runnable[Input, Output]:
|
|
1861
|
-
"""Create a new Runnable that retries the original Runnable on exceptions.
|
|
1833
|
+
"""Create a new `Runnable` that retries the original `Runnable` on exceptions.
|
|
1862
1834
|
|
|
1863
1835
|
Args:
|
|
1864
1836
|
retry_if_exception_type: A tuple of exception types to retry on.
|
|
1865
|
-
Defaults to (Exception,).
|
|
1866
1837
|
wait_exponential_jitter: Whether to add jitter to the wait
|
|
1867
|
-
time between retries.
|
|
1838
|
+
time between retries.
|
|
1868
1839
|
stop_after_attempt: The maximum number of attempts to make before
|
|
1869
|
-
giving up.
|
|
1840
|
+
giving up.
|
|
1870
1841
|
exponential_jitter_params: Parameters for
|
|
1871
|
-
|
|
1872
|
-
|
|
1842
|
+
`tenacity.wait_exponential_jitter`. Namely: `initial`, `max`,
|
|
1843
|
+
`exp_base`, and `jitter` (all float values).
|
|
1873
1844
|
|
|
1874
1845
|
Returns:
|
|
1875
1846
|
A new Runnable that retries the original Runnable on exceptions.
|
|
@@ -1916,12 +1887,12 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1916
1887
|
)
|
|
1917
1888
|
|
|
1918
1889
|
def map(self) -> Runnable[list[Input], list[Output]]:
|
|
1919
|
-
"""Return a new
|
|
1890
|
+
"""Return a new `Runnable` that maps a list of inputs to a list of outputs.
|
|
1920
1891
|
|
|
1921
|
-
Calls
|
|
1892
|
+
Calls `invoke` with each input.
|
|
1922
1893
|
|
|
1923
1894
|
Returns:
|
|
1924
|
-
A new
|
|
1895
|
+
A new `Runnable` that maps a list of inputs to a list of outputs.
|
|
1925
1896
|
|
|
1926
1897
|
Example:
|
|
1927
1898
|
```python
|
|
@@ -1945,25 +1916,24 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1945
1916
|
exceptions_to_handle: tuple[type[BaseException], ...] = (Exception,),
|
|
1946
1917
|
exception_key: str | None = None,
|
|
1947
1918
|
) -> RunnableWithFallbacksT[Input, Output]:
|
|
1948
|
-
"""Add fallbacks to a
|
|
1919
|
+
"""Add fallbacks to a `Runnable`, returning a new `Runnable`.
|
|
1949
1920
|
|
|
1950
|
-
The new
|
|
1921
|
+
The new `Runnable` will try the original `Runnable`, and then each fallback
|
|
1951
1922
|
in order, upon failures.
|
|
1952
1923
|
|
|
1953
1924
|
Args:
|
|
1954
|
-
fallbacks: A sequence of runnables to try if the original
|
|
1925
|
+
fallbacks: A sequence of runnables to try if the original `Runnable`
|
|
1955
1926
|
fails.
|
|
1956
1927
|
exceptions_to_handle: A tuple of exception types to handle.
|
|
1957
|
-
Defaults to ``(Exception,)``.
|
|
1958
1928
|
exception_key: If string is specified then handled exceptions will be passed
|
|
1959
1929
|
to fallbacks as part of the input under the specified key.
|
|
1960
|
-
If None
|
|
1961
|
-
If used, the base
|
|
1962
|
-
dictionary as input.
|
|
1930
|
+
If `None`, exceptions will not be passed to fallbacks.
|
|
1931
|
+
If used, the base `Runnable` and its fallbacks must accept a
|
|
1932
|
+
dictionary as input.
|
|
1963
1933
|
|
|
1964
1934
|
Returns:
|
|
1965
|
-
A new
|
|
1966
|
-
|
|
1935
|
+
A new `Runnable` that will try the original `Runnable`, and then each
|
|
1936
|
+
Fallback in order, upon failures.
|
|
1967
1937
|
|
|
1968
1938
|
Example:
|
|
1969
1939
|
```python
|
|
@@ -1988,18 +1958,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1988
1958
|
```
|
|
1989
1959
|
|
|
1990
1960
|
Args:
|
|
1991
|
-
fallbacks: A sequence of runnables to try if the original
|
|
1961
|
+
fallbacks: A sequence of runnables to try if the original `Runnable`
|
|
1992
1962
|
fails.
|
|
1993
1963
|
exceptions_to_handle: A tuple of exception types to handle.
|
|
1994
1964
|
exception_key: If string is specified then handled exceptions will be passed
|
|
1995
1965
|
to fallbacks as part of the input under the specified key.
|
|
1996
|
-
If None
|
|
1997
|
-
If used, the base
|
|
1966
|
+
If `None`, exceptions will not be passed to fallbacks.
|
|
1967
|
+
If used, the base `Runnable` and its fallbacks must accept a
|
|
1998
1968
|
dictionary as input.
|
|
1999
1969
|
|
|
2000
1970
|
Returns:
|
|
2001
|
-
A new
|
|
2002
|
-
|
|
1971
|
+
A new `Runnable` that will try the original `Runnable`, and then each
|
|
1972
|
+
Fallback in order, upon failures.
|
|
2003
1973
|
|
|
2004
1974
|
"""
|
|
2005
1975
|
# Import locally to prevent circular import
|
|
@@ -2029,10 +1999,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2029
1999
|
) -> Output:
|
|
2030
2000
|
"""Call with config.
|
|
2031
2001
|
|
|
2032
|
-
Helper method to transform an
|
|
2002
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2033
2003
|
with callbacks.
|
|
2034
2004
|
|
|
2035
|
-
Use this method to implement
|
|
2005
|
+
Use this method to implement `invoke` in subclasses.
|
|
2036
2006
|
|
|
2037
2007
|
"""
|
|
2038
2008
|
config = ensure_config(config)
|
|
@@ -2080,10 +2050,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2080
2050
|
) -> Output:
|
|
2081
2051
|
"""Async call with config.
|
|
2082
2052
|
|
|
2083
|
-
Helper method to transform an
|
|
2053
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2084
2054
|
with callbacks.
|
|
2085
2055
|
|
|
2086
|
-
Use this method to implement
|
|
2056
|
+
Use this method to implement `ainvoke` in subclasses.
|
|
2087
2057
|
"""
|
|
2088
2058
|
config = ensure_config(config)
|
|
2089
2059
|
callback_manager = get_async_callback_manager_for_config(config)
|
|
@@ -2127,8 +2097,8 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2127
2097
|
) -> list[Output]:
|
|
2128
2098
|
"""Transform a list of inputs to a list of outputs, with callbacks.
|
|
2129
2099
|
|
|
2130
|
-
Helper method to transform an
|
|
2131
|
-
with callbacks. Use this method to implement
|
|
2100
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2101
|
+
with callbacks. Use this method to implement `invoke` in subclasses.
|
|
2132
2102
|
|
|
2133
2103
|
"""
|
|
2134
2104
|
if not inputs:
|
|
@@ -2195,10 +2165,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2195
2165
|
) -> list[Output]:
|
|
2196
2166
|
"""Transform a list of inputs to a list of outputs, with callbacks.
|
|
2197
2167
|
|
|
2198
|
-
Helper method to transform an
|
|
2168
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2199
2169
|
with callbacks.
|
|
2200
2170
|
|
|
2201
|
-
Use this method to implement
|
|
2171
|
+
Use this method to implement `invoke` in subclasses.
|
|
2202
2172
|
|
|
2203
2173
|
"""
|
|
2204
2174
|
if not inputs:
|
|
@@ -2265,10 +2235,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2265
2235
|
) -> Iterator[Output]:
|
|
2266
2236
|
"""Transform a stream with config.
|
|
2267
2237
|
|
|
2268
|
-
Helper method to transform an
|
|
2269
|
-
|
|
2238
|
+
Helper method to transform an `Iterator` of `Input` values into an
|
|
2239
|
+
`Iterator` of `Output` values, with callbacks.
|
|
2270
2240
|
|
|
2271
|
-
Use this to implement
|
|
2241
|
+
Use this to implement `stream` or `transform` in `Runnable` subclasses.
|
|
2272
2242
|
|
|
2273
2243
|
"""
|
|
2274
2244
|
# tee the input so we can iterate over it twice
|
|
@@ -2362,10 +2332,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2362
2332
|
) -> AsyncIterator[Output]:
|
|
2363
2333
|
"""Transform a stream with config.
|
|
2364
2334
|
|
|
2365
|
-
Helper method to transform an Async
|
|
2366
|
-
Async
|
|
2335
|
+
Helper method to transform an Async `Iterator` of `Input` values into an
|
|
2336
|
+
Async `Iterator` of `Output` values, with callbacks.
|
|
2367
2337
|
|
|
2368
|
-
Use this to implement
|
|
2338
|
+
Use this to implement `astream` or `atransform` in `Runnable` subclasses.
|
|
2369
2339
|
|
|
2370
2340
|
"""
|
|
2371
2341
|
# tee the input so we can iterate over it twice
|
|
@@ -2456,23 +2426,23 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2456
2426
|
description: str | None = None,
|
|
2457
2427
|
arg_types: dict[str, type] | None = None,
|
|
2458
2428
|
) -> BaseTool:
|
|
2459
|
-
"""Create a
|
|
2429
|
+
"""Create a `BaseTool` from a `Runnable`.
|
|
2460
2430
|
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
from
|
|
2464
|
-
|
|
2465
|
-
the schema can be specified directly with
|
|
2466
|
-
pass
|
|
2431
|
+
`as_tool` will instantiate a `BaseTool` with a name, description, and
|
|
2432
|
+
`args_schema` from a `Runnable`. Where possible, schemas are inferred
|
|
2433
|
+
from `runnable.get_input_schema`. Alternatively (e.g., if the
|
|
2434
|
+
`Runnable` takes a dict as input and the specific dict keys are not typed),
|
|
2435
|
+
the schema can be specified directly with `args_schema`. You can also
|
|
2436
|
+
pass `arg_types` to just specify the required arguments and their types.
|
|
2467
2437
|
|
|
2468
2438
|
Args:
|
|
2469
|
-
args_schema: The schema for the tool.
|
|
2470
|
-
name: The name of the tool.
|
|
2471
|
-
description: The description of the tool.
|
|
2472
|
-
arg_types: A dictionary of argument names to types.
|
|
2439
|
+
args_schema: The schema for the tool.
|
|
2440
|
+
name: The name of the tool.
|
|
2441
|
+
description: The description of the tool.
|
|
2442
|
+
arg_types: A dictionary of argument names to types.
|
|
2473
2443
|
|
|
2474
2444
|
Returns:
|
|
2475
|
-
A
|
|
2445
|
+
A `BaseTool` instance.
|
|
2476
2446
|
|
|
2477
2447
|
Typed dict input:
|
|
2478
2448
|
|
|
@@ -2495,7 +2465,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2495
2465
|
as_tool.invoke({"a": 3, "b": [1, 2]})
|
|
2496
2466
|
```
|
|
2497
2467
|
|
|
2498
|
-
|
|
2468
|
+
`dict` input, specifying schema via `args_schema`:
|
|
2499
2469
|
|
|
2500
2470
|
```python
|
|
2501
2471
|
from typing import Any
|
|
@@ -2516,7 +2486,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2516
2486
|
as_tool.invoke({"a": 3, "b": [1, 2]})
|
|
2517
2487
|
```
|
|
2518
2488
|
|
|
2519
|
-
|
|
2489
|
+
`dict` input, specifying schema via `arg_types`:
|
|
2520
2490
|
|
|
2521
2491
|
```python
|
|
2522
2492
|
from typing import Any
|
|
@@ -2579,10 +2549,10 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2579
2549
|
|
|
2580
2550
|
@override
|
|
2581
2551
|
def to_json(self) -> SerializedConstructor | SerializedNotImplemented:
|
|
2582
|
-
"""Serialize the
|
|
2552
|
+
"""Serialize the `Runnable` to JSON.
|
|
2583
2553
|
|
|
2584
2554
|
Returns:
|
|
2585
|
-
A JSON-serializable representation of the
|
|
2555
|
+
A JSON-serializable representation of the `Runnable`.
|
|
2586
2556
|
|
|
2587
2557
|
"""
|
|
2588
2558
|
dumped = super().to_json()
|
|
@@ -2593,16 +2563,16 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2593
2563
|
def configurable_fields(
|
|
2594
2564
|
self, **kwargs: AnyConfigurableField
|
|
2595
2565
|
) -> RunnableSerializable[Input, Output]:
|
|
2596
|
-
"""Configure particular
|
|
2566
|
+
"""Configure particular `Runnable` fields at runtime.
|
|
2597
2567
|
|
|
2598
2568
|
Args:
|
|
2599
|
-
**kwargs: A dictionary of
|
|
2569
|
+
**kwargs: A dictionary of `ConfigurableField` instances to configure.
|
|
2600
2570
|
|
|
2601
2571
|
Raises:
|
|
2602
|
-
ValueError: If a configuration key is not found in the
|
|
2572
|
+
ValueError: If a configuration key is not found in the `Runnable`.
|
|
2603
2573
|
|
|
2604
2574
|
Returns:
|
|
2605
|
-
A new
|
|
2575
|
+
A new `Runnable` with the fields configured.
|
|
2606
2576
|
|
|
2607
2577
|
```python
|
|
2608
2578
|
from langchain_core.runnables import ConfigurableField
|
|
@@ -2652,20 +2622,18 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2652
2622
|
prefix_keys: bool = False,
|
|
2653
2623
|
**kwargs: Runnable[Input, Output] | Callable[[], Runnable[Input, Output]],
|
|
2654
2624
|
) -> RunnableSerializable[Input, Output]:
|
|
2655
|
-
"""Configure alternatives for
|
|
2625
|
+
"""Configure alternatives for `Runnable` objects that can be set at runtime.
|
|
2656
2626
|
|
|
2657
2627
|
Args:
|
|
2658
|
-
which: The
|
|
2628
|
+
which: The `ConfigurableField` instance that will be used to select the
|
|
2659
2629
|
alternative.
|
|
2660
2630
|
default_key: The default key to use if no alternative is selected.
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
**kwargs: A dictionary of keys to ``Runnable`` instances or callables that
|
|
2665
|
-
return ``Runnable`` instances.
|
|
2631
|
+
prefix_keys: Whether to prefix the keys with the `ConfigurableField` id.
|
|
2632
|
+
**kwargs: A dictionary of keys to `Runnable` instances or callables that
|
|
2633
|
+
return `Runnable` instances.
|
|
2666
2634
|
|
|
2667
2635
|
Returns:
|
|
2668
|
-
A new
|
|
2636
|
+
A new `Runnable` with the alternatives configured.
|
|
2669
2637
|
|
|
2670
2638
|
```python
|
|
2671
2639
|
from langchain_anthropic import ChatAnthropic
|
|
@@ -2787,26 +2755,26 @@ def _seq_output_schema(
|
|
|
2787
2755
|
|
|
2788
2756
|
|
|
2789
2757
|
class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
2790
|
-
"""Sequence of
|
|
2758
|
+
"""Sequence of `Runnable` objects, where the output of one is the input of the next.
|
|
2791
2759
|
|
|
2792
|
-
|
|
2760
|
+
**`RunnableSequence`** is the most important composition operator in LangChain
|
|
2793
2761
|
as it is used in virtually every chain.
|
|
2794
2762
|
|
|
2795
|
-
A
|
|
2796
|
-
|
|
2797
|
-
|
|
2763
|
+
A `RunnableSequence` can be instantiated directly or more commonly by using the
|
|
2764
|
+
`|` operator where either the left or right operands (or both) must be a
|
|
2765
|
+
`Runnable`.
|
|
2798
2766
|
|
|
2799
|
-
Any
|
|
2767
|
+
Any `RunnableSequence` automatically supports sync, async, batch.
|
|
2800
2768
|
|
|
2801
|
-
The default implementations of
|
|
2802
|
-
asyncio gather and will be faster than naive invocation of
|
|
2803
|
-
for IO bound
|
|
2769
|
+
The default implementations of `batch` and `abatch` utilize threadpools and
|
|
2770
|
+
asyncio gather and will be faster than naive invocation of `invoke` or `ainvoke`
|
|
2771
|
+
for IO bound `Runnable`s.
|
|
2804
2772
|
|
|
2805
2773
|
Batching is implemented by invoking the batch method on each component of the
|
|
2806
|
-
|
|
2774
|
+
`RunnableSequence` in order.
|
|
2807
2775
|
|
|
2808
|
-
A
|
|
2809
|
-
all components of the sequence implement a
|
|
2776
|
+
A `RunnableSequence` preserves the streaming properties of its components, so if
|
|
2777
|
+
all components of the sequence implement a `transform` method -- which
|
|
2810
2778
|
is the method that implements the logic to map a streaming input to a streaming
|
|
2811
2779
|
output -- then the sequence will be able to stream input to output!
|
|
2812
2780
|
|
|
@@ -2815,15 +2783,15 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2815
2783
|
multiple blocking components, streaming begins after the last one.
|
|
2816
2784
|
|
|
2817
2785
|
!!! note
|
|
2818
|
-
|
|
2819
|
-
use a
|
|
2820
|
-
|
|
2786
|
+
`RunnableLambdas` do not support `transform` by default! So if you need to
|
|
2787
|
+
use a `RunnableLambdas` be careful about where you place them in a
|
|
2788
|
+
`RunnableSequence` (if you need to use the `stream`/`astream` methods).
|
|
2821
2789
|
|
|
2822
2790
|
If you need arbitrary logic and need streaming, you can subclass
|
|
2823
|
-
Runnable, and implement
|
|
2791
|
+
Runnable, and implement `transform` for whatever logic you need.
|
|
2824
2792
|
|
|
2825
2793
|
Here is a simple example that uses simple functions to illustrate the use of
|
|
2826
|
-
|
|
2794
|
+
`RunnableSequence`:
|
|
2827
2795
|
|
|
2828
2796
|
```python
|
|
2829
2797
|
from langchain_core.runnables import RunnableLambda
|
|
@@ -2874,11 +2842,11 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2874
2842
|
# purposes. It allows specifying the `Input` on the first type, the `Output` of
|
|
2875
2843
|
# the last type.
|
|
2876
2844
|
first: Runnable[Input, Any]
|
|
2877
|
-
"""The first
|
|
2845
|
+
"""The first `Runnable` in the sequence."""
|
|
2878
2846
|
middle: list[Runnable[Any, Any]] = Field(default_factory=list)
|
|
2879
|
-
"""The middle
|
|
2847
|
+
"""The middle `Runnable` in the sequence."""
|
|
2880
2848
|
last: Runnable[Any, Output]
|
|
2881
|
-
"""The last
|
|
2849
|
+
"""The last `Runnable` in the sequence."""
|
|
2882
2850
|
|
|
2883
2851
|
def __init__(
|
|
2884
2852
|
self,
|
|
@@ -2888,14 +2856,14 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2888
2856
|
middle: list[Runnable[Any, Any]] | None = None,
|
|
2889
2857
|
last: Runnable[Any, Any] | None = None,
|
|
2890
2858
|
) -> None:
|
|
2891
|
-
"""Create a new
|
|
2859
|
+
"""Create a new `RunnableSequence`.
|
|
2892
2860
|
|
|
2893
2861
|
Args:
|
|
2894
2862
|
steps: The steps to include in the sequence.
|
|
2895
|
-
name: The name of the
|
|
2896
|
-
first: The first
|
|
2897
|
-
middle: The middle
|
|
2898
|
-
last: The last Runnable in the sequence.
|
|
2863
|
+
name: The name of the `Runnable`.
|
|
2864
|
+
first: The first `Runnable` in the sequence.
|
|
2865
|
+
middle: The middle `Runnable` objects in the sequence.
|
|
2866
|
+
last: The last Runnable in the sequence.
|
|
2899
2867
|
|
|
2900
2868
|
Raises:
|
|
2901
2869
|
ValueError: If the sequence has less than 2 steps.
|
|
@@ -2921,19 +2889,19 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2921
2889
|
@classmethod
|
|
2922
2890
|
@override
|
|
2923
2891
|
def get_lc_namespace(cls) -> list[str]:
|
|
2924
|
-
"""Get the namespace of the
|
|
2892
|
+
"""Get the namespace of the LangChain object.
|
|
2925
2893
|
|
|
2926
2894
|
Returns:
|
|
2927
|
-
|
|
2895
|
+
`["langchain", "schema", "runnable"]`
|
|
2928
2896
|
"""
|
|
2929
2897
|
return ["langchain", "schema", "runnable"]
|
|
2930
2898
|
|
|
2931
2899
|
@property
|
|
2932
2900
|
def steps(self) -> list[Runnable[Any, Any]]:
|
|
2933
|
-
"""All the
|
|
2901
|
+
"""All the `Runnable`s that make up the sequence in order.
|
|
2934
2902
|
|
|
2935
2903
|
Returns:
|
|
2936
|
-
A list of
|
|
2904
|
+
A list of `Runnable`s.
|
|
2937
2905
|
"""
|
|
2938
2906
|
return [self.first, *self.middle, self.last]
|
|
2939
2907
|
|
|
@@ -2950,24 +2918,24 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2950
2918
|
@property
|
|
2951
2919
|
@override
|
|
2952
2920
|
def InputType(self) -> type[Input]:
|
|
2953
|
-
"""The type of the input to the
|
|
2921
|
+
"""The type of the input to the `Runnable`."""
|
|
2954
2922
|
return self.first.InputType
|
|
2955
2923
|
|
|
2956
2924
|
@property
|
|
2957
2925
|
@override
|
|
2958
2926
|
def OutputType(self) -> type[Output]:
|
|
2959
|
-
"""The type of the output of the
|
|
2927
|
+
"""The type of the output of the `Runnable`."""
|
|
2960
2928
|
return self.last.OutputType
|
|
2961
2929
|
|
|
2962
2930
|
@override
|
|
2963
2931
|
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
2964
|
-
"""Get the input schema of the
|
|
2932
|
+
"""Get the input schema of the `Runnable`.
|
|
2965
2933
|
|
|
2966
2934
|
Args:
|
|
2967
|
-
config: The config to use.
|
|
2935
|
+
config: The config to use.
|
|
2968
2936
|
|
|
2969
2937
|
Returns:
|
|
2970
|
-
The input schema of the
|
|
2938
|
+
The input schema of the `Runnable`.
|
|
2971
2939
|
|
|
2972
2940
|
"""
|
|
2973
2941
|
return _seq_input_schema(self.steps, config)
|
|
@@ -2976,13 +2944,13 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2976
2944
|
def get_output_schema(
|
|
2977
2945
|
self, config: RunnableConfig | None = None
|
|
2978
2946
|
) -> type[BaseModel]:
|
|
2979
|
-
"""Get the output schema of the
|
|
2947
|
+
"""Get the output schema of the `Runnable`.
|
|
2980
2948
|
|
|
2981
2949
|
Args:
|
|
2982
|
-
config: The config to use.
|
|
2950
|
+
config: The config to use.
|
|
2983
2951
|
|
|
2984
2952
|
Returns:
|
|
2985
|
-
The output schema of the
|
|
2953
|
+
The output schema of the `Runnable`.
|
|
2986
2954
|
|
|
2987
2955
|
"""
|
|
2988
2956
|
return _seq_output_schema(self.steps, config)
|
|
@@ -2990,10 +2958,10 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2990
2958
|
@property
|
|
2991
2959
|
@override
|
|
2992
2960
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
2993
|
-
"""Get the config specs of the
|
|
2961
|
+
"""Get the config specs of the `Runnable`.
|
|
2994
2962
|
|
|
2995
2963
|
Returns:
|
|
2996
|
-
The config specs of the
|
|
2964
|
+
The config specs of the `Runnable`.
|
|
2997
2965
|
|
|
2998
2966
|
"""
|
|
2999
2967
|
# Import locally to prevent circular import
|
|
@@ -3003,16 +2971,16 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
3003
2971
|
|
|
3004
2972
|
@override
|
|
3005
2973
|
def get_graph(self, config: RunnableConfig | None = None) -> Graph:
|
|
3006
|
-
"""Get the graph representation of the
|
|
2974
|
+
"""Get the graph representation of the `Runnable`.
|
|
3007
2975
|
|
|
3008
2976
|
Args:
|
|
3009
|
-
config: The config to use.
|
|
2977
|
+
config: The config to use.
|
|
3010
2978
|
|
|
3011
2979
|
Returns:
|
|
3012
|
-
The graph representation of the
|
|
2980
|
+
The graph representation of the `Runnable`.
|
|
3013
2981
|
|
|
3014
2982
|
Raises:
|
|
3015
|
-
ValueError: If a
|
|
2983
|
+
ValueError: If a `Runnable` has no first or last node.
|
|
3016
2984
|
|
|
3017
2985
|
"""
|
|
3018
2986
|
# Import locally to prevent circular import
|
|
@@ -3532,19 +3500,19 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
3532
3500
|
|
|
3533
3501
|
|
|
3534
3502
|
class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
3535
|
-
"""Runnable that runs a mapping of
|
|
3503
|
+
"""Runnable that runs a mapping of `Runnable`s in parallel.
|
|
3536
3504
|
|
|
3537
3505
|
Returns a mapping of their outputs.
|
|
3538
3506
|
|
|
3539
|
-
|
|
3540
|
-
alongside
|
|
3507
|
+
`RunnableParallel` is one of the two main composition primitives for the LCEL,
|
|
3508
|
+
alongside `RunnableSequence`. It invokes `Runnable`s concurrently, providing the
|
|
3541
3509
|
same input to each.
|
|
3542
3510
|
|
|
3543
|
-
A
|
|
3511
|
+
A `RunnableParallel` can be instantiated directly or by using a dict literal
|
|
3544
3512
|
within a sequence.
|
|
3545
3513
|
|
|
3546
3514
|
Here is a simple example that uses functions to illustrate the use of
|
|
3547
|
-
|
|
3515
|
+
`RunnableParallel`:
|
|
3548
3516
|
|
|
3549
3517
|
```python
|
|
3550
3518
|
from langchain_core.runnables import RunnableLambda
|
|
@@ -3587,8 +3555,8 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3587
3555
|
await sequence.abatch([1, 2, 3])
|
|
3588
3556
|
```
|
|
3589
3557
|
|
|
3590
|
-
|
|
3591
|
-
example, we simultaneously stream output from two different
|
|
3558
|
+
`RunnableParallel` makes it easy to run `Runnable`s in parallel. In the below
|
|
3559
|
+
example, we simultaneously stream output from two different `Runnable` objects:
|
|
3592
3560
|
|
|
3593
3561
|
```python
|
|
3594
3562
|
from langchain_core.prompts import ChatPromptTemplate
|
|
@@ -3630,10 +3598,10 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3630
3598
|
| Callable[[Input], Any]
|
|
3631
3599
|
| Mapping[str, Runnable[Input, Any] | Callable[[Input], Any]],
|
|
3632
3600
|
) -> None:
|
|
3633
|
-
"""Create a
|
|
3601
|
+
"""Create a `RunnableParallel`.
|
|
3634
3602
|
|
|
3635
3603
|
Args:
|
|
3636
|
-
steps__: The steps to include.
|
|
3604
|
+
steps__: The steps to include.
|
|
3637
3605
|
**kwargs: Additional steps to include.
|
|
3638
3606
|
|
|
3639
3607
|
"""
|
|
@@ -3652,10 +3620,10 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3652
3620
|
@classmethod
|
|
3653
3621
|
@override
|
|
3654
3622
|
def get_lc_namespace(cls) -> list[str]:
|
|
3655
|
-
"""Get the namespace of the
|
|
3623
|
+
"""Get the namespace of the LangChain object.
|
|
3656
3624
|
|
|
3657
3625
|
Returns:
|
|
3658
|
-
|
|
3626
|
+
`["langchain", "schema", "runnable"]`
|
|
3659
3627
|
"""
|
|
3660
3628
|
return ["langchain", "schema", "runnable"]
|
|
3661
3629
|
|
|
@@ -3665,14 +3633,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3665
3633
|
|
|
3666
3634
|
@override
|
|
3667
3635
|
def get_name(self, suffix: str | None = None, *, name: str | None = None) -> str:
|
|
3668
|
-
"""Get the name of the
|
|
3636
|
+
"""Get the name of the `Runnable`.
|
|
3669
3637
|
|
|
3670
3638
|
Args:
|
|
3671
|
-
suffix: The suffix to use.
|
|
3672
|
-
name: The name to use.
|
|
3639
|
+
suffix: The suffix to use.
|
|
3640
|
+
name: The name to use.
|
|
3673
3641
|
|
|
3674
3642
|
Returns:
|
|
3675
|
-
The name of the
|
|
3643
|
+
The name of the `Runnable`.
|
|
3676
3644
|
|
|
3677
3645
|
"""
|
|
3678
3646
|
name = name or self.name or f"RunnableParallel<{','.join(self.steps__.keys())}>"
|
|
@@ -3681,7 +3649,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3681
3649
|
@property
|
|
3682
3650
|
@override
|
|
3683
3651
|
def InputType(self) -> Any:
|
|
3684
|
-
"""The type of the input to the
|
|
3652
|
+
"""The type of the input to the `Runnable`."""
|
|
3685
3653
|
for step in self.steps__.values():
|
|
3686
3654
|
if step.InputType:
|
|
3687
3655
|
return step.InputType
|
|
@@ -3690,13 +3658,13 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3690
3658
|
|
|
3691
3659
|
@override
|
|
3692
3660
|
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
3693
|
-
"""Get the input schema of the
|
|
3661
|
+
"""Get the input schema of the `Runnable`.
|
|
3694
3662
|
|
|
3695
3663
|
Args:
|
|
3696
|
-
config: The config to use.
|
|
3664
|
+
config: The config to use.
|
|
3697
3665
|
|
|
3698
3666
|
Returns:
|
|
3699
|
-
The input schema of the
|
|
3667
|
+
The input schema of the `Runnable`.
|
|
3700
3668
|
|
|
3701
3669
|
"""
|
|
3702
3670
|
if all(
|
|
@@ -3721,13 +3689,13 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3721
3689
|
def get_output_schema(
|
|
3722
3690
|
self, config: RunnableConfig | None = None
|
|
3723
3691
|
) -> type[BaseModel]:
|
|
3724
|
-
"""Get the output schema of the
|
|
3692
|
+
"""Get the output schema of the `Runnable`.
|
|
3725
3693
|
|
|
3726
3694
|
Args:
|
|
3727
|
-
config: The config to use.
|
|
3695
|
+
config: The config to use.
|
|
3728
3696
|
|
|
3729
3697
|
Returns:
|
|
3730
|
-
The output schema of the
|
|
3698
|
+
The output schema of the `Runnable`.
|
|
3731
3699
|
|
|
3732
3700
|
"""
|
|
3733
3701
|
fields = {k: (v.OutputType, ...) for k, v in self.steps__.items()}
|
|
@@ -3736,10 +3704,10 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3736
3704
|
@property
|
|
3737
3705
|
@override
|
|
3738
3706
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
3739
|
-
"""Get the config specs of the
|
|
3707
|
+
"""Get the config specs of the `Runnable`.
|
|
3740
3708
|
|
|
3741
3709
|
Returns:
|
|
3742
|
-
The config specs of the
|
|
3710
|
+
The config specs of the `Runnable`.
|
|
3743
3711
|
|
|
3744
3712
|
"""
|
|
3745
3713
|
return get_unique_config_specs(
|
|
@@ -3748,16 +3716,16 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3748
3716
|
|
|
3749
3717
|
@override
|
|
3750
3718
|
def get_graph(self, config: RunnableConfig | None = None) -> Graph:
|
|
3751
|
-
"""Get the graph representation of the
|
|
3719
|
+
"""Get the graph representation of the `Runnable`.
|
|
3752
3720
|
|
|
3753
3721
|
Args:
|
|
3754
|
-
config: The config to use.
|
|
3722
|
+
config: The config to use.
|
|
3755
3723
|
|
|
3756
3724
|
Returns:
|
|
3757
|
-
The graph representation of the
|
|
3725
|
+
The graph representation of the `Runnable`.
|
|
3758
3726
|
|
|
3759
3727
|
Raises:
|
|
3760
|
-
ValueError: If a
|
|
3728
|
+
ValueError: If a `Runnable` has no first or last node.
|
|
3761
3729
|
|
|
3762
3730
|
"""
|
|
3763
3731
|
# Import locally to prevent circular import
|
|
@@ -4057,24 +4025,24 @@ RunnableMap = RunnableParallel
|
|
|
4057
4025
|
|
|
4058
4026
|
|
|
4059
4027
|
class RunnableGenerator(Runnable[Input, Output]):
|
|
4060
|
-
"""
|
|
4028
|
+
"""`Runnable` that runs a generator function.
|
|
4061
4029
|
|
|
4062
|
-
|
|
4030
|
+
`RunnableGenerator`s can be instantiated directly or by using a generator within
|
|
4063
4031
|
a sequence.
|
|
4064
4032
|
|
|
4065
|
-
|
|
4033
|
+
`RunnableGenerator`s can be used to implement custom behavior, such as custom
|
|
4066
4034
|
output parsers, while preserving streaming capabilities. Given a generator function
|
|
4067
|
-
with a signature
|
|
4068
|
-
|
|
4035
|
+
with a signature `Iterator[A] -> Iterator[B]`, wrapping it in a
|
|
4036
|
+
`RunnableGenerator` allows it to emit output chunks as soon as they are streamed
|
|
4069
4037
|
in from the previous step.
|
|
4070
4038
|
|
|
4071
4039
|
!!! note
|
|
4072
|
-
If a generator function has a
|
|
4040
|
+
If a generator function has a `signature A -> Iterator[B]`, such that it
|
|
4073
4041
|
requires its input from the previous step to be completed before emitting chunks
|
|
4074
4042
|
(e.g., most LLMs need the entire prompt available to start generating), it can
|
|
4075
|
-
instead be wrapped in a
|
|
4043
|
+
instead be wrapped in a `RunnableLambda`.
|
|
4076
4044
|
|
|
4077
|
-
Here is an example to show the basic mechanics of a
|
|
4045
|
+
Here is an example to show the basic mechanics of a `RunnableGenerator`:
|
|
4078
4046
|
|
|
4079
4047
|
```python
|
|
4080
4048
|
from typing import Any, AsyncIterator, Iterator
|
|
@@ -4104,7 +4072,7 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4104
4072
|
[p async for p in runnable.astream(None)] # ["Have", " a", " nice", " day"]
|
|
4105
4073
|
```
|
|
4106
4074
|
|
|
4107
|
-
|
|
4075
|
+
`RunnableGenerator` makes it easy to implement custom behavior within a streaming
|
|
4108
4076
|
context. Below we show an example:
|
|
4109
4077
|
|
|
4110
4078
|
```python
|
|
@@ -4157,12 +4125,12 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4157
4125
|
*,
|
|
4158
4126
|
name: str | None = None,
|
|
4159
4127
|
) -> None:
|
|
4160
|
-
"""Initialize a
|
|
4128
|
+
"""Initialize a `RunnableGenerator`.
|
|
4161
4129
|
|
|
4162
4130
|
Args:
|
|
4163
4131
|
transform: The transform function.
|
|
4164
|
-
atransform: The async transform function.
|
|
4165
|
-
name: The name of the
|
|
4132
|
+
atransform: The async transform function.
|
|
4133
|
+
name: The name of the `Runnable`.
|
|
4166
4134
|
|
|
4167
4135
|
Raises:
|
|
4168
4136
|
TypeError: If the transform is not a generator function.
|
|
@@ -4359,20 +4327,20 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4359
4327
|
|
|
4360
4328
|
|
|
4361
4329
|
class RunnableLambda(Runnable[Input, Output]):
|
|
4362
|
-
"""
|
|
4330
|
+
"""`RunnableLambda` converts a python callable into a `Runnable`.
|
|
4363
4331
|
|
|
4364
|
-
Wrapping a callable in a
|
|
4332
|
+
Wrapping a callable in a `RunnableLambda` makes the callable usable
|
|
4365
4333
|
within either a sync or async context.
|
|
4366
4334
|
|
|
4367
|
-
|
|
4335
|
+
`RunnableLambda` can be composed as any other `Runnable` and provides
|
|
4368
4336
|
seamless integration with LangChain tracing.
|
|
4369
4337
|
|
|
4370
|
-
|
|
4338
|
+
`RunnableLambda` is best suited for code that does not need to support
|
|
4371
4339
|
streaming. If you need to support streaming (i.e., be able to operate
|
|
4372
|
-
on chunks of inputs and yield chunks of outputs), use
|
|
4340
|
+
on chunks of inputs and yield chunks of outputs), use `RunnableGenerator`
|
|
4373
4341
|
instead.
|
|
4374
4342
|
|
|
4375
|
-
Note that if a
|
|
4343
|
+
Note that if a `RunnableLambda` returns an instance of `Runnable`, that
|
|
4376
4344
|
instance is invoked (or streamed) during execution.
|
|
4377
4345
|
|
|
4378
4346
|
Examples:
|
|
@@ -4431,7 +4399,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4431
4399
|
| None = None,
|
|
4432
4400
|
name: str | None = None,
|
|
4433
4401
|
) -> None:
|
|
4434
|
-
"""Create a
|
|
4402
|
+
"""Create a `RunnableLambda` from a callable, and async callable or both.
|
|
4435
4403
|
|
|
4436
4404
|
Accepts both sync and async variants to allow providing efficient
|
|
4437
4405
|
implementations for sync and async execution.
|
|
@@ -4439,12 +4407,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4439
4407
|
Args:
|
|
4440
4408
|
func: Either sync or async callable
|
|
4441
4409
|
afunc: An async callable that takes an input and returns an output.
|
|
4442
|
-
|
|
4443
|
-
name: The name of the
|
|
4410
|
+
|
|
4411
|
+
name: The name of the `Runnable`.
|
|
4444
4412
|
|
|
4445
4413
|
Raises:
|
|
4446
|
-
TypeError: If the
|
|
4447
|
-
TypeError: If both
|
|
4414
|
+
TypeError: If the `func` is not a callable type.
|
|
4415
|
+
TypeError: If both `func` and `afunc` are provided.
|
|
4448
4416
|
|
|
4449
4417
|
"""
|
|
4450
4418
|
if afunc is not None:
|
|
@@ -4484,7 +4452,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4484
4452
|
@property
|
|
4485
4453
|
@override
|
|
4486
4454
|
def InputType(self) -> Any:
|
|
4487
|
-
"""The type of the input to this
|
|
4455
|
+
"""The type of the input to this `Runnable`."""
|
|
4488
4456
|
func = getattr(self, "func", None) or self.afunc
|
|
4489
4457
|
try:
|
|
4490
4458
|
params = inspect.signature(func).parameters
|
|
@@ -4497,13 +4465,13 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4497
4465
|
|
|
4498
4466
|
@override
|
|
4499
4467
|
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
4500
|
-
"""The
|
|
4468
|
+
"""The Pydantic schema for the input to this `Runnable`.
|
|
4501
4469
|
|
|
4502
4470
|
Args:
|
|
4503
|
-
config: The config to use.
|
|
4471
|
+
config: The config to use.
|
|
4504
4472
|
|
|
4505
4473
|
Returns:
|
|
4506
|
-
The input schema for this
|
|
4474
|
+
The input schema for this `Runnable`.
|
|
4507
4475
|
|
|
4508
4476
|
"""
|
|
4509
4477
|
func = getattr(self, "func", None) or self.afunc
|
|
@@ -4542,10 +4510,10 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4542
4510
|
@property
|
|
4543
4511
|
@override
|
|
4544
4512
|
def OutputType(self) -> Any:
|
|
4545
|
-
"""The type of the output of this
|
|
4513
|
+
"""The type of the output of this `Runnable` as a type annotation.
|
|
4546
4514
|
|
|
4547
4515
|
Returns:
|
|
4548
|
-
The type of the output of this
|
|
4516
|
+
The type of the output of this `Runnable`.
|
|
4549
4517
|
|
|
4550
4518
|
"""
|
|
4551
4519
|
func = getattr(self, "func", None) or self.afunc
|
|
@@ -4592,11 +4560,11 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4592
4560
|
|
|
4593
4561
|
@functools.cached_property
|
|
4594
4562
|
def deps(self) -> list[Runnable]:
|
|
4595
|
-
"""The dependencies of this
|
|
4563
|
+
"""The dependencies of this `Runnable`.
|
|
4596
4564
|
|
|
4597
4565
|
Returns:
|
|
4598
|
-
The dependencies of this
|
|
4599
|
-
variables that are
|
|
4566
|
+
The dependencies of this `Runnable`. If the function has nonlocal
|
|
4567
|
+
variables that are `Runnable`s, they are considered dependencies.
|
|
4600
4568
|
|
|
4601
4569
|
"""
|
|
4602
4570
|
if hasattr(self, "func"):
|
|
@@ -4664,7 +4632,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4664
4632
|
__hash__ = None # type: ignore[assignment]
|
|
4665
4633
|
|
|
4666
4634
|
def __repr__(self) -> str:
|
|
4667
|
-
"""Return a string representation of this
|
|
4635
|
+
"""Return a string representation of this `Runnable`."""
|
|
4668
4636
|
if self._repr is None:
|
|
4669
4637
|
if hasattr(self, "func") and isinstance(self.func, itemgetter):
|
|
4670
4638
|
self._repr = f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
|
@@ -4830,18 +4798,18 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4830
4798
|
config: RunnableConfig | None = None,
|
|
4831
4799
|
**kwargs: Any | None,
|
|
4832
4800
|
) -> Output:
|
|
4833
|
-
"""Invoke this
|
|
4801
|
+
"""Invoke this `Runnable` synchronously.
|
|
4834
4802
|
|
|
4835
4803
|
Args:
|
|
4836
|
-
input: The input to this
|
|
4837
|
-
config: The config to use.
|
|
4838
|
-
kwargs: Additional keyword arguments.
|
|
4804
|
+
input: The input to this `Runnable`.
|
|
4805
|
+
config: The config to use.
|
|
4806
|
+
**kwargs: Additional keyword arguments.
|
|
4839
4807
|
|
|
4840
4808
|
Returns:
|
|
4841
|
-
The output of this
|
|
4809
|
+
The output of this `Runnable`.
|
|
4842
4810
|
|
|
4843
4811
|
Raises:
|
|
4844
|
-
TypeError: If the
|
|
4812
|
+
TypeError: If the `Runnable` is a coroutine function.
|
|
4845
4813
|
|
|
4846
4814
|
"""
|
|
4847
4815
|
if hasattr(self, "func"):
|
|
@@ -4861,15 +4829,15 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4861
4829
|
config: RunnableConfig | None = None,
|
|
4862
4830
|
**kwargs: Any | None,
|
|
4863
4831
|
) -> Output:
|
|
4864
|
-
"""Invoke this
|
|
4832
|
+
"""Invoke this `Runnable` asynchronously.
|
|
4865
4833
|
|
|
4866
4834
|
Args:
|
|
4867
|
-
input: The input to this
|
|
4868
|
-
config: The config to use.
|
|
4869
|
-
kwargs: Additional keyword arguments.
|
|
4835
|
+
input: The input to this `Runnable`.
|
|
4836
|
+
config: The config to use.
|
|
4837
|
+
**kwargs: Additional keyword arguments.
|
|
4870
4838
|
|
|
4871
4839
|
Returns:
|
|
4872
|
-
The output of this
|
|
4840
|
+
The output of this `Runnable`.
|
|
4873
4841
|
|
|
4874
4842
|
"""
|
|
4875
4843
|
return await self._acall_with_config(
|
|
@@ -5102,12 +5070,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
5102
5070
|
class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
5103
5071
|
"""RunnableEachBase class.
|
|
5104
5072
|
|
|
5105
|
-
|
|
5073
|
+
`Runnable` that calls another `Runnable` for each element of the input sequence.
|
|
5106
5074
|
|
|
5107
|
-
Use only if creating a new
|
|
5075
|
+
Use only if creating a new `RunnableEach` subclass with different `__init__`
|
|
5108
5076
|
args.
|
|
5109
5077
|
|
|
5110
|
-
See documentation for
|
|
5078
|
+
See documentation for `RunnableEach` for more details.
|
|
5111
5079
|
|
|
5112
5080
|
"""
|
|
5113
5081
|
|
|
@@ -5131,7 +5099,7 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5131
5099
|
None,
|
|
5132
5100
|
),
|
|
5133
5101
|
# create model needs access to appropriate type annotations to be
|
|
5134
|
-
# able to construct the
|
|
5102
|
+
# able to construct the Pydantic model.
|
|
5135
5103
|
# When we create the model, we pass information about the namespace
|
|
5136
5104
|
# where the model is being created, so the type annotations can
|
|
5137
5105
|
# be resolved correctly as well.
|
|
@@ -5154,7 +5122,7 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5154
5122
|
self.get_name("Output"),
|
|
5155
5123
|
root=list[schema], # type: ignore[valid-type]
|
|
5156
5124
|
# create model needs access to appropriate type annotations to be
|
|
5157
|
-
# able to construct the
|
|
5125
|
+
# able to construct the Pydantic model.
|
|
5158
5126
|
# When we create the model, we pass information about the namespace
|
|
5159
5127
|
# where the model is being created, so the type annotations can
|
|
5160
5128
|
# be resolved correctly as well.
|
|
@@ -5181,10 +5149,10 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5181
5149
|
@classmethod
|
|
5182
5150
|
@override
|
|
5183
5151
|
def get_lc_namespace(cls) -> list[str]:
|
|
5184
|
-
"""Get the namespace of the
|
|
5152
|
+
"""Get the namespace of the LangChain object.
|
|
5185
5153
|
|
|
5186
5154
|
Returns:
|
|
5187
|
-
|
|
5155
|
+
`["langchain", "schema", "runnable"]`
|
|
5188
5156
|
"""
|
|
5189
5157
|
return ["langchain", "schema", "runnable"]
|
|
5190
5158
|
|
|
@@ -5243,13 +5211,13 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5243
5211
|
class RunnableEach(RunnableEachBase[Input, Output]):
|
|
5244
5212
|
"""RunnableEach class.
|
|
5245
5213
|
|
|
5246
|
-
|
|
5214
|
+
`Runnable` that calls another `Runnable` for each element of the input sequence.
|
|
5247
5215
|
|
|
5248
|
-
It allows you to call multiple inputs with the bounded
|
|
5216
|
+
It allows you to call multiple inputs with the bounded `Runnable`.
|
|
5249
5217
|
|
|
5250
|
-
|
|
5218
|
+
`RunnableEach` makes it easy to run multiple inputs for the `Runnable`.
|
|
5251
5219
|
In the below example, we associate and run three inputs
|
|
5252
|
-
with a
|
|
5220
|
+
with a `Runnable`:
|
|
5253
5221
|
|
|
5254
5222
|
```python
|
|
5255
5223
|
from langchain_core.runnables.base import RunnableEach
|
|
@@ -5299,22 +5267,22 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5299
5267
|
| Callable[[Run, RunnableConfig], None]
|
|
5300
5268
|
| None = None,
|
|
5301
5269
|
) -> RunnableEach[Input, Output]:
|
|
5302
|
-
"""Bind lifecycle listeners to a
|
|
5270
|
+
"""Bind lifecycle listeners to a `Runnable`, returning a new `Runnable`.
|
|
5303
5271
|
|
|
5304
|
-
The
|
|
5305
|
-
|
|
5272
|
+
The `Run` object contains information about the run, including its `id`,
|
|
5273
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
5306
5274
|
any tags or metadata added to the run.
|
|
5307
5275
|
|
|
5308
5276
|
Args:
|
|
5309
|
-
on_start: Called before the
|
|
5310
|
-
object.
|
|
5311
|
-
on_end: Called after the
|
|
5312
|
-
object.
|
|
5313
|
-
on_error: Called if the
|
|
5314
|
-
object.
|
|
5277
|
+
on_start: Called before the `Runnable` starts running, with the `Run`
|
|
5278
|
+
object.
|
|
5279
|
+
on_end: Called after the `Runnable` finishes running, with the `Run`
|
|
5280
|
+
object.
|
|
5281
|
+
on_error: Called if the `Runnable` throws an error, with the `Run`
|
|
5282
|
+
object.
|
|
5315
5283
|
|
|
5316
5284
|
Returns:
|
|
5317
|
-
A new
|
|
5285
|
+
A new `Runnable` with the listeners bound.
|
|
5318
5286
|
|
|
5319
5287
|
"""
|
|
5320
5288
|
return RunnableEach(
|
|
@@ -5330,24 +5298,24 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5330
5298
|
on_end: AsyncListener | None = None,
|
|
5331
5299
|
on_error: AsyncListener | None = None,
|
|
5332
5300
|
) -> RunnableEach[Input, Output]:
|
|
5333
|
-
"""Bind async lifecycle listeners to a
|
|
5301
|
+
"""Bind async lifecycle listeners to a `Runnable`.
|
|
5334
5302
|
|
|
5335
|
-
Returns a new
|
|
5303
|
+
Returns a new `Runnable`.
|
|
5336
5304
|
|
|
5337
|
-
The
|
|
5338
|
-
|
|
5305
|
+
The `Run` object contains information about the run, including its `id`,
|
|
5306
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
5339
5307
|
any tags or metadata added to the run.
|
|
5340
5308
|
|
|
5341
5309
|
Args:
|
|
5342
|
-
on_start: Called asynchronously before the
|
|
5343
|
-
with the
|
|
5344
|
-
on_end: Called asynchronously after the
|
|
5345
|
-
with the
|
|
5346
|
-
on_error: Called asynchronously if the
|
|
5347
|
-
with the
|
|
5310
|
+
on_start: Called asynchronously before the `Runnable` starts running,
|
|
5311
|
+
with the `Run` object.
|
|
5312
|
+
on_end: Called asynchronously after the `Runnable` finishes running,
|
|
5313
|
+
with the `Run` object.
|
|
5314
|
+
on_error: Called asynchronously if the `Runnable` throws an error,
|
|
5315
|
+
with the `Run` object.
|
|
5348
5316
|
|
|
5349
5317
|
Returns:
|
|
5350
|
-
A new
|
|
5318
|
+
A new `Runnable` with the listeners bound.
|
|
5351
5319
|
|
|
5352
5320
|
"""
|
|
5353
5321
|
return RunnableEach(
|
|
@@ -5358,46 +5326,46 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5358
5326
|
|
|
5359
5327
|
|
|
5360
5328
|
class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[no-redef]
|
|
5361
|
-
"""
|
|
5329
|
+
"""`Runnable` that delegates calls to another `Runnable` with a set of kwargs.
|
|
5362
5330
|
|
|
5363
|
-
Use only if creating a new
|
|
5331
|
+
Use only if creating a new `RunnableBinding` subclass with different `__init__`
|
|
5364
5332
|
args.
|
|
5365
5333
|
|
|
5366
|
-
See documentation for
|
|
5334
|
+
See documentation for `RunnableBinding` for more details.
|
|
5367
5335
|
|
|
5368
5336
|
"""
|
|
5369
5337
|
|
|
5370
5338
|
bound: Runnable[Input, Output]
|
|
5371
|
-
"""The underlying
|
|
5339
|
+
"""The underlying `Runnable` that this `Runnable` delegates to."""
|
|
5372
5340
|
|
|
5373
5341
|
kwargs: Mapping[str, Any] = Field(default_factory=dict)
|
|
5374
|
-
"""kwargs to pass to the underlying
|
|
5342
|
+
"""kwargs to pass to the underlying `Runnable` when running.
|
|
5375
5343
|
|
|
5376
|
-
For example, when the
|
|
5377
|
-
|
|
5344
|
+
For example, when the `Runnable` binding is invoked the underlying
|
|
5345
|
+
`Runnable` will be invoked with the same input but with these additional
|
|
5378
5346
|
kwargs.
|
|
5379
5347
|
|
|
5380
5348
|
"""
|
|
5381
5349
|
|
|
5382
5350
|
config: RunnableConfig = Field(default_factory=RunnableConfig)
|
|
5383
|
-
"""The config to bind to the underlying
|
|
5351
|
+
"""The config to bind to the underlying `Runnable`."""
|
|
5384
5352
|
|
|
5385
5353
|
config_factories: list[Callable[[RunnableConfig], RunnableConfig]] = Field(
|
|
5386
5354
|
default_factory=list
|
|
5387
5355
|
)
|
|
5388
|
-
"""The config factories to bind to the underlying
|
|
5356
|
+
"""The config factories to bind to the underlying `Runnable`."""
|
|
5389
5357
|
|
|
5390
5358
|
# Union[Type[Input], BaseModel] + things like list[str]
|
|
5391
5359
|
custom_input_type: Any | None = None
|
|
5392
|
-
"""Override the input type of the underlying
|
|
5360
|
+
"""Override the input type of the underlying `Runnable` with a custom type.
|
|
5393
5361
|
|
|
5394
|
-
The type can be a
|
|
5362
|
+
The type can be a Pydantic model, or a type annotation (e.g., `list[str]`).
|
|
5395
5363
|
"""
|
|
5396
5364
|
# Union[Type[Output], BaseModel] + things like list[str]
|
|
5397
5365
|
custom_output_type: Any | None = None
|
|
5398
|
-
"""Override the output type of the underlying
|
|
5366
|
+
"""Override the output type of the underlying `Runnable` with a custom type.
|
|
5399
5367
|
|
|
5400
|
-
The type can be a
|
|
5368
|
+
The type can be a Pydantic model, or a type annotation (e.g., `list[str]`).
|
|
5401
5369
|
"""
|
|
5402
5370
|
|
|
5403
5371
|
model_config = ConfigDict(
|
|
@@ -5416,24 +5384,24 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
|
|
|
5416
5384
|
custom_output_type: type[Output] | BaseModel | None = None,
|
|
5417
5385
|
**other_kwargs: Any,
|
|
5418
5386
|
) -> None:
|
|
5419
|
-
"""Create a
|
|
5387
|
+
"""Create a `RunnableBinding` from a `Runnable` and kwargs.
|
|
5420
5388
|
|
|
5421
5389
|
Args:
|
|
5422
|
-
bound: The underlying
|
|
5390
|
+
bound: The underlying `Runnable` that this `Runnable` delegates calls
|
|
5423
5391
|
to.
|
|
5424
|
-
kwargs: optional kwargs to pass to the underlying
|
|
5425
|
-
the underlying
|
|
5426
|
-
|
|
5427
|
-
|
|
5428
|
-
config: optional config to bind to the underlying
|
|
5429
|
-
|
|
5392
|
+
kwargs: optional kwargs to pass to the underlying `Runnable`, when running
|
|
5393
|
+
the underlying `Runnable` (e.g., via `invoke`, `batch`,
|
|
5394
|
+
`transform`, or `stream` or async variants)
|
|
5395
|
+
|
|
5396
|
+
config: optional config to bind to the underlying `Runnable`.
|
|
5397
|
+
|
|
5430
5398
|
config_factories: optional list of config factories to apply to the
|
|
5431
|
-
config before binding to the underlying
|
|
5432
|
-
|
|
5399
|
+
config before binding to the underlying `Runnable`.
|
|
5400
|
+
|
|
5433
5401
|
custom_input_type: Specify to override the input type of the underlying
|
|
5434
|
-
|
|
5402
|
+
`Runnable` with a custom type.
|
|
5435
5403
|
custom_output_type: Specify to override the output type of the underlying
|
|
5436
|
-
|
|
5404
|
+
`Runnable` with a custom type.
|
|
5437
5405
|
**other_kwargs: Unpacked into the base class.
|
|
5438
5406
|
"""
|
|
5439
5407
|
super().__init__(
|
|
@@ -5504,10 +5472,10 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
|
|
|
5504
5472
|
@classmethod
|
|
5505
5473
|
@override
|
|
5506
5474
|
def get_lc_namespace(cls) -> list[str]:
|
|
5507
|
-
"""Get the namespace of the
|
|
5475
|
+
"""Get the namespace of the LangChain object.
|
|
5508
5476
|
|
|
5509
5477
|
Returns:
|
|
5510
|
-
|
|
5478
|
+
`["langchain", "schema", "runnable"]`
|
|
5511
5479
|
"""
|
|
5512
5480
|
return ["langchain", "schema", "runnable"]
|
|
5513
5481
|
|
|
@@ -5760,33 +5728,33 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
|
|
|
5760
5728
|
|
|
5761
5729
|
|
|
5762
5730
|
class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
|
|
5763
|
-
"""Wrap a
|
|
5731
|
+
"""Wrap a `Runnable` with additional functionality.
|
|
5764
5732
|
|
|
5765
|
-
A
|
|
5766
|
-
preserves the essential features of
|
|
5733
|
+
A `RunnableBinding` can be thought of as a "runnable decorator" that
|
|
5734
|
+
preserves the essential features of `Runnable`; i.e., batching, streaming,
|
|
5767
5735
|
and async support, while adding additional functionality.
|
|
5768
5736
|
|
|
5769
|
-
Any class that inherits from
|
|
5770
|
-
Runnables expose a standard set of methods for creating
|
|
5771
|
-
or sub-classes of
|
|
5772
|
-
|
|
5737
|
+
Any class that inherits from `Runnable` can be bound to a `RunnableBinding`.
|
|
5738
|
+
Runnables expose a standard set of methods for creating `RunnableBindings`
|
|
5739
|
+
or sub-classes of `RunnableBindings` (e.g., `RunnableRetry`,
|
|
5740
|
+
`RunnableWithFallbacks`) that add additional functionality.
|
|
5773
5741
|
|
|
5774
5742
|
These methods include:
|
|
5775
5743
|
|
|
5776
|
-
-
|
|
5777
|
-
-
|
|
5778
|
-
|
|
5779
|
-
-
|
|
5780
|
-
-
|
|
5781
|
-
|
|
5782
|
-
-
|
|
5783
|
-
-
|
|
5744
|
+
- `bind`: Bind kwargs to pass to the underlying `Runnable` when running it.
|
|
5745
|
+
- `with_config`: Bind config to pass to the underlying `Runnable` when running
|
|
5746
|
+
it.
|
|
5747
|
+
- `with_listeners`: Bind lifecycle listeners to the underlying `Runnable`.
|
|
5748
|
+
- `with_types`: Override the input and output types of the underlying
|
|
5749
|
+
`Runnable`.
|
|
5750
|
+
- `with_retry`: Bind a retry policy to the underlying `Runnable`.
|
|
5751
|
+
- `with_fallbacks`: Bind a fallback policy to the underlying `Runnable`.
|
|
5784
5752
|
|
|
5785
5753
|
Example:
|
|
5786
|
-
`bind`: Bind kwargs to pass to the underlying
|
|
5754
|
+
`bind`: Bind kwargs to pass to the underlying `Runnable` when running it.
|
|
5787
5755
|
|
|
5788
5756
|
```python
|
|
5789
|
-
# Create a Runnable binding that invokes the
|
|
5757
|
+
# Create a Runnable binding that invokes the chat model with the
|
|
5790
5758
|
# additional kwarg `stop=['-']` when running it.
|
|
5791
5759
|
from langchain_community.chat_models import ChatOpenAI
|
|
5792
5760
|
|
|
@@ -5797,7 +5765,7 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-re
|
|
|
5797
5765
|
runnable_binding = model.bind(stop=["-"])
|
|
5798
5766
|
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`
|
|
5799
5767
|
```
|
|
5800
|
-
Can also be done by instantiating a
|
|
5768
|
+
Can also be done by instantiating a `RunnableBinding` directly (not
|
|
5801
5769
|
recommended):
|
|
5802
5770
|
|
|
5803
5771
|
```python
|
|
@@ -5813,13 +5781,13 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-re
|
|
|
5813
5781
|
|
|
5814
5782
|
@override
|
|
5815
5783
|
def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
|
|
5816
|
-
"""Bind additional kwargs to a
|
|
5784
|
+
"""Bind additional kwargs to a `Runnable`, returning a new `Runnable`.
|
|
5817
5785
|
|
|
5818
5786
|
Args:
|
|
5819
|
-
**kwargs: The kwargs to bind to the
|
|
5787
|
+
**kwargs: The kwargs to bind to the `Runnable`.
|
|
5820
5788
|
|
|
5821
5789
|
Returns:
|
|
5822
|
-
A new
|
|
5790
|
+
A new `Runnable` with the same type and config as the original,
|
|
5823
5791
|
but with the additional kwargs bound.
|
|
5824
5792
|
|
|
5825
5793
|
"""
|
|
@@ -5862,22 +5830,22 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-re
|
|
|
5862
5830
|
| Callable[[Run, RunnableConfig], None]
|
|
5863
5831
|
| None = None,
|
|
5864
5832
|
) -> Runnable[Input, Output]:
|
|
5865
|
-
"""Bind lifecycle listeners to a
|
|
5833
|
+
"""Bind lifecycle listeners to a `Runnable`, returning a new `Runnable`.
|
|
5866
5834
|
|
|
5867
|
-
The
|
|
5868
|
-
|
|
5835
|
+
The `Run` object contains information about the run, including its `id`,
|
|
5836
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
5869
5837
|
any tags or metadata added to the run.
|
|
5870
5838
|
|
|
5871
5839
|
Args:
|
|
5872
|
-
on_start: Called before the
|
|
5873
|
-
object.
|
|
5874
|
-
on_end: Called after the
|
|
5875
|
-
object.
|
|
5876
|
-
on_error: Called if the
|
|
5877
|
-
object.
|
|
5840
|
+
on_start: Called before the `Runnable` starts running, with the `Run`
|
|
5841
|
+
object.
|
|
5842
|
+
on_end: Called after the `Runnable` finishes running, with the `Run`
|
|
5843
|
+
object.
|
|
5844
|
+
on_error: Called if the `Runnable` throws an error, with the `Run`
|
|
5845
|
+
object.
|
|
5878
5846
|
|
|
5879
5847
|
Returns:
|
|
5880
|
-
A new
|
|
5848
|
+
A new `Runnable` with the listeners bound.
|
|
5881
5849
|
"""
|
|
5882
5850
|
|
|
5883
5851
|
def listener_config_factory(config: RunnableConfig) -> RunnableConfig:
|
|
@@ -6004,16 +5972,16 @@ RunnableLike = (
|
|
|
6004
5972
|
|
|
6005
5973
|
|
|
6006
5974
|
def coerce_to_runnable(thing: RunnableLike) -> Runnable[Input, Output]:
|
|
6007
|
-
"""Coerce a
|
|
5975
|
+
"""Coerce a `Runnable`-like object into a `Runnable`.
|
|
6008
5976
|
|
|
6009
5977
|
Args:
|
|
6010
|
-
thing: A
|
|
5978
|
+
thing: A `Runnable`-like object.
|
|
6011
5979
|
|
|
6012
5980
|
Returns:
|
|
6013
|
-
A
|
|
5981
|
+
A `Runnable`.
|
|
6014
5982
|
|
|
6015
5983
|
Raises:
|
|
6016
|
-
TypeError: If the object is not
|
|
5984
|
+
TypeError: If the object is not `Runnable`-like.
|
|
6017
5985
|
"""
|
|
6018
5986
|
if isinstance(thing, Runnable):
|
|
6019
5987
|
return thing
|
|
@@ -6060,16 +6028,16 @@ def chain(
|
|
|
6060
6028
|
| Callable[[Input], Coroutine[Any, Any, Output]]
|
|
6061
6029
|
| Callable[[Input], AsyncIterator[Output]],
|
|
6062
6030
|
) -> Runnable[Input, Output]:
|
|
6063
|
-
"""Decorate a function to make it a
|
|
6031
|
+
"""Decorate a function to make it a `Runnable`.
|
|
6064
6032
|
|
|
6065
|
-
Sets the name of the
|
|
6033
|
+
Sets the name of the `Runnable` to the name of the function.
|
|
6066
6034
|
Any runnables called by the function will be traced as dependencies.
|
|
6067
6035
|
|
|
6068
6036
|
Args:
|
|
6069
|
-
func: A
|
|
6037
|
+
func: A `Callable`.
|
|
6070
6038
|
|
|
6071
6039
|
Returns:
|
|
6072
|
-
A
|
|
6040
|
+
A `Runnable`.
|
|
6073
6041
|
|
|
6074
6042
|
Example:
|
|
6075
6043
|
```python
|
|
@@ -6081,10 +6049,10 @@ def chain(
|
|
|
6081
6049
|
@chain
|
|
6082
6050
|
def my_func(fields):
|
|
6083
6051
|
prompt = PromptTemplate("Hello, {name}!")
|
|
6084
|
-
|
|
6052
|
+
model = OpenAI()
|
|
6085
6053
|
formatted = prompt.invoke(**fields)
|
|
6086
6054
|
|
|
6087
|
-
for chunk in
|
|
6055
|
+
for chunk in model.stream(formatted):
|
|
6088
6056
|
yield chunk
|
|
6089
6057
|
```
|
|
6090
6058
|
"""
|