langchain-core 0.3.79__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +52 -65
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +19 -19
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +323 -334
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +441 -507
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +48 -63
- langchain_core/document_loaders/base.py +23 -23
- langchain_core/document_loaders/langsmith.py +37 -37
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +62 -65
- langchain_core/documents/compressor.py +4 -4
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +10 -11
- langchain_core/globals.py +3 -151
- langchain_core/indexing/api.py +61 -66
- langchain_core/indexing/base.py +58 -58
- langchain_core/indexing/in_memory.py +3 -3
- langchain_core/language_models/__init__.py +14 -27
- langchain_core/language_models/_utils.py +270 -84
- langchain_core/language_models/base.py +55 -162
- langchain_core/language_models/chat_models.py +442 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +61 -39
- langchain_core/language_models/llms.py +123 -231
- langchain_core/load/dump.py +4 -5
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +39 -40
- langchain_core/messages/__init__.py +61 -22
- langchain_core/messages/ai.py +368 -163
- langchain_core/messages/base.py +214 -43
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +41 -38
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +38 -28
- langchain_core/messages/tool.py +96 -103
- langchain_core/messages/utils.py +478 -504
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +58 -61
- langchain_core/output_parsers/json.py +7 -8
- langchain_core/output_parsers/list.py +5 -7
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +14 -19
- langchain_core/output_parsers/pydantic.py +12 -13
- langchain_core/output_parsers/string.py +2 -2
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +8 -10
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +8 -8
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +12 -12
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +45 -55
- langchain_core/prompts/chat.py +254 -313
- langchain_core/prompts/dict.py +5 -5
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +4 -4
- langchain_core/prompts/structured.py +42 -50
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +49 -190
- langchain_core/runnables/base.py +1484 -1709
- langchain_core/runnables/branch.py +45 -61
- langchain_core/runnables/config.py +80 -88
- langchain_core/runnables/configurable.py +117 -134
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +85 -95
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +38 -50
- langchain_core/runnables/graph_png.py +15 -16
- langchain_core/runnables/history.py +135 -148
- langchain_core/runnables/passthrough.py +124 -150
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +25 -30
- langchain_core/runnables/schema.py +79 -74
- langchain_core/runnables/utils.py +62 -68
- langchain_core/stores.py +81 -115
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +179 -187
- langchain_core/tools/convert.py +131 -139
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +11 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +30 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +50 -60
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +115 -70
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +43 -43
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +46 -8
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -191
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +37 -42
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +8 -8
- langchain_core/utils/mustache.py +9 -11
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +80 -54
- langchain_core/vectorstores/base.py +129 -164
- langchain_core/vectorstores/in_memory.py +99 -174
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -447
- langchain_core/memory.py +0 -120
- langchain_core/messages/content_blocks.py +0 -176
- langchain_core/prompts/pipeline.py +0 -138
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-0.3.79.dist-info/RECORD +0 -174
- langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
langchain_core/prompts/base.py
CHANGED
|
@@ -6,17 +6,14 @@ import contextlib
|
|
|
6
6
|
import json
|
|
7
7
|
import typing
|
|
8
8
|
from abc import ABC, abstractmethod
|
|
9
|
-
from collections.abc import Mapping
|
|
9
|
+
from collections.abc import Callable, Mapping
|
|
10
10
|
from functools import cached_property
|
|
11
11
|
from pathlib import Path
|
|
12
12
|
from typing import (
|
|
13
13
|
TYPE_CHECKING,
|
|
14
14
|
Any,
|
|
15
|
-
Callable,
|
|
16
15
|
Generic,
|
|
17
|
-
Optional,
|
|
18
16
|
TypeVar,
|
|
19
|
-
Union,
|
|
20
17
|
)
|
|
21
18
|
|
|
22
19
|
import yaml
|
|
@@ -57,16 +54,16 @@ class BasePromptTemplate(
|
|
|
57
54
|
input_types: typing.Dict[str, Any] = Field(default_factory=dict, exclude=True) # noqa: UP006
|
|
58
55
|
"""A dictionary of the types of the variables the prompt template expects.
|
|
59
56
|
If not provided, all variables are assumed to be strings."""
|
|
60
|
-
output_parser:
|
|
57
|
+
output_parser: BaseOutputParser | None = None
|
|
61
58
|
"""How to parse the output of calling an LLM on this formatted prompt."""
|
|
62
59
|
partial_variables: Mapping[str, Any] = Field(default_factory=dict)
|
|
63
60
|
"""A dictionary of the partial variables the prompt template carries.
|
|
64
61
|
|
|
65
62
|
Partial variables populate the template so that you don't need to
|
|
66
63
|
pass them in every time you call the prompt."""
|
|
67
|
-
metadata:
|
|
64
|
+
metadata: typing.Dict[str, Any] | None = None # noqa: UP006
|
|
68
65
|
"""Metadata to be used for tracing."""
|
|
69
|
-
tags:
|
|
66
|
+
tags: list[str] | None = None
|
|
70
67
|
"""Tags to be used for tracing."""
|
|
71
68
|
|
|
72
69
|
@model_validator(mode="after")
|
|
@@ -99,10 +96,10 @@ class BasePromptTemplate(
|
|
|
99
96
|
|
|
100
97
|
@classmethod
|
|
101
98
|
def get_lc_namespace(cls) -> list[str]:
|
|
102
|
-
"""Get the namespace of the
|
|
99
|
+
"""Get the namespace of the LangChain object.
|
|
103
100
|
|
|
104
101
|
Returns:
|
|
105
|
-
|
|
102
|
+
`["langchain", "schema", "prompt_template"]`
|
|
106
103
|
"""
|
|
107
104
|
return ["langchain", "schema", "prompt_template"]
|
|
108
105
|
|
|
@@ -123,19 +120,17 @@ class BasePromptTemplate(
|
|
|
123
120
|
@override
|
|
124
121
|
def OutputType(self) -> Any:
|
|
125
122
|
"""Return the output type of the prompt."""
|
|
126
|
-
return
|
|
123
|
+
return StringPromptValue | ChatPromptValueConcrete
|
|
127
124
|
|
|
128
125
|
@override
|
|
129
|
-
def get_input_schema(
|
|
130
|
-
self, config: Optional[RunnableConfig] = None
|
|
131
|
-
) -> type[BaseModel]:
|
|
126
|
+
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
132
127
|
"""Get the input schema for the prompt.
|
|
133
128
|
|
|
134
129
|
Args:
|
|
135
|
-
config:
|
|
130
|
+
config: configuration for the prompt.
|
|
136
131
|
|
|
137
132
|
Returns:
|
|
138
|
-
|
|
133
|
+
The input schema for the prompt.
|
|
139
134
|
"""
|
|
140
135
|
# This is correct, but pydantic typings/mypy don't think so.
|
|
141
136
|
required_input_variables = {
|
|
@@ -195,7 +190,7 @@ class BasePromptTemplate(
|
|
|
195
190
|
|
|
196
191
|
@override
|
|
197
192
|
def invoke(
|
|
198
|
-
self, input: dict, config:
|
|
193
|
+
self, input: dict, config: RunnableConfig | None = None, **kwargs: Any
|
|
199
194
|
) -> PromptValue:
|
|
200
195
|
"""Invoke the prompt.
|
|
201
196
|
|
|
@@ -204,7 +199,7 @@ class BasePromptTemplate(
|
|
|
204
199
|
config: RunnableConfig, configuration for the prompt.
|
|
205
200
|
|
|
206
201
|
Returns:
|
|
207
|
-
|
|
202
|
+
The output of the prompt.
|
|
208
203
|
"""
|
|
209
204
|
config = ensure_config(config)
|
|
210
205
|
if self.metadata:
|
|
@@ -221,7 +216,7 @@ class BasePromptTemplate(
|
|
|
221
216
|
|
|
222
217
|
@override
|
|
223
218
|
async def ainvoke(
|
|
224
|
-
self, input: dict, config:
|
|
219
|
+
self, input: dict, config: RunnableConfig | None = None, **kwargs: Any
|
|
225
220
|
) -> PromptValue:
|
|
226
221
|
"""Async invoke the prompt.
|
|
227
222
|
|
|
@@ -230,7 +225,7 @@ class BasePromptTemplate(
|
|
|
230
225
|
config: RunnableConfig, configuration for the prompt.
|
|
231
226
|
|
|
232
227
|
Returns:
|
|
233
|
-
|
|
228
|
+
The output of the prompt.
|
|
234
229
|
"""
|
|
235
230
|
config = ensure_config(config)
|
|
236
231
|
if self.metadata:
|
|
@@ -250,31 +245,31 @@ class BasePromptTemplate(
|
|
|
250
245
|
"""Create Prompt Value.
|
|
251
246
|
|
|
252
247
|
Args:
|
|
253
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
248
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
254
249
|
|
|
255
250
|
Returns:
|
|
256
|
-
|
|
251
|
+
The output of the prompt.
|
|
257
252
|
"""
|
|
258
253
|
|
|
259
254
|
async def aformat_prompt(self, **kwargs: Any) -> PromptValue:
|
|
260
255
|
"""Async create Prompt Value.
|
|
261
256
|
|
|
262
257
|
Args:
|
|
263
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
258
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
264
259
|
|
|
265
260
|
Returns:
|
|
266
|
-
|
|
261
|
+
The output of the prompt.
|
|
267
262
|
"""
|
|
268
263
|
return self.format_prompt(**kwargs)
|
|
269
264
|
|
|
270
|
-
def partial(self, **kwargs:
|
|
265
|
+
def partial(self, **kwargs: str | Callable[[], str]) -> BasePromptTemplate:
|
|
271
266
|
"""Return a partial of the prompt template.
|
|
272
267
|
|
|
273
268
|
Args:
|
|
274
|
-
kwargs:
|
|
269
|
+
**kwargs: partial variables to set.
|
|
275
270
|
|
|
276
271
|
Returns:
|
|
277
|
-
|
|
272
|
+
A partial of the prompt template.
|
|
278
273
|
"""
|
|
279
274
|
prompt_dict = self.__dict__.copy()
|
|
280
275
|
prompt_dict["input_variables"] = list(
|
|
@@ -295,34 +290,30 @@ class BasePromptTemplate(
|
|
|
295
290
|
"""Format the prompt with the inputs.
|
|
296
291
|
|
|
297
292
|
Args:
|
|
298
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
293
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
299
294
|
|
|
300
295
|
Returns:
|
|
301
296
|
A formatted string.
|
|
302
297
|
|
|
303
298
|
Example:
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
prompt.format(variable1="foo")
|
|
308
|
-
|
|
299
|
+
```python
|
|
300
|
+
prompt.format(variable1="foo")
|
|
301
|
+
```
|
|
309
302
|
"""
|
|
310
303
|
|
|
311
304
|
async def aformat(self, **kwargs: Any) -> FormatOutputType:
|
|
312
305
|
"""Async format the prompt with the inputs.
|
|
313
306
|
|
|
314
307
|
Args:
|
|
315
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
308
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
316
309
|
|
|
317
310
|
Returns:
|
|
318
311
|
A formatted string.
|
|
319
312
|
|
|
320
313
|
Example:
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
await prompt.aformat(variable1="foo")
|
|
325
|
-
|
|
314
|
+
```python
|
|
315
|
+
await prompt.aformat(variable1="foo")
|
|
316
|
+
```
|
|
326
317
|
"""
|
|
327
318
|
return self.format(**kwargs)
|
|
328
319
|
|
|
@@ -335,17 +326,17 @@ class BasePromptTemplate(
|
|
|
335
326
|
"""Return dictionary representation of prompt.
|
|
336
327
|
|
|
337
328
|
Args:
|
|
338
|
-
kwargs: Any additional arguments to pass to the dictionary.
|
|
329
|
+
**kwargs: Any additional arguments to pass to the dictionary.
|
|
339
330
|
|
|
340
331
|
Returns:
|
|
341
|
-
|
|
332
|
+
Dictionary representation of the prompt.
|
|
342
333
|
"""
|
|
343
334
|
prompt_dict = super().model_dump(**kwargs)
|
|
344
335
|
with contextlib.suppress(NotImplementedError):
|
|
345
336
|
prompt_dict["_type"] = self._prompt_type
|
|
346
337
|
return prompt_dict
|
|
347
338
|
|
|
348
|
-
def save(self, file_path:
|
|
339
|
+
def save(self, file_path: Path | str) -> None:
|
|
349
340
|
"""Save the prompt.
|
|
350
341
|
|
|
351
342
|
Args:
|
|
@@ -357,10 +348,9 @@ class BasePromptTemplate(
|
|
|
357
348
|
NotImplementedError: If the prompt type is not implemented.
|
|
358
349
|
|
|
359
350
|
Example:
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
351
|
+
```python
|
|
352
|
+
prompt.save(file_path="path/prompt.yaml")
|
|
353
|
+
```
|
|
364
354
|
"""
|
|
365
355
|
if self.partial_variables:
|
|
366
356
|
msg = "Cannot save prompt with partial variables."
|
|
@@ -379,10 +369,10 @@ class BasePromptTemplate(
|
|
|
379
369
|
directory_path.mkdir(parents=True, exist_ok=True)
|
|
380
370
|
|
|
381
371
|
if save_path.suffix == ".json":
|
|
382
|
-
with save_path.open("w") as f:
|
|
372
|
+
with save_path.open("w", encoding="utf-8") as f:
|
|
383
373
|
json.dump(prompt_dict, f, indent=4)
|
|
384
374
|
elif save_path.suffix.endswith((".yaml", ".yml")):
|
|
385
|
-
with save_path.open("w") as f:
|
|
375
|
+
with save_path.open("w", encoding="utf-8") as f:
|
|
386
376
|
yaml.dump(prompt_dict, f, default_flow_style=False)
|
|
387
377
|
else:
|
|
388
378
|
msg = f"{save_path} must be json or yaml"
|
|
@@ -431,16 +421,16 @@ def format_document(doc: Document, prompt: BasePromptTemplate[str]) -> str:
|
|
|
431
421
|
string of the document formatted.
|
|
432
422
|
|
|
433
423
|
Example:
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
from langchain_core.prompts import PromptTemplate
|
|
424
|
+
```python
|
|
425
|
+
from langchain_core.documents import Document
|
|
426
|
+
from langchain_core.prompts import PromptTemplate
|
|
438
427
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
428
|
+
doc = Document(page_content="This is a joke", metadata={"page": "1"})
|
|
429
|
+
prompt = PromptTemplate.from_template("Page {page}: {page_content}")
|
|
430
|
+
format_document(doc, prompt)
|
|
431
|
+
>>> "Page 1: This is a joke"
|
|
443
432
|
|
|
433
|
+
```
|
|
444
434
|
"""
|
|
445
435
|
return prompt.format(**_get_document_info(doc, prompt))
|
|
446
436
|
|