langchain-core 0.3.74__py3-none-any.whl → 0.3.76__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/_api/beta_decorator.py +18 -41
- langchain_core/_api/deprecation.py +20 -7
- langchain_core/_api/path.py +19 -2
- langchain_core/_import_utils.py +7 -0
- langchain_core/agents.py +10 -6
- langchain_core/beta/runnables/context.py +2 -3
- langchain_core/callbacks/base.py +11 -4
- langchain_core/callbacks/file.py +13 -2
- langchain_core/callbacks/manager.py +129 -78
- langchain_core/callbacks/usage.py +4 -2
- langchain_core/chat_history.py +10 -12
- langchain_core/document_loaders/base.py +34 -9
- langchain_core/document_loaders/langsmith.py +3 -0
- langchain_core/documents/base.py +36 -11
- langchain_core/documents/compressor.py +9 -6
- langchain_core/documents/transformers.py +4 -2
- langchain_core/embeddings/fake.py +8 -5
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/exceptions.py +7 -0
- langchain_core/globals.py +17 -28
- langchain_core/indexing/api.py +56 -44
- langchain_core/indexing/base.py +7 -10
- langchain_core/indexing/in_memory.py +23 -3
- langchain_core/language_models/__init__.py +3 -2
- langchain_core/language_models/base.py +64 -39
- langchain_core/language_models/chat_models.py +130 -42
- langchain_core/language_models/fake_chat_models.py +10 -11
- langchain_core/language_models/llms.py +49 -17
- langchain_core/load/dump.py +5 -7
- langchain_core/load/load.py +15 -1
- langchain_core/load/serializable.py +38 -43
- langchain_core/memory.py +7 -3
- langchain_core/messages/ai.py +36 -16
- langchain_core/messages/base.py +13 -6
- langchain_core/messages/content_blocks.py +23 -2
- langchain_core/messages/human.py +2 -6
- langchain_core/messages/modifier.py +1 -1
- langchain_core/messages/system.py +2 -6
- langchain_core/messages/tool.py +36 -16
- langchain_core/messages/utils.py +198 -87
- langchain_core/output_parsers/base.py +5 -2
- langchain_core/output_parsers/json.py +4 -4
- langchain_core/output_parsers/list.py +7 -22
- langchain_core/output_parsers/openai_functions.py +3 -0
- langchain_core/output_parsers/openai_tools.py +8 -1
- langchain_core/output_parsers/pydantic.py +4 -0
- langchain_core/output_parsers/string.py +5 -1
- langchain_core/output_parsers/transform.py +2 -2
- langchain_core/output_parsers/xml.py +23 -22
- langchain_core/outputs/chat_generation.py +18 -7
- langchain_core/outputs/generation.py +14 -3
- langchain_core/outputs/llm_result.py +8 -1
- langchain_core/prompt_values.py +10 -4
- langchain_core/prompts/base.py +4 -9
- langchain_core/prompts/chat.py +88 -61
- langchain_core/prompts/dict.py +16 -8
- langchain_core/prompts/few_shot.py +9 -11
- langchain_core/prompts/few_shot_with_templates.py +5 -1
- langchain_core/prompts/image.py +12 -5
- langchain_core/prompts/message.py +5 -6
- langchain_core/prompts/pipeline.py +13 -8
- langchain_core/prompts/prompt.py +22 -8
- langchain_core/prompts/string.py +18 -10
- langchain_core/prompts/structured.py +7 -2
- langchain_core/rate_limiters.py +2 -2
- langchain_core/retrievers.py +7 -6
- langchain_core/runnables/base.py +842 -567
- langchain_core/runnables/branch.py +15 -20
- langchain_core/runnables/config.py +11 -17
- langchain_core/runnables/configurable.py +34 -19
- langchain_core/runnables/fallbacks.py +24 -17
- langchain_core/runnables/graph.py +47 -40
- langchain_core/runnables/graph_ascii.py +40 -17
- langchain_core/runnables/graph_mermaid.py +27 -15
- langchain_core/runnables/graph_png.py +27 -31
- langchain_core/runnables/history.py +56 -59
- langchain_core/runnables/passthrough.py +47 -24
- langchain_core/runnables/retry.py +10 -6
- langchain_core/runnables/router.py +10 -9
- langchain_core/runnables/schema.py +2 -0
- langchain_core/runnables/utils.py +51 -89
- langchain_core/stores.py +13 -25
- langchain_core/structured_query.py +3 -7
- langchain_core/sys_info.py +9 -8
- langchain_core/tools/base.py +30 -23
- langchain_core/tools/convert.py +24 -13
- langchain_core/tools/simple.py +35 -3
- langchain_core/tools/structured.py +26 -3
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +2 -2
- langchain_core/tracers/context.py +5 -1
- langchain_core/tracers/core.py +109 -39
- langchain_core/tracers/evaluation.py +22 -26
- langchain_core/tracers/event_stream.py +41 -28
- langchain_core/tracers/langchain.py +12 -3
- langchain_core/tracers/langchain_v1.py +10 -2
- langchain_core/tracers/log_stream.py +57 -18
- langchain_core/tracers/root_listeners.py +4 -20
- langchain_core/tracers/run_collector.py +6 -16
- langchain_core/tracers/schemas.py +5 -1
- langchain_core/utils/aiter.py +14 -6
- langchain_core/utils/env.py +3 -0
- langchain_core/utils/function_calling.py +49 -30
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +11 -3
- langchain_core/utils/json.py +5 -2
- langchain_core/utils/json_schema.py +15 -5
- langchain_core/utils/loading.py +5 -1
- langchain_core/utils/mustache.py +24 -15
- langchain_core/utils/pydantic.py +32 -4
- langchain_core/utils/utils.py +24 -8
- langchain_core/vectorstores/base.py +7 -20
- langchain_core/vectorstores/in_memory.py +18 -12
- langchain_core/vectorstores/utils.py +18 -12
- langchain_core/version.py +1 -1
- langchain_core-0.3.76.dist-info/METADATA +77 -0
- langchain_core-0.3.76.dist-info/RECORD +174 -0
- langchain_core-0.3.74.dist-info/METADATA +0 -108
- langchain_core-0.3.74.dist-info/RECORD +0 -174
- {langchain_core-0.3.74.dist-info → langchain_core-0.3.76.dist-info}/WHEEL +0 -0
- {langchain_core-0.3.74.dist-info → langchain_core-0.3.76.dist-info}/entry_points.txt +0 -0
langchain_core/indexing/api.py
CHANGED
|
@@ -185,6 +185,9 @@ def _get_document_with_hash(
|
|
|
185
185
|
When changing the key encoder, you must change the
|
|
186
186
|
index as well to avoid duplicated documents in the cache.
|
|
187
187
|
|
|
188
|
+
Raises:
|
|
189
|
+
ValueError: If the metadata cannot be serialized using json.
|
|
190
|
+
|
|
188
191
|
Returns:
|
|
189
192
|
Document with a unique identifier based on the hash of the content and metadata.
|
|
190
193
|
"""
|
|
@@ -291,21 +294,21 @@ def index(
|
|
|
291
294
|
documents were deleted, which documents should be skipped.
|
|
292
295
|
|
|
293
296
|
For the time being, documents are indexed using their hashes, and users
|
|
294
|
-
|
|
297
|
+
are not able to specify the uid of the document.
|
|
295
298
|
|
|
296
299
|
Important:
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
300
|
+
* In full mode, the loader should be returning
|
|
301
|
+
the entire dataset, and not just a subset of the dataset.
|
|
302
|
+
Otherwise, the auto_cleanup will remove documents that it is not
|
|
303
|
+
supposed to.
|
|
304
|
+
* In incremental mode, if documents associated with a particular
|
|
305
|
+
source id appear across different batches, the indexing API
|
|
306
|
+
will do some redundant work. This will still result in the
|
|
307
|
+
correct end state of the index, but will unfortunately not be
|
|
308
|
+
100% efficient. For example, if a given document is split into 15
|
|
309
|
+
chunks, and we index them using a batch size of 5, we'll have 3 batches
|
|
310
|
+
all with the same source id. In general, to avoid doing too much
|
|
311
|
+
redundant work select as big a batch size as possible.
|
|
309
312
|
* The `scoped_full` mode is suitable if determining an appropriate batch size
|
|
310
313
|
is challenging or if your data loader cannot return the entire dataset at
|
|
311
314
|
once. This mode keeps track of source IDs in memory, which should be fine
|
|
@@ -315,23 +318,22 @@ def index(
|
|
|
315
318
|
Args:
|
|
316
319
|
docs_source: Data loader or iterable of documents to index.
|
|
317
320
|
record_manager: Timestamped set to keep track of which documents were
|
|
318
|
-
|
|
321
|
+
updated.
|
|
319
322
|
vector_store: VectorStore or DocumentIndex to index the documents into.
|
|
320
323
|
batch_size: Batch size to use when indexing. Default is 100.
|
|
321
324
|
cleanup: How to handle clean up of documents. Default is None.
|
|
325
|
+
|
|
322
326
|
- incremental: Cleans up all documents that haven't been updated AND
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
to minimize the probability of users seeing duplicated
|
|
327
|
-
content.
|
|
327
|
+
that are associated with source ids that were seen during indexing.
|
|
328
|
+
Clean up is done continuously during indexing helping to minimize the
|
|
329
|
+
probability of users seeing duplicated content.
|
|
328
330
|
- full: Delete all documents that have not been returned by the loader
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
331
|
+
during this run of indexing.
|
|
332
|
+
Clean up runs after all documents have been indexed.
|
|
333
|
+
This means that users may see duplicated content during indexing.
|
|
332
334
|
- scoped_full: Similar to Full, but only deletes all documents
|
|
333
|
-
|
|
334
|
-
|
|
335
|
+
that haven't been updated AND that are associated with
|
|
336
|
+
source ids that were seen during indexing.
|
|
335
337
|
- None: Do not delete any documents.
|
|
336
338
|
source_id_key: Optional key that helps identify the original source
|
|
337
339
|
of the document. Default is None.
|
|
@@ -358,10 +360,9 @@ def index(
|
|
|
358
360
|
When changing the key encoder, you must change the
|
|
359
361
|
index as well to avoid duplicated documents in the cache.
|
|
360
362
|
upsert_kwargs: Additional keyword arguments to pass to the add_documents
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
upsert_kwargs={"vector_field": "embedding"}
|
|
363
|
+
method of the VectorStore or the upsert method of the DocumentIndex.
|
|
364
|
+
For example, you can use this to specify a custom vector_field:
|
|
365
|
+
upsert_kwargs={"vector_field": "embedding"}
|
|
365
366
|
.. versionadded:: 0.3.10
|
|
366
367
|
|
|
367
368
|
Returns:
|
|
@@ -374,6 +375,9 @@ def index(
|
|
|
374
375
|
ValueError: If vectorstore does not have
|
|
375
376
|
"delete" and "add_documents" required methods.
|
|
376
377
|
ValueError: If source_id_key is not None, but is not a string or callable.
|
|
378
|
+
TypeError: If ``vectorstore`` is not a VectorStore or a DocumentIndex.
|
|
379
|
+
AssertionError: If ``source_id`` is None when cleanup mode is incremental.
|
|
380
|
+
(should be unreachable code).
|
|
377
381
|
|
|
378
382
|
.. version_modified:: 0.3.25
|
|
379
383
|
|
|
@@ -656,22 +660,22 @@ async def aindex(
|
|
|
656
660
|
Args:
|
|
657
661
|
docs_source: Data loader or iterable of documents to index.
|
|
658
662
|
record_manager: Timestamped set to keep track of which documents were
|
|
659
|
-
|
|
663
|
+
updated.
|
|
660
664
|
vector_store: VectorStore or DocumentIndex to index the documents into.
|
|
661
665
|
batch_size: Batch size to use when indexing. Default is 100.
|
|
662
666
|
cleanup: How to handle clean up of documents. Default is None.
|
|
667
|
+
|
|
663
668
|
- incremental: Cleans up all documents that haven't been updated AND
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
This means that users may see duplicated content during indexing.
|
|
669
|
+
that are associated with source ids that were seen during indexing.
|
|
670
|
+
Clean up is done continuously during indexing helping to minimize the
|
|
671
|
+
probability of users seeing duplicated content.
|
|
672
|
+
- full: Delete all documents that have not been returned by the loader
|
|
673
|
+
during this run of indexing.
|
|
674
|
+
Clean up runs after all documents have been indexed.
|
|
675
|
+
This means that users may see duplicated content during indexing.
|
|
672
676
|
- scoped_full: Similar to Full, but only deletes all documents
|
|
673
|
-
|
|
674
|
-
|
|
677
|
+
that haven't been updated AND that are associated with
|
|
678
|
+
source ids that were seen during indexing.
|
|
675
679
|
- None: Do not delete any documents.
|
|
676
680
|
source_id_key: Optional key that helps identify the original source
|
|
677
681
|
of the document. Default is None.
|
|
@@ -680,6 +684,12 @@ async def aindex(
|
|
|
680
684
|
force_update: Force update documents even if they are present in the
|
|
681
685
|
record manager. Useful if you are re-indexing with updated embeddings.
|
|
682
686
|
Default is False.
|
|
687
|
+
key_encoder: Hashing algorithm to use for hashing the document content and
|
|
688
|
+
metadata. Default is "sha1".
|
|
689
|
+
Other options include "blake2b", "sha256", and "sha512".
|
|
690
|
+
|
|
691
|
+
.. versionadded:: 0.3.66
|
|
692
|
+
|
|
683
693
|
key_encoder: Hashing algorithm to use for hashing the document.
|
|
684
694
|
If not provided, a default encoder using SHA-1 will be used.
|
|
685
695
|
SHA-1 is not collision-resistant, and a motivated attacker
|
|
@@ -691,11 +701,10 @@ async def aindex(
|
|
|
691
701
|
|
|
692
702
|
When changing the key encoder, you must change the
|
|
693
703
|
index as well to avoid duplicated documents in the cache.
|
|
694
|
-
upsert_kwargs: Additional keyword arguments to pass to the
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
upsert_kwargs={"vector_field": "embedding"}
|
|
704
|
+
upsert_kwargs: Additional keyword arguments to pass to the add_documents
|
|
705
|
+
method of the VectorStore or the upsert method of the DocumentIndex.
|
|
706
|
+
For example, you can use this to specify a custom vector_field:
|
|
707
|
+
upsert_kwargs={"vector_field": "embedding"}
|
|
699
708
|
.. versionadded:: 0.3.10
|
|
700
709
|
|
|
701
710
|
Returns:
|
|
@@ -708,6 +717,9 @@ async def aindex(
|
|
|
708
717
|
ValueError: If vectorstore does not have
|
|
709
718
|
"adelete" and "aadd_documents" required methods.
|
|
710
719
|
ValueError: If source_id_key is not None, but is not a string or callable.
|
|
720
|
+
TypeError: If ``vector_store`` is not a VectorStore or DocumentIndex.
|
|
721
|
+
AssertionError: If ``source_id_key`` is None when cleanup mode is
|
|
722
|
+
incremental or ``scoped_full`` (should be unreachable).
|
|
711
723
|
|
|
712
724
|
.. version_modified:: 0.3.25
|
|
713
725
|
|
langchain_core/indexing/base.py
CHANGED
|
@@ -7,6 +7,8 @@ import time
|
|
|
7
7
|
from abc import ABC, abstractmethod
|
|
8
8
|
from typing import TYPE_CHECKING, Any, Optional, TypedDict
|
|
9
9
|
|
|
10
|
+
from typing_extensions import override
|
|
11
|
+
|
|
10
12
|
from langchain_core._api import beta
|
|
11
13
|
from langchain_core.retrievers import BaseRetriever
|
|
12
14
|
from langchain_core.runnables import run_in_executor
|
|
@@ -254,14 +256,14 @@ class InMemoryRecordManager(RecordManager):
|
|
|
254
256
|
"""In-memory schema creation is simply ensuring the structure is initialized."""
|
|
255
257
|
|
|
256
258
|
async def acreate_schema(self) -> None:
|
|
257
|
-
"""
|
|
259
|
+
"""In-memory schema creation is simply ensuring the structure is initialized."""
|
|
258
260
|
|
|
261
|
+
@override
|
|
259
262
|
def get_time(self) -> float:
|
|
260
|
-
"""Get the current server time as a high resolution timestamp!"""
|
|
261
263
|
return time.time()
|
|
262
264
|
|
|
265
|
+
@override
|
|
263
266
|
async def aget_time(self) -> float:
|
|
264
|
-
"""Async get the current server time as a high resolution timestamp!"""
|
|
265
267
|
return self.get_time()
|
|
266
268
|
|
|
267
269
|
def update(
|
|
@@ -322,11 +324,6 @@ class InMemoryRecordManager(RecordManager):
|
|
|
322
324
|
raise an error.
|
|
323
325
|
This is meant to help prevent time-drift issues since
|
|
324
326
|
time may not be monotonically increasing!
|
|
325
|
-
|
|
326
|
-
Raises:
|
|
327
|
-
ValueError: If the length of keys doesn't match the length of group
|
|
328
|
-
ids.
|
|
329
|
-
ValueError: If time_at_least is in the future.
|
|
330
327
|
"""
|
|
331
328
|
self.update(keys, group_ids=group_ids, time_at_least=time_at_least)
|
|
332
329
|
|
|
@@ -488,8 +485,8 @@ class DeleteResponse(TypedDict, total=False):
|
|
|
488
485
|
failed: Sequence[str]
|
|
489
486
|
"""The IDs that failed to be deleted.
|
|
490
487
|
|
|
491
|
-
|
|
492
|
-
|
|
488
|
+
.. warning::
|
|
489
|
+
Deleting an ID that does not exist is **NOT** considered a failure.
|
|
493
490
|
"""
|
|
494
491
|
|
|
495
492
|
num_failed: int
|
|
@@ -32,7 +32,17 @@ class InMemoryDocumentIndex(DocumentIndex):
|
|
|
32
32
|
|
|
33
33
|
@override
|
|
34
34
|
def upsert(self, items: Sequence[Document], /, **kwargs: Any) -> UpsertResponse:
|
|
35
|
-
"""Upsert
|
|
35
|
+
"""Upsert documents into the index.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
items: Sequence of documents to add to the index.
|
|
39
|
+
**kwargs: Additional keyword arguments.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
A response object that contains the list of IDs that were
|
|
43
|
+
successfully added or updated in the index and the list of IDs that
|
|
44
|
+
failed to be added or updated.
|
|
45
|
+
"""
|
|
36
46
|
ok_ids = []
|
|
37
47
|
|
|
38
48
|
for item in items:
|
|
@@ -51,7 +61,18 @@ class InMemoryDocumentIndex(DocumentIndex):
|
|
|
51
61
|
|
|
52
62
|
@override
|
|
53
63
|
def delete(self, ids: Optional[list[str]] = None, **kwargs: Any) -> DeleteResponse:
|
|
54
|
-
"""Delete by
|
|
64
|
+
"""Delete by IDs.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
ids: List of ids to delete.
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
ValueError: If ids is None.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
A response object that contains the list of IDs that were successfully
|
|
74
|
+
deleted and the list of IDs that failed to be deleted.
|
|
75
|
+
"""
|
|
55
76
|
if ids is None:
|
|
56
77
|
msg = "IDs must be provided for deletion"
|
|
57
78
|
raise ValueError(msg)
|
|
@@ -69,7 +90,6 @@ class InMemoryDocumentIndex(DocumentIndex):
|
|
|
69
90
|
|
|
70
91
|
@override
|
|
71
92
|
def get(self, ids: Sequence[str], /, **kwargs: Any) -> list[Document]:
|
|
72
|
-
"""Get by ids."""
|
|
73
93
|
return [self.store[id_] for id_ in ids if id_ in self.store]
|
|
74
94
|
|
|
75
95
|
@override
|
|
@@ -26,7 +26,8 @@ https://python.langchain.com/docs/how_to/custom_chat_model/
|
|
|
26
26
|
**LLMs**
|
|
27
27
|
|
|
28
28
|
Language models that takes a string as input and returns a string.
|
|
29
|
-
These are traditionally older models (newer models generally are Chat Models,
|
|
29
|
+
These are traditionally older models (newer models generally are Chat Models,
|
|
30
|
+
see below).
|
|
30
31
|
|
|
31
32
|
Although the underlying models are string in, string out, the LangChain wrappers
|
|
32
33
|
also allow these models to take messages as input. This gives them the same interface
|
|
@@ -39,7 +40,7 @@ Please see the following guide for more information on how to implement a custom
|
|
|
39
40
|
https://python.langchain.com/docs/how_to/custom_llm/
|
|
40
41
|
|
|
41
42
|
|
|
42
|
-
"""
|
|
43
|
+
"""
|
|
43
44
|
|
|
44
45
|
from typing import TYPE_CHECKING
|
|
45
46
|
|
|
@@ -22,19 +22,31 @@ from typing_extensions import TypeAlias, TypedDict, override
|
|
|
22
22
|
from langchain_core._api import deprecated
|
|
23
23
|
from langchain_core.caches import BaseCache
|
|
24
24
|
from langchain_core.callbacks import Callbacks
|
|
25
|
+
from langchain_core.globals import get_verbose
|
|
25
26
|
from langchain_core.messages import (
|
|
26
27
|
AnyMessage,
|
|
27
28
|
BaseMessage,
|
|
28
29
|
MessageLikeRepresentation,
|
|
29
30
|
get_buffer_string,
|
|
30
31
|
)
|
|
31
|
-
from langchain_core.prompt_values import
|
|
32
|
+
from langchain_core.prompt_values import (
|
|
33
|
+
ChatPromptValueConcrete,
|
|
34
|
+
PromptValue,
|
|
35
|
+
StringPromptValue,
|
|
36
|
+
)
|
|
32
37
|
from langchain_core.runnables import Runnable, RunnableSerializable
|
|
33
38
|
from langchain_core.utils import get_pydantic_field_names
|
|
34
39
|
|
|
35
40
|
if TYPE_CHECKING:
|
|
36
41
|
from langchain_core.outputs import LLMResult
|
|
37
42
|
|
|
43
|
+
try:
|
|
44
|
+
from transformers import GPT2TokenizerFast # type: ignore[import-not-found]
|
|
45
|
+
|
|
46
|
+
_HAS_TRANSFORMERS = True
|
|
47
|
+
except ImportError:
|
|
48
|
+
_HAS_TRANSFORMERS = False
|
|
49
|
+
|
|
38
50
|
|
|
39
51
|
class LangSmithParams(TypedDict, total=False):
|
|
40
52
|
"""LangSmith parameters for tracing."""
|
|
@@ -57,18 +69,22 @@ class LangSmithParams(TypedDict, total=False):
|
|
|
57
69
|
def get_tokenizer() -> Any:
|
|
58
70
|
"""Get a GPT-2 tokenizer instance.
|
|
59
71
|
|
|
60
|
-
This function is cached to avoid re-loading the tokenizer
|
|
61
|
-
|
|
72
|
+
This function is cached to avoid re-loading the tokenizer every time it is called.
|
|
73
|
+
|
|
74
|
+
Raises:
|
|
75
|
+
ImportError: If the transformers package is not installed.
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
The GPT-2 tokenizer instance.
|
|
79
|
+
|
|
62
80
|
"""
|
|
63
|
-
|
|
64
|
-
from transformers import GPT2TokenizerFast # type: ignore[import-not-found]
|
|
65
|
-
except ImportError as e:
|
|
81
|
+
if not _HAS_TRANSFORMERS:
|
|
66
82
|
msg = (
|
|
67
83
|
"Could not import transformers python package. "
|
|
68
84
|
"This is needed in order to calculate get_token_ids. "
|
|
69
85
|
"Please install it with `pip install transformers`."
|
|
70
86
|
)
|
|
71
|
-
raise ImportError(msg)
|
|
87
|
+
raise ImportError(msg)
|
|
72
88
|
# create a GPT-2 tokenizer instance
|
|
73
89
|
return GPT2TokenizerFast.from_pretrained("gpt2")
|
|
74
90
|
|
|
@@ -89,8 +105,6 @@ LanguageModelOutputVar = TypeVar("LanguageModelOutputVar", BaseMessage, str)
|
|
|
89
105
|
|
|
90
106
|
|
|
91
107
|
def _get_verbosity() -> bool:
|
|
92
|
-
from langchain_core.globals import get_verbose
|
|
93
|
-
|
|
94
108
|
return get_verbose()
|
|
95
109
|
|
|
96
110
|
|
|
@@ -99,7 +113,8 @@ class BaseLanguageModel(
|
|
|
99
113
|
):
|
|
100
114
|
"""Abstract base class for interfacing with language models.
|
|
101
115
|
|
|
102
|
-
All language model wrappers inherited from BaseLanguageModel
|
|
116
|
+
All language model wrappers inherited from ``BaseLanguageModel``.
|
|
117
|
+
|
|
103
118
|
"""
|
|
104
119
|
|
|
105
120
|
cache: Union[BaseCache, bool, None] = Field(default=None, exclude=True)
|
|
@@ -108,9 +123,10 @@ class BaseLanguageModel(
|
|
|
108
123
|
* If true, will use the global cache.
|
|
109
124
|
* If false, will not use a cache
|
|
110
125
|
* If None, will use the global cache if it's set, otherwise no cache.
|
|
111
|
-
* If instance of BaseCache
|
|
126
|
+
* If instance of ``BaseCache``, will use the provided cache.
|
|
112
127
|
|
|
113
128
|
Caching is not currently supported for streaming methods of models.
|
|
129
|
+
|
|
114
130
|
"""
|
|
115
131
|
verbose: bool = Field(default_factory=_get_verbosity, exclude=True, repr=False)
|
|
116
132
|
"""Whether to print out response text."""
|
|
@@ -140,6 +156,7 @@ class BaseLanguageModel(
|
|
|
140
156
|
|
|
141
157
|
Returns:
|
|
142
158
|
The verbosity setting to use.
|
|
159
|
+
|
|
143
160
|
"""
|
|
144
161
|
if verbose is None:
|
|
145
162
|
return _get_verbosity()
|
|
@@ -149,11 +166,6 @@ class BaseLanguageModel(
|
|
|
149
166
|
@override
|
|
150
167
|
def InputType(self) -> TypeAlias:
|
|
151
168
|
"""Get the input type for this runnable."""
|
|
152
|
-
from langchain_core.prompt_values import (
|
|
153
|
-
ChatPromptValueConcrete,
|
|
154
|
-
StringPromptValue,
|
|
155
|
-
)
|
|
156
|
-
|
|
157
169
|
# This is a version of LanguageModelInput which replaces the abstract
|
|
158
170
|
# base class BaseMessage with a union of its subclasses, which makes
|
|
159
171
|
# for a much better schema.
|
|
@@ -177,10 +189,11 @@ class BaseLanguageModel(
|
|
|
177
189
|
API.
|
|
178
190
|
|
|
179
191
|
Use this method when you want to:
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
192
|
+
|
|
193
|
+
1. Take advantage of batched calls,
|
|
194
|
+
2. Need more output from the model than just the top generated value,
|
|
195
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
196
|
+
type (e.g., pure text completion models vs chat models).
|
|
184
197
|
|
|
185
198
|
Args:
|
|
186
199
|
prompts: List of PromptValues. A PromptValue is an object that can be
|
|
@@ -195,7 +208,8 @@ class BaseLanguageModel(
|
|
|
195
208
|
|
|
196
209
|
Returns:
|
|
197
210
|
An LLMResult, which contains a list of candidate Generations for each input
|
|
198
|
-
|
|
211
|
+
prompt and additional model provider-specific output.
|
|
212
|
+
|
|
199
213
|
"""
|
|
200
214
|
|
|
201
215
|
@abstractmethod
|
|
@@ -212,10 +226,11 @@ class BaseLanguageModel(
|
|
|
212
226
|
API.
|
|
213
227
|
|
|
214
228
|
Use this method when you want to:
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
229
|
+
|
|
230
|
+
1. Take advantage of batched calls,
|
|
231
|
+
2. Need more output from the model than just the top generated value,
|
|
232
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
233
|
+
type (e.g., pure text completion models vs chat models).
|
|
219
234
|
|
|
220
235
|
Args:
|
|
221
236
|
prompts: List of PromptValues. A PromptValue is an object that can be
|
|
@@ -229,8 +244,9 @@ class BaseLanguageModel(
|
|
|
229
244
|
to the model provider API call.
|
|
230
245
|
|
|
231
246
|
Returns:
|
|
232
|
-
An LLMResult
|
|
233
|
-
|
|
247
|
+
An ``LLMResult``, which contains a list of candidate Generations for each
|
|
248
|
+
input prompt and additional model provider-specific output.
|
|
249
|
+
|
|
234
250
|
"""
|
|
235
251
|
|
|
236
252
|
def with_structured_output(
|
|
@@ -248,8 +264,8 @@ class BaseLanguageModel(
|
|
|
248
264
|
) -> str:
|
|
249
265
|
"""Pass a single string input to the model and return a string.
|
|
250
266
|
|
|
251
|
-
|
|
252
|
-
|
|
267
|
+
Use this method when passing in raw text. If you want to pass in specific types
|
|
268
|
+
of chat messages, use predict_messages.
|
|
253
269
|
|
|
254
270
|
Args:
|
|
255
271
|
text: String input to pass to the model.
|
|
@@ -260,6 +276,7 @@ class BaseLanguageModel(
|
|
|
260
276
|
|
|
261
277
|
Returns:
|
|
262
278
|
Top model prediction as a string.
|
|
279
|
+
|
|
263
280
|
"""
|
|
264
281
|
|
|
265
282
|
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
@@ -274,7 +291,7 @@ class BaseLanguageModel(
|
|
|
274
291
|
"""Pass a message sequence to the model and return a message.
|
|
275
292
|
|
|
276
293
|
Use this method when passing in chat messages. If you want to pass in raw text,
|
|
277
|
-
|
|
294
|
+
use predict.
|
|
278
295
|
|
|
279
296
|
Args:
|
|
280
297
|
messages: A sequence of chat messages corresponding to a single model input.
|
|
@@ -285,6 +302,7 @@ class BaseLanguageModel(
|
|
|
285
302
|
|
|
286
303
|
Returns:
|
|
287
304
|
Top model prediction as a message.
|
|
305
|
+
|
|
288
306
|
"""
|
|
289
307
|
|
|
290
308
|
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
@@ -295,7 +313,7 @@ class BaseLanguageModel(
|
|
|
295
313
|
"""Asynchronously pass a string to the model and return a string.
|
|
296
314
|
|
|
297
315
|
Use this method when calling pure text generation models and only the top
|
|
298
|
-
|
|
316
|
+
candidate generation is needed.
|
|
299
317
|
|
|
300
318
|
Args:
|
|
301
319
|
text: String input to pass to the model.
|
|
@@ -306,6 +324,7 @@ class BaseLanguageModel(
|
|
|
306
324
|
|
|
307
325
|
Returns:
|
|
308
326
|
Top model prediction as a string.
|
|
327
|
+
|
|
309
328
|
"""
|
|
310
329
|
|
|
311
330
|
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
@@ -319,8 +338,8 @@ class BaseLanguageModel(
|
|
|
319
338
|
) -> BaseMessage:
|
|
320
339
|
"""Asynchronously pass messages to the model and return a message.
|
|
321
340
|
|
|
322
|
-
Use this method when calling chat models and only the top
|
|
323
|
-
|
|
341
|
+
Use this method when calling chat models and only the top candidate generation
|
|
342
|
+
is needed.
|
|
324
343
|
|
|
325
344
|
Args:
|
|
326
345
|
messages: A sequence of chat messages corresponding to a single model input.
|
|
@@ -331,6 +350,7 @@ class BaseLanguageModel(
|
|
|
331
350
|
|
|
332
351
|
Returns:
|
|
333
352
|
Top model prediction as a message.
|
|
353
|
+
|
|
334
354
|
"""
|
|
335
355
|
|
|
336
356
|
@property
|
|
@@ -346,7 +366,8 @@ class BaseLanguageModel(
|
|
|
346
366
|
|
|
347
367
|
Returns:
|
|
348
368
|
A list of ids corresponding to the tokens in the text, in order they occur
|
|
349
|
-
|
|
369
|
+
in the text.
|
|
370
|
+
|
|
350
371
|
"""
|
|
351
372
|
if self.custom_get_token_ids is not None:
|
|
352
373
|
return self.custom_get_token_ids(text)
|
|
@@ -362,6 +383,7 @@ class BaseLanguageModel(
|
|
|
362
383
|
|
|
363
384
|
Returns:
|
|
364
385
|
The integer number of tokens in the text.
|
|
386
|
+
|
|
365
387
|
"""
|
|
366
388
|
return len(self.get_token_ids(text))
|
|
367
389
|
|
|
@@ -374,16 +396,18 @@ class BaseLanguageModel(
|
|
|
374
396
|
|
|
375
397
|
Useful for checking if an input fits in a model's context window.
|
|
376
398
|
|
|
377
|
-
|
|
378
|
-
|
|
399
|
+
.. note::
|
|
400
|
+
The base implementation of ``get_num_tokens_from_messages`` ignores tool
|
|
401
|
+
schemas.
|
|
379
402
|
|
|
380
403
|
Args:
|
|
381
404
|
messages: The message inputs to tokenize.
|
|
382
|
-
tools: If provided, sequence of dict, BaseModel
|
|
383
|
-
to be converted to tool schemas.
|
|
405
|
+
tools: If provided, sequence of dict, ``BaseModel``, function, or
|
|
406
|
+
``BaseTools`` to be converted to tool schemas.
|
|
384
407
|
|
|
385
408
|
Returns:
|
|
386
409
|
The sum of the number of tokens across the messages.
|
|
410
|
+
|
|
387
411
|
"""
|
|
388
412
|
if tools is not None:
|
|
389
413
|
warnings.warn(
|
|
@@ -396,6 +420,7 @@ class BaseLanguageModel(
|
|
|
396
420
|
def _all_required_field_names(cls) -> set:
|
|
397
421
|
"""DEPRECATED: Kept for backwards compatibility.
|
|
398
422
|
|
|
399
|
-
Use get_pydantic_field_names
|
|
423
|
+
Use ``get_pydantic_field_names``.
|
|
424
|
+
|
|
400
425
|
"""
|
|
401
426
|
return get_pydantic_field_names(cls)
|