langchain-core 1.0.0a8__py3-none-any.whl → 1.0.0rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +0 -1
- langchain_core/_api/beta_decorator.py +17 -20
- langchain_core/_api/deprecation.py +30 -35
- langchain_core/_import_utils.py +1 -1
- langchain_core/agents.py +7 -6
- langchain_core/caches.py +4 -10
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +232 -243
- langchain_core/callbacks/file.py +33 -33
- langchain_core/callbacks/manager.py +353 -416
- langchain_core/callbacks/stdout.py +21 -22
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +54 -51
- langchain_core/chat_history.py +42 -57
- langchain_core/document_loaders/langsmith.py +21 -21
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +37 -40
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +46 -52
- langchain_core/exceptions.py +5 -5
- langchain_core/indexing/api.py +11 -11
- langchain_core/indexing/base.py +24 -24
- langchain_core/language_models/__init__.py +0 -2
- langchain_core/language_models/_utils.py +51 -53
- langchain_core/language_models/base.py +23 -24
- langchain_core/language_models/chat_models.py +121 -144
- langchain_core/language_models/fake_chat_models.py +5 -5
- langchain_core/language_models/llms.py +10 -12
- langchain_core/load/dump.py +1 -1
- langchain_core/load/load.py +16 -16
- langchain_core/load/serializable.py +35 -34
- langchain_core/messages/__init__.py +1 -16
- langchain_core/messages/ai.py +105 -104
- langchain_core/messages/base.py +26 -26
- langchain_core/messages/block_translators/__init__.py +17 -17
- langchain_core/messages/block_translators/anthropic.py +2 -2
- langchain_core/messages/block_translators/bedrock_converse.py +2 -2
- langchain_core/messages/block_translators/google_genai.py +2 -2
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +2 -2
- langchain_core/messages/block_translators/openai.py +4 -4
- langchain_core/messages/chat.py +1 -1
- langchain_core/messages/content.py +189 -193
- langchain_core/messages/function.py +5 -5
- langchain_core/messages/human.py +15 -17
- langchain_core/messages/modifier.py +1 -1
- langchain_core/messages/system.py +12 -14
- langchain_core/messages/tool.py +45 -49
- langchain_core/messages/utils.py +384 -396
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +22 -23
- langchain_core/output_parsers/json.py +3 -3
- langchain_core/output_parsers/list.py +1 -1
- langchain_core/output_parsers/openai_functions.py +46 -44
- langchain_core/output_parsers/openai_tools.py +7 -7
- langchain_core/output_parsers/pydantic.py +10 -11
- langchain_core/output_parsers/string.py +1 -1
- langchain_core/output_parsers/transform.py +2 -2
- langchain_core/output_parsers/xml.py +1 -1
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +14 -14
- langchain_core/outputs/generation.py +5 -5
- langchain_core/outputs/llm_result.py +5 -5
- langchain_core/prompt_values.py +5 -5
- langchain_core/prompts/__init__.py +3 -23
- langchain_core/prompts/base.py +32 -37
- langchain_core/prompts/chat.py +216 -222
- langchain_core/prompts/dict.py +2 -2
- langchain_core/prompts/few_shot.py +76 -83
- langchain_core/prompts/few_shot_with_templates.py +6 -8
- langchain_core/prompts/image.py +11 -13
- langchain_core/prompts/loading.py +1 -1
- langchain_core/prompts/message.py +2 -2
- langchain_core/prompts/prompt.py +14 -16
- langchain_core/prompts/string.py +19 -7
- langchain_core/prompts/structured.py +24 -25
- langchain_core/rate_limiters.py +36 -38
- langchain_core/retrievers.py +41 -182
- langchain_core/runnables/base.py +565 -590
- langchain_core/runnables/branch.py +7 -7
- langchain_core/runnables/config.py +37 -44
- langchain_core/runnables/configurable.py +8 -9
- langchain_core/runnables/fallbacks.py +8 -8
- langchain_core/runnables/graph.py +28 -27
- langchain_core/runnables/graph_ascii.py +19 -18
- langchain_core/runnables/graph_mermaid.py +20 -31
- langchain_core/runnables/graph_png.py +7 -7
- langchain_core/runnables/history.py +20 -20
- langchain_core/runnables/passthrough.py +8 -8
- langchain_core/runnables/retry.py +3 -3
- langchain_core/runnables/router.py +1 -1
- langchain_core/runnables/schema.py +33 -33
- langchain_core/runnables/utils.py +30 -34
- langchain_core/stores.py +72 -102
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +63 -63
- langchain_core/tools/convert.py +92 -92
- langchain_core/tools/render.py +9 -9
- langchain_core/tools/retriever.py +1 -1
- langchain_core/tools/simple.py +6 -7
- langchain_core/tools/structured.py +17 -18
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +35 -35
- langchain_core/tracers/context.py +12 -17
- langchain_core/tracers/event_stream.py +3 -3
- langchain_core/tracers/langchain.py +8 -8
- langchain_core/tracers/log_stream.py +17 -18
- langchain_core/tracers/memory_stream.py +2 -2
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/utils/aiter.py +31 -31
- langchain_core/utils/env.py +5 -5
- langchain_core/utils/function_calling.py +48 -120
- langchain_core/utils/html.py +4 -4
- langchain_core/utils/input.py +2 -2
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +19 -19
- langchain_core/utils/json.py +1 -1
- langchain_core/utils/json_schema.py +2 -2
- langchain_core/utils/mustache.py +5 -5
- langchain_core/utils/pydantic.py +17 -17
- langchain_core/utils/strings.py +4 -4
- langchain_core/utils/utils.py +25 -28
- langchain_core/vectorstores/base.py +43 -64
- langchain_core/vectorstores/in_memory.py +83 -85
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc1.dist-info}/METADATA +23 -11
- langchain_core-1.0.0rc1.dist-info/RECORD +172 -0
- langchain_core/memory.py +0 -120
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core-1.0.0a8.dist-info/RECORD +0 -176
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc1.dist-info}/WHEEL +0 -0
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,21 @@ 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
|
-
Defaults to False
|
|
877
|
-
**kwargs: Additional keyword arguments to pass to the
|
|
874
|
+
Defaults to `False`.
|
|
875
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
878
876
|
|
|
879
877
|
Returns:
|
|
880
|
-
A list of outputs from the
|
|
878
|
+
A list of outputs from the `Runnable`.
|
|
881
879
|
"""
|
|
882
880
|
if not inputs:
|
|
883
881
|
return []
|
|
@@ -928,23 +926,23 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
928
926
|
return_exceptions: bool = False,
|
|
929
927
|
**kwargs: Any | None,
|
|
930
928
|
) -> Iterator[tuple[int, Output | Exception]]:
|
|
931
|
-
"""Run
|
|
929
|
+
"""Run `invoke` in parallel on a list of inputs.
|
|
932
930
|
|
|
933
931
|
Yields results as they complete.
|
|
934
932
|
|
|
935
933
|
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.
|
|
934
|
+
inputs: A list of inputs to the `Runnable`.
|
|
935
|
+
config: A config to use when invoking the `Runnable`.
|
|
936
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
937
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
938
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
939
|
+
for more details.
|
|
942
940
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
943
|
-
Defaults to False
|
|
944
|
-
**kwargs: Additional keyword arguments to pass to the
|
|
941
|
+
Defaults to `False`.
|
|
942
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
945
943
|
|
|
946
944
|
Yields:
|
|
947
|
-
Tuples of the index of the input and the output from the
|
|
945
|
+
Tuples of the index of the input and the output from the `Runnable`.
|
|
948
946
|
|
|
949
947
|
"""
|
|
950
948
|
if not inputs:
|
|
@@ -992,26 +990,26 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
992
990
|
return_exceptions: bool = False,
|
|
993
991
|
**kwargs: Any | None,
|
|
994
992
|
) -> list[Output]:
|
|
995
|
-
"""Default implementation runs
|
|
993
|
+
"""Default implementation runs `ainvoke` in parallel using `asyncio.gather`.
|
|
996
994
|
|
|
997
|
-
The default implementation of
|
|
995
|
+
The default implementation of `batch` works well for IO bound runnables.
|
|
998
996
|
|
|
999
997
|
Subclasses should override this method if they can batch more efficiently;
|
|
1000
|
-
e.g., if the underlying
|
|
998
|
+
e.g., if the underlying `Runnable` uses an API which supports a batch mode.
|
|
1001
999
|
|
|
1002
1000
|
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.
|
|
1001
|
+
inputs: A list of inputs to the `Runnable`.
|
|
1002
|
+
config: A config to use when invoking the `Runnable`.
|
|
1003
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
1004
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
1005
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
1006
|
+
for more details.
|
|
1009
1007
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
1010
|
-
Defaults to False
|
|
1011
|
-
**kwargs: Additional keyword arguments to pass to the
|
|
1008
|
+
Defaults to `False`.
|
|
1009
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1012
1010
|
|
|
1013
1011
|
Returns:
|
|
1014
|
-
A list of outputs from the
|
|
1012
|
+
A list of outputs from the `Runnable`.
|
|
1015
1013
|
|
|
1016
1014
|
"""
|
|
1017
1015
|
if not inputs:
|
|
@@ -1059,23 +1057,23 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1059
1057
|
return_exceptions: bool = False,
|
|
1060
1058
|
**kwargs: Any | None,
|
|
1061
1059
|
) -> AsyncIterator[tuple[int, Output | Exception]]:
|
|
1062
|
-
"""Run
|
|
1060
|
+
"""Run `ainvoke` in parallel on a list of inputs.
|
|
1063
1061
|
|
|
1064
1062
|
Yields results as they complete.
|
|
1065
1063
|
|
|
1066
1064
|
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.
|
|
1065
|
+
inputs: A list of inputs to the `Runnable`.
|
|
1066
|
+
config: A config to use when invoking the `Runnable`.
|
|
1067
|
+
The config supports standard keys like `'tags'`, `'metadata'` for
|
|
1068
|
+
tracing purposes, `'max_concurrency'` for controlling how much work to
|
|
1069
|
+
do in parallel, and other keys. Please refer to the `RunnableConfig`
|
|
1070
|
+
for more details.
|
|
1073
1071
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
1074
|
-
Defaults to False
|
|
1075
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1072
|
+
Defaults to `False`.
|
|
1073
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1076
1074
|
|
|
1077
1075
|
Yields:
|
|
1078
|
-
A tuple of the index of the input and the output from the
|
|
1076
|
+
A tuple of the index of the input and the output from the `Runnable`.
|
|
1079
1077
|
|
|
1080
1078
|
"""
|
|
1081
1079
|
if not inputs:
|
|
@@ -1116,17 +1114,17 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1116
1114
|
config: RunnableConfig | None = None,
|
|
1117
1115
|
**kwargs: Any | None,
|
|
1118
1116
|
) -> Iterator[Output]:
|
|
1119
|
-
"""Default implementation of
|
|
1117
|
+
"""Default implementation of `stream`, which calls `invoke`.
|
|
1120
1118
|
|
|
1121
1119
|
Subclasses should override this method if they support streaming output.
|
|
1122
1120
|
|
|
1123
1121
|
Args:
|
|
1124
|
-
input: The input to the
|
|
1125
|
-
config: The config to use for the
|
|
1126
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1122
|
+
input: The input to the `Runnable`.
|
|
1123
|
+
config: The config to use for the `Runnable`.
|
|
1124
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1127
1125
|
|
|
1128
1126
|
Yields:
|
|
1129
|
-
The output of the
|
|
1127
|
+
The output of the `Runnable`.
|
|
1130
1128
|
|
|
1131
1129
|
"""
|
|
1132
1130
|
yield self.invoke(input, config, **kwargs)
|
|
@@ -1137,17 +1135,17 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1137
1135
|
config: RunnableConfig | None = None,
|
|
1138
1136
|
**kwargs: Any | None,
|
|
1139
1137
|
) -> AsyncIterator[Output]:
|
|
1140
|
-
"""Default implementation of
|
|
1138
|
+
"""Default implementation of `astream`, which calls `ainvoke`.
|
|
1141
1139
|
|
|
1142
1140
|
Subclasses should override this method if they support streaming output.
|
|
1143
1141
|
|
|
1144
1142
|
Args:
|
|
1145
|
-
input: The input to the
|
|
1146
|
-
config: The config to use for the
|
|
1147
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1143
|
+
input: The input to the `Runnable`.
|
|
1144
|
+
config: The config to use for the `Runnable`.
|
|
1145
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1148
1146
|
|
|
1149
1147
|
Yields:
|
|
1150
|
-
The output of the
|
|
1148
|
+
The output of the `Runnable`.
|
|
1151
1149
|
|
|
1152
1150
|
"""
|
|
1153
1151
|
yield await self.ainvoke(input, config, **kwargs)
|
|
@@ -1201,7 +1199,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1201
1199
|
exclude_tags: Sequence[str] | None = None,
|
|
1202
1200
|
**kwargs: Any,
|
|
1203
1201
|
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]:
|
|
1204
|
-
"""Stream all output from a
|
|
1202
|
+
"""Stream all output from a `Runnable`, as reported to the callback system.
|
|
1205
1203
|
|
|
1206
1204
|
This includes all inner runs of LLMs, Retrievers, Tools, etc.
|
|
1207
1205
|
|
|
@@ -1212,20 +1210,20 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1212
1210
|
The Jsonpatch ops can be applied in order to construct state.
|
|
1213
1211
|
|
|
1214
1212
|
Args:
|
|
1215
|
-
input: The input to the
|
|
1216
|
-
config: The config to use for the
|
|
1213
|
+
input: The input to the `Runnable`.
|
|
1214
|
+
config: The config to use for the `Runnable`.
|
|
1217
1215
|
diff: Whether to yield diffs between each step or the current state.
|
|
1218
|
-
with_streamed_output_list: Whether to yield the
|
|
1216
|
+
with_streamed_output_list: Whether to yield the `streamed_output` list.
|
|
1219
1217
|
include_names: Only include logs with these names.
|
|
1220
1218
|
include_types: Only include logs with these types.
|
|
1221
1219
|
include_tags: Only include logs with these tags.
|
|
1222
1220
|
exclude_names: Exclude logs with these names.
|
|
1223
1221
|
exclude_types: Exclude logs with these types.
|
|
1224
1222
|
exclude_tags: Exclude logs with these tags.
|
|
1225
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1223
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1226
1224
|
|
|
1227
1225
|
Yields:
|
|
1228
|
-
A
|
|
1226
|
+
A `RunLogPatch` or `RunLog` object.
|
|
1229
1227
|
|
|
1230
1228
|
"""
|
|
1231
1229
|
stream = LogStreamCallbackHandler(
|
|
@@ -1269,28 +1267,26 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1269
1267
|
) -> AsyncIterator[StreamEvent]:
|
|
1270
1268
|
"""Generate a stream of events.
|
|
1271
1269
|
|
|
1272
|
-
Use to create an iterator over
|
|
1273
|
-
about the progress of the
|
|
1270
|
+
Use to create an iterator over `StreamEvent` that provide real-time information
|
|
1271
|
+
about the progress of the `Runnable`, including `StreamEvent` from intermediate
|
|
1274
1272
|
results.
|
|
1275
1273
|
|
|
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]**
|
|
1274
|
+
A `StreamEvent` is a dictionary with the following schema:
|
|
1275
|
+
|
|
1276
|
+
- `event`: Event names are of the format:
|
|
1277
|
+
`on_[runnable_type]_(start|stream|end)`.
|
|
1278
|
+
- `name`: The name of the `Runnable` that generated the event.
|
|
1279
|
+
- `run_id`: Randomly generated ID associated with the given execution of the
|
|
1280
|
+
`Runnable` that emitted the event. A child `Runnable` that gets invoked as
|
|
1281
|
+
part of the execution of a parent `Runnable` is assigned its own unique ID.
|
|
1282
|
+
- `parent_ids`: The IDs of the parent runnables that generated the event. The
|
|
1283
|
+
root `Runnable` will have an empty list. The order of the parent IDs is from
|
|
1284
|
+
the root to the immediate parent. Only available for v2 version of the API.
|
|
1285
|
+
The v1 version of the API will return an empty list.
|
|
1286
|
+
- `tags`: The tags of the `Runnable` that generated the event.
|
|
1287
|
+
- `metadata`: The metadata of the `Runnable` that generated the event.
|
|
1288
|
+
- `data`: The data associated with the event. The contents of this field
|
|
1289
|
+
depend on the type of event. See the table below for more details.
|
|
1294
1290
|
|
|
1295
1291
|
Below is a table that illustrates some events that might be emitted by various
|
|
1296
1292
|
chains. Metadata fields have been omitted from the table for brevity.
|
|
@@ -1299,39 +1295,23 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1299
1295
|
!!! note
|
|
1300
1296
|
This reference table is for the v2 version of the schema.
|
|
1301
1297
|
|
|
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
|
-
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1298
|
+
| event | name | chunk | input | output |
|
|
1299
|
+
| ---------------------- | -------------------- | ----------------------------------- | ------------------------------------------------- | --------------------------------------------------- |
|
|
1300
|
+
| `on_chat_model_start` | `'[model name]'` | | `{"messages": [[SystemMessage, HumanMessage]]}` | |
|
|
1301
|
+
| `on_chat_model_stream` | `'[model name]'` | `AIMessageChunk(content="hello")` | | |
|
|
1302
|
+
| `on_chat_model_end` | `'[model name]'` | | `{"messages": [[SystemMessage, HumanMessage]]}` | `AIMessageChunk(content="hello world")` |
|
|
1303
|
+
| `on_llm_start` | `'[model name]'` | | `{'input': 'hello'}` | |
|
|
1304
|
+
| `on_llm_stream` | `'[model name]'` | `'Hello' ` | | |
|
|
1305
|
+
| `on_llm_end` | `'[model name]'` | | `'Hello human!'` | |
|
|
1306
|
+
| `on_chain_start` | `'format_docs'` | | | |
|
|
1307
|
+
| `on_chain_stream` | `'format_docs'` | `'hello world!, goodbye world!'` | | |
|
|
1308
|
+
| `on_chain_end` | `'format_docs'` | | `[Document(...)]` | `'hello world!, goodbye world!'` |
|
|
1309
|
+
| `on_tool_start` | `'some_tool'` | | `{"x": 1, "y": "2"}` | |
|
|
1310
|
+
| `on_tool_end` | `'some_tool'` | | | `{"x": 1, "y": "2"}` |
|
|
1311
|
+
| `on_retriever_start` | `'[retriever name]'` | | `{"query": "hello"}` | |
|
|
1312
|
+
| `on_retriever_end` | `'[retriever name]'` | | `{"query": "hello"}` | `[Document(...), ..]` |
|
|
1313
|
+
| `on_prompt_start` | `'[template_name]'` | | `{"question": "hello"}` | |
|
|
1314
|
+
| `on_prompt_end` | `'[template_name]'` | | `{"question": "hello"}` | `ChatPromptValue(messages: [SystemMessage, ...])` |
|
|
1335
1315
|
|
|
1336
1316
|
In addition to the standard events, users can also dispatch custom events (see example below).
|
|
1337
1317
|
|
|
@@ -1339,17 +1319,14 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1339
1319
|
|
|
1340
1320
|
A custom event has following format:
|
|
1341
1321
|
|
|
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
|
-
+-----------+------+-----------------------------------------------------------------------------------------------------------+
|
|
1322
|
+
| Attribute | Type | Description |
|
|
1323
|
+
| ----------- | ------ | --------------------------------------------------------------------------------------------------------- |
|
|
1324
|
+
| `name` | `str` | A user defined name for the event. |
|
|
1325
|
+
| `data` | `Any` | The data associated with the event. This can be anything, though we suggest making it JSON serializable. |
|
|
1349
1326
|
|
|
1350
1327
|
Here are declarations associated with the standard events shown above:
|
|
1351
1328
|
|
|
1352
|
-
|
|
1329
|
+
`format_docs`:
|
|
1353
1330
|
|
|
1354
1331
|
```python
|
|
1355
1332
|
def format_docs(docs: list[Document]) -> str:
|
|
@@ -1360,7 +1337,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1360
1337
|
format_docs = RunnableLambda(format_docs)
|
|
1361
1338
|
```
|
|
1362
1339
|
|
|
1363
|
-
|
|
1340
|
+
`some_tool`:
|
|
1364
1341
|
|
|
1365
1342
|
```python
|
|
1366
1343
|
@tool
|
|
@@ -1369,7 +1346,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1369
1346
|
return {"x": x, "y": y}
|
|
1370
1347
|
```
|
|
1371
1348
|
|
|
1372
|
-
|
|
1349
|
+
`prompt`:
|
|
1373
1350
|
|
|
1374
1351
|
```python
|
|
1375
1352
|
template = ChatPromptTemplate.from_messages(
|
|
@@ -1379,8 +1356,8 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1379
1356
|
]
|
|
1380
1357
|
).with_config({"run_name": "my_template", "tags": ["my_template"]})
|
|
1381
1358
|
```
|
|
1382
|
-
Example:
|
|
1383
1359
|
|
|
1360
|
+
Example:
|
|
1384
1361
|
```python
|
|
1385
1362
|
from langchain_core.runnables import RunnableLambda
|
|
1386
1363
|
|
|
@@ -1420,9 +1397,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1420
1397
|
]
|
|
1421
1398
|
```
|
|
1422
1399
|
|
|
1423
|
-
Example: Dispatch Custom Event
|
|
1424
|
-
|
|
1425
|
-
```python
|
|
1400
|
+
```python title="Example: Dispatch Custom Event"
|
|
1426
1401
|
from langchain_core.callbacks.manager import (
|
|
1427
1402
|
adispatch_custom_event,
|
|
1428
1403
|
)
|
|
@@ -1451,32 +1426,32 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1451
1426
|
|
|
1452
1427
|
async for event in slow_thing.astream_events("some_input", version="v2"):
|
|
1453
1428
|
print(event)
|
|
1454
|
-
|
|
1429
|
+
``
|
|
1455
1430
|
|
|
1456
1431
|
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
|
|
1432
|
+
input: The input to the `Runnable`.
|
|
1433
|
+
config: The config to use for the `Runnable`.
|
|
1434
|
+
version: The version of the schema to use either `'v2'` or `'v1'`.
|
|
1435
|
+
Users should use `'v2'`.
|
|
1436
|
+
`'v1'` is for backwards compatibility and will be deprecated
|
|
1437
|
+
in `0.4.0`.
|
|
1438
|
+
No default will be assigned until the API is stabilized.
|
|
1439
|
+
custom events will only be surfaced in `'v2'`.
|
|
1440
|
+
include_names: Only include events from `Runnable` objects with matching names.
|
|
1441
|
+
include_types: Only include events from `Runnable` objects with matching types.
|
|
1442
|
+
include_tags: Only include events from `Runnable` objects with matching tags.
|
|
1443
|
+
exclude_names: Exclude events from `Runnable` objects with matching names.
|
|
1444
|
+
exclude_types: Exclude events from `Runnable` objects with matching types.
|
|
1445
|
+
exclude_tags: Exclude events from `Runnable` objects with matching tags.
|
|
1446
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1447
|
+
These will be passed to `astream_log` as this implementation
|
|
1448
|
+
of `astream_events` is built on top of `astream_log`.
|
|
1474
1449
|
|
|
1475
1450
|
Yields:
|
|
1476
|
-
An async stream of
|
|
1451
|
+
An async stream of `StreamEvent`.
|
|
1477
1452
|
|
|
1478
1453
|
Raises:
|
|
1479
|
-
NotImplementedError: If the version is not
|
|
1454
|
+
NotImplementedError: If the version is not `'v1'` or `'v2'`.
|
|
1480
1455
|
|
|
1481
1456
|
""" # noqa: E501
|
|
1482
1457
|
if version == "v2":
|
|
@@ -1523,18 +1498,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1523
1498
|
) -> Iterator[Output]:
|
|
1524
1499
|
"""Transform inputs to outputs.
|
|
1525
1500
|
|
|
1526
|
-
Default implementation of transform, which buffers input and calls
|
|
1501
|
+
Default implementation of transform, which buffers input and calls `astream`.
|
|
1527
1502
|
|
|
1528
1503
|
Subclasses should override this method if they can start producing output while
|
|
1529
1504
|
input is still being generated.
|
|
1530
1505
|
|
|
1531
1506
|
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
|
|
1507
|
+
input: An iterator of inputs to the `Runnable`.
|
|
1508
|
+
config: The config to use for the `Runnable`.
|
|
1509
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1535
1510
|
|
|
1536
1511
|
Yields:
|
|
1537
|
-
The output of the
|
|
1512
|
+
The output of the `Runnable`.
|
|
1538
1513
|
|
|
1539
1514
|
"""
|
|
1540
1515
|
final: Input
|
|
@@ -1568,18 +1543,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1568
1543
|
) -> AsyncIterator[Output]:
|
|
1569
1544
|
"""Transform inputs to outputs.
|
|
1570
1545
|
|
|
1571
|
-
Default implementation of atransform, which buffers input and calls
|
|
1546
|
+
Default implementation of atransform, which buffers input and calls `astream`.
|
|
1572
1547
|
|
|
1573
1548
|
Subclasses should override this method if they can start producing output while
|
|
1574
1549
|
input is still being generated.
|
|
1575
1550
|
|
|
1576
1551
|
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
|
|
1552
|
+
input: An async iterator of inputs to the `Runnable`.
|
|
1553
|
+
config: The config to use for the `Runnable`.
|
|
1554
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1580
1555
|
|
|
1581
1556
|
Yields:
|
|
1582
|
-
The output of the
|
|
1557
|
+
The output of the `Runnable`.
|
|
1583
1558
|
|
|
1584
1559
|
"""
|
|
1585
1560
|
final: Input
|
|
@@ -1607,32 +1582,32 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1607
1582
|
yield output
|
|
1608
1583
|
|
|
1609
1584
|
def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
|
|
1610
|
-
"""Bind arguments to a
|
|
1585
|
+
"""Bind arguments to a `Runnable`, returning a new `Runnable`.
|
|
1611
1586
|
|
|
1612
|
-
Useful when a
|
|
1613
|
-
in the output of the previous
|
|
1587
|
+
Useful when a `Runnable` in a chain requires an argument that is not
|
|
1588
|
+
in the output of the previous `Runnable` or included in the user input.
|
|
1614
1589
|
|
|
1615
1590
|
Args:
|
|
1616
|
-
kwargs: The arguments to bind to the
|
|
1591
|
+
**kwargs: The arguments to bind to the `Runnable`.
|
|
1617
1592
|
|
|
1618
1593
|
Returns:
|
|
1619
|
-
A new
|
|
1594
|
+
A new `Runnable` with the arguments bound.
|
|
1620
1595
|
|
|
1621
1596
|
Example:
|
|
1622
1597
|
```python
|
|
1623
1598
|
from langchain_ollama import ChatOllama
|
|
1624
1599
|
from langchain_core.output_parsers import StrOutputParser
|
|
1625
1600
|
|
|
1626
|
-
|
|
1601
|
+
model = ChatOllama(model="llama3.1")
|
|
1627
1602
|
|
|
1628
|
-
# Without bind
|
|
1629
|
-
chain =
|
|
1603
|
+
# Without bind
|
|
1604
|
+
chain = model | StrOutputParser()
|
|
1630
1605
|
|
|
1631
1606
|
chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
|
|
1632
1607
|
# Output is 'One two three four five.'
|
|
1633
1608
|
|
|
1634
|
-
# With bind
|
|
1635
|
-
chain =
|
|
1609
|
+
# With bind
|
|
1610
|
+
chain = model.bind(stop=["three"]) | StrOutputParser()
|
|
1636
1611
|
|
|
1637
1612
|
chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
|
|
1638
1613
|
# Output is 'One two'
|
|
@@ -1646,14 +1621,14 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1646
1621
|
# Sadly Unpack is not well-supported by mypy so this will have to be untyped
|
|
1647
1622
|
**kwargs: Any,
|
|
1648
1623
|
) -> Runnable[Input, Output]:
|
|
1649
|
-
"""Bind config to a
|
|
1624
|
+
"""Bind config to a `Runnable`, returning a new `Runnable`.
|
|
1650
1625
|
|
|
1651
1626
|
Args:
|
|
1652
|
-
config: The config to bind to the
|
|
1653
|
-
kwargs: Additional keyword arguments to pass to the
|
|
1627
|
+
config: The config to bind to the `Runnable`.
|
|
1628
|
+
**kwargs: Additional keyword arguments to pass to the `Runnable`.
|
|
1654
1629
|
|
|
1655
1630
|
Returns:
|
|
1656
|
-
A new
|
|
1631
|
+
A new `Runnable` with the config bound.
|
|
1657
1632
|
|
|
1658
1633
|
"""
|
|
1659
1634
|
return RunnableBinding(
|
|
@@ -1678,22 +1653,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1678
1653
|
| Callable[[Run, RunnableConfig], None]
|
|
1679
1654
|
| None = None,
|
|
1680
1655
|
) -> Runnable[Input, Output]:
|
|
1681
|
-
"""Bind lifecycle listeners to a
|
|
1656
|
+
"""Bind lifecycle listeners to a `Runnable`, returning a new `Runnable`.
|
|
1682
1657
|
|
|
1683
|
-
The Run object contains information about the run, including its
|
|
1684
|
-
|
|
1658
|
+
The Run object contains information about the run, including its `id`,
|
|
1659
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
1685
1660
|
any tags or metadata added to the run.
|
|
1686
1661
|
|
|
1687
1662
|
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.
|
|
1663
|
+
on_start: Called before the `Runnable` starts running, with the `Run`
|
|
1664
|
+
object.
|
|
1665
|
+
on_end: Called after the `Runnable` finishes running, with the `Run`
|
|
1666
|
+
object.
|
|
1667
|
+
on_error: Called if the `Runnable` throws an error, with the `Run`
|
|
1668
|
+
object.
|
|
1694
1669
|
|
|
1695
1670
|
Returns:
|
|
1696
|
-
A new
|
|
1671
|
+
A new `Runnable` with the listeners bound.
|
|
1697
1672
|
|
|
1698
1673
|
Example:
|
|
1699
1674
|
```python
|
|
@@ -1744,24 +1719,24 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1744
1719
|
on_end: AsyncListener | None = None,
|
|
1745
1720
|
on_error: AsyncListener | None = None,
|
|
1746
1721
|
) -> Runnable[Input, Output]:
|
|
1747
|
-
"""Bind async lifecycle listeners to a
|
|
1722
|
+
"""Bind async lifecycle listeners to a `Runnable`.
|
|
1748
1723
|
|
|
1749
|
-
Returns a new
|
|
1724
|
+
Returns a new `Runnable`.
|
|
1750
1725
|
|
|
1751
|
-
The Run object contains information about the run, including its
|
|
1752
|
-
|
|
1726
|
+
The Run object contains information about the run, including its `id`,
|
|
1727
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
1753
1728
|
any tags or metadata added to the run.
|
|
1754
1729
|
|
|
1755
1730
|
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
|
|
1731
|
+
on_start: Called asynchronously before the `Runnable` starts running,
|
|
1732
|
+
with the `Run` object.
|
|
1733
|
+
on_end: Called asynchronously after the `Runnable` finishes running,
|
|
1734
|
+
with the `Run` object.
|
|
1735
|
+
on_error: Called asynchronously if the `Runnable` throws an error,
|
|
1736
|
+
with the `Run` object.
|
|
1762
1737
|
|
|
1763
1738
|
Returns:
|
|
1764
|
-
A new
|
|
1739
|
+
A new `Runnable` with the listeners bound.
|
|
1765
1740
|
|
|
1766
1741
|
Example:
|
|
1767
1742
|
```python
|
|
@@ -1834,11 +1809,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1834
1809
|
input_type: type[Input] | None = None,
|
|
1835
1810
|
output_type: type[Output] | None = None,
|
|
1836
1811
|
) -> Runnable[Input, Output]:
|
|
1837
|
-
"""Bind input and output types to a
|
|
1812
|
+
"""Bind input and output types to a `Runnable`, returning a new `Runnable`.
|
|
1838
1813
|
|
|
1839
1814
|
Args:
|
|
1840
|
-
input_type: The input type to bind to the
|
|
1841
|
-
output_type: The output type to bind to the
|
|
1815
|
+
input_type: The input type to bind to the `Runnable`.
|
|
1816
|
+
output_type: The output type to bind to the `Runnable`.
|
|
1842
1817
|
|
|
1843
1818
|
Returns:
|
|
1844
1819
|
A new Runnable with the types bound.
|
|
@@ -1858,18 +1833,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1858
1833
|
exponential_jitter_params: ExponentialJitterParams | None = None,
|
|
1859
1834
|
stop_after_attempt: int = 3,
|
|
1860
1835
|
) -> Runnable[Input, Output]:
|
|
1861
|
-
"""Create a new Runnable that retries the original Runnable on exceptions.
|
|
1836
|
+
"""Create a new `Runnable` that retries the original `Runnable` on exceptions.
|
|
1862
1837
|
|
|
1863
1838
|
Args:
|
|
1864
1839
|
retry_if_exception_type: A tuple of exception types to retry on.
|
|
1865
1840
|
Defaults to (Exception,).
|
|
1866
1841
|
wait_exponential_jitter: Whether to add jitter to the wait
|
|
1867
|
-
time between retries. Defaults to True
|
|
1842
|
+
time between retries. Defaults to `True`.
|
|
1868
1843
|
stop_after_attempt: The maximum number of attempts to make before
|
|
1869
1844
|
giving up. Defaults to 3.
|
|
1870
1845
|
exponential_jitter_params: Parameters for
|
|
1871
|
-
|
|
1872
|
-
|
|
1846
|
+
`tenacity.wait_exponential_jitter`. Namely: `initial`, `max`,
|
|
1847
|
+
`exp_base`, and `jitter` (all float values).
|
|
1873
1848
|
|
|
1874
1849
|
Returns:
|
|
1875
1850
|
A new Runnable that retries the original Runnable on exceptions.
|
|
@@ -1916,12 +1891,12 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1916
1891
|
)
|
|
1917
1892
|
|
|
1918
1893
|
def map(self) -> Runnable[list[Input], list[Output]]:
|
|
1919
|
-
"""Return a new
|
|
1894
|
+
"""Return a new `Runnable` that maps a list of inputs to a list of outputs.
|
|
1920
1895
|
|
|
1921
|
-
Calls
|
|
1896
|
+
Calls `invoke` with each input.
|
|
1922
1897
|
|
|
1923
1898
|
Returns:
|
|
1924
|
-
A new
|
|
1899
|
+
A new `Runnable` that maps a list of inputs to a list of outputs.
|
|
1925
1900
|
|
|
1926
1901
|
Example:
|
|
1927
1902
|
```python
|
|
@@ -1945,25 +1920,25 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1945
1920
|
exceptions_to_handle: tuple[type[BaseException], ...] = (Exception,),
|
|
1946
1921
|
exception_key: str | None = None,
|
|
1947
1922
|
) -> RunnableWithFallbacksT[Input, Output]:
|
|
1948
|
-
"""Add fallbacks to a
|
|
1923
|
+
"""Add fallbacks to a `Runnable`, returning a new `Runnable`.
|
|
1949
1924
|
|
|
1950
|
-
The new
|
|
1925
|
+
The new `Runnable` will try the original `Runnable`, and then each fallback
|
|
1951
1926
|
in order, upon failures.
|
|
1952
1927
|
|
|
1953
1928
|
Args:
|
|
1954
|
-
fallbacks: A sequence of runnables to try if the original
|
|
1929
|
+
fallbacks: A sequence of runnables to try if the original `Runnable`
|
|
1955
1930
|
fails.
|
|
1956
1931
|
exceptions_to_handle: A tuple of exception types to handle.
|
|
1957
|
-
Defaults to
|
|
1932
|
+
Defaults to `(Exception,)`.
|
|
1958
1933
|
exception_key: If string is specified then handled exceptions will be passed
|
|
1959
1934
|
to fallbacks as part of the input under the specified key.
|
|
1960
|
-
If None
|
|
1961
|
-
If used, the base
|
|
1962
|
-
dictionary as input.
|
|
1935
|
+
If `None`, exceptions will not be passed to fallbacks.
|
|
1936
|
+
If used, the base `Runnable` and its fallbacks must accept a
|
|
1937
|
+
dictionary as input.
|
|
1963
1938
|
|
|
1964
1939
|
Returns:
|
|
1965
|
-
A new
|
|
1966
|
-
|
|
1940
|
+
A new `Runnable` that will try the original `Runnable`, and then each
|
|
1941
|
+
Fallback in order, upon failures.
|
|
1967
1942
|
|
|
1968
1943
|
Example:
|
|
1969
1944
|
```python
|
|
@@ -1988,18 +1963,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1988
1963
|
```
|
|
1989
1964
|
|
|
1990
1965
|
Args:
|
|
1991
|
-
fallbacks: A sequence of runnables to try if the original
|
|
1966
|
+
fallbacks: A sequence of runnables to try if the original `Runnable`
|
|
1992
1967
|
fails.
|
|
1993
1968
|
exceptions_to_handle: A tuple of exception types to handle.
|
|
1994
1969
|
exception_key: If string is specified then handled exceptions will be passed
|
|
1995
1970
|
to fallbacks as part of the input under the specified key.
|
|
1996
|
-
If None
|
|
1997
|
-
If used, the base
|
|
1971
|
+
If `None`, exceptions will not be passed to fallbacks.
|
|
1972
|
+
If used, the base `Runnable` and its fallbacks must accept a
|
|
1998
1973
|
dictionary as input.
|
|
1999
1974
|
|
|
2000
1975
|
Returns:
|
|
2001
|
-
A new
|
|
2002
|
-
|
|
1976
|
+
A new `Runnable` that will try the original `Runnable`, and then each
|
|
1977
|
+
Fallback in order, upon failures.
|
|
2003
1978
|
|
|
2004
1979
|
"""
|
|
2005
1980
|
# Import locally to prevent circular import
|
|
@@ -2029,10 +2004,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2029
2004
|
) -> Output:
|
|
2030
2005
|
"""Call with config.
|
|
2031
2006
|
|
|
2032
|
-
Helper method to transform an
|
|
2007
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2033
2008
|
with callbacks.
|
|
2034
2009
|
|
|
2035
|
-
Use this method to implement
|
|
2010
|
+
Use this method to implement `invoke` in subclasses.
|
|
2036
2011
|
|
|
2037
2012
|
"""
|
|
2038
2013
|
config = ensure_config(config)
|
|
@@ -2080,10 +2055,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2080
2055
|
) -> Output:
|
|
2081
2056
|
"""Async call with config.
|
|
2082
2057
|
|
|
2083
|
-
Helper method to transform an
|
|
2058
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2084
2059
|
with callbacks.
|
|
2085
2060
|
|
|
2086
|
-
Use this method to implement
|
|
2061
|
+
Use this method to implement `ainvoke` in subclasses.
|
|
2087
2062
|
"""
|
|
2088
2063
|
config = ensure_config(config)
|
|
2089
2064
|
callback_manager = get_async_callback_manager_for_config(config)
|
|
@@ -2127,8 +2102,8 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2127
2102
|
) -> list[Output]:
|
|
2128
2103
|
"""Transform a list of inputs to a list of outputs, with callbacks.
|
|
2129
2104
|
|
|
2130
|
-
Helper method to transform an
|
|
2131
|
-
with callbacks. Use this method to implement
|
|
2105
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2106
|
+
with callbacks. Use this method to implement `invoke` in subclasses.
|
|
2132
2107
|
|
|
2133
2108
|
"""
|
|
2134
2109
|
if not inputs:
|
|
@@ -2195,10 +2170,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2195
2170
|
) -> list[Output]:
|
|
2196
2171
|
"""Transform a list of inputs to a list of outputs, with callbacks.
|
|
2197
2172
|
|
|
2198
|
-
Helper method to transform an
|
|
2173
|
+
Helper method to transform an `Input` value to an `Output` value,
|
|
2199
2174
|
with callbacks.
|
|
2200
2175
|
|
|
2201
|
-
Use this method to implement
|
|
2176
|
+
Use this method to implement `invoke` in subclasses.
|
|
2202
2177
|
|
|
2203
2178
|
"""
|
|
2204
2179
|
if not inputs:
|
|
@@ -2265,10 +2240,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2265
2240
|
) -> Iterator[Output]:
|
|
2266
2241
|
"""Transform a stream with config.
|
|
2267
2242
|
|
|
2268
|
-
Helper method to transform an
|
|
2269
|
-
|
|
2243
|
+
Helper method to transform an `Iterator` of `Input` values into an
|
|
2244
|
+
`Iterator` of `Output` values, with callbacks.
|
|
2270
2245
|
|
|
2271
|
-
Use this to implement
|
|
2246
|
+
Use this to implement `stream` or `transform` in `Runnable` subclasses.
|
|
2272
2247
|
|
|
2273
2248
|
"""
|
|
2274
2249
|
# tee the input so we can iterate over it twice
|
|
@@ -2362,10 +2337,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2362
2337
|
) -> AsyncIterator[Output]:
|
|
2363
2338
|
"""Transform a stream with config.
|
|
2364
2339
|
|
|
2365
|
-
Helper method to transform an Async
|
|
2366
|
-
Async
|
|
2340
|
+
Helper method to transform an Async `Iterator` of `Input` values into an
|
|
2341
|
+
Async `Iterator` of `Output` values, with callbacks.
|
|
2367
2342
|
|
|
2368
|
-
Use this to implement
|
|
2343
|
+
Use this to implement `astream` or `atransform` in `Runnable` subclasses.
|
|
2369
2344
|
|
|
2370
2345
|
"""
|
|
2371
2346
|
# tee the input so we can iterate over it twice
|
|
@@ -2456,23 +2431,23 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2456
2431
|
description: str | None = None,
|
|
2457
2432
|
arg_types: dict[str, type] | None = None,
|
|
2458
2433
|
) -> BaseTool:
|
|
2459
|
-
"""Create a
|
|
2434
|
+
"""Create a `BaseTool` from a `Runnable`.
|
|
2460
2435
|
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
from
|
|
2464
|
-
|
|
2465
|
-
the schema can be specified directly with
|
|
2466
|
-
pass
|
|
2436
|
+
`as_tool` will instantiate a `BaseTool` with a name, description, and
|
|
2437
|
+
`args_schema` from a `Runnable`. Where possible, schemas are inferred
|
|
2438
|
+
from `runnable.get_input_schema`. Alternatively (e.g., if the
|
|
2439
|
+
`Runnable` takes a dict as input and the specific dict keys are not typed),
|
|
2440
|
+
the schema can be specified directly with `args_schema`. You can also
|
|
2441
|
+
pass `arg_types` to just specify the required arguments and their types.
|
|
2467
2442
|
|
|
2468
2443
|
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.
|
|
2444
|
+
args_schema: The schema for the tool.
|
|
2445
|
+
name: The name of the tool.
|
|
2446
|
+
description: The description of the tool.
|
|
2447
|
+
arg_types: A dictionary of argument names to types.
|
|
2473
2448
|
|
|
2474
2449
|
Returns:
|
|
2475
|
-
A
|
|
2450
|
+
A `BaseTool` instance.
|
|
2476
2451
|
|
|
2477
2452
|
Typed dict input:
|
|
2478
2453
|
|
|
@@ -2495,7 +2470,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2495
2470
|
as_tool.invoke({"a": 3, "b": [1, 2]})
|
|
2496
2471
|
```
|
|
2497
2472
|
|
|
2498
|
-
|
|
2473
|
+
`dict` input, specifying schema via `args_schema`:
|
|
2499
2474
|
|
|
2500
2475
|
```python
|
|
2501
2476
|
from typing import Any
|
|
@@ -2516,7 +2491,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2516
2491
|
as_tool.invoke({"a": 3, "b": [1, 2]})
|
|
2517
2492
|
```
|
|
2518
2493
|
|
|
2519
|
-
|
|
2494
|
+
`dict` input, specifying schema via `arg_types`:
|
|
2520
2495
|
|
|
2521
2496
|
```python
|
|
2522
2497
|
from typing import Any
|
|
@@ -2579,10 +2554,10 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2579
2554
|
|
|
2580
2555
|
@override
|
|
2581
2556
|
def to_json(self) -> SerializedConstructor | SerializedNotImplemented:
|
|
2582
|
-
"""Serialize the
|
|
2557
|
+
"""Serialize the `Runnable` to JSON.
|
|
2583
2558
|
|
|
2584
2559
|
Returns:
|
|
2585
|
-
A JSON-serializable representation of the
|
|
2560
|
+
A JSON-serializable representation of the `Runnable`.
|
|
2586
2561
|
|
|
2587
2562
|
"""
|
|
2588
2563
|
dumped = super().to_json()
|
|
@@ -2593,16 +2568,16 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2593
2568
|
def configurable_fields(
|
|
2594
2569
|
self, **kwargs: AnyConfigurableField
|
|
2595
2570
|
) -> RunnableSerializable[Input, Output]:
|
|
2596
|
-
"""Configure particular
|
|
2571
|
+
"""Configure particular `Runnable` fields at runtime.
|
|
2597
2572
|
|
|
2598
2573
|
Args:
|
|
2599
|
-
**kwargs: A dictionary of
|
|
2574
|
+
**kwargs: A dictionary of `ConfigurableField` instances to configure.
|
|
2600
2575
|
|
|
2601
2576
|
Raises:
|
|
2602
|
-
ValueError: If a configuration key is not found in the
|
|
2577
|
+
ValueError: If a configuration key is not found in the `Runnable`.
|
|
2603
2578
|
|
|
2604
2579
|
Returns:
|
|
2605
|
-
A new
|
|
2580
|
+
A new `Runnable` with the fields configured.
|
|
2606
2581
|
|
|
2607
2582
|
```python
|
|
2608
2583
|
from langchain_core.runnables import ConfigurableField
|
|
@@ -2652,20 +2627,20 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2652
2627
|
prefix_keys: bool = False,
|
|
2653
2628
|
**kwargs: Runnable[Input, Output] | Callable[[], Runnable[Input, Output]],
|
|
2654
2629
|
) -> RunnableSerializable[Input, Output]:
|
|
2655
|
-
"""Configure alternatives for
|
|
2630
|
+
"""Configure alternatives for `Runnable` objects that can be set at runtime.
|
|
2656
2631
|
|
|
2657
2632
|
Args:
|
|
2658
|
-
which: The
|
|
2633
|
+
which: The `ConfigurableField` instance that will be used to select the
|
|
2659
2634
|
alternative.
|
|
2660
2635
|
default_key: The default key to use if no alternative is selected.
|
|
2661
|
-
Defaults to
|
|
2662
|
-
prefix_keys: Whether to prefix the keys with the
|
|
2663
|
-
Defaults to False
|
|
2664
|
-
**kwargs: A dictionary of keys to
|
|
2665
|
-
return
|
|
2636
|
+
Defaults to `'default'`.
|
|
2637
|
+
prefix_keys: Whether to prefix the keys with the `ConfigurableField` id.
|
|
2638
|
+
Defaults to `False`.
|
|
2639
|
+
**kwargs: A dictionary of keys to `Runnable` instances or callables that
|
|
2640
|
+
return `Runnable` instances.
|
|
2666
2641
|
|
|
2667
2642
|
Returns:
|
|
2668
|
-
A new
|
|
2643
|
+
A new `Runnable` with the alternatives configured.
|
|
2669
2644
|
|
|
2670
2645
|
```python
|
|
2671
2646
|
from langchain_anthropic import ChatAnthropic
|
|
@@ -2787,26 +2762,26 @@ def _seq_output_schema(
|
|
|
2787
2762
|
|
|
2788
2763
|
|
|
2789
2764
|
class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
2790
|
-
"""Sequence of
|
|
2765
|
+
"""Sequence of `Runnable` objects, where the output of one is the input of the next.
|
|
2791
2766
|
|
|
2792
|
-
|
|
2767
|
+
**`RunnableSequence`** is the most important composition operator in LangChain
|
|
2793
2768
|
as it is used in virtually every chain.
|
|
2794
2769
|
|
|
2795
|
-
A
|
|
2796
|
-
|
|
2797
|
-
|
|
2770
|
+
A `RunnableSequence` can be instantiated directly or more commonly by using the
|
|
2771
|
+
`|` operator where either the left or right operands (or both) must be a
|
|
2772
|
+
`Runnable`.
|
|
2798
2773
|
|
|
2799
|
-
Any
|
|
2774
|
+
Any `RunnableSequence` automatically supports sync, async, batch.
|
|
2800
2775
|
|
|
2801
|
-
The default implementations of
|
|
2802
|
-
asyncio gather and will be faster than naive invocation of
|
|
2803
|
-
for IO bound
|
|
2776
|
+
The default implementations of `batch` and `abatch` utilize threadpools and
|
|
2777
|
+
asyncio gather and will be faster than naive invocation of `invoke` or `ainvoke`
|
|
2778
|
+
for IO bound `Runnable`s.
|
|
2804
2779
|
|
|
2805
2780
|
Batching is implemented by invoking the batch method on each component of the
|
|
2806
|
-
|
|
2781
|
+
`RunnableSequence` in order.
|
|
2807
2782
|
|
|
2808
|
-
A
|
|
2809
|
-
all components of the sequence implement a
|
|
2783
|
+
A `RunnableSequence` preserves the streaming properties of its components, so if
|
|
2784
|
+
all components of the sequence implement a `transform` method -- which
|
|
2810
2785
|
is the method that implements the logic to map a streaming input to a streaming
|
|
2811
2786
|
output -- then the sequence will be able to stream input to output!
|
|
2812
2787
|
|
|
@@ -2815,15 +2790,15 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2815
2790
|
multiple blocking components, streaming begins after the last one.
|
|
2816
2791
|
|
|
2817
2792
|
!!! note
|
|
2818
|
-
|
|
2819
|
-
use a
|
|
2820
|
-
|
|
2793
|
+
`RunnableLambdas` do not support `transform` by default! So if you need to
|
|
2794
|
+
use a `RunnableLambdas` be careful about where you place them in a
|
|
2795
|
+
`RunnableSequence` (if you need to use the `stream`/`astream` methods).
|
|
2821
2796
|
|
|
2822
2797
|
If you need arbitrary logic and need streaming, you can subclass
|
|
2823
|
-
Runnable, and implement
|
|
2798
|
+
Runnable, and implement `transform` for whatever logic you need.
|
|
2824
2799
|
|
|
2825
2800
|
Here is a simple example that uses simple functions to illustrate the use of
|
|
2826
|
-
|
|
2801
|
+
`RunnableSequence`:
|
|
2827
2802
|
|
|
2828
2803
|
```python
|
|
2829
2804
|
from langchain_core.runnables import RunnableLambda
|
|
@@ -2874,11 +2849,11 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2874
2849
|
# purposes. It allows specifying the `Input` on the first type, the `Output` of
|
|
2875
2850
|
# the last type.
|
|
2876
2851
|
first: Runnable[Input, Any]
|
|
2877
|
-
"""The first
|
|
2852
|
+
"""The first `Runnable` in the sequence."""
|
|
2878
2853
|
middle: list[Runnable[Any, Any]] = Field(default_factory=list)
|
|
2879
|
-
"""The middle
|
|
2854
|
+
"""The middle `Runnable` in the sequence."""
|
|
2880
2855
|
last: Runnable[Any, Output]
|
|
2881
|
-
"""The last
|
|
2856
|
+
"""The last `Runnable` in the sequence."""
|
|
2882
2857
|
|
|
2883
2858
|
def __init__(
|
|
2884
2859
|
self,
|
|
@@ -2888,14 +2863,14 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2888
2863
|
middle: list[Runnable[Any, Any]] | None = None,
|
|
2889
2864
|
last: Runnable[Any, Any] | None = None,
|
|
2890
2865
|
) -> None:
|
|
2891
|
-
"""Create a new
|
|
2866
|
+
"""Create a new `RunnableSequence`.
|
|
2892
2867
|
|
|
2893
2868
|
Args:
|
|
2894
2869
|
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.
|
|
2870
|
+
name: The name of the `Runnable`.
|
|
2871
|
+
first: The first `Runnable` in the sequence.
|
|
2872
|
+
middle: The middle `Runnable` objects in the sequence.
|
|
2873
|
+
last: The last Runnable in the sequence.
|
|
2899
2874
|
|
|
2900
2875
|
Raises:
|
|
2901
2876
|
ValueError: If the sequence has less than 2 steps.
|
|
@@ -2924,16 +2899,16 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2924
2899
|
"""Get the namespace of the langchain object.
|
|
2925
2900
|
|
|
2926
2901
|
Returns:
|
|
2927
|
-
|
|
2902
|
+
`["langchain", "schema", "runnable"]`
|
|
2928
2903
|
"""
|
|
2929
2904
|
return ["langchain", "schema", "runnable"]
|
|
2930
2905
|
|
|
2931
2906
|
@property
|
|
2932
2907
|
def steps(self) -> list[Runnable[Any, Any]]:
|
|
2933
|
-
"""All the
|
|
2908
|
+
"""All the `Runnable`s that make up the sequence in order.
|
|
2934
2909
|
|
|
2935
2910
|
Returns:
|
|
2936
|
-
A list of
|
|
2911
|
+
A list of `Runnable`s.
|
|
2937
2912
|
"""
|
|
2938
2913
|
return [self.first, *self.middle, self.last]
|
|
2939
2914
|
|
|
@@ -2950,24 +2925,24 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2950
2925
|
@property
|
|
2951
2926
|
@override
|
|
2952
2927
|
def InputType(self) -> type[Input]:
|
|
2953
|
-
"""The type of the input to the
|
|
2928
|
+
"""The type of the input to the `Runnable`."""
|
|
2954
2929
|
return self.first.InputType
|
|
2955
2930
|
|
|
2956
2931
|
@property
|
|
2957
2932
|
@override
|
|
2958
2933
|
def OutputType(self) -> type[Output]:
|
|
2959
|
-
"""The type of the output of the
|
|
2934
|
+
"""The type of the output of the `Runnable`."""
|
|
2960
2935
|
return self.last.OutputType
|
|
2961
2936
|
|
|
2962
2937
|
@override
|
|
2963
2938
|
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
2964
|
-
"""Get the input schema of the
|
|
2939
|
+
"""Get the input schema of the `Runnable`.
|
|
2965
2940
|
|
|
2966
2941
|
Args:
|
|
2967
|
-
config: The config to use.
|
|
2942
|
+
config: The config to use.
|
|
2968
2943
|
|
|
2969
2944
|
Returns:
|
|
2970
|
-
The input schema of the
|
|
2945
|
+
The input schema of the `Runnable`.
|
|
2971
2946
|
|
|
2972
2947
|
"""
|
|
2973
2948
|
return _seq_input_schema(self.steps, config)
|
|
@@ -2976,13 +2951,13 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2976
2951
|
def get_output_schema(
|
|
2977
2952
|
self, config: RunnableConfig | None = None
|
|
2978
2953
|
) -> type[BaseModel]:
|
|
2979
|
-
"""Get the output schema of the
|
|
2954
|
+
"""Get the output schema of the `Runnable`.
|
|
2980
2955
|
|
|
2981
2956
|
Args:
|
|
2982
|
-
config: The config to use.
|
|
2957
|
+
config: The config to use.
|
|
2983
2958
|
|
|
2984
2959
|
Returns:
|
|
2985
|
-
The output schema of the
|
|
2960
|
+
The output schema of the `Runnable`.
|
|
2986
2961
|
|
|
2987
2962
|
"""
|
|
2988
2963
|
return _seq_output_schema(self.steps, config)
|
|
@@ -2990,10 +2965,10 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2990
2965
|
@property
|
|
2991
2966
|
@override
|
|
2992
2967
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
2993
|
-
"""Get the config specs of the
|
|
2968
|
+
"""Get the config specs of the `Runnable`.
|
|
2994
2969
|
|
|
2995
2970
|
Returns:
|
|
2996
|
-
The config specs of the
|
|
2971
|
+
The config specs of the `Runnable`.
|
|
2997
2972
|
|
|
2998
2973
|
"""
|
|
2999
2974
|
# Import locally to prevent circular import
|
|
@@ -3003,16 +2978,16 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
3003
2978
|
|
|
3004
2979
|
@override
|
|
3005
2980
|
def get_graph(self, config: RunnableConfig | None = None) -> Graph:
|
|
3006
|
-
"""Get the graph representation of the
|
|
2981
|
+
"""Get the graph representation of the `Runnable`.
|
|
3007
2982
|
|
|
3008
2983
|
Args:
|
|
3009
|
-
config: The config to use.
|
|
2984
|
+
config: The config to use.
|
|
3010
2985
|
|
|
3011
2986
|
Returns:
|
|
3012
|
-
The graph representation of the
|
|
2987
|
+
The graph representation of the `Runnable`.
|
|
3013
2988
|
|
|
3014
2989
|
Raises:
|
|
3015
|
-
ValueError: If a
|
|
2990
|
+
ValueError: If a `Runnable` has no first or last node.
|
|
3016
2991
|
|
|
3017
2992
|
"""
|
|
3018
2993
|
# Import locally to prevent circular import
|
|
@@ -3532,19 +3507,19 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
3532
3507
|
|
|
3533
3508
|
|
|
3534
3509
|
class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
3535
|
-
"""Runnable that runs a mapping of
|
|
3510
|
+
"""Runnable that runs a mapping of `Runnable`s in parallel.
|
|
3536
3511
|
|
|
3537
3512
|
Returns a mapping of their outputs.
|
|
3538
3513
|
|
|
3539
|
-
|
|
3540
|
-
alongside
|
|
3514
|
+
`RunnableParallel` is one of the two main composition primitives for the LCEL,
|
|
3515
|
+
alongside `RunnableSequence`. It invokes `Runnable`s concurrently, providing the
|
|
3541
3516
|
same input to each.
|
|
3542
3517
|
|
|
3543
|
-
A
|
|
3518
|
+
A `RunnableParallel` can be instantiated directly or by using a dict literal
|
|
3544
3519
|
within a sequence.
|
|
3545
3520
|
|
|
3546
3521
|
Here is a simple example that uses functions to illustrate the use of
|
|
3547
|
-
|
|
3522
|
+
`RunnableParallel`:
|
|
3548
3523
|
|
|
3549
3524
|
```python
|
|
3550
3525
|
from langchain_core.runnables import RunnableLambda
|
|
@@ -3587,8 +3562,8 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3587
3562
|
await sequence.abatch([1, 2, 3])
|
|
3588
3563
|
```
|
|
3589
3564
|
|
|
3590
|
-
|
|
3591
|
-
example, we simultaneously stream output from two different
|
|
3565
|
+
`RunnableParallel` makes it easy to run `Runnable`s in parallel. In the below
|
|
3566
|
+
example, we simultaneously stream output from two different `Runnable` objects:
|
|
3592
3567
|
|
|
3593
3568
|
```python
|
|
3594
3569
|
from langchain_core.prompts import ChatPromptTemplate
|
|
@@ -3630,10 +3605,10 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3630
3605
|
| Callable[[Input], Any]
|
|
3631
3606
|
| Mapping[str, Runnable[Input, Any] | Callable[[Input], Any]],
|
|
3632
3607
|
) -> None:
|
|
3633
|
-
"""Create a
|
|
3608
|
+
"""Create a `RunnableParallel`.
|
|
3634
3609
|
|
|
3635
3610
|
Args:
|
|
3636
|
-
steps__: The steps to include.
|
|
3611
|
+
steps__: The steps to include.
|
|
3637
3612
|
**kwargs: Additional steps to include.
|
|
3638
3613
|
|
|
3639
3614
|
"""
|
|
@@ -3655,7 +3630,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3655
3630
|
"""Get the namespace of the langchain object.
|
|
3656
3631
|
|
|
3657
3632
|
Returns:
|
|
3658
|
-
|
|
3633
|
+
`["langchain", "schema", "runnable"]`
|
|
3659
3634
|
"""
|
|
3660
3635
|
return ["langchain", "schema", "runnable"]
|
|
3661
3636
|
|
|
@@ -3665,14 +3640,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3665
3640
|
|
|
3666
3641
|
@override
|
|
3667
3642
|
def get_name(self, suffix: str | None = None, *, name: str | None = None) -> str:
|
|
3668
|
-
"""Get the name of the
|
|
3643
|
+
"""Get the name of the `Runnable`.
|
|
3669
3644
|
|
|
3670
3645
|
Args:
|
|
3671
|
-
suffix: The suffix to use.
|
|
3672
|
-
name: The name to use.
|
|
3646
|
+
suffix: The suffix to use.
|
|
3647
|
+
name: The name to use.
|
|
3673
3648
|
|
|
3674
3649
|
Returns:
|
|
3675
|
-
The name of the
|
|
3650
|
+
The name of the `Runnable`.
|
|
3676
3651
|
|
|
3677
3652
|
"""
|
|
3678
3653
|
name = name or self.name or f"RunnableParallel<{','.join(self.steps__.keys())}>"
|
|
@@ -3681,7 +3656,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3681
3656
|
@property
|
|
3682
3657
|
@override
|
|
3683
3658
|
def InputType(self) -> Any:
|
|
3684
|
-
"""The type of the input to the
|
|
3659
|
+
"""The type of the input to the `Runnable`."""
|
|
3685
3660
|
for step in self.steps__.values():
|
|
3686
3661
|
if step.InputType:
|
|
3687
3662
|
return step.InputType
|
|
@@ -3690,13 +3665,13 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3690
3665
|
|
|
3691
3666
|
@override
|
|
3692
3667
|
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
3693
|
-
"""Get the input schema of the
|
|
3668
|
+
"""Get the input schema of the `Runnable`.
|
|
3694
3669
|
|
|
3695
3670
|
Args:
|
|
3696
|
-
config: The config to use.
|
|
3671
|
+
config: The config to use.
|
|
3697
3672
|
|
|
3698
3673
|
Returns:
|
|
3699
|
-
The input schema of the
|
|
3674
|
+
The input schema of the `Runnable`.
|
|
3700
3675
|
|
|
3701
3676
|
"""
|
|
3702
3677
|
if all(
|
|
@@ -3721,13 +3696,13 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3721
3696
|
def get_output_schema(
|
|
3722
3697
|
self, config: RunnableConfig | None = None
|
|
3723
3698
|
) -> type[BaseModel]:
|
|
3724
|
-
"""Get the output schema of the
|
|
3699
|
+
"""Get the output schema of the `Runnable`.
|
|
3725
3700
|
|
|
3726
3701
|
Args:
|
|
3727
|
-
config: The config to use.
|
|
3702
|
+
config: The config to use.
|
|
3728
3703
|
|
|
3729
3704
|
Returns:
|
|
3730
|
-
The output schema of the
|
|
3705
|
+
The output schema of the `Runnable`.
|
|
3731
3706
|
|
|
3732
3707
|
"""
|
|
3733
3708
|
fields = {k: (v.OutputType, ...) for k, v in self.steps__.items()}
|
|
@@ -3736,10 +3711,10 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3736
3711
|
@property
|
|
3737
3712
|
@override
|
|
3738
3713
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
3739
|
-
"""Get the config specs of the
|
|
3714
|
+
"""Get the config specs of the `Runnable`.
|
|
3740
3715
|
|
|
3741
3716
|
Returns:
|
|
3742
|
-
The config specs of the
|
|
3717
|
+
The config specs of the `Runnable`.
|
|
3743
3718
|
|
|
3744
3719
|
"""
|
|
3745
3720
|
return get_unique_config_specs(
|
|
@@ -3748,16 +3723,16 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3748
3723
|
|
|
3749
3724
|
@override
|
|
3750
3725
|
def get_graph(self, config: RunnableConfig | None = None) -> Graph:
|
|
3751
|
-
"""Get the graph representation of the
|
|
3726
|
+
"""Get the graph representation of the `Runnable`.
|
|
3752
3727
|
|
|
3753
3728
|
Args:
|
|
3754
|
-
config: The config to use.
|
|
3729
|
+
config: The config to use.
|
|
3755
3730
|
|
|
3756
3731
|
Returns:
|
|
3757
|
-
The graph representation of the
|
|
3732
|
+
The graph representation of the `Runnable`.
|
|
3758
3733
|
|
|
3759
3734
|
Raises:
|
|
3760
|
-
ValueError: If a
|
|
3735
|
+
ValueError: If a `Runnable` has no first or last node.
|
|
3761
3736
|
|
|
3762
3737
|
"""
|
|
3763
3738
|
# Import locally to prevent circular import
|
|
@@ -4057,24 +4032,24 @@ RunnableMap = RunnableParallel
|
|
|
4057
4032
|
|
|
4058
4033
|
|
|
4059
4034
|
class RunnableGenerator(Runnable[Input, Output]):
|
|
4060
|
-
"""
|
|
4035
|
+
"""`Runnable` that runs a generator function.
|
|
4061
4036
|
|
|
4062
|
-
|
|
4037
|
+
`RunnableGenerator`s can be instantiated directly or by using a generator within
|
|
4063
4038
|
a sequence.
|
|
4064
4039
|
|
|
4065
|
-
|
|
4040
|
+
`RunnableGenerator`s can be used to implement custom behavior, such as custom
|
|
4066
4041
|
output parsers, while preserving streaming capabilities. Given a generator function
|
|
4067
|
-
with a signature
|
|
4068
|
-
|
|
4042
|
+
with a signature `Iterator[A] -> Iterator[B]`, wrapping it in a
|
|
4043
|
+
`RunnableGenerator` allows it to emit output chunks as soon as they are streamed
|
|
4069
4044
|
in from the previous step.
|
|
4070
4045
|
|
|
4071
4046
|
!!! note
|
|
4072
|
-
If a generator function has a
|
|
4047
|
+
If a generator function has a `signature A -> Iterator[B]`, such that it
|
|
4073
4048
|
requires its input from the previous step to be completed before emitting chunks
|
|
4074
4049
|
(e.g., most LLMs need the entire prompt available to start generating), it can
|
|
4075
|
-
instead be wrapped in a
|
|
4050
|
+
instead be wrapped in a `RunnableLambda`.
|
|
4076
4051
|
|
|
4077
|
-
Here is an example to show the basic mechanics of a
|
|
4052
|
+
Here is an example to show the basic mechanics of a `RunnableGenerator`:
|
|
4078
4053
|
|
|
4079
4054
|
```python
|
|
4080
4055
|
from typing import Any, AsyncIterator, Iterator
|
|
@@ -4104,7 +4079,7 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4104
4079
|
[p async for p in runnable.astream(None)] # ["Have", " a", " nice", " day"]
|
|
4105
4080
|
```
|
|
4106
4081
|
|
|
4107
|
-
|
|
4082
|
+
`RunnableGenerator` makes it easy to implement custom behavior within a streaming
|
|
4108
4083
|
context. Below we show an example:
|
|
4109
4084
|
|
|
4110
4085
|
```python
|
|
@@ -4157,12 +4132,12 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4157
4132
|
*,
|
|
4158
4133
|
name: str | None = None,
|
|
4159
4134
|
) -> None:
|
|
4160
|
-
"""Initialize a
|
|
4135
|
+
"""Initialize a `RunnableGenerator`.
|
|
4161
4136
|
|
|
4162
4137
|
Args:
|
|
4163
4138
|
transform: The transform function.
|
|
4164
|
-
atransform: The async transform function.
|
|
4165
|
-
name: The name of the
|
|
4139
|
+
atransform: The async transform function.
|
|
4140
|
+
name: The name of the `Runnable`.
|
|
4166
4141
|
|
|
4167
4142
|
Raises:
|
|
4168
4143
|
TypeError: If the transform is not a generator function.
|
|
@@ -4359,20 +4334,20 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4359
4334
|
|
|
4360
4335
|
|
|
4361
4336
|
class RunnableLambda(Runnable[Input, Output]):
|
|
4362
|
-
"""
|
|
4337
|
+
"""`RunnableLambda` converts a python callable into a `Runnable`.
|
|
4363
4338
|
|
|
4364
|
-
Wrapping a callable in a
|
|
4339
|
+
Wrapping a callable in a `RunnableLambda` makes the callable usable
|
|
4365
4340
|
within either a sync or async context.
|
|
4366
4341
|
|
|
4367
|
-
|
|
4342
|
+
`RunnableLambda` can be composed as any other `Runnable` and provides
|
|
4368
4343
|
seamless integration with LangChain tracing.
|
|
4369
4344
|
|
|
4370
|
-
|
|
4345
|
+
`RunnableLambda` is best suited for code that does not need to support
|
|
4371
4346
|
streaming. If you need to support streaming (i.e., be able to operate
|
|
4372
|
-
on chunks of inputs and yield chunks of outputs), use
|
|
4347
|
+
on chunks of inputs and yield chunks of outputs), use `RunnableGenerator`
|
|
4373
4348
|
instead.
|
|
4374
4349
|
|
|
4375
|
-
Note that if a
|
|
4350
|
+
Note that if a `RunnableLambda` returns an instance of `Runnable`, that
|
|
4376
4351
|
instance is invoked (or streamed) during execution.
|
|
4377
4352
|
|
|
4378
4353
|
Examples:
|
|
@@ -4431,7 +4406,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4431
4406
|
| None = None,
|
|
4432
4407
|
name: str | None = None,
|
|
4433
4408
|
) -> None:
|
|
4434
|
-
"""Create a
|
|
4409
|
+
"""Create a `RunnableLambda` from a callable, and async callable or both.
|
|
4435
4410
|
|
|
4436
4411
|
Accepts both sync and async variants to allow providing efficient
|
|
4437
4412
|
implementations for sync and async execution.
|
|
@@ -4439,12 +4414,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4439
4414
|
Args:
|
|
4440
4415
|
func: Either sync or async callable
|
|
4441
4416
|
afunc: An async callable that takes an input and returns an output.
|
|
4442
|
-
|
|
4443
|
-
name: The name of the
|
|
4417
|
+
|
|
4418
|
+
name: The name of the `Runnable`.
|
|
4444
4419
|
|
|
4445
4420
|
Raises:
|
|
4446
|
-
TypeError: If the
|
|
4447
|
-
TypeError: If both
|
|
4421
|
+
TypeError: If the `func` is not a callable type.
|
|
4422
|
+
TypeError: If both `func` and `afunc` are provided.
|
|
4448
4423
|
|
|
4449
4424
|
"""
|
|
4450
4425
|
if afunc is not None:
|
|
@@ -4484,7 +4459,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4484
4459
|
@property
|
|
4485
4460
|
@override
|
|
4486
4461
|
def InputType(self) -> Any:
|
|
4487
|
-
"""The type of the input to this
|
|
4462
|
+
"""The type of the input to this `Runnable`."""
|
|
4488
4463
|
func = getattr(self, "func", None) or self.afunc
|
|
4489
4464
|
try:
|
|
4490
4465
|
params = inspect.signature(func).parameters
|
|
@@ -4497,13 +4472,13 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4497
4472
|
|
|
4498
4473
|
@override
|
|
4499
4474
|
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
4500
|
-
"""The
|
|
4475
|
+
"""The Pydantic schema for the input to this `Runnable`.
|
|
4501
4476
|
|
|
4502
4477
|
Args:
|
|
4503
|
-
config: The config to use.
|
|
4478
|
+
config: The config to use.
|
|
4504
4479
|
|
|
4505
4480
|
Returns:
|
|
4506
|
-
The input schema for this
|
|
4481
|
+
The input schema for this `Runnable`.
|
|
4507
4482
|
|
|
4508
4483
|
"""
|
|
4509
4484
|
func = getattr(self, "func", None) or self.afunc
|
|
@@ -4542,10 +4517,10 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4542
4517
|
@property
|
|
4543
4518
|
@override
|
|
4544
4519
|
def OutputType(self) -> Any:
|
|
4545
|
-
"""The type of the output of this
|
|
4520
|
+
"""The type of the output of this `Runnable` as a type annotation.
|
|
4546
4521
|
|
|
4547
4522
|
Returns:
|
|
4548
|
-
The type of the output of this
|
|
4523
|
+
The type of the output of this `Runnable`.
|
|
4549
4524
|
|
|
4550
4525
|
"""
|
|
4551
4526
|
func = getattr(self, "func", None) or self.afunc
|
|
@@ -4592,11 +4567,11 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4592
4567
|
|
|
4593
4568
|
@functools.cached_property
|
|
4594
4569
|
def deps(self) -> list[Runnable]:
|
|
4595
|
-
"""The dependencies of this
|
|
4570
|
+
"""The dependencies of this `Runnable`.
|
|
4596
4571
|
|
|
4597
4572
|
Returns:
|
|
4598
|
-
The dependencies of this
|
|
4599
|
-
variables that are
|
|
4573
|
+
The dependencies of this `Runnable`. If the function has nonlocal
|
|
4574
|
+
variables that are `Runnable`s, they are considered dependencies.
|
|
4600
4575
|
|
|
4601
4576
|
"""
|
|
4602
4577
|
if hasattr(self, "func"):
|
|
@@ -4664,7 +4639,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4664
4639
|
__hash__ = None # type: ignore[assignment]
|
|
4665
4640
|
|
|
4666
4641
|
def __repr__(self) -> str:
|
|
4667
|
-
"""Return a string representation of this
|
|
4642
|
+
"""Return a string representation of this `Runnable`."""
|
|
4668
4643
|
if self._repr is None:
|
|
4669
4644
|
if hasattr(self, "func") and isinstance(self.func, itemgetter):
|
|
4670
4645
|
self._repr = f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
|
@@ -4830,18 +4805,18 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4830
4805
|
config: RunnableConfig | None = None,
|
|
4831
4806
|
**kwargs: Any | None,
|
|
4832
4807
|
) -> Output:
|
|
4833
|
-
"""Invoke this
|
|
4808
|
+
"""Invoke this `Runnable` synchronously.
|
|
4834
4809
|
|
|
4835
4810
|
Args:
|
|
4836
|
-
input: The input to this
|
|
4837
|
-
config: The config to use.
|
|
4838
|
-
kwargs: Additional keyword arguments.
|
|
4811
|
+
input: The input to this `Runnable`.
|
|
4812
|
+
config: The config to use.
|
|
4813
|
+
**kwargs: Additional keyword arguments.
|
|
4839
4814
|
|
|
4840
4815
|
Returns:
|
|
4841
|
-
The output of this
|
|
4816
|
+
The output of this `Runnable`.
|
|
4842
4817
|
|
|
4843
4818
|
Raises:
|
|
4844
|
-
TypeError: If the
|
|
4819
|
+
TypeError: If the `Runnable` is a coroutine function.
|
|
4845
4820
|
|
|
4846
4821
|
"""
|
|
4847
4822
|
if hasattr(self, "func"):
|
|
@@ -4861,15 +4836,15 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4861
4836
|
config: RunnableConfig | None = None,
|
|
4862
4837
|
**kwargs: Any | None,
|
|
4863
4838
|
) -> Output:
|
|
4864
|
-
"""Invoke this
|
|
4839
|
+
"""Invoke this `Runnable` asynchronously.
|
|
4865
4840
|
|
|
4866
4841
|
Args:
|
|
4867
|
-
input: The input to this
|
|
4868
|
-
config: The config to use.
|
|
4869
|
-
kwargs: Additional keyword arguments.
|
|
4842
|
+
input: The input to this `Runnable`.
|
|
4843
|
+
config: The config to use.
|
|
4844
|
+
**kwargs: Additional keyword arguments.
|
|
4870
4845
|
|
|
4871
4846
|
Returns:
|
|
4872
|
-
The output of this
|
|
4847
|
+
The output of this `Runnable`.
|
|
4873
4848
|
|
|
4874
4849
|
"""
|
|
4875
4850
|
return await self._acall_with_config(
|
|
@@ -5102,12 +5077,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
5102
5077
|
class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
5103
5078
|
"""RunnableEachBase class.
|
|
5104
5079
|
|
|
5105
|
-
|
|
5080
|
+
`Runnable` that calls another `Runnable` for each element of the input sequence.
|
|
5106
5081
|
|
|
5107
|
-
Use only if creating a new
|
|
5082
|
+
Use only if creating a new `RunnableEach` subclass with different `__init__`
|
|
5108
5083
|
args.
|
|
5109
5084
|
|
|
5110
|
-
See documentation for
|
|
5085
|
+
See documentation for `RunnableEach` for more details.
|
|
5111
5086
|
|
|
5112
5087
|
"""
|
|
5113
5088
|
|
|
@@ -5131,7 +5106,7 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5131
5106
|
None,
|
|
5132
5107
|
),
|
|
5133
5108
|
# create model needs access to appropriate type annotations to be
|
|
5134
|
-
# able to construct the
|
|
5109
|
+
# able to construct the Pydantic model.
|
|
5135
5110
|
# When we create the model, we pass information about the namespace
|
|
5136
5111
|
# where the model is being created, so the type annotations can
|
|
5137
5112
|
# be resolved correctly as well.
|
|
@@ -5154,7 +5129,7 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5154
5129
|
self.get_name("Output"),
|
|
5155
5130
|
root=list[schema], # type: ignore[valid-type]
|
|
5156
5131
|
# create model needs access to appropriate type annotations to be
|
|
5157
|
-
# able to construct the
|
|
5132
|
+
# able to construct the Pydantic model.
|
|
5158
5133
|
# When we create the model, we pass information about the namespace
|
|
5159
5134
|
# where the model is being created, so the type annotations can
|
|
5160
5135
|
# be resolved correctly as well.
|
|
@@ -5184,7 +5159,7 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5184
5159
|
"""Get the namespace of the langchain object.
|
|
5185
5160
|
|
|
5186
5161
|
Returns:
|
|
5187
|
-
|
|
5162
|
+
`["langchain", "schema", "runnable"]`
|
|
5188
5163
|
"""
|
|
5189
5164
|
return ["langchain", "schema", "runnable"]
|
|
5190
5165
|
|
|
@@ -5243,13 +5218,13 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5243
5218
|
class RunnableEach(RunnableEachBase[Input, Output]):
|
|
5244
5219
|
"""RunnableEach class.
|
|
5245
5220
|
|
|
5246
|
-
|
|
5221
|
+
`Runnable` that calls another `Runnable` for each element of the input sequence.
|
|
5247
5222
|
|
|
5248
|
-
It allows you to call multiple inputs with the bounded
|
|
5223
|
+
It allows you to call multiple inputs with the bounded `Runnable`.
|
|
5249
5224
|
|
|
5250
|
-
|
|
5225
|
+
`RunnableEach` makes it easy to run multiple inputs for the `Runnable`.
|
|
5251
5226
|
In the below example, we associate and run three inputs
|
|
5252
|
-
with a
|
|
5227
|
+
with a `Runnable`:
|
|
5253
5228
|
|
|
5254
5229
|
```python
|
|
5255
5230
|
from langchain_core.runnables.base import RunnableEach
|
|
@@ -5299,22 +5274,22 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5299
5274
|
| Callable[[Run, RunnableConfig], None]
|
|
5300
5275
|
| None = None,
|
|
5301
5276
|
) -> RunnableEach[Input, Output]:
|
|
5302
|
-
"""Bind lifecycle listeners to a
|
|
5277
|
+
"""Bind lifecycle listeners to a `Runnable`, returning a new `Runnable`.
|
|
5303
5278
|
|
|
5304
|
-
The
|
|
5305
|
-
|
|
5279
|
+
The `Run` object contains information about the run, including its `id`,
|
|
5280
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
5306
5281
|
any tags or metadata added to the run.
|
|
5307
5282
|
|
|
5308
5283
|
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.
|
|
5284
|
+
on_start: Called before the `Runnable` starts running, with the `Run`
|
|
5285
|
+
object.
|
|
5286
|
+
on_end: Called after the `Runnable` finishes running, with the `Run`
|
|
5287
|
+
object.
|
|
5288
|
+
on_error: Called if the `Runnable` throws an error, with the `Run`
|
|
5289
|
+
object.
|
|
5315
5290
|
|
|
5316
5291
|
Returns:
|
|
5317
|
-
A new
|
|
5292
|
+
A new `Runnable` with the listeners bound.
|
|
5318
5293
|
|
|
5319
5294
|
"""
|
|
5320
5295
|
return RunnableEach(
|
|
@@ -5330,24 +5305,24 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5330
5305
|
on_end: AsyncListener | None = None,
|
|
5331
5306
|
on_error: AsyncListener | None = None,
|
|
5332
5307
|
) -> RunnableEach[Input, Output]:
|
|
5333
|
-
"""Bind async lifecycle listeners to a
|
|
5308
|
+
"""Bind async lifecycle listeners to a `Runnable`.
|
|
5334
5309
|
|
|
5335
|
-
Returns a new
|
|
5310
|
+
Returns a new `Runnable`.
|
|
5336
5311
|
|
|
5337
|
-
The
|
|
5338
|
-
|
|
5312
|
+
The `Run` object contains information about the run, including its `id`,
|
|
5313
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
5339
5314
|
any tags or metadata added to the run.
|
|
5340
5315
|
|
|
5341
5316
|
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
|
|
5317
|
+
on_start: Called asynchronously before the `Runnable` starts running,
|
|
5318
|
+
with the `Run` object.
|
|
5319
|
+
on_end: Called asynchronously after the `Runnable` finishes running,
|
|
5320
|
+
with the `Run` object.
|
|
5321
|
+
on_error: Called asynchronously if the `Runnable` throws an error,
|
|
5322
|
+
with the `Run` object.
|
|
5348
5323
|
|
|
5349
5324
|
Returns:
|
|
5350
|
-
A new
|
|
5325
|
+
A new `Runnable` with the listeners bound.
|
|
5351
5326
|
|
|
5352
5327
|
"""
|
|
5353
5328
|
return RunnableEach(
|
|
@@ -5358,46 +5333,46 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5358
5333
|
|
|
5359
5334
|
|
|
5360
5335
|
class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[no-redef]
|
|
5361
|
-
"""
|
|
5336
|
+
"""`Runnable` that delegates calls to another `Runnable` with a set of kwargs.
|
|
5362
5337
|
|
|
5363
|
-
Use only if creating a new
|
|
5338
|
+
Use only if creating a new `RunnableBinding` subclass with different `__init__`
|
|
5364
5339
|
args.
|
|
5365
5340
|
|
|
5366
|
-
See documentation for
|
|
5341
|
+
See documentation for `RunnableBinding` for more details.
|
|
5367
5342
|
|
|
5368
5343
|
"""
|
|
5369
5344
|
|
|
5370
5345
|
bound: Runnable[Input, Output]
|
|
5371
|
-
"""The underlying
|
|
5346
|
+
"""The underlying `Runnable` that this `Runnable` delegates to."""
|
|
5372
5347
|
|
|
5373
5348
|
kwargs: Mapping[str, Any] = Field(default_factory=dict)
|
|
5374
|
-
"""kwargs to pass to the underlying
|
|
5349
|
+
"""kwargs to pass to the underlying `Runnable` when running.
|
|
5375
5350
|
|
|
5376
|
-
For example, when the
|
|
5377
|
-
|
|
5351
|
+
For example, when the `Runnable` binding is invoked the underlying
|
|
5352
|
+
`Runnable` will be invoked with the same input but with these additional
|
|
5378
5353
|
kwargs.
|
|
5379
5354
|
|
|
5380
5355
|
"""
|
|
5381
5356
|
|
|
5382
5357
|
config: RunnableConfig = Field(default_factory=RunnableConfig)
|
|
5383
|
-
"""The config to bind to the underlying
|
|
5358
|
+
"""The config to bind to the underlying `Runnable`."""
|
|
5384
5359
|
|
|
5385
5360
|
config_factories: list[Callable[[RunnableConfig], RunnableConfig]] = Field(
|
|
5386
5361
|
default_factory=list
|
|
5387
5362
|
)
|
|
5388
|
-
"""The config factories to bind to the underlying
|
|
5363
|
+
"""The config factories to bind to the underlying `Runnable`."""
|
|
5389
5364
|
|
|
5390
5365
|
# Union[Type[Input], BaseModel] + things like list[str]
|
|
5391
5366
|
custom_input_type: Any | None = None
|
|
5392
|
-
"""Override the input type of the underlying
|
|
5367
|
+
"""Override the input type of the underlying `Runnable` with a custom type.
|
|
5393
5368
|
|
|
5394
|
-
The type can be a
|
|
5369
|
+
The type can be a Pydantic model, or a type annotation (e.g., `list[str]`).
|
|
5395
5370
|
"""
|
|
5396
5371
|
# Union[Type[Output], BaseModel] + things like list[str]
|
|
5397
5372
|
custom_output_type: Any | None = None
|
|
5398
|
-
"""Override the output type of the underlying
|
|
5373
|
+
"""Override the output type of the underlying `Runnable` with a custom type.
|
|
5399
5374
|
|
|
5400
|
-
The type can be a
|
|
5375
|
+
The type can be a Pydantic model, or a type annotation (e.g., `list[str]`).
|
|
5401
5376
|
"""
|
|
5402
5377
|
|
|
5403
5378
|
model_config = ConfigDict(
|
|
@@ -5416,24 +5391,24 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
|
|
|
5416
5391
|
custom_output_type: type[Output] | BaseModel | None = None,
|
|
5417
5392
|
**other_kwargs: Any,
|
|
5418
5393
|
) -> None:
|
|
5419
|
-
"""Create a
|
|
5394
|
+
"""Create a `RunnableBinding` from a `Runnable` and kwargs.
|
|
5420
5395
|
|
|
5421
5396
|
Args:
|
|
5422
|
-
bound: The underlying
|
|
5397
|
+
bound: The underlying `Runnable` that this `Runnable` delegates calls
|
|
5423
5398
|
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
|
-
|
|
5399
|
+
kwargs: optional kwargs to pass to the underlying `Runnable`, when running
|
|
5400
|
+
the underlying `Runnable` (e.g., via `invoke`, `batch`,
|
|
5401
|
+
`transform`, or `stream` or async variants)
|
|
5402
|
+
|
|
5403
|
+
config: optional config to bind to the underlying `Runnable`.
|
|
5404
|
+
|
|
5430
5405
|
config_factories: optional list of config factories to apply to the
|
|
5431
|
-
config before binding to the underlying
|
|
5432
|
-
|
|
5406
|
+
config before binding to the underlying `Runnable`.
|
|
5407
|
+
|
|
5433
5408
|
custom_input_type: Specify to override the input type of the underlying
|
|
5434
|
-
|
|
5409
|
+
`Runnable` with a custom type.
|
|
5435
5410
|
custom_output_type: Specify to override the output type of the underlying
|
|
5436
|
-
|
|
5411
|
+
`Runnable` with a custom type.
|
|
5437
5412
|
**other_kwargs: Unpacked into the base class.
|
|
5438
5413
|
"""
|
|
5439
5414
|
super().__init__(
|
|
@@ -5507,7 +5482,7 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
|
|
|
5507
5482
|
"""Get the namespace of the langchain object.
|
|
5508
5483
|
|
|
5509
5484
|
Returns:
|
|
5510
|
-
|
|
5485
|
+
`["langchain", "schema", "runnable"]`
|
|
5511
5486
|
"""
|
|
5512
5487
|
return ["langchain", "schema", "runnable"]
|
|
5513
5488
|
|
|
@@ -5760,30 +5735,30 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
|
|
|
5760
5735
|
|
|
5761
5736
|
|
|
5762
5737
|
class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
|
|
5763
|
-
"""Wrap a
|
|
5738
|
+
"""Wrap a `Runnable` with additional functionality.
|
|
5764
5739
|
|
|
5765
|
-
A
|
|
5766
|
-
preserves the essential features of
|
|
5740
|
+
A `RunnableBinding` can be thought of as a "runnable decorator" that
|
|
5741
|
+
preserves the essential features of `Runnable`; i.e., batching, streaming,
|
|
5767
5742
|
and async support, while adding additional functionality.
|
|
5768
5743
|
|
|
5769
|
-
Any class that inherits from
|
|
5770
|
-
Runnables expose a standard set of methods for creating
|
|
5771
|
-
or sub-classes of
|
|
5772
|
-
|
|
5744
|
+
Any class that inherits from `Runnable` can be bound to a `RunnableBinding`.
|
|
5745
|
+
Runnables expose a standard set of methods for creating `RunnableBindings`
|
|
5746
|
+
or sub-classes of `RunnableBindings` (e.g., `RunnableRetry`,
|
|
5747
|
+
`RunnableWithFallbacks`) that add additional functionality.
|
|
5773
5748
|
|
|
5774
5749
|
These methods include:
|
|
5775
5750
|
|
|
5776
|
-
-
|
|
5777
|
-
-
|
|
5778
|
-
|
|
5779
|
-
-
|
|
5780
|
-
-
|
|
5781
|
-
|
|
5782
|
-
-
|
|
5783
|
-
-
|
|
5751
|
+
- `bind`: Bind kwargs to pass to the underlying `Runnable` when running it.
|
|
5752
|
+
- `with_config`: Bind config to pass to the underlying `Runnable` when running
|
|
5753
|
+
it.
|
|
5754
|
+
- `with_listeners`: Bind lifecycle listeners to the underlying `Runnable`.
|
|
5755
|
+
- `with_types`: Override the input and output types of the underlying
|
|
5756
|
+
`Runnable`.
|
|
5757
|
+
- `with_retry`: Bind a retry policy to the underlying `Runnable`.
|
|
5758
|
+
- `with_fallbacks`: Bind a fallback policy to the underlying `Runnable`.
|
|
5784
5759
|
|
|
5785
5760
|
Example:
|
|
5786
|
-
`bind`: Bind kwargs to pass to the underlying
|
|
5761
|
+
`bind`: Bind kwargs to pass to the underlying `Runnable` when running it.
|
|
5787
5762
|
|
|
5788
5763
|
```python
|
|
5789
5764
|
# Create a Runnable binding that invokes the ChatModel with the
|
|
@@ -5797,7 +5772,7 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-re
|
|
|
5797
5772
|
runnable_binding = model.bind(stop=["-"])
|
|
5798
5773
|
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`
|
|
5799
5774
|
```
|
|
5800
|
-
Can also be done by instantiating a
|
|
5775
|
+
Can also be done by instantiating a `RunnableBinding` directly (not
|
|
5801
5776
|
recommended):
|
|
5802
5777
|
|
|
5803
5778
|
```python
|
|
@@ -5813,13 +5788,13 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-re
|
|
|
5813
5788
|
|
|
5814
5789
|
@override
|
|
5815
5790
|
def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
|
|
5816
|
-
"""Bind additional kwargs to a
|
|
5791
|
+
"""Bind additional kwargs to a `Runnable`, returning a new `Runnable`.
|
|
5817
5792
|
|
|
5818
5793
|
Args:
|
|
5819
|
-
**kwargs: The kwargs to bind to the
|
|
5794
|
+
**kwargs: The kwargs to bind to the `Runnable`.
|
|
5820
5795
|
|
|
5821
5796
|
Returns:
|
|
5822
|
-
A new
|
|
5797
|
+
A new `Runnable` with the same type and config as the original,
|
|
5823
5798
|
but with the additional kwargs bound.
|
|
5824
5799
|
|
|
5825
5800
|
"""
|
|
@@ -5862,22 +5837,22 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-re
|
|
|
5862
5837
|
| Callable[[Run, RunnableConfig], None]
|
|
5863
5838
|
| None = None,
|
|
5864
5839
|
) -> Runnable[Input, Output]:
|
|
5865
|
-
"""Bind lifecycle listeners to a
|
|
5840
|
+
"""Bind lifecycle listeners to a `Runnable`, returning a new `Runnable`.
|
|
5866
5841
|
|
|
5867
|
-
The
|
|
5868
|
-
|
|
5842
|
+
The `Run` object contains information about the run, including its `id`,
|
|
5843
|
+
`type`, `input`, `output`, `error`, `start_time`, `end_time`, and
|
|
5869
5844
|
any tags or metadata added to the run.
|
|
5870
5845
|
|
|
5871
5846
|
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.
|
|
5847
|
+
on_start: Called before the `Runnable` starts running, with the `Run`
|
|
5848
|
+
object.
|
|
5849
|
+
on_end: Called after the `Runnable` finishes running, with the `Run`
|
|
5850
|
+
object.
|
|
5851
|
+
on_error: Called if the `Runnable` throws an error, with the `Run`
|
|
5852
|
+
object.
|
|
5878
5853
|
|
|
5879
5854
|
Returns:
|
|
5880
|
-
A new
|
|
5855
|
+
A new `Runnable` with the listeners bound.
|
|
5881
5856
|
"""
|
|
5882
5857
|
|
|
5883
5858
|
def listener_config_factory(config: RunnableConfig) -> RunnableConfig:
|
|
@@ -6004,16 +5979,16 @@ RunnableLike = (
|
|
|
6004
5979
|
|
|
6005
5980
|
|
|
6006
5981
|
def coerce_to_runnable(thing: RunnableLike) -> Runnable[Input, Output]:
|
|
6007
|
-
"""Coerce a
|
|
5982
|
+
"""Coerce a `Runnable`-like object into a `Runnable`.
|
|
6008
5983
|
|
|
6009
5984
|
Args:
|
|
6010
|
-
thing: A
|
|
5985
|
+
thing: A `Runnable`-like object.
|
|
6011
5986
|
|
|
6012
5987
|
Returns:
|
|
6013
|
-
A
|
|
5988
|
+
A `Runnable`.
|
|
6014
5989
|
|
|
6015
5990
|
Raises:
|
|
6016
|
-
TypeError: If the object is not
|
|
5991
|
+
TypeError: If the object is not `Runnable`-like.
|
|
6017
5992
|
"""
|
|
6018
5993
|
if isinstance(thing, Runnable):
|
|
6019
5994
|
return thing
|
|
@@ -6060,16 +6035,16 @@ def chain(
|
|
|
6060
6035
|
| Callable[[Input], Coroutine[Any, Any, Output]]
|
|
6061
6036
|
| Callable[[Input], AsyncIterator[Output]],
|
|
6062
6037
|
) -> Runnable[Input, Output]:
|
|
6063
|
-
"""Decorate a function to make it a
|
|
6038
|
+
"""Decorate a function to make it a `Runnable`.
|
|
6064
6039
|
|
|
6065
|
-
Sets the name of the
|
|
6040
|
+
Sets the name of the `Runnable` to the name of the function.
|
|
6066
6041
|
Any runnables called by the function will be traced as dependencies.
|
|
6067
6042
|
|
|
6068
6043
|
Args:
|
|
6069
|
-
func: A
|
|
6044
|
+
func: A `Callable`.
|
|
6070
6045
|
|
|
6071
6046
|
Returns:
|
|
6072
|
-
A
|
|
6047
|
+
A `Runnable`.
|
|
6073
6048
|
|
|
6074
6049
|
Example:
|
|
6075
6050
|
```python
|
|
@@ -6081,10 +6056,10 @@ def chain(
|
|
|
6081
6056
|
@chain
|
|
6082
6057
|
def my_func(fields):
|
|
6083
6058
|
prompt = PromptTemplate("Hello, {name}!")
|
|
6084
|
-
|
|
6059
|
+
model = OpenAI()
|
|
6085
6060
|
formatted = prompt.invoke(**fields)
|
|
6086
6061
|
|
|
6087
|
-
for chunk in
|
|
6062
|
+
for chunk in model.stream(formatted):
|
|
6088
6063
|
yield chunk
|
|
6089
6064
|
```
|
|
6090
6065
|
"""
|