langchain-core 1.0.0a8__py3-none-any.whl → 1.0.0rc2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +0 -1
- langchain_core/_api/beta_decorator.py +17 -20
- langchain_core/_api/deprecation.py +30 -35
- langchain_core/_import_utils.py +1 -1
- langchain_core/agents.py +10 -9
- langchain_core/caches.py +46 -56
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +232 -243
- langchain_core/callbacks/file.py +33 -33
- langchain_core/callbacks/manager.py +353 -416
- langchain_core/callbacks/stdout.py +21 -22
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +54 -51
- langchain_core/chat_history.py +43 -58
- langchain_core/document_loaders/base.py +21 -21
- langchain_core/document_loaders/langsmith.py +22 -22
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +46 -49
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/semantic_similarity.py +4 -6
- langchain_core/exceptions.py +7 -8
- langchain_core/indexing/api.py +19 -25
- langchain_core/indexing/base.py +24 -24
- langchain_core/language_models/__init__.py +11 -27
- langchain_core/language_models/_utils.py +53 -54
- langchain_core/language_models/base.py +30 -24
- langchain_core/language_models/chat_models.py +123 -148
- langchain_core/language_models/fake_chat_models.py +7 -7
- langchain_core/language_models/llms.py +14 -16
- langchain_core/load/dump.py +3 -4
- langchain_core/load/load.py +7 -16
- langchain_core/load/serializable.py +37 -36
- langchain_core/messages/__init__.py +1 -16
- langchain_core/messages/ai.py +122 -123
- langchain_core/messages/base.py +31 -31
- langchain_core/messages/block_translators/__init__.py +17 -17
- langchain_core/messages/block_translators/anthropic.py +3 -3
- langchain_core/messages/block_translators/bedrock_converse.py +3 -3
- langchain_core/messages/block_translators/google_genai.py +5 -4
- 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 +3 -3
- langchain_core/messages/block_translators/openai.py +5 -5
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +222 -209
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +17 -24
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +12 -22
- langchain_core/messages/tool.py +53 -69
- langchain_core/messages/utils.py +399 -417
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +46 -47
- langchain_core/output_parsers/json.py +3 -4
- langchain_core/output_parsers/list.py +2 -2
- langchain_core/output_parsers/openai_functions.py +46 -44
- langchain_core/output_parsers/openai_tools.py +11 -16
- langchain_core/output_parsers/pydantic.py +10 -11
- langchain_core/output_parsers/string.py +2 -2
- langchain_core/output_parsers/transform.py +2 -2
- langchain_core/output_parsers/xml.py +1 -1
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +14 -14
- langchain_core/outputs/generation.py +6 -6
- langchain_core/outputs/llm_result.py +5 -5
- langchain_core/prompt_values.py +11 -11
- langchain_core/prompts/__init__.py +3 -23
- langchain_core/prompts/base.py +33 -38
- langchain_core/prompts/chat.py +222 -229
- langchain_core/prompts/dict.py +3 -3
- langchain_core/prompts/few_shot.py +76 -83
- langchain_core/prompts/few_shot_with_templates.py +7 -9
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +1 -1
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +20 -23
- langchain_core/prompts/string.py +20 -8
- langchain_core/prompts/structured.py +26 -27
- langchain_core/rate_limiters.py +50 -58
- langchain_core/retrievers.py +41 -182
- langchain_core/runnables/base.py +565 -597
- langchain_core/runnables/branch.py +8 -8
- langchain_core/runnables/config.py +37 -44
- langchain_core/runnables/configurable.py +9 -10
- langchain_core/runnables/fallbacks.py +9 -9
- langchain_core/runnables/graph.py +46 -50
- langchain_core/runnables/graph_ascii.py +19 -18
- langchain_core/runnables/graph_mermaid.py +20 -31
- langchain_core/runnables/graph_png.py +7 -7
- langchain_core/runnables/history.py +22 -22
- langchain_core/runnables/passthrough.py +11 -11
- langchain_core/runnables/retry.py +3 -3
- langchain_core/runnables/router.py +2 -2
- langchain_core/runnables/schema.py +33 -33
- langchain_core/runnables/utils.py +30 -34
- langchain_core/stores.py +72 -102
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +70 -71
- langchain_core/tools/convert.py +100 -104
- langchain_core/tools/render.py +9 -9
- langchain_core/tools/retriever.py +7 -7
- langchain_core/tools/simple.py +6 -7
- langchain_core/tools/structured.py +18 -24
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +35 -35
- langchain_core/tracers/context.py +12 -17
- langchain_core/tracers/event_stream.py +3 -3
- langchain_core/tracers/langchain.py +8 -8
- langchain_core/tracers/log_stream.py +17 -18
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +2 -2
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +1 -2
- langchain_core/utils/__init__.py +1 -1
- langchain_core/utils/aiter.py +32 -32
- langchain_core/utils/env.py +5 -5
- langchain_core/utils/function_calling.py +59 -154
- langchain_core/utils/html.py +4 -4
- langchain_core/utils/input.py +3 -3
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +20 -20
- langchain_core/utils/json.py +1 -1
- langchain_core/utils/json_schema.py +2 -2
- langchain_core/utils/mustache.py +5 -5
- langchain_core/utils/pydantic.py +17 -17
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/utils.py +25 -28
- langchain_core/vectorstores/base.py +55 -87
- langchain_core/vectorstores/in_memory.py +83 -85
- langchain_core/vectorstores/utils.py +2 -2
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc2.dist-info}/METADATA +23 -11
- langchain_core-1.0.0rc2.dist-info/RECORD +172 -0
- langchain_core/memory.py +0 -120
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core-1.0.0a8.dist-info/RECORD +0 -176
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc2.dist-info}/WHEEL +0 -0
langchain_core/indexing/base.py
CHANGED
|
@@ -61,7 +61,7 @@ class RecordManager(ABC):
|
|
|
61
61
|
"""Initialize the record manager.
|
|
62
62
|
|
|
63
63
|
Args:
|
|
64
|
-
namespace
|
|
64
|
+
namespace: The namespace for the record manager.
|
|
65
65
|
"""
|
|
66
66
|
self.namespace = namespace
|
|
67
67
|
|
|
@@ -244,7 +244,7 @@ class InMemoryRecordManager(RecordManager):
|
|
|
244
244
|
"""Initialize the in-memory record manager.
|
|
245
245
|
|
|
246
246
|
Args:
|
|
247
|
-
namespace
|
|
247
|
+
namespace: The namespace for the record manager.
|
|
248
248
|
"""
|
|
249
249
|
super().__init__(namespace)
|
|
250
250
|
# Each key points to a dictionary
|
|
@@ -278,10 +278,10 @@ class InMemoryRecordManager(RecordManager):
|
|
|
278
278
|
Args:
|
|
279
279
|
keys: A list of record keys to upsert.
|
|
280
280
|
group_ids: A list of group IDs corresponding to the keys.
|
|
281
|
-
|
|
281
|
+
|
|
282
282
|
time_at_least: Optional timestamp. Implementation can use this
|
|
283
283
|
to optionally verify that the timestamp IS at least this time
|
|
284
|
-
in the system that stores.
|
|
284
|
+
in the system that stores.
|
|
285
285
|
E.g., use to validate that the time in the postgres database
|
|
286
286
|
is equal to or larger than the given timestamp, if not
|
|
287
287
|
raise an error.
|
|
@@ -315,10 +315,10 @@ class InMemoryRecordManager(RecordManager):
|
|
|
315
315
|
Args:
|
|
316
316
|
keys: A list of record keys to upsert.
|
|
317
317
|
group_ids: A list of group IDs corresponding to the keys.
|
|
318
|
-
|
|
318
|
+
|
|
319
319
|
time_at_least: Optional timestamp. Implementation can use this
|
|
320
320
|
to optionally verify that the timestamp IS at least this time
|
|
321
|
-
in the system that stores.
|
|
321
|
+
in the system that stores.
|
|
322
322
|
E.g., use to validate that the time in the postgres database
|
|
323
323
|
is equal to or larger than the given timestamp, if not
|
|
324
324
|
raise an error.
|
|
@@ -361,13 +361,13 @@ class InMemoryRecordManager(RecordManager):
|
|
|
361
361
|
|
|
362
362
|
Args:
|
|
363
363
|
before: Filter to list records updated before this time.
|
|
364
|
-
|
|
364
|
+
|
|
365
365
|
after: Filter to list records updated after this time.
|
|
366
|
-
|
|
366
|
+
|
|
367
367
|
group_ids: Filter to list records with specific group IDs.
|
|
368
|
-
|
|
368
|
+
|
|
369
369
|
limit: optional limit on the number of records to return.
|
|
370
|
-
|
|
370
|
+
|
|
371
371
|
|
|
372
372
|
Returns:
|
|
373
373
|
A list of keys for the matching records.
|
|
@@ -397,13 +397,13 @@ class InMemoryRecordManager(RecordManager):
|
|
|
397
397
|
|
|
398
398
|
Args:
|
|
399
399
|
before: Filter to list records updated before this time.
|
|
400
|
-
|
|
400
|
+
|
|
401
401
|
after: Filter to list records updated after this time.
|
|
402
|
-
|
|
402
|
+
|
|
403
403
|
group_ids: Filter to list records with specific group IDs.
|
|
404
|
-
|
|
404
|
+
|
|
405
405
|
limit: optional limit on the number of records to return.
|
|
406
|
-
|
|
406
|
+
|
|
407
407
|
|
|
408
408
|
Returns:
|
|
409
409
|
A list of keys for the matching records.
|
|
@@ -529,7 +529,7 @@ class DocumentIndex(BaseRetriever):
|
|
|
529
529
|
**kwargs: Additional keyword arguments.
|
|
530
530
|
|
|
531
531
|
Returns:
|
|
532
|
-
|
|
532
|
+
A response object that contains the list of IDs that were
|
|
533
533
|
successfully added or updated in the vectorstore and the list of IDs that
|
|
534
534
|
failed to be added or updated.
|
|
535
535
|
"""
|
|
@@ -552,7 +552,7 @@ class DocumentIndex(BaseRetriever):
|
|
|
552
552
|
**kwargs: Additional keyword arguments.
|
|
553
553
|
|
|
554
554
|
Returns:
|
|
555
|
-
|
|
555
|
+
A response object that contains the list of IDs that were
|
|
556
556
|
successfully added or updated in the vectorstore and the list of IDs that
|
|
557
557
|
failed to be added or updated.
|
|
558
558
|
"""
|
|
@@ -571,12 +571,12 @@ class DocumentIndex(BaseRetriever):
|
|
|
571
571
|
|
|
572
572
|
Args:
|
|
573
573
|
ids: List of ids to delete.
|
|
574
|
-
kwargs: Additional keyword arguments. This is up to the implementation.
|
|
574
|
+
**kwargs: Additional keyword arguments. This is up to the implementation.
|
|
575
575
|
For example, can include an option to delete the entire index,
|
|
576
576
|
or else issue a non-blocking delete etc.
|
|
577
577
|
|
|
578
578
|
Returns:
|
|
579
|
-
|
|
579
|
+
A response object that contains the list of IDs that were
|
|
580
580
|
successfully deleted and the list of IDs that failed to be deleted.
|
|
581
581
|
"""
|
|
582
582
|
|
|
@@ -589,11 +589,11 @@ class DocumentIndex(BaseRetriever):
|
|
|
589
589
|
|
|
590
590
|
Args:
|
|
591
591
|
ids: List of ids to delete.
|
|
592
|
-
kwargs: Additional keyword arguments. This is up to the implementation.
|
|
592
|
+
**kwargs: Additional keyword arguments. This is up to the implementation.
|
|
593
593
|
For example, can include an option to delete the entire index.
|
|
594
594
|
|
|
595
595
|
Returns:
|
|
596
|
-
|
|
596
|
+
A response object that contains the list of IDs that were
|
|
597
597
|
successfully deleted and the list of IDs that failed to be deleted.
|
|
598
598
|
"""
|
|
599
599
|
return await run_in_executor(
|
|
@@ -624,10 +624,10 @@ class DocumentIndex(BaseRetriever):
|
|
|
624
624
|
|
|
625
625
|
Args:
|
|
626
626
|
ids: List of IDs to get.
|
|
627
|
-
kwargs: Additional keyword arguments. These are up to the implementation.
|
|
627
|
+
**kwargs: Additional keyword arguments. These are up to the implementation.
|
|
628
628
|
|
|
629
629
|
Returns:
|
|
630
|
-
|
|
630
|
+
List of documents that were found.
|
|
631
631
|
"""
|
|
632
632
|
|
|
633
633
|
async def aget(
|
|
@@ -650,10 +650,10 @@ class DocumentIndex(BaseRetriever):
|
|
|
650
650
|
|
|
651
651
|
Args:
|
|
652
652
|
ids: List of IDs to get.
|
|
653
|
-
kwargs: Additional keyword arguments. These are up to the implementation.
|
|
653
|
+
**kwargs: Additional keyword arguments. These are up to the implementation.
|
|
654
654
|
|
|
655
655
|
Returns:
|
|
656
|
-
|
|
656
|
+
List of documents that were found.
|
|
657
657
|
"""
|
|
658
658
|
return await run_in_executor(
|
|
659
659
|
None,
|
|
@@ -1,45 +1,29 @@
|
|
|
1
1
|
"""Language models.
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
LangChain has two main classes to work with language models: chat models and
|
|
4
|
+
"old-fashioned" LLMs.
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
and "old-fashioned" **LLMs**.
|
|
8
|
-
|
|
9
|
-
**Chat Models**
|
|
6
|
+
**Chat models**
|
|
10
7
|
|
|
11
8
|
Language models that use a sequence of messages as inputs and return chat messages
|
|
12
|
-
as outputs (as opposed to using plain text).
|
|
13
|
-
older models are generally LLMs, see below). Chat models support the assignment of
|
|
9
|
+
as outputs (as opposed to using plain text). Chat models support the assignment of
|
|
14
10
|
distinct roles to conversation messages, helping to distinguish messages from the AI,
|
|
15
11
|
users, and instructions such as system messages.
|
|
16
12
|
|
|
17
13
|
The key abstraction for chat models is `BaseChatModel`. Implementations
|
|
18
|
-
should inherit from this class.
|
|
19
|
-
information on how to implement a custom chat model.
|
|
20
|
-
|
|
21
|
-
To implement a custom Chat Model, inherit from `BaseChatModel`. See
|
|
22
|
-
the following guide for more information on how to implement a custom Chat Model:
|
|
14
|
+
should inherit from this class.
|
|
23
15
|
|
|
24
|
-
https://
|
|
16
|
+
See existing [chat model integrations](https://docs.langchain.com/oss/python/integrations/chat).
|
|
25
17
|
|
|
26
18
|
**LLMs**
|
|
27
19
|
|
|
28
20
|
Language models that takes a string as input and returns a string.
|
|
29
|
-
These are traditionally older models (newer models generally are
|
|
30
|
-
see below).
|
|
31
|
-
|
|
32
|
-
Although the underlying models are string in, string out, the LangChain wrappers
|
|
33
|
-
also allow these models to take messages as input. This gives them the same interface
|
|
34
|
-
as Chat Models. When messages are passed in as input, they will be formatted into a
|
|
35
|
-
string under the hood before being passed to the underlying model.
|
|
36
|
-
|
|
37
|
-
To implement a custom LLM, inherit from `BaseLLM` or `LLM`.
|
|
38
|
-
Please see the following guide for more information on how to implement a custom LLM:
|
|
39
|
-
|
|
40
|
-
https://python.langchain.com/docs/how_to/custom_llm/
|
|
41
|
-
|
|
21
|
+
These are traditionally older models (newer models generally are chat models).
|
|
42
22
|
|
|
23
|
+
Although the underlying models are string in, string out, the LangChain wrappers also
|
|
24
|
+
allow these models to take messages as input. This gives them the same interface as
|
|
25
|
+
chat models. When messages are passed in as input, they will be formatted into a string
|
|
26
|
+
under the hood before being passed to the underlying model.
|
|
43
27
|
"""
|
|
44
28
|
|
|
45
29
|
from typing import TYPE_CHECKING
|
|
@@ -19,7 +19,7 @@ def is_openai_data_block(
|
|
|
19
19
|
) -> bool:
|
|
20
20
|
"""Check whether a block contains multimodal data in OpenAI Chat Completions format.
|
|
21
21
|
|
|
22
|
-
Supports both data and ID-style blocks (e.g.
|
|
22
|
+
Supports both data and ID-style blocks (e.g. `'file_data'` and `'file_id'`)
|
|
23
23
|
|
|
24
24
|
If additional keys are present, they are ignored / will not affect outcome as long
|
|
25
25
|
as the required keys are present and valid.
|
|
@@ -30,12 +30,12 @@ def is_openai_data_block(
|
|
|
30
30
|
- "image": Only match image_url blocks
|
|
31
31
|
- "audio": Only match input_audio blocks
|
|
32
32
|
- "file": Only match file blocks
|
|
33
|
-
If None
|
|
33
|
+
If `None`, match any valid OpenAI data block type. Note that this means that
|
|
34
34
|
if the block has a valid OpenAI data type but the filter_ is set to a
|
|
35
35
|
different type, this function will return False.
|
|
36
36
|
|
|
37
37
|
Returns:
|
|
38
|
-
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_
|
|
39
39
|
(if provided).
|
|
40
40
|
|
|
41
41
|
"""
|
|
@@ -89,21 +89,20 @@ class ParsedDataUri(TypedDict):
|
|
|
89
89
|
def _parse_data_uri(uri: str) -> ParsedDataUri | None:
|
|
90
90
|
"""Parse a data URI into its components.
|
|
91
91
|
|
|
92
|
-
If parsing fails, return None
|
|
92
|
+
If parsing fails, return `None`. If either MIME type or data is missing, return
|
|
93
|
+
`None`.
|
|
93
94
|
|
|
94
95
|
Example:
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
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
|
+
```
|
|
107
106
|
"""
|
|
108
107
|
regex = r"^data:(?P<mime_type>[^;]+);base64,(?P<data>.+)$"
|
|
109
108
|
match = re.match(regex, uri)
|
|
@@ -133,8 +132,8 @@ def _normalize_messages(
|
|
|
133
132
|
- LangChain v1 standard content blocks
|
|
134
133
|
|
|
135
134
|
This function extends support to:
|
|
136
|
-
- `Audio
|
|
137
|
-
`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
|
|
138
137
|
Chat Completions format
|
|
139
138
|
- Images are technically supported but we expect chat models to handle them
|
|
140
139
|
directly; this may change in the future
|
|
@@ -148,50 +147,50 @@ def _normalize_messages(
|
|
|
148
147
|
|
|
149
148
|
??? note "v0 Content Block Schemas"
|
|
150
149
|
|
|
151
|
-
|
|
150
|
+
`URLContentBlock`:
|
|
152
151
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
``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
|
+
```
|
|
163
160
|
|
|
164
|
-
|
|
161
|
+
`Base64ContentBlock`:
|
|
165
162
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
163
|
+
```python
|
|
164
|
+
{
|
|
165
|
+
mime_type: NotRequired[str]
|
|
166
|
+
type: Literal['image', 'audio', 'file'],
|
|
167
|
+
source_type: Literal['base64'],
|
|
168
|
+
data: str,
|
|
169
|
+
}
|
|
170
|
+
```
|
|
172
171
|
|
|
173
|
-
|
|
172
|
+
`IDContentBlock`:
|
|
174
173
|
|
|
175
174
|
(In practice, this was never used)
|
|
176
175
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
``PlainTextContentBlock``:
|
|
176
|
+
```python
|
|
177
|
+
{
|
|
178
|
+
type: Literal["image", "audio", "file"],
|
|
179
|
+
source_type: Literal["id"],
|
|
180
|
+
id: str,
|
|
181
|
+
}
|
|
182
|
+
```
|
|
186
183
|
|
|
187
|
-
|
|
184
|
+
`PlainTextContentBlock`:
|
|
188
185
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
186
|
+
```python
|
|
187
|
+
{
|
|
188
|
+
mime_type: NotRequired[str]
|
|
189
|
+
type: Literal['file'],
|
|
190
|
+
source_type: Literal['text'],
|
|
191
|
+
url: str,
|
|
192
|
+
}
|
|
193
|
+
```
|
|
195
194
|
|
|
196
195
|
If a v1 message is passed in, it will be returned as-is, meaning it is safe to
|
|
197
196
|
always pass in v1 messages to this function for assurance.
|
|
@@ -222,7 +221,7 @@ def _normalize_messages(
|
|
|
222
221
|
"type": Literal['file'],
|
|
223
222
|
"file": Union[
|
|
224
223
|
{
|
|
225
|
-
"filename":
|
|
224
|
+
"filename": str | None = "$FILENAME",
|
|
226
225
|
"file_data": str = "$BASE64_ENCODED_FILE",
|
|
227
226
|
},
|
|
228
227
|
{
|
|
@@ -96,9 +96,16 @@ def _get_token_ids_default_method(text: str) -> list[int]:
|
|
|
96
96
|
|
|
97
97
|
|
|
98
98
|
LanguageModelInput = PromptValue | str | Sequence[MessageLikeRepresentation]
|
|
99
|
+
"""Input to a language model."""
|
|
100
|
+
|
|
99
101
|
LanguageModelOutput = BaseMessage | str
|
|
102
|
+
"""Output from a language model."""
|
|
103
|
+
|
|
100
104
|
LanguageModelLike = Runnable[LanguageModelInput, LanguageModelOutput]
|
|
105
|
+
"""Input/output interface for a language model."""
|
|
106
|
+
|
|
101
107
|
LanguageModelOutputVar = TypeVar("LanguageModelOutputVar", AIMessage, str)
|
|
108
|
+
"""Type variable for the output of a language model."""
|
|
102
109
|
|
|
103
110
|
|
|
104
111
|
def _get_verbosity() -> bool:
|
|
@@ -110,20 +117,19 @@ class BaseLanguageModel(
|
|
|
110
117
|
):
|
|
111
118
|
"""Abstract base class for interfacing with language models.
|
|
112
119
|
|
|
113
|
-
All language model wrappers inherited from
|
|
120
|
+
All language model wrappers inherited from `BaseLanguageModel`.
|
|
114
121
|
|
|
115
122
|
"""
|
|
116
123
|
|
|
117
124
|
cache: BaseCache | bool | None = Field(default=None, exclude=True)
|
|
118
125
|
"""Whether to cache the response.
|
|
119
126
|
|
|
120
|
-
* If
|
|
121
|
-
* If
|
|
122
|
-
* If None
|
|
123
|
-
* 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.
|
|
124
131
|
|
|
125
132
|
Caching is not currently supported for streaming methods of models.
|
|
126
|
-
|
|
127
133
|
"""
|
|
128
134
|
verbose: bool = Field(default_factory=_get_verbosity, exclude=True, repr=False)
|
|
129
135
|
"""Whether to print out response text."""
|
|
@@ -144,9 +150,9 @@ class BaseLanguageModel(
|
|
|
144
150
|
|
|
145
151
|
@field_validator("verbose", mode="before")
|
|
146
152
|
def set_verbose(cls, verbose: bool | None) -> bool: # noqa: FBT001
|
|
147
|
-
"""If verbose is None
|
|
153
|
+
"""If verbose is `None`, set it.
|
|
148
154
|
|
|
149
|
-
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.
|
|
150
156
|
|
|
151
157
|
Args:
|
|
152
158
|
verbose: The verbosity setting to use.
|
|
@@ -162,7 +168,7 @@ class BaseLanguageModel(
|
|
|
162
168
|
@property
|
|
163
169
|
@override
|
|
164
170
|
def InputType(self) -> TypeAlias:
|
|
165
|
-
"""Get the input type for this
|
|
171
|
+
"""Get the input type for this `Runnable`."""
|
|
166
172
|
# This is a version of LanguageModelInput which replaces the abstract
|
|
167
173
|
# base class BaseMessage with a union of its subclasses, which makes
|
|
168
174
|
# for a much better schema.
|
|
@@ -186,12 +192,12 @@ class BaseLanguageModel(
|
|
|
186
192
|
1. Take advantage of batched calls,
|
|
187
193
|
2. Need more output from the model than just the top generated value,
|
|
188
194
|
3. Are building chains that are agnostic to the underlying language model
|
|
189
|
-
|
|
195
|
+
type (e.g., pure text completion models vs chat models).
|
|
190
196
|
|
|
191
197
|
Args:
|
|
192
|
-
prompts: List of
|
|
193
|
-
converted to match the format of any language model (string for
|
|
194
|
-
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).
|
|
195
201
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
196
202
|
first occurrence of any of these substrings.
|
|
197
203
|
callbacks: Callbacks to pass through. Used for executing additional
|
|
@@ -200,8 +206,8 @@ class BaseLanguageModel(
|
|
|
200
206
|
to the model provider API call.
|
|
201
207
|
|
|
202
208
|
Returns:
|
|
203
|
-
An LLMResult
|
|
204
|
-
prompt and additional model provider-specific output.
|
|
209
|
+
An `LLMResult`, which contains a list of candidate `Generation` objects for
|
|
210
|
+
each input prompt and additional model provider-specific output.
|
|
205
211
|
|
|
206
212
|
"""
|
|
207
213
|
|
|
@@ -223,12 +229,12 @@ class BaseLanguageModel(
|
|
|
223
229
|
1. Take advantage of batched calls,
|
|
224
230
|
2. Need more output from the model than just the top generated value,
|
|
225
231
|
3. Are building chains that are agnostic to the underlying language model
|
|
226
|
-
|
|
232
|
+
type (e.g., pure text completion models vs chat models).
|
|
227
233
|
|
|
228
234
|
Args:
|
|
229
|
-
prompts: List of
|
|
230
|
-
converted to match the format of any language model (string for
|
|
231
|
-
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).
|
|
232
238
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
233
239
|
first occurrence of any of these substrings.
|
|
234
240
|
callbacks: Callbacks to pass through. Used for executing additional
|
|
@@ -237,8 +243,8 @@ class BaseLanguageModel(
|
|
|
237
243
|
to the model provider API call.
|
|
238
244
|
|
|
239
245
|
Returns:
|
|
240
|
-
An
|
|
241
|
-
input prompt and additional model provider-specific output.
|
|
246
|
+
An `LLMResult`, which contains a list of candidate `Generation` objects for
|
|
247
|
+
each input prompt and additional model provider-specific output.
|
|
242
248
|
|
|
243
249
|
"""
|
|
244
250
|
|
|
@@ -294,13 +300,13 @@ class BaseLanguageModel(
|
|
|
294
300
|
Useful for checking if an input fits in a model's context window.
|
|
295
301
|
|
|
296
302
|
!!! note
|
|
297
|
-
The base implementation of
|
|
303
|
+
The base implementation of `get_num_tokens_from_messages` ignores tool
|
|
298
304
|
schemas.
|
|
299
305
|
|
|
300
306
|
Args:
|
|
301
307
|
messages: The message inputs to tokenize.
|
|
302
|
-
tools: If provided, sequence of dict,
|
|
303
|
-
|
|
308
|
+
tools: If provided, sequence of dict, `BaseModel`, function, or
|
|
309
|
+
`BaseTool` objects to be converted to tool schemas.
|
|
304
310
|
|
|
305
311
|
Returns:
|
|
306
312
|
The sum of the number of tokens across the messages.
|