langchain-core 0.4.0.dev0__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +45 -70
- langchain_core/_api/deprecation.py +80 -80
- langchain_core/_api/path.py +22 -8
- langchain_core/_import_utils.py +10 -4
- langchain_core/agents.py +25 -21
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +341 -348
- langchain_core/callbacks/file.py +55 -44
- langchain_core/callbacks/manager.py +546 -683
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +35 -36
- langchain_core/callbacks/usage.py +65 -70
- langchain_core/chat_history.py +48 -55
- langchain_core/document_loaders/base.py +46 -21
- langchain_core/document_loaders/langsmith.py +39 -36
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +96 -74
- langchain_core/documents/compressor.py +12 -9
- langchain_core/documents/transformers.py +29 -28
- langchain_core/embeddings/fake.py +56 -57
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +15 -9
- langchain_core/globals.py +4 -163
- langchain_core/indexing/api.py +132 -125
- langchain_core/indexing/base.py +64 -67
- langchain_core/indexing/in_memory.py +26 -6
- langchain_core/language_models/__init__.py +15 -27
- langchain_core/language_models/_utils.py +267 -117
- langchain_core/language_models/base.py +92 -177
- langchain_core/language_models/chat_models.py +547 -407
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +72 -118
- langchain_core/language_models/llms.py +168 -242
- langchain_core/load/dump.py +8 -11
- langchain_core/load/load.py +32 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +50 -56
- langchain_core/messages/__init__.py +36 -51
- langchain_core/messages/ai.py +377 -150
- langchain_core/messages/base.py +239 -47
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -3
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +7 -7
- langchain_core/messages/human.py +44 -38
- langchain_core/messages/modifier.py +3 -2
- langchain_core/messages/system.py +40 -27
- langchain_core/messages/tool.py +160 -58
- langchain_core/messages/utils.py +527 -638
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +68 -104
- langchain_core/output_parsers/json.py +13 -17
- langchain_core/output_parsers/list.py +11 -33
- langchain_core/output_parsers/openai_functions.py +56 -74
- langchain_core/output_parsers/openai_tools.py +68 -109
- langchain_core/output_parsers/pydantic.py +15 -13
- langchain_core/output_parsers/string.py +6 -2
- langchain_core/output_parsers/transform.py +17 -60
- langchain_core/output_parsers/xml.py +34 -44
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +26 -11
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +17 -6
- langchain_core/outputs/llm_result.py +15 -8
- langchain_core/prompt_values.py +29 -123
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -63
- langchain_core/prompts/chat.py +259 -288
- langchain_core/prompts/dict.py +19 -11
- langchain_core/prompts/few_shot.py +84 -90
- langchain_core/prompts/few_shot_with_templates.py +14 -12
- langchain_core/prompts/image.py +19 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +7 -8
- langchain_core/prompts/prompt.py +42 -43
- langchain_core/prompts/string.py +37 -16
- langchain_core/prompts/structured.py +43 -46
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +52 -192
- langchain_core/runnables/base.py +1727 -1683
- langchain_core/runnables/branch.py +52 -73
- langchain_core/runnables/config.py +89 -103
- langchain_core/runnables/configurable.py +128 -130
- langchain_core/runnables/fallbacks.py +93 -82
- langchain_core/runnables/graph.py +127 -127
- langchain_core/runnables/graph_ascii.py +63 -41
- langchain_core/runnables/graph_mermaid.py +87 -70
- langchain_core/runnables/graph_png.py +31 -36
- langchain_core/runnables/history.py +145 -161
- langchain_core/runnables/passthrough.py +141 -144
- langchain_core/runnables/retry.py +84 -68
- langchain_core/runnables/router.py +33 -37
- langchain_core/runnables/schema.py +79 -72
- langchain_core/runnables/utils.py +95 -139
- langchain_core/stores.py +85 -131
- langchain_core/structured_query.py +11 -15
- langchain_core/sys_info.py +31 -32
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +221 -247
- langchain_core/tools/convert.py +144 -161
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -19
- langchain_core/tools/simple.py +52 -29
- langchain_core/tools/structured.py +56 -60
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +103 -112
- langchain_core/tracers/context.py +29 -48
- langchain_core/tracers/core.py +142 -105
- langchain_core/tracers/evaluation.py +30 -34
- langchain_core/tracers/event_stream.py +162 -117
- langchain_core/tracers/langchain.py +34 -36
- langchain_core/tracers/log_stream.py +87 -49
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +18 -34
- langchain_core/tracers/run_collector.py +8 -20
- langchain_core/tracers/schemas.py +0 -125
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +47 -9
- langchain_core/utils/aiter.py +70 -66
- langchain_core/utils/env.py +12 -9
- langchain_core/utils/function_calling.py +139 -206
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +48 -45
- langchain_core/utils/json.py +14 -4
- langchain_core/utils/json_schema.py +159 -43
- langchain_core/utils/mustache.py +32 -25
- langchain_core/utils/pydantic.py +67 -40
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +104 -62
- langchain_core/vectorstores/base.py +131 -179
- langchain_core/vectorstores/in_memory.py +113 -182
- langchain_core/vectorstores/utils.py +23 -17
- langchain_core/version.py +1 -1
- langchain_core-1.0.0.dist-info/METADATA +68 -0
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -448
- langchain_core/memory.py +0 -116
- langchain_core/messages/content_blocks.py +0 -1435
- langchain_core/prompts/pipeline.py +0 -133
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -23
- langchain_core/utils/loading.py +0 -31
- langchain_core/v1/__init__.py +0 -1
- langchain_core/v1/chat_models.py +0 -1047
- langchain_core/v1/messages.py +0 -755
- langchain_core-0.4.0.dev0.dist-info/METADATA +0 -108
- langchain_core-0.4.0.dev0.dist-info/RECORD +0 -177
- langchain_core-0.4.0.dev0.dist-info/entry_points.txt +0 -4
|
@@ -5,13 +5,10 @@ from __future__ import annotations
|
|
|
5
5
|
import asyncio
|
|
6
6
|
import inspect
|
|
7
7
|
import threading
|
|
8
|
-
from collections.abc import Awaitable
|
|
8
|
+
from collections.abc import Awaitable, Callable
|
|
9
9
|
from typing import (
|
|
10
10
|
TYPE_CHECKING,
|
|
11
11
|
Any,
|
|
12
|
-
Callable,
|
|
13
|
-
Optional,
|
|
14
|
-
Union,
|
|
15
12
|
cast,
|
|
16
13
|
)
|
|
17
14
|
|
|
@@ -54,10 +51,10 @@ def identity(x: Other) -> Other:
|
|
|
54
51
|
"""Identity function.
|
|
55
52
|
|
|
56
53
|
Args:
|
|
57
|
-
x
|
|
54
|
+
x: input.
|
|
58
55
|
|
|
59
56
|
Returns:
|
|
60
|
-
|
|
57
|
+
output.
|
|
61
58
|
"""
|
|
62
59
|
return x
|
|
63
60
|
|
|
@@ -66,10 +63,10 @@ async def aidentity(x: Other) -> Other:
|
|
|
66
63
|
"""Async identity function.
|
|
67
64
|
|
|
68
65
|
Args:
|
|
69
|
-
x
|
|
66
|
+
x: input.
|
|
70
67
|
|
|
71
68
|
Returns:
|
|
72
|
-
|
|
69
|
+
output.
|
|
73
70
|
"""
|
|
74
71
|
return x
|
|
75
72
|
|
|
@@ -86,67 +83,66 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
86
83
|
and experiment with.
|
|
87
84
|
|
|
88
85
|
Examples:
|
|
86
|
+
```python
|
|
87
|
+
from langchain_core.runnables import (
|
|
88
|
+
RunnableLambda,
|
|
89
|
+
RunnableParallel,
|
|
90
|
+
RunnablePassthrough,
|
|
91
|
+
)
|
|
89
92
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
RunnableLambda,
|
|
94
|
-
RunnableParallel,
|
|
95
|
-
RunnablePassthrough,
|
|
96
|
-
)
|
|
93
|
+
runnable = RunnableParallel(
|
|
94
|
+
origin=RunnablePassthrough(), modified=lambda x: x + 1
|
|
95
|
+
)
|
|
97
96
|
|
|
98
|
-
|
|
99
|
-
origin=RunnablePassthrough(),
|
|
100
|
-
modified=lambda x: x+1
|
|
101
|
-
)
|
|
97
|
+
runnable.invoke(1) # {'origin': 1, 'modified': 2}
|
|
102
98
|
|
|
103
|
-
runnable.invoke(1) # {'origin': 1, 'modified': 2}
|
|
104
99
|
|
|
100
|
+
def fake_llm(prompt: str) -> str: # Fake LLM for the example
|
|
101
|
+
return "completion"
|
|
105
102
|
|
|
106
|
-
def fake_llm(prompt: str) -> str: # Fake LLM for the example
|
|
107
|
-
return "completion"
|
|
108
103
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
104
|
+
chain = RunnableLambda(fake_llm) | {
|
|
105
|
+
"original": RunnablePassthrough(), # Original LLM output
|
|
106
|
+
"parsed": lambda text: text[::-1], # Parsing logic
|
|
107
|
+
}
|
|
113
108
|
|
|
114
|
-
|
|
109
|
+
chain.invoke("hello") # {'original': 'completion', 'parsed': 'noitelpmoc'}
|
|
110
|
+
```
|
|
115
111
|
|
|
116
112
|
In some cases, it may be useful to pass the input through while adding some
|
|
117
113
|
keys to the output. In this case, you can use the `assign` method:
|
|
118
114
|
|
|
119
|
-
|
|
115
|
+
```python
|
|
116
|
+
from langchain_core.runnables import RunnablePassthrough
|
|
120
117
|
|
|
121
|
-
from langchain_core.runnables import RunnablePassthrough
|
|
122
118
|
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
def fake_llm(prompt: str) -> str: # Fake LLM for the example
|
|
120
|
+
return "completion"
|
|
125
121
|
|
|
126
|
-
runnable = {
|
|
127
|
-
'llm1': fake_llm,
|
|
128
|
-
'llm2': fake_llm,
|
|
129
|
-
} | RunnablePassthrough.assign(
|
|
130
|
-
total_chars=lambda inputs: len(inputs['llm1'] + inputs['llm2'])
|
|
131
|
-
)
|
|
132
122
|
|
|
133
|
-
|
|
134
|
-
|
|
123
|
+
runnable = {
|
|
124
|
+
"llm1": fake_llm,
|
|
125
|
+
"llm2": fake_llm,
|
|
126
|
+
} | RunnablePassthrough.assign(
|
|
127
|
+
total_chars=lambda inputs: len(inputs["llm1"] + inputs["llm2"])
|
|
128
|
+
)
|
|
135
129
|
|
|
130
|
+
runnable.invoke("hello")
|
|
131
|
+
# {'llm1': 'completion', 'llm2': 'completion', 'total_chars': 20}
|
|
132
|
+
```
|
|
136
133
|
"""
|
|
137
134
|
|
|
138
|
-
input_type:
|
|
135
|
+
input_type: type[Other] | None = None
|
|
139
136
|
|
|
140
|
-
func:
|
|
141
|
-
|
|
142
|
-
|
|
137
|
+
func: Callable[[Other], None] | Callable[[Other, RunnableConfig], None] | None = (
|
|
138
|
+
None
|
|
139
|
+
)
|
|
143
140
|
|
|
144
|
-
afunc:
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
] = None
|
|
141
|
+
afunc: (
|
|
142
|
+
Callable[[Other], Awaitable[None]]
|
|
143
|
+
| Callable[[Other, RunnableConfig], Awaitable[None]]
|
|
144
|
+
| None
|
|
145
|
+
) = None
|
|
150
146
|
|
|
151
147
|
@override
|
|
152
148
|
def __repr_args__(self) -> Any:
|
|
@@ -156,23 +152,16 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
156
152
|
|
|
157
153
|
def __init__(
|
|
158
154
|
self,
|
|
159
|
-
func:
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
] = None,
|
|
168
|
-
afunc: Optional[
|
|
169
|
-
Union[
|
|
170
|
-
Callable[[Other], Awaitable[None]],
|
|
171
|
-
Callable[[Other, RunnableConfig], Awaitable[None]],
|
|
172
|
-
]
|
|
173
|
-
] = None,
|
|
155
|
+
func: Callable[[Other], None]
|
|
156
|
+
| Callable[[Other, RunnableConfig], None]
|
|
157
|
+
| Callable[[Other], Awaitable[None]]
|
|
158
|
+
| Callable[[Other, RunnableConfig], Awaitable[None]]
|
|
159
|
+
| None = None,
|
|
160
|
+
afunc: Callable[[Other], Awaitable[None]]
|
|
161
|
+
| Callable[[Other, RunnableConfig], Awaitable[None]]
|
|
162
|
+
| None = None,
|
|
174
163
|
*,
|
|
175
|
-
input_type:
|
|
164
|
+
input_type: type[Other] | None = None,
|
|
176
165
|
**kwargs: Any,
|
|
177
166
|
) -> None:
|
|
178
167
|
"""Create e RunnablePassthrough.
|
|
@@ -186,16 +175,21 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
186
175
|
afunc = func
|
|
187
176
|
func = None
|
|
188
177
|
|
|
189
|
-
super().__init__(func=func, afunc=afunc, input_type=input_type, **kwargs)
|
|
178
|
+
super().__init__(func=func, afunc=afunc, input_type=input_type, **kwargs)
|
|
190
179
|
|
|
191
180
|
@classmethod
|
|
192
181
|
@override
|
|
193
182
|
def is_lc_serializable(cls) -> bool:
|
|
183
|
+
"""Return True as this class is serializable."""
|
|
194
184
|
return True
|
|
195
185
|
|
|
196
186
|
@classmethod
|
|
197
|
-
@override
|
|
198
187
|
def get_lc_namespace(cls) -> list[str]:
|
|
188
|
+
"""Get the namespace of the LangChain object.
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
`["langchain", "schema", "runnable"]`
|
|
192
|
+
"""
|
|
199
193
|
return ["langchain", "schema", "runnable"]
|
|
200
194
|
|
|
201
195
|
@property
|
|
@@ -212,14 +206,9 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
212
206
|
@override
|
|
213
207
|
def assign(
|
|
214
208
|
cls,
|
|
215
|
-
**kwargs:
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
Mapping[
|
|
219
|
-
str,
|
|
220
|
-
Union[Runnable[dict[str, Any], Any], Callable[[dict[str, Any]], Any]],
|
|
221
|
-
],
|
|
222
|
-
],
|
|
209
|
+
**kwargs: Runnable[dict[str, Any], Any]
|
|
210
|
+
| Callable[[dict[str, Any]], Any]
|
|
211
|
+
| Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]],
|
|
223
212
|
) -> RunnableAssign:
|
|
224
213
|
"""Merge the Dict input with the output produced by the mapping argument.
|
|
225
214
|
|
|
@@ -235,7 +224,7 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
235
224
|
|
|
236
225
|
@override
|
|
237
226
|
def invoke(
|
|
238
|
-
self, input: Other, config:
|
|
227
|
+
self, input: Other, config: RunnableConfig | None = None, **kwargs: Any
|
|
239
228
|
) -> Other:
|
|
240
229
|
if self.func is not None:
|
|
241
230
|
call_func_with_variable_args(
|
|
@@ -247,8 +236,8 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
247
236
|
async def ainvoke(
|
|
248
237
|
self,
|
|
249
238
|
input: Other,
|
|
250
|
-
config:
|
|
251
|
-
**kwargs:
|
|
239
|
+
config: RunnableConfig | None = None,
|
|
240
|
+
**kwargs: Any | None,
|
|
252
241
|
) -> Other:
|
|
253
242
|
if self.afunc is not None:
|
|
254
243
|
await acall_func_with_variable_args(
|
|
@@ -264,7 +253,7 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
264
253
|
def transform(
|
|
265
254
|
self,
|
|
266
255
|
input: Iterator[Other],
|
|
267
|
-
config:
|
|
256
|
+
config: RunnableConfig | None = None,
|
|
268
257
|
**kwargs: Any,
|
|
269
258
|
) -> Iterator[Other]:
|
|
270
259
|
if self.func is None:
|
|
@@ -295,7 +284,7 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
295
284
|
async def atransform(
|
|
296
285
|
self,
|
|
297
286
|
input: AsyncIterator[Other],
|
|
298
|
-
config:
|
|
287
|
+
config: RunnableConfig | None = None,
|
|
299
288
|
**kwargs: Any,
|
|
300
289
|
) -> AsyncIterator[Other]:
|
|
301
290
|
if self.afunc is None and self.func is None:
|
|
@@ -338,7 +327,7 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
338
327
|
def stream(
|
|
339
328
|
self,
|
|
340
329
|
input: Other,
|
|
341
|
-
config:
|
|
330
|
+
config: RunnableConfig | None = None,
|
|
342
331
|
**kwargs: Any,
|
|
343
332
|
) -> Iterator[Other]:
|
|
344
333
|
return self.transform(iter([input]), config, **kwargs)
|
|
@@ -347,7 +336,7 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
|
|
|
347
336
|
async def astream(
|
|
348
337
|
self,
|
|
349
338
|
input: Other,
|
|
350
|
-
config:
|
|
339
|
+
config: RunnableConfig | None = None,
|
|
351
340
|
**kwargs: Any,
|
|
352
341
|
) -> AsyncIterator[Other]:
|
|
353
342
|
async def input_aiter() -> AsyncIterator[Other]:
|
|
@@ -369,32 +358,35 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
369
358
|
on the mapper's logic.
|
|
370
359
|
|
|
371
360
|
Examples:
|
|
372
|
-
|
|
361
|
+
```python
|
|
362
|
+
# This is a RunnableAssign
|
|
363
|
+
from langchain_core.runnables.passthrough import (
|
|
364
|
+
RunnableAssign,
|
|
365
|
+
RunnableParallel,
|
|
366
|
+
)
|
|
367
|
+
from langchain_core.runnables.base import RunnableLambda
|
|
373
368
|
|
|
374
|
-
# This is a RunnableAssign
|
|
375
|
-
from langchain_core.runnables.passthrough import (
|
|
376
|
-
RunnableAssign,
|
|
377
|
-
RunnableParallel,
|
|
378
|
-
)
|
|
379
|
-
from langchain_core.runnables.base import RunnableLambda
|
|
380
369
|
|
|
381
|
-
|
|
382
|
-
|
|
370
|
+
def add_ten(x: dict[str, int]) -> dict[str, int]:
|
|
371
|
+
return {"added": x["input"] + 10}
|
|
383
372
|
|
|
384
|
-
mapper = RunnableParallel(
|
|
385
|
-
{"add_step": RunnableLambda(add_ten),}
|
|
386
|
-
)
|
|
387
373
|
|
|
388
|
-
|
|
374
|
+
mapper = RunnableParallel(
|
|
375
|
+
{
|
|
376
|
+
"add_step": RunnableLambda(add_ten),
|
|
377
|
+
}
|
|
378
|
+
)
|
|
389
379
|
|
|
390
|
-
|
|
391
|
-
runnable_assign.invoke({"input": 5})
|
|
392
|
-
# returns {'input': 5, 'add_step': {'added': 15}}
|
|
380
|
+
runnable_assign = RunnableAssign(mapper)
|
|
393
381
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
382
|
+
# Synchronous example
|
|
383
|
+
runnable_assign.invoke({"input": 5})
|
|
384
|
+
# returns {'input': 5, 'add_step': {'added': 15}}
|
|
397
385
|
|
|
386
|
+
# Asynchronous example
|
|
387
|
+
await runnable_assign.ainvoke({"input": 5})
|
|
388
|
+
# returns {'input': 5, 'add_step': {'added': 15}}
|
|
389
|
+
```
|
|
398
390
|
"""
|
|
399
391
|
|
|
400
392
|
mapper: RunnableParallel
|
|
@@ -403,25 +395,29 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
403
395
|
"""Create a RunnableAssign.
|
|
404
396
|
|
|
405
397
|
Args:
|
|
406
|
-
mapper: A
|
|
398
|
+
mapper: A `RunnableParallel` instance that will be used to transform the
|
|
407
399
|
input dictionary.
|
|
408
400
|
"""
|
|
409
|
-
super().__init__(mapper=mapper, **kwargs)
|
|
401
|
+
super().__init__(mapper=mapper, **kwargs)
|
|
410
402
|
|
|
411
403
|
@classmethod
|
|
412
404
|
@override
|
|
413
405
|
def is_lc_serializable(cls) -> bool:
|
|
406
|
+
"""Return True as this class is serializable."""
|
|
414
407
|
return True
|
|
415
408
|
|
|
416
409
|
@classmethod
|
|
417
410
|
@override
|
|
418
411
|
def get_lc_namespace(cls) -> list[str]:
|
|
412
|
+
"""Get the namespace of the LangChain object.
|
|
413
|
+
|
|
414
|
+
Returns:
|
|
415
|
+
`["langchain", "schema", "runnable"]`
|
|
416
|
+
"""
|
|
419
417
|
return ["langchain", "schema", "runnable"]
|
|
420
418
|
|
|
421
419
|
@override
|
|
422
|
-
def get_name(
|
|
423
|
-
self, suffix: Optional[str] = None, *, name: Optional[str] = None
|
|
424
|
-
) -> str:
|
|
420
|
+
def get_name(self, suffix: str | None = None, *, name: str | None = None) -> str:
|
|
425
421
|
name = (
|
|
426
422
|
name
|
|
427
423
|
or self.name
|
|
@@ -430,9 +426,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
430
426
|
return super().get_name(suffix, name=name)
|
|
431
427
|
|
|
432
428
|
@override
|
|
433
|
-
def get_input_schema(
|
|
434
|
-
self, config: Optional[RunnableConfig] = None
|
|
435
|
-
) -> type[BaseModel]:
|
|
429
|
+
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
436
430
|
map_input_schema = self.mapper.get_input_schema(config)
|
|
437
431
|
if not issubclass(map_input_schema, RootModel):
|
|
438
432
|
# ie. it's a dict
|
|
@@ -442,7 +436,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
442
436
|
|
|
443
437
|
@override
|
|
444
438
|
def get_output_schema(
|
|
445
|
-
self, config:
|
|
439
|
+
self, config: RunnableConfig | None = None
|
|
446
440
|
) -> type[BaseModel]:
|
|
447
441
|
map_input_schema = self.mapper.get_input_schema(config)
|
|
448
442
|
map_output_schema = self.mapper.get_output_schema(config)
|
|
@@ -507,7 +501,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
507
501
|
def invoke(
|
|
508
502
|
self,
|
|
509
503
|
input: dict[str, Any],
|
|
510
|
-
config:
|
|
504
|
+
config: RunnableConfig | None = None,
|
|
511
505
|
**kwargs: Any,
|
|
512
506
|
) -> dict[str, Any]:
|
|
513
507
|
return self._call_with_config(self._invoke, input, config, **kwargs)
|
|
@@ -536,7 +530,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
536
530
|
async def ainvoke(
|
|
537
531
|
self,
|
|
538
532
|
input: dict[str, Any],
|
|
539
|
-
config:
|
|
533
|
+
config: RunnableConfig | None = None,
|
|
540
534
|
**kwargs: Any,
|
|
541
535
|
) -> dict[str, Any]:
|
|
542
536
|
return await self._acall_with_config(self._ainvoke, input, config, **kwargs)
|
|
@@ -591,7 +585,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
591
585
|
def transform(
|
|
592
586
|
self,
|
|
593
587
|
input: Iterator[dict[str, Any]],
|
|
594
|
-
config:
|
|
588
|
+
config: RunnableConfig | None = None,
|
|
595
589
|
**kwargs: Any | None,
|
|
596
590
|
) -> Iterator[dict[str, Any]]:
|
|
597
591
|
yield from self._transform_stream_with_config(
|
|
@@ -643,7 +637,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
643
637
|
async def atransform(
|
|
644
638
|
self,
|
|
645
639
|
input: AsyncIterator[dict[str, Any]],
|
|
646
|
-
config:
|
|
640
|
+
config: RunnableConfig | None = None,
|
|
647
641
|
**kwargs: Any,
|
|
648
642
|
) -> AsyncIterator[dict[str, Any]]:
|
|
649
643
|
async for chunk in self._atransform_stream_with_config(
|
|
@@ -655,7 +649,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
655
649
|
def stream(
|
|
656
650
|
self,
|
|
657
651
|
input: dict[str, Any],
|
|
658
|
-
config:
|
|
652
|
+
config: RunnableConfig | None = None,
|
|
659
653
|
**kwargs: Any,
|
|
660
654
|
) -> Iterator[dict[str, Any]]:
|
|
661
655
|
return self.transform(iter([input]), config, **kwargs)
|
|
@@ -664,7 +658,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
664
658
|
async def astream(
|
|
665
659
|
self,
|
|
666
660
|
input: dict[str, Any],
|
|
667
|
-
config:
|
|
661
|
+
config: RunnableConfig | None = None,
|
|
668
662
|
**kwargs: Any,
|
|
669
663
|
) -> AsyncIterator[dict[str, Any]]:
|
|
670
664
|
async def input_aiter() -> AsyncIterator[dict[str, Any]]:
|
|
@@ -683,54 +677,57 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
683
677
|
the selected keys.
|
|
684
678
|
|
|
685
679
|
Example:
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
runnable = RunnablePick(keys=['name', 'age'])
|
|
680
|
+
```python
|
|
681
|
+
from langchain_core.runnables.passthrough import RunnablePick
|
|
682
|
+
|
|
683
|
+
input_data = {
|
|
684
|
+
"name": "John",
|
|
685
|
+
"age": 30,
|
|
686
|
+
"city": "New York",
|
|
687
|
+
"country": "USA",
|
|
688
|
+
}
|
|
698
689
|
|
|
699
|
-
|
|
690
|
+
runnable = RunnablePick(keys=["name", "age"])
|
|
700
691
|
|
|
701
|
-
|
|
692
|
+
output_data = runnable.invoke(input_data)
|
|
702
693
|
|
|
694
|
+
print(output_data) # Output: {'name': 'John', 'age': 30}
|
|
695
|
+
```
|
|
703
696
|
"""
|
|
704
697
|
|
|
705
|
-
keys:
|
|
698
|
+
keys: str | list[str]
|
|
706
699
|
|
|
707
|
-
def __init__(self, keys:
|
|
700
|
+
def __init__(self, keys: str | list[str], **kwargs: Any) -> None:
|
|
708
701
|
"""Create a RunnablePick.
|
|
709
702
|
|
|
710
703
|
Args:
|
|
711
704
|
keys: A single key or a list of keys to pick from the input dictionary.
|
|
712
705
|
"""
|
|
713
|
-
super().__init__(keys=keys, **kwargs)
|
|
706
|
+
super().__init__(keys=keys, **kwargs)
|
|
714
707
|
|
|
715
708
|
@classmethod
|
|
716
709
|
@override
|
|
717
710
|
def is_lc_serializable(cls) -> bool:
|
|
711
|
+
"""Return True as this class is serializable."""
|
|
718
712
|
return True
|
|
719
713
|
|
|
720
714
|
@classmethod
|
|
721
715
|
@override
|
|
722
716
|
def get_lc_namespace(cls) -> list[str]:
|
|
723
|
-
"""Get the namespace of the
|
|
717
|
+
"""Get the namespace of the LangChain object.
|
|
718
|
+
|
|
719
|
+
Returns:
|
|
720
|
+
`["langchain", "schema", "runnable"]`
|
|
721
|
+
"""
|
|
724
722
|
return ["langchain", "schema", "runnable"]
|
|
725
723
|
|
|
726
724
|
@override
|
|
727
|
-
def get_name(
|
|
728
|
-
self, suffix: Optional[str] = None, *, name: Optional[str] = None
|
|
729
|
-
) -> str:
|
|
725
|
+
def get_name(self, suffix: str | None = None, *, name: str | None = None) -> str:
|
|
730
726
|
name = (
|
|
731
727
|
name
|
|
732
728
|
or self.name
|
|
733
|
-
or
|
|
729
|
+
or "RunnablePick"
|
|
730
|
+
f"<{','.join([self.keys] if isinstance(self.keys, str) else self.keys)}>"
|
|
734
731
|
)
|
|
735
732
|
return super().get_name(suffix, name=name)
|
|
736
733
|
|
|
@@ -756,7 +753,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
756
753
|
def invoke(
|
|
757
754
|
self,
|
|
758
755
|
input: dict[str, Any],
|
|
759
|
-
config:
|
|
756
|
+
config: RunnableConfig | None = None,
|
|
760
757
|
**kwargs: Any,
|
|
761
758
|
) -> dict[str, Any]:
|
|
762
759
|
return self._call_with_config(self._invoke, input, config, **kwargs)
|
|
@@ -771,7 +768,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
771
768
|
async def ainvoke(
|
|
772
769
|
self,
|
|
773
770
|
input: dict[str, Any],
|
|
774
|
-
config:
|
|
771
|
+
config: RunnableConfig | None = None,
|
|
775
772
|
**kwargs: Any,
|
|
776
773
|
) -> dict[str, Any]:
|
|
777
774
|
return await self._acall_with_config(self._ainvoke, input, config, **kwargs)
|
|
@@ -789,7 +786,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
789
786
|
def transform(
|
|
790
787
|
self,
|
|
791
788
|
input: Iterator[dict[str, Any]],
|
|
792
|
-
config:
|
|
789
|
+
config: RunnableConfig | None = None,
|
|
793
790
|
**kwargs: Any,
|
|
794
791
|
) -> Iterator[dict[str, Any]]:
|
|
795
792
|
yield from self._transform_stream_with_config(
|
|
@@ -809,7 +806,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
809
806
|
async def atransform(
|
|
810
807
|
self,
|
|
811
808
|
input: AsyncIterator[dict[str, Any]],
|
|
812
|
-
config:
|
|
809
|
+
config: RunnableConfig | None = None,
|
|
813
810
|
**kwargs: Any,
|
|
814
811
|
) -> AsyncIterator[dict[str, Any]]:
|
|
815
812
|
async for chunk in self._atransform_stream_with_config(
|
|
@@ -821,7 +818,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
821
818
|
def stream(
|
|
822
819
|
self,
|
|
823
820
|
input: dict[str, Any],
|
|
824
|
-
config:
|
|
821
|
+
config: RunnableConfig | None = None,
|
|
825
822
|
**kwargs: Any,
|
|
826
823
|
) -> Iterator[dict[str, Any]]:
|
|
827
824
|
return self.transform(iter([input]), config, **kwargs)
|
|
@@ -830,7 +827,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
|
|
|
830
827
|
async def astream(
|
|
831
828
|
self,
|
|
832
829
|
input: dict[str, Any],
|
|
833
|
-
config:
|
|
830
|
+
config: RunnableConfig | None = None,
|
|
834
831
|
**kwargs: Any,
|
|
835
832
|
) -> AsyncIterator[dict[str, Any]]:
|
|
836
833
|
async def input_aiter() -> AsyncIterator[dict[str, Any]]:
|