langchain-core 1.0.0a6__py3-none-any.whl → 1.0.3__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.
- 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 +51 -64
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +20 -22
- langchain_core/caches.py +65 -66
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +321 -336
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +436 -513
- 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 +53 -68
- langchain_core/document_loaders/base.py +27 -25
- langchain_core/document_loaders/blob_loaders.py +1 -1
- langchain_core/document_loaders/langsmith.py +44 -48
- langchain_core/documents/__init__.py +23 -3
- langchain_core/documents/base.py +98 -90
- langchain_core/documents/compressor.py +10 -10
- langchain_core/documents/transformers.py +34 -35
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +28 -32
- langchain_core/exceptions.py +21 -20
- langchain_core/globals.py +3 -151
- langchain_core/indexing/__init__.py +1 -1
- langchain_core/indexing/api.py +121 -126
- langchain_core/indexing/base.py +73 -75
- langchain_core/indexing/in_memory.py +4 -6
- langchain_core/language_models/__init__.py +14 -29
- langchain_core/language_models/_utils.py +58 -61
- langchain_core/language_models/base.py +53 -162
- langchain_core/language_models/chat_models.py +298 -387
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +42 -36
- langchain_core/language_models/llms.py +125 -235
- langchain_core/load/dump.py +9 -12
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +42 -40
- langchain_core/messages/__init__.py +10 -16
- langchain_core/messages/ai.py +148 -148
- langchain_core/messages/base.py +53 -51
- langchain_core/messages/block_translators/__init__.py +19 -22
- langchain_core/messages/block_translators/anthropic.py +6 -6
- langchain_core/messages/block_translators/bedrock_converse.py +5 -5
- langchain_core/messages/block_translators/google_genai.py +10 -7
- langchain_core/messages/block_translators/google_vertexai.py +4 -32
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +5 -5
- langchain_core/messages/block_translators/openai.py +11 -11
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +337 -328
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +24 -31
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +19 -29
- langchain_core/messages/tool.py +74 -90
- langchain_core/messages/utils.py +474 -504
- langchain_core/output_parsers/__init__.py +13 -10
- langchain_core/output_parsers/base.py +61 -61
- langchain_core/output_parsers/format_instructions.py +9 -4
- langchain_core/output_parsers/json.py +12 -10
- langchain_core/output_parsers/list.py +21 -23
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +16 -21
- langchain_core/output_parsers/pydantic.py +13 -14
- langchain_core/output_parsers/string.py +5 -5
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +35 -34
- 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 +10 -11
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +11 -17
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -56
- langchain_core/prompts/chat.py +275 -325
- 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 +4 -6
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +26 -10
- langchain_core/prompts/structured.py +49 -53
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +61 -198
- langchain_core/runnables/base.py +1476 -1626
- langchain_core/runnables/branch.py +53 -57
- langchain_core/runnables/config.py +72 -89
- langchain_core/runnables/configurable.py +120 -137
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +91 -97
- 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 +75 -80
- langchain_core/runnables/utils.py +60 -67
- langchain_core/stores.py +85 -121
- 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 +284 -229
- langchain_core/tools/convert.py +160 -155
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +32 -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 +49 -53
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +65 -64
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +45 -45
- 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 +2 -2
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -186
- 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 +36 -40
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +9 -9
- langchain_core/utils/mustache.py +8 -10
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +6 -9
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +66 -62
- langchain_core/vectorstores/base.py +182 -216
- langchain_core/vectorstores/in_memory.py +101 -176
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- langchain_core-1.0.3.dist-info/METADATA +69 -0
- langchain_core-1.0.3.dist-info/RECORD +172 -0
- {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.3.dist-info}/WHEEL +1 -1
- langchain_core/memory.py +0 -120
- langchain_core/messages/block_translators/ollama.py +0 -47
- 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-1.0.0a6.dist-info/METADATA +0 -67
- langchain_core-1.0.0a6.dist-info/RECORD +0 -181
- langchain_core-1.0.0a6.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
|
|
@@ -51,22 +48,24 @@ class BasePromptTemplate(
|
|
|
51
48
|
"""A list of the names of the variables whose values are required as inputs to the
|
|
52
49
|
prompt."""
|
|
53
50
|
optional_variables: list[str] = Field(default=[])
|
|
54
|
-
"""
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
"""A list of the names of the variables for placeholder or `MessagePlaceholder` that
|
|
52
|
+
are optional.
|
|
53
|
+
|
|
54
|
+
These variables are auto inferred from the prompt and user need not provide them."""
|
|
57
55
|
input_types: typing.Dict[str, Any] = Field(default_factory=dict, exclude=True) # noqa: UP006
|
|
58
56
|
"""A dictionary of the types of the variables the prompt template expects.
|
|
57
|
+
|
|
59
58
|
If not provided, all variables are assumed to be strings."""
|
|
60
|
-
output_parser:
|
|
59
|
+
output_parser: BaseOutputParser | None = None
|
|
61
60
|
"""How to parse the output of calling an LLM on this formatted prompt."""
|
|
62
61
|
partial_variables: Mapping[str, Any] = Field(default_factory=dict)
|
|
63
62
|
"""A dictionary of the partial variables the prompt template carries.
|
|
64
63
|
|
|
65
64
|
Partial variables populate the template so that you don't need to
|
|
66
65
|
pass them in every time you call the prompt."""
|
|
67
|
-
metadata:
|
|
66
|
+
metadata: typing.Dict[str, Any] | None = None # noqa: UP006
|
|
68
67
|
"""Metadata to be used for tracing."""
|
|
69
|
-
tags:
|
|
68
|
+
tags: list[str] | None = None
|
|
70
69
|
"""Tags to be used for tracing."""
|
|
71
70
|
|
|
72
71
|
@model_validator(mode="after")
|
|
@@ -99,10 +98,10 @@ class BasePromptTemplate(
|
|
|
99
98
|
|
|
100
99
|
@classmethod
|
|
101
100
|
def get_lc_namespace(cls) -> list[str]:
|
|
102
|
-
"""Get the namespace of the
|
|
101
|
+
"""Get the namespace of the LangChain object.
|
|
103
102
|
|
|
104
103
|
Returns:
|
|
105
|
-
|
|
104
|
+
`["langchain", "schema", "prompt_template"]`
|
|
106
105
|
"""
|
|
107
106
|
return ["langchain", "schema", "prompt_template"]
|
|
108
107
|
|
|
@@ -123,19 +122,17 @@ class BasePromptTemplate(
|
|
|
123
122
|
@override
|
|
124
123
|
def OutputType(self) -> Any:
|
|
125
124
|
"""Return the output type of the prompt."""
|
|
126
|
-
return
|
|
125
|
+
return StringPromptValue | ChatPromptValueConcrete
|
|
127
126
|
|
|
128
127
|
@override
|
|
129
|
-
def get_input_schema(
|
|
130
|
-
self, config: Optional[RunnableConfig] = None
|
|
131
|
-
) -> type[BaseModel]:
|
|
128
|
+
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
132
129
|
"""Get the input schema for the prompt.
|
|
133
130
|
|
|
134
131
|
Args:
|
|
135
|
-
config:
|
|
132
|
+
config: configuration for the prompt.
|
|
136
133
|
|
|
137
134
|
Returns:
|
|
138
|
-
|
|
135
|
+
The input schema for the prompt.
|
|
139
136
|
"""
|
|
140
137
|
# This is correct, but pydantic typings/mypy don't think so.
|
|
141
138
|
required_input_variables = {
|
|
@@ -195,7 +192,7 @@ class BasePromptTemplate(
|
|
|
195
192
|
|
|
196
193
|
@override
|
|
197
194
|
def invoke(
|
|
198
|
-
self, input: dict, config:
|
|
195
|
+
self, input: dict, config: RunnableConfig | None = None, **kwargs: Any
|
|
199
196
|
) -> PromptValue:
|
|
200
197
|
"""Invoke the prompt.
|
|
201
198
|
|
|
@@ -204,7 +201,7 @@ class BasePromptTemplate(
|
|
|
204
201
|
config: RunnableConfig, configuration for the prompt.
|
|
205
202
|
|
|
206
203
|
Returns:
|
|
207
|
-
|
|
204
|
+
The output of the prompt.
|
|
208
205
|
"""
|
|
209
206
|
config = ensure_config(config)
|
|
210
207
|
if self.metadata:
|
|
@@ -221,7 +218,7 @@ class BasePromptTemplate(
|
|
|
221
218
|
|
|
222
219
|
@override
|
|
223
220
|
async def ainvoke(
|
|
224
|
-
self, input: dict, config:
|
|
221
|
+
self, input: dict, config: RunnableConfig | None = None, **kwargs: Any
|
|
225
222
|
) -> PromptValue:
|
|
226
223
|
"""Async invoke the prompt.
|
|
227
224
|
|
|
@@ -230,7 +227,7 @@ class BasePromptTemplate(
|
|
|
230
227
|
config: RunnableConfig, configuration for the prompt.
|
|
231
228
|
|
|
232
229
|
Returns:
|
|
233
|
-
|
|
230
|
+
The output of the prompt.
|
|
234
231
|
"""
|
|
235
232
|
config = ensure_config(config)
|
|
236
233
|
if self.metadata:
|
|
@@ -250,31 +247,31 @@ class BasePromptTemplate(
|
|
|
250
247
|
"""Create Prompt Value.
|
|
251
248
|
|
|
252
249
|
Args:
|
|
253
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
250
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
254
251
|
|
|
255
252
|
Returns:
|
|
256
|
-
|
|
253
|
+
The output of the prompt.
|
|
257
254
|
"""
|
|
258
255
|
|
|
259
256
|
async def aformat_prompt(self, **kwargs: Any) -> PromptValue:
|
|
260
257
|
"""Async create Prompt Value.
|
|
261
258
|
|
|
262
259
|
Args:
|
|
263
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
260
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
264
261
|
|
|
265
262
|
Returns:
|
|
266
|
-
|
|
263
|
+
The output of the prompt.
|
|
267
264
|
"""
|
|
268
265
|
return self.format_prompt(**kwargs)
|
|
269
266
|
|
|
270
|
-
def partial(self, **kwargs:
|
|
267
|
+
def partial(self, **kwargs: str | Callable[[], str]) -> BasePromptTemplate:
|
|
271
268
|
"""Return a partial of the prompt template.
|
|
272
269
|
|
|
273
270
|
Args:
|
|
274
|
-
kwargs:
|
|
271
|
+
**kwargs: partial variables to set.
|
|
275
272
|
|
|
276
273
|
Returns:
|
|
277
|
-
|
|
274
|
+
A partial of the prompt template.
|
|
278
275
|
"""
|
|
279
276
|
prompt_dict = self.__dict__.copy()
|
|
280
277
|
prompt_dict["input_variables"] = list(
|
|
@@ -295,34 +292,30 @@ class BasePromptTemplate(
|
|
|
295
292
|
"""Format the prompt with the inputs.
|
|
296
293
|
|
|
297
294
|
Args:
|
|
298
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
295
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
299
296
|
|
|
300
297
|
Returns:
|
|
301
298
|
A formatted string.
|
|
302
299
|
|
|
303
300
|
Example:
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
prompt.format(variable1="foo")
|
|
308
|
-
|
|
301
|
+
```python
|
|
302
|
+
prompt.format(variable1="foo")
|
|
303
|
+
```
|
|
309
304
|
"""
|
|
310
305
|
|
|
311
306
|
async def aformat(self, **kwargs: Any) -> FormatOutputType:
|
|
312
307
|
"""Async format the prompt with the inputs.
|
|
313
308
|
|
|
314
309
|
Args:
|
|
315
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
310
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
316
311
|
|
|
317
312
|
Returns:
|
|
318
313
|
A formatted string.
|
|
319
314
|
|
|
320
315
|
Example:
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
await prompt.aformat(variable1="foo")
|
|
325
|
-
|
|
316
|
+
```python
|
|
317
|
+
await prompt.aformat(variable1="foo")
|
|
318
|
+
```
|
|
326
319
|
"""
|
|
327
320
|
return self.format(**kwargs)
|
|
328
321
|
|
|
@@ -335,17 +328,17 @@ class BasePromptTemplate(
|
|
|
335
328
|
"""Return dictionary representation of prompt.
|
|
336
329
|
|
|
337
330
|
Args:
|
|
338
|
-
kwargs: Any additional arguments to pass to the dictionary.
|
|
331
|
+
**kwargs: Any additional arguments to pass to the dictionary.
|
|
339
332
|
|
|
340
333
|
Returns:
|
|
341
|
-
|
|
334
|
+
Dictionary representation of the prompt.
|
|
342
335
|
"""
|
|
343
336
|
prompt_dict = super().model_dump(**kwargs)
|
|
344
337
|
with contextlib.suppress(NotImplementedError):
|
|
345
338
|
prompt_dict["_type"] = self._prompt_type
|
|
346
339
|
return prompt_dict
|
|
347
340
|
|
|
348
|
-
def save(self, file_path:
|
|
341
|
+
def save(self, file_path: Path | str) -> None:
|
|
349
342
|
"""Save the prompt.
|
|
350
343
|
|
|
351
344
|
Args:
|
|
@@ -357,10 +350,9 @@ class BasePromptTemplate(
|
|
|
357
350
|
NotImplementedError: If the prompt type is not implemented.
|
|
358
351
|
|
|
359
352
|
Example:
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
353
|
+
```python
|
|
354
|
+
prompt.save(file_path="path/prompt.yaml")
|
|
355
|
+
```
|
|
364
356
|
"""
|
|
365
357
|
if self.partial_variables:
|
|
366
358
|
msg = "Cannot save prompt with partial variables."
|
|
@@ -431,16 +423,16 @@ def format_document(doc: Document, prompt: BasePromptTemplate[str]) -> str:
|
|
|
431
423
|
string of the document formatted.
|
|
432
424
|
|
|
433
425
|
Example:
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
from langchain_core.prompts import PromptTemplate
|
|
426
|
+
```python
|
|
427
|
+
from langchain_core.documents import Document
|
|
428
|
+
from langchain_core.prompts import PromptTemplate
|
|
438
429
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
430
|
+
doc = Document(page_content="This is a joke", metadata={"page": "1"})
|
|
431
|
+
prompt = PromptTemplate.from_template("Page {page}: {page_content}")
|
|
432
|
+
format_document(doc, prompt)
|
|
433
|
+
>>> "Page 1: This is a joke"
|
|
443
434
|
|
|
435
|
+
```
|
|
444
436
|
"""
|
|
445
437
|
return prompt.format(**_get_document_info(doc, prompt))
|
|
446
438
|
|