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
|
@@ -3,10 +3,8 @@ from collections.abc import Sequence
|
|
|
3
3
|
from typing import (
|
|
4
4
|
TYPE_CHECKING,
|
|
5
5
|
Literal,
|
|
6
|
-
Optional,
|
|
7
6
|
TypedDict,
|
|
8
7
|
TypeVar,
|
|
9
|
-
Union,
|
|
10
8
|
)
|
|
11
9
|
|
|
12
10
|
if TYPE_CHECKING:
|
|
@@ -17,11 +15,11 @@ from langchain_core.messages.content import (
|
|
|
17
15
|
|
|
18
16
|
|
|
19
17
|
def is_openai_data_block(
|
|
20
|
-
block: dict, filter_:
|
|
18
|
+
block: dict, filter_: Literal["image", "audio", "file"] | None = None
|
|
21
19
|
) -> bool:
|
|
22
20
|
"""Check whether a block contains multimodal data in OpenAI Chat Completions format.
|
|
23
21
|
|
|
24
|
-
Supports both data and ID-style blocks (e.g.
|
|
22
|
+
Supports both data and ID-style blocks (e.g. `'file_data'` and `'file_id'`)
|
|
25
23
|
|
|
26
24
|
If additional keys are present, they are ignored / will not affect outcome as long
|
|
27
25
|
as the required keys are present and valid.
|
|
@@ -32,12 +30,12 @@ def is_openai_data_block(
|
|
|
32
30
|
- "image": Only match image_url blocks
|
|
33
31
|
- "audio": Only match input_audio blocks
|
|
34
32
|
- "file": Only match file blocks
|
|
35
|
-
If None
|
|
33
|
+
If `None`, match any valid OpenAI data block type. Note that this means that
|
|
36
34
|
if the block has a valid OpenAI data type but the filter_ is set to a
|
|
37
35
|
different type, this function will return False.
|
|
38
36
|
|
|
39
37
|
Returns:
|
|
40
|
-
True if the block is a valid OpenAI data block and matches the filter_
|
|
38
|
+
`True` if the block is a valid OpenAI data block and matches the filter_
|
|
41
39
|
(if provided).
|
|
42
40
|
|
|
43
41
|
"""
|
|
@@ -88,24 +86,23 @@ class ParsedDataUri(TypedDict):
|
|
|
88
86
|
mime_type: str
|
|
89
87
|
|
|
90
88
|
|
|
91
|
-
def _parse_data_uri(uri: str) ->
|
|
89
|
+
def _parse_data_uri(uri: str) -> ParsedDataUri | None:
|
|
92
90
|
"""Parse a data URI into its components.
|
|
93
91
|
|
|
94
|
-
If parsing fails, return None
|
|
92
|
+
If parsing fails, return `None`. If either MIME type or data is missing, return
|
|
93
|
+
`None`.
|
|
95
94
|
|
|
96
95
|
Example:
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
96
|
+
```python
|
|
97
|
+
data_uri = "data:image/jpeg;base64,/9j/4AAQSkZJRg..."
|
|
98
|
+
parsed = _parse_data_uri(data_uri)
|
|
99
|
+
|
|
100
|
+
assert parsed == {
|
|
101
|
+
"source_type": "base64",
|
|
102
|
+
"mime_type": "image/jpeg",
|
|
103
|
+
"data": "/9j/4AAQSkZJRg...",
|
|
104
|
+
}
|
|
105
|
+
```
|
|
109
106
|
"""
|
|
110
107
|
regex = r"^data:(?P<mime_type>[^;]+);base64,(?P<data>.+)$"
|
|
111
108
|
match = re.match(regex, uri)
|
|
@@ -135,65 +132,65 @@ def _normalize_messages(
|
|
|
135
132
|
- LangChain v1 standard content blocks
|
|
136
133
|
|
|
137
134
|
This function extends support to:
|
|
138
|
-
- `Audio
|
|
139
|
-
`file
|
|
135
|
+
- `[Audio](https://platform.openai.com/docs/api-reference/chat/create) and
|
|
136
|
+
`[file](https://platform.openai.com/docs/api-reference/files) data in OpenAI
|
|
140
137
|
Chat Completions format
|
|
141
138
|
- Images are technically supported but we expect chat models to handle them
|
|
142
139
|
directly; this may change in the future
|
|
143
140
|
- LangChain v0 standard content blocks for backward compatibility
|
|
144
141
|
|
|
145
|
-
|
|
142
|
+
!!! warning "Behavior changed in 1.0.0"
|
|
146
143
|
In previous versions, this function returned messages in LangChain v0 format.
|
|
147
144
|
Now, it returns messages in LangChain v1 format, which upgraded chat models now
|
|
148
145
|
expect to receive when passing back in message history. For backward
|
|
149
146
|
compatibility, this function will convert v0 message content to v1 format.
|
|
150
147
|
|
|
151
|
-
|
|
148
|
+
??? note "v0 Content Block Schemas"
|
|
152
149
|
|
|
153
|
-
|
|
150
|
+
`URLContentBlock`:
|
|
154
151
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
``Base64ContentBlock``:
|
|
152
|
+
```python
|
|
153
|
+
{
|
|
154
|
+
mime_type: NotRequired[str]
|
|
155
|
+
type: Literal['image', 'audio', 'file'],
|
|
156
|
+
source_type: Literal['url'],
|
|
157
|
+
url: str,
|
|
158
|
+
}
|
|
159
|
+
```
|
|
165
160
|
|
|
166
|
-
|
|
161
|
+
`Base64ContentBlock`:
|
|
167
162
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
163
|
+
```python
|
|
164
|
+
{
|
|
165
|
+
mime_type: NotRequired[str]
|
|
166
|
+
type: Literal['image', 'audio', 'file'],
|
|
167
|
+
source_type: Literal['base64'],
|
|
168
|
+
data: str,
|
|
169
|
+
}
|
|
170
|
+
```
|
|
174
171
|
|
|
175
|
-
|
|
172
|
+
`IDContentBlock`:
|
|
176
173
|
|
|
177
174
|
(In practice, this was never used)
|
|
178
175
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
``PlainTextContentBlock``:
|
|
176
|
+
```python
|
|
177
|
+
{
|
|
178
|
+
type: Literal["image", "audio", "file"],
|
|
179
|
+
source_type: Literal["id"],
|
|
180
|
+
id: str,
|
|
181
|
+
}
|
|
182
|
+
```
|
|
188
183
|
|
|
189
|
-
|
|
184
|
+
`PlainTextContentBlock`:
|
|
190
185
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
186
|
+
```python
|
|
187
|
+
{
|
|
188
|
+
mime_type: NotRequired[str]
|
|
189
|
+
type: Literal['file'],
|
|
190
|
+
source_type: Literal['text'],
|
|
191
|
+
url: str,
|
|
192
|
+
}
|
|
193
|
+
```
|
|
197
194
|
|
|
198
195
|
If a v1 message is passed in, it will be returned as-is, meaning it is safe to
|
|
199
196
|
always pass in v1 messages to this function for assurance.
|
|
@@ -224,7 +221,7 @@ def _normalize_messages(
|
|
|
224
221
|
"type": Literal['file'],
|
|
225
222
|
"file": Union[
|
|
226
223
|
{
|
|
227
|
-
"filename":
|
|
224
|
+
"filename": str | None = "$FILENAME",
|
|
228
225
|
"file_data": str = "$BASE64_ENCODED_FILE",
|
|
229
226
|
},
|
|
230
227
|
{
|
|
@@ -304,7 +301,7 @@ def _ensure_message_copy(message: T, formatted_message: T) -> T:
|
|
|
304
301
|
|
|
305
302
|
|
|
306
303
|
def _update_content_block(
|
|
307
|
-
formatted_message: "BaseMessage", idx: int, new_block:
|
|
304
|
+
formatted_message: "BaseMessage", idx: int, new_block: ContentBlock | dict
|
|
308
305
|
) -> None:
|
|
309
306
|
"""Update a content block at the given index, handling type issues."""
|
|
310
307
|
# Type ignore needed because:
|
|
@@ -4,23 +4,19 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import warnings
|
|
6
6
|
from abc import ABC, abstractmethod
|
|
7
|
-
from collections.abc import Mapping, Sequence
|
|
7
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
8
8
|
from functools import cache
|
|
9
9
|
from typing import (
|
|
10
10
|
TYPE_CHECKING,
|
|
11
11
|
Any,
|
|
12
|
-
Callable,
|
|
13
12
|
Literal,
|
|
14
|
-
Optional,
|
|
15
13
|
TypeAlias,
|
|
16
14
|
TypeVar,
|
|
17
|
-
Union,
|
|
18
15
|
)
|
|
19
16
|
|
|
20
17
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
21
18
|
from typing_extensions import TypedDict, override
|
|
22
19
|
|
|
23
|
-
from langchain_core._api import deprecated
|
|
24
20
|
from langchain_core.caches import BaseCache
|
|
25
21
|
from langchain_core.callbacks import Callbacks
|
|
26
22
|
from langchain_core.globals import get_verbose
|
|
@@ -37,7 +33,6 @@ from langchain_core.prompt_values import (
|
|
|
37
33
|
StringPromptValue,
|
|
38
34
|
)
|
|
39
35
|
from langchain_core.runnables import Runnable, RunnableSerializable
|
|
40
|
-
from langchain_core.utils import get_pydantic_field_names
|
|
41
36
|
|
|
42
37
|
if TYPE_CHECKING:
|
|
43
38
|
from langchain_core.outputs import LLMResult
|
|
@@ -59,11 +54,11 @@ class LangSmithParams(TypedDict, total=False):
|
|
|
59
54
|
"""Name of the model."""
|
|
60
55
|
ls_model_type: Literal["chat", "llm"]
|
|
61
56
|
"""Type of the model. Should be 'chat' or 'llm'."""
|
|
62
|
-
ls_temperature:
|
|
57
|
+
ls_temperature: float | None
|
|
63
58
|
"""Temperature for generation."""
|
|
64
|
-
ls_max_tokens:
|
|
59
|
+
ls_max_tokens: int | None
|
|
65
60
|
"""Max tokens for generation."""
|
|
66
|
-
ls_stop:
|
|
61
|
+
ls_stop: list[str] | None
|
|
67
62
|
"""Stop words for generation."""
|
|
68
63
|
|
|
69
64
|
|
|
@@ -100,10 +95,17 @@ def _get_token_ids_default_method(text: str) -> list[int]:
|
|
|
100
95
|
return tokenizer.encode(text)
|
|
101
96
|
|
|
102
97
|
|
|
103
|
-
LanguageModelInput =
|
|
104
|
-
|
|
98
|
+
LanguageModelInput = PromptValue | str | Sequence[MessageLikeRepresentation]
|
|
99
|
+
"""Input to a language model."""
|
|
100
|
+
|
|
101
|
+
LanguageModelOutput = BaseMessage | str
|
|
102
|
+
"""Output from a language model."""
|
|
103
|
+
|
|
105
104
|
LanguageModelLike = Runnable[LanguageModelInput, LanguageModelOutput]
|
|
105
|
+
"""Input/output interface for a language model."""
|
|
106
|
+
|
|
106
107
|
LanguageModelOutputVar = TypeVar("LanguageModelOutputVar", AIMessage, str)
|
|
108
|
+
"""Type variable for the output of a language model."""
|
|
107
109
|
|
|
108
110
|
|
|
109
111
|
def _get_verbosity() -> bool:
|
|
@@ -115,30 +117,29 @@ class BaseLanguageModel(
|
|
|
115
117
|
):
|
|
116
118
|
"""Abstract base class for interfacing with language models.
|
|
117
119
|
|
|
118
|
-
All language model wrappers inherited from
|
|
120
|
+
All language model wrappers inherited from `BaseLanguageModel`.
|
|
119
121
|
|
|
120
122
|
"""
|
|
121
123
|
|
|
122
|
-
cache:
|
|
124
|
+
cache: BaseCache | bool | None = Field(default=None, exclude=True)
|
|
123
125
|
"""Whether to cache the response.
|
|
124
126
|
|
|
125
|
-
* If
|
|
126
|
-
* If
|
|
127
|
-
* If None
|
|
128
|
-
* If instance of
|
|
127
|
+
* If `True`, will use the global cache.
|
|
128
|
+
* If `False`, will not use a cache
|
|
129
|
+
* If `None`, will use the global cache if it's set, otherwise no cache.
|
|
130
|
+
* If instance of `BaseCache`, will use the provided cache.
|
|
129
131
|
|
|
130
132
|
Caching is not currently supported for streaming methods of models.
|
|
131
|
-
|
|
132
133
|
"""
|
|
133
134
|
verbose: bool = Field(default_factory=_get_verbosity, exclude=True, repr=False)
|
|
134
135
|
"""Whether to print out response text."""
|
|
135
136
|
callbacks: Callbacks = Field(default=None, exclude=True)
|
|
136
137
|
"""Callbacks to add to the run trace."""
|
|
137
|
-
tags:
|
|
138
|
+
tags: list[str] | None = Field(default=None, exclude=True)
|
|
138
139
|
"""Tags to add to the run trace."""
|
|
139
|
-
metadata:
|
|
140
|
+
metadata: dict[str, Any] | None = Field(default=None, exclude=True)
|
|
140
141
|
"""Metadata to add to the run trace."""
|
|
141
|
-
custom_get_token_ids:
|
|
142
|
+
custom_get_token_ids: Callable[[str], list[int]] | None = Field(
|
|
142
143
|
default=None, exclude=True
|
|
143
144
|
)
|
|
144
145
|
"""Optional encoder to use for counting tokens."""
|
|
@@ -148,10 +149,10 @@ class BaseLanguageModel(
|
|
|
148
149
|
)
|
|
149
150
|
|
|
150
151
|
@field_validator("verbose", mode="before")
|
|
151
|
-
def set_verbose(cls, verbose:
|
|
152
|
-
"""If verbose is None
|
|
152
|
+
def set_verbose(cls, verbose: bool | None) -> bool: # noqa: FBT001
|
|
153
|
+
"""If verbose is `None`, set it.
|
|
153
154
|
|
|
154
|
-
This allows users to pass in None as verbose to access the global setting.
|
|
155
|
+
This allows users to pass in `None` as verbose to access the global setting.
|
|
155
156
|
|
|
156
157
|
Args:
|
|
157
158
|
verbose: The verbosity setting to use.
|
|
@@ -167,21 +168,17 @@ class BaseLanguageModel(
|
|
|
167
168
|
@property
|
|
168
169
|
@override
|
|
169
170
|
def InputType(self) -> TypeAlias:
|
|
170
|
-
"""Get the input type for this
|
|
171
|
+
"""Get the input type for this `Runnable`."""
|
|
171
172
|
# This is a version of LanguageModelInput which replaces the abstract
|
|
172
173
|
# base class BaseMessage with a union of its subclasses, which makes
|
|
173
174
|
# for a much better schema.
|
|
174
|
-
return
|
|
175
|
-
str,
|
|
176
|
-
Union[StringPromptValue, ChatPromptValueConcrete],
|
|
177
|
-
list[AnyMessage],
|
|
178
|
-
]
|
|
175
|
+
return str | StringPromptValue | ChatPromptValueConcrete | list[AnyMessage]
|
|
179
176
|
|
|
180
177
|
@abstractmethod
|
|
181
178
|
def generate_prompt(
|
|
182
179
|
self,
|
|
183
180
|
prompts: list[PromptValue],
|
|
184
|
-
stop:
|
|
181
|
+
stop: list[str] | None = None,
|
|
185
182
|
callbacks: Callbacks = None,
|
|
186
183
|
**kwargs: Any,
|
|
187
184
|
) -> LLMResult:
|
|
@@ -195,22 +192,22 @@ class BaseLanguageModel(
|
|
|
195
192
|
1. Take advantage of batched calls,
|
|
196
193
|
2. Need more output from the model than just the top generated value,
|
|
197
194
|
3. Are building chains that are agnostic to the underlying language model
|
|
198
|
-
|
|
195
|
+
type (e.g., pure text completion models vs chat models).
|
|
199
196
|
|
|
200
197
|
Args:
|
|
201
|
-
prompts: List of
|
|
202
|
-
converted to match the format of any language model (string for
|
|
203
|
-
text generation models and
|
|
198
|
+
prompts: List of `PromptValue` objects. A `PromptValue` is an object that
|
|
199
|
+
can be converted to match the format of any language model (string for
|
|
200
|
+
pure text generation models and `BaseMessage` objects for chat models).
|
|
204
201
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
205
202
|
first occurrence of any of these substrings.
|
|
206
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
203
|
+
callbacks: `Callbacks` to pass through. Used for executing additional
|
|
207
204
|
functionality, such as logging or streaming, throughout generation.
|
|
208
205
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
209
206
|
to the model provider API call.
|
|
210
207
|
|
|
211
208
|
Returns:
|
|
212
|
-
An LLMResult
|
|
213
|
-
|
|
209
|
+
An `LLMResult`, which contains a list of candidate `Generation` objects for
|
|
210
|
+
each input prompt and additional model provider-specific output.
|
|
214
211
|
|
|
215
212
|
"""
|
|
216
213
|
|
|
@@ -218,7 +215,7 @@ class BaseLanguageModel(
|
|
|
218
215
|
async def agenerate_prompt(
|
|
219
216
|
self,
|
|
220
217
|
prompts: list[PromptValue],
|
|
221
|
-
stop:
|
|
218
|
+
stop: list[str] | None = None,
|
|
222
219
|
callbacks: Callbacks = None,
|
|
223
220
|
**kwargs: Any,
|
|
224
221
|
) -> LLMResult:
|
|
@@ -232,144 +229,47 @@ class BaseLanguageModel(
|
|
|
232
229
|
1. Take advantage of batched calls,
|
|
233
230
|
2. Need more output from the model than just the top generated value,
|
|
234
231
|
3. Are building chains that are agnostic to the underlying language model
|
|
235
|
-
|
|
232
|
+
type (e.g., pure text completion models vs chat models).
|
|
236
233
|
|
|
237
234
|
Args:
|
|
238
|
-
prompts: List of
|
|
239
|
-
converted to match the format of any language model (string for
|
|
240
|
-
text generation models and
|
|
235
|
+
prompts: List of `PromptValue` objects. A `PromptValue` is an object that
|
|
236
|
+
can be converted to match the format of any language model (string for
|
|
237
|
+
pure text generation models and `BaseMessage` objects for chat models).
|
|
241
238
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
242
239
|
first occurrence of any of these substrings.
|
|
243
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
240
|
+
callbacks: `Callbacks` to pass through. Used for executing additional
|
|
244
241
|
functionality, such as logging or streaming, throughout generation.
|
|
245
242
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
246
243
|
to the model provider API call.
|
|
247
244
|
|
|
248
245
|
Returns:
|
|
249
|
-
An
|
|
250
|
-
|
|
246
|
+
An `LLMResult`, which contains a list of candidate `Generation` objects for
|
|
247
|
+
each input prompt and additional model provider-specific output.
|
|
251
248
|
|
|
252
249
|
"""
|
|
253
250
|
|
|
254
251
|
def with_structured_output(
|
|
255
|
-
self, schema:
|
|
256
|
-
) -> Runnable[LanguageModelInput,
|
|
252
|
+
self, schema: dict | type, **kwargs: Any
|
|
253
|
+
) -> Runnable[LanguageModelInput, dict | BaseModel]:
|
|
257
254
|
"""Not implemented on this class."""
|
|
258
255
|
# Implement this on child class if there is a way of steering the model to
|
|
259
256
|
# generate responses that match a given schema.
|
|
260
257
|
raise NotImplementedError
|
|
261
258
|
|
|
262
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
263
|
-
@abstractmethod
|
|
264
|
-
def predict(
|
|
265
|
-
self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
|
|
266
|
-
) -> str:
|
|
267
|
-
"""Pass a single string input to the model and return a string.
|
|
268
|
-
|
|
269
|
-
Use this method when passing in raw text. If you want to pass in specific types
|
|
270
|
-
of chat messages, use predict_messages.
|
|
271
|
-
|
|
272
|
-
Args:
|
|
273
|
-
text: String input to pass to the model.
|
|
274
|
-
stop: Stop words to use when generating. Model output is cut off at the
|
|
275
|
-
first occurrence of any of these substrings.
|
|
276
|
-
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
277
|
-
to the model provider API call.
|
|
278
|
-
|
|
279
|
-
Returns:
|
|
280
|
-
Top model prediction as a string.
|
|
281
|
-
|
|
282
|
-
"""
|
|
283
|
-
|
|
284
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
285
|
-
@abstractmethod
|
|
286
|
-
def predict_messages(
|
|
287
|
-
self,
|
|
288
|
-
messages: list[BaseMessage],
|
|
289
|
-
*,
|
|
290
|
-
stop: Optional[Sequence[str]] = None,
|
|
291
|
-
**kwargs: Any,
|
|
292
|
-
) -> BaseMessage:
|
|
293
|
-
"""Pass a message sequence to the model and return a message.
|
|
294
|
-
|
|
295
|
-
Use this method when passing in chat messages. If you want to pass in raw text,
|
|
296
|
-
use predict.
|
|
297
|
-
|
|
298
|
-
Args:
|
|
299
|
-
messages: A sequence of chat messages corresponding to a single model input.
|
|
300
|
-
stop: Stop words to use when generating. Model output is cut off at the
|
|
301
|
-
first occurrence of any of these substrings.
|
|
302
|
-
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
303
|
-
to the model provider API call.
|
|
304
|
-
|
|
305
|
-
Returns:
|
|
306
|
-
Top model prediction as a message.
|
|
307
|
-
|
|
308
|
-
"""
|
|
309
|
-
|
|
310
|
-
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
311
|
-
@abstractmethod
|
|
312
|
-
async def apredict(
|
|
313
|
-
self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
|
|
314
|
-
) -> str:
|
|
315
|
-
"""Asynchronously pass a string to the model and return a string.
|
|
316
|
-
|
|
317
|
-
Use this method when calling pure text generation models and only the top
|
|
318
|
-
candidate generation is needed.
|
|
319
|
-
|
|
320
|
-
Args:
|
|
321
|
-
text: String input to pass to the model.
|
|
322
|
-
stop: Stop words to use when generating. Model output is cut off at the
|
|
323
|
-
first occurrence of any of these substrings.
|
|
324
|
-
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
325
|
-
to the model provider API call.
|
|
326
|
-
|
|
327
|
-
Returns:
|
|
328
|
-
Top model prediction as a string.
|
|
329
|
-
|
|
330
|
-
"""
|
|
331
|
-
|
|
332
|
-
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
333
|
-
@abstractmethod
|
|
334
|
-
async def apredict_messages(
|
|
335
|
-
self,
|
|
336
|
-
messages: list[BaseMessage],
|
|
337
|
-
*,
|
|
338
|
-
stop: Optional[Sequence[str]] = None,
|
|
339
|
-
**kwargs: Any,
|
|
340
|
-
) -> BaseMessage:
|
|
341
|
-
"""Asynchronously pass messages to the model and return a message.
|
|
342
|
-
|
|
343
|
-
Use this method when calling chat models and only the top candidate generation
|
|
344
|
-
is needed.
|
|
345
|
-
|
|
346
|
-
Args:
|
|
347
|
-
messages: A sequence of chat messages corresponding to a single model input.
|
|
348
|
-
stop: Stop words to use when generating. Model output is cut off at the
|
|
349
|
-
first occurrence of any of these substrings.
|
|
350
|
-
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
351
|
-
to the model provider API call.
|
|
352
|
-
|
|
353
|
-
Returns:
|
|
354
|
-
Top model prediction as a message.
|
|
355
|
-
|
|
356
|
-
"""
|
|
357
|
-
|
|
358
259
|
@property
|
|
359
260
|
def _identifying_params(self) -> Mapping[str, Any]:
|
|
360
261
|
"""Get the identifying parameters."""
|
|
361
262
|
return self.lc_attributes
|
|
362
263
|
|
|
363
264
|
def get_token_ids(self, text: str) -> list[int]:
|
|
364
|
-
"""Return the ordered
|
|
265
|
+
"""Return the ordered IDs of the tokens in a text.
|
|
365
266
|
|
|
366
267
|
Args:
|
|
367
268
|
text: The string input to tokenize.
|
|
368
269
|
|
|
369
270
|
Returns:
|
|
370
|
-
A list of
|
|
371
|
-
|
|
372
|
-
|
|
271
|
+
A list of IDs corresponding to the tokens in the text, in order they occur
|
|
272
|
+
in the text.
|
|
373
273
|
"""
|
|
374
274
|
if self.custom_get_token_ids is not None:
|
|
375
275
|
return self.custom_get_token_ids(text)
|
|
@@ -392,20 +292,20 @@ class BaseLanguageModel(
|
|
|
392
292
|
def get_num_tokens_from_messages(
|
|
393
293
|
self,
|
|
394
294
|
messages: list[BaseMessage],
|
|
395
|
-
tools:
|
|
295
|
+
tools: Sequence | None = None,
|
|
396
296
|
) -> int:
|
|
397
297
|
"""Get the number of tokens in the messages.
|
|
398
298
|
|
|
399
299
|
Useful for checking if an input fits in a model's context window.
|
|
400
300
|
|
|
401
|
-
|
|
402
|
-
The base implementation of
|
|
301
|
+
!!! note
|
|
302
|
+
The base implementation of `get_num_tokens_from_messages` ignores tool
|
|
403
303
|
schemas.
|
|
404
304
|
|
|
405
305
|
Args:
|
|
406
306
|
messages: The message inputs to tokenize.
|
|
407
|
-
tools: If provided, sequence of dict,
|
|
408
|
-
|
|
307
|
+
tools: If provided, sequence of dict, `BaseModel`, function, or
|
|
308
|
+
`BaseTool` objects to be converted to tool schemas.
|
|
409
309
|
|
|
410
310
|
Returns:
|
|
411
311
|
The sum of the number of tokens across the messages.
|
|
@@ -417,12 +317,3 @@ class BaseLanguageModel(
|
|
|
417
317
|
stacklevel=2,
|
|
418
318
|
)
|
|
419
319
|
return sum(self.get_num_tokens(get_buffer_string([m])) for m in messages)
|
|
420
|
-
|
|
421
|
-
@classmethod
|
|
422
|
-
def _all_required_field_names(cls) -> set:
|
|
423
|
-
"""DEPRECATED: Kept for backwards compatibility.
|
|
424
|
-
|
|
425
|
-
Use ``get_pydantic_field_names``.
|
|
426
|
-
|
|
427
|
-
"""
|
|
428
|
-
return get_pydantic_field_names(cls)
|