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/rate_limiters.py
CHANGED
|
@@ -21,11 +21,8 @@ class BaseRateLimiter(abc.ABC):
|
|
|
21
21
|
Current limitations:
|
|
22
22
|
|
|
23
23
|
- Rate limiting information is not surfaced in tracing or callbacks. This means
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
!!! version-added "Added in version 0.2.24"
|
|
24
|
+
that the total time it takes to invoke a chat model will encompass both
|
|
25
|
+
the time spent waiting for tokens and the time spent making the request.
|
|
29
26
|
"""
|
|
30
27
|
|
|
31
28
|
@abc.abstractmethod
|
|
@@ -33,18 +30,18 @@ class BaseRateLimiter(abc.ABC):
|
|
|
33
30
|
"""Attempt to acquire the necessary tokens for the rate limiter.
|
|
34
31
|
|
|
35
32
|
This method blocks until the required tokens are available if `blocking`
|
|
36
|
-
is set to True
|
|
33
|
+
is set to `True`.
|
|
37
34
|
|
|
38
|
-
If `blocking` is set to False
|
|
35
|
+
If `blocking` is set to `False`, the method will immediately return the result
|
|
39
36
|
of the attempt to acquire the tokens.
|
|
40
37
|
|
|
41
38
|
Args:
|
|
42
|
-
blocking: If True
|
|
43
|
-
If False
|
|
44
|
-
the attempt.
|
|
39
|
+
blocking: If `True`, the method will block until the tokens are available.
|
|
40
|
+
If `False`, the method will return immediately with the result of
|
|
41
|
+
the attempt.
|
|
45
42
|
|
|
46
43
|
Returns:
|
|
47
|
-
|
|
44
|
+
`True` if the tokens were successfully acquired, `False` otherwise.
|
|
48
45
|
"""
|
|
49
46
|
|
|
50
47
|
@abc.abstractmethod
|
|
@@ -52,18 +49,18 @@ class BaseRateLimiter(abc.ABC):
|
|
|
52
49
|
"""Attempt to acquire the necessary tokens for the rate limiter.
|
|
53
50
|
|
|
54
51
|
This method blocks until the required tokens are available if `blocking`
|
|
55
|
-
is set to True
|
|
52
|
+
is set to `True`.
|
|
56
53
|
|
|
57
|
-
If `blocking` is set to False
|
|
54
|
+
If `blocking` is set to `False`, the method will immediately return the result
|
|
58
55
|
of the attempt to acquire the tokens.
|
|
59
56
|
|
|
60
57
|
Args:
|
|
61
|
-
blocking: If True
|
|
62
|
-
If False
|
|
63
|
-
the attempt.
|
|
58
|
+
blocking: If `True`, the method will block until the tokens are available.
|
|
59
|
+
If `False`, the method will return immediately with the result of
|
|
60
|
+
the attempt.
|
|
64
61
|
|
|
65
62
|
Returns:
|
|
66
|
-
|
|
63
|
+
`True` if the tokens were successfully acquired, `False` otherwise.
|
|
67
64
|
"""
|
|
68
65
|
|
|
69
66
|
|
|
@@ -84,45 +81,40 @@ class InMemoryRateLimiter(BaseRateLimiter):
|
|
|
84
81
|
not enough tokens in the bucket, the request is blocked until there are
|
|
85
82
|
enough tokens.
|
|
86
83
|
|
|
87
|
-
These
|
|
84
|
+
These tokens have nothing to do with LLM tokens. They are just
|
|
88
85
|
a way to keep track of how many requests can be made at a given time.
|
|
89
86
|
|
|
90
87
|
Current limitations:
|
|
91
88
|
|
|
92
89
|
- The rate limiter is not designed to work across different processes. It is
|
|
93
|
-
|
|
90
|
+
an in-memory rate limiter, but it is thread safe.
|
|
94
91
|
- The rate limiter only supports time-based rate limiting. It does not take
|
|
95
|
-
|
|
92
|
+
into account the size of the request or any other factors.
|
|
96
93
|
|
|
97
94
|
Example:
|
|
95
|
+
```python
|
|
96
|
+
import time
|
|
98
97
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
import time
|
|
102
|
-
|
|
103
|
-
from langchain_core.rate_limiters import InMemoryRateLimiter
|
|
104
|
-
|
|
105
|
-
rate_limiter = InMemoryRateLimiter(
|
|
106
|
-
requests_per_second=0.1, # <-- Can only make a request once every 10 seconds!!
|
|
107
|
-
check_every_n_seconds=0.1, # Wake up every 100 ms to check whether allowed to make a request,
|
|
108
|
-
max_bucket_size=10, # Controls the maximum burst size.
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
from langchain_anthropic import ChatAnthropic
|
|
112
|
-
|
|
113
|
-
model = ChatAnthropic(
|
|
114
|
-
model_name="claude-3-opus-20240229", rate_limiter=rate_limiter
|
|
115
|
-
)
|
|
98
|
+
from langchain_core.rate_limiters import InMemoryRateLimiter
|
|
116
99
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
100
|
+
rate_limiter = InMemoryRateLimiter(
|
|
101
|
+
requests_per_second=0.1, # <-- Can only make a request once every 10 seconds!!
|
|
102
|
+
check_every_n_seconds=0.1, # Wake up every 100 ms to check whether allowed to make a request,
|
|
103
|
+
max_bucket_size=10, # Controls the maximum burst size.
|
|
104
|
+
)
|
|
122
105
|
|
|
106
|
+
from langchain_anthropic import ChatAnthropic
|
|
123
107
|
|
|
124
|
-
|
|
108
|
+
model = ChatAnthropic(
|
|
109
|
+
model_name="claude-sonnet-4-5-20250929", rate_limiter=rate_limiter
|
|
110
|
+
)
|
|
125
111
|
|
|
112
|
+
for _ in range(5):
|
|
113
|
+
tic = time.time()
|
|
114
|
+
model.invoke("hello")
|
|
115
|
+
toc = time.time()
|
|
116
|
+
print(toc - tic)
|
|
117
|
+
```
|
|
126
118
|
""" # noqa: E501
|
|
127
119
|
|
|
128
120
|
def __init__(
|
|
@@ -134,7 +126,7 @@ class InMemoryRateLimiter(BaseRateLimiter):
|
|
|
134
126
|
) -> None:
|
|
135
127
|
"""A rate limiter based on a token bucket.
|
|
136
128
|
|
|
137
|
-
These
|
|
129
|
+
These tokens have nothing to do with LLM tokens. They are just
|
|
138
130
|
a way to keep track of how many requests can be made at a given time.
|
|
139
131
|
|
|
140
132
|
This rate limiter is designed to work in a threaded environment.
|
|
@@ -147,11 +139,11 @@ class InMemoryRateLimiter(BaseRateLimiter):
|
|
|
147
139
|
Args:
|
|
148
140
|
requests_per_second: The number of tokens to add per second to the bucket.
|
|
149
141
|
The tokens represent "credit" that can be used to make requests.
|
|
150
|
-
check_every_n_seconds:
|
|
142
|
+
check_every_n_seconds: Check whether the tokens are available
|
|
151
143
|
every this many seconds. Can be a float to represent
|
|
152
144
|
fractions of a second.
|
|
153
145
|
max_bucket_size: The maximum number of tokens that can be in the bucket.
|
|
154
|
-
Must be at least 1
|
|
146
|
+
Must be at least `1`. Used to prevent bursts of requests.
|
|
155
147
|
"""
|
|
156
148
|
# Number of requests that we can make per second.
|
|
157
149
|
self.requests_per_second = requests_per_second
|
|
@@ -201,18 +193,18 @@ class InMemoryRateLimiter(BaseRateLimiter):
|
|
|
201
193
|
"""Attempt to acquire a token from the rate limiter.
|
|
202
194
|
|
|
203
195
|
This method blocks until the required tokens are available if `blocking`
|
|
204
|
-
is set to True
|
|
196
|
+
is set to `True`.
|
|
205
197
|
|
|
206
|
-
If `blocking` is set to False
|
|
198
|
+
If `blocking` is set to `False`, the method will immediately return the result
|
|
207
199
|
of the attempt to acquire the tokens.
|
|
208
200
|
|
|
209
201
|
Args:
|
|
210
|
-
blocking: If True
|
|
211
|
-
If False
|
|
212
|
-
the attempt.
|
|
202
|
+
blocking: If `True`, the method will block until the tokens are available.
|
|
203
|
+
If `False`, the method will return immediately with the result of
|
|
204
|
+
the attempt.
|
|
213
205
|
|
|
214
206
|
Returns:
|
|
215
|
-
|
|
207
|
+
`True` if the tokens were successfully acquired, `False` otherwise.
|
|
216
208
|
"""
|
|
217
209
|
if not blocking:
|
|
218
210
|
return self._consume()
|
|
@@ -225,18 +217,18 @@ class InMemoryRateLimiter(BaseRateLimiter):
|
|
|
225
217
|
"""Attempt to acquire a token from the rate limiter. Async version.
|
|
226
218
|
|
|
227
219
|
This method blocks until the required tokens are available if `blocking`
|
|
228
|
-
is set to True
|
|
220
|
+
is set to `True`.
|
|
229
221
|
|
|
230
|
-
If `blocking` is set to False
|
|
222
|
+
If `blocking` is set to `False`, the method will immediately return the result
|
|
231
223
|
of the attempt to acquire the tokens.
|
|
232
224
|
|
|
233
225
|
Args:
|
|
234
|
-
blocking: If True
|
|
235
|
-
If False
|
|
236
|
-
the attempt.
|
|
226
|
+
blocking: If `True`, the method will block until the tokens are available.
|
|
227
|
+
If `False`, the method will return immediately with the result of
|
|
228
|
+
the attempt.
|
|
237
229
|
|
|
238
230
|
Returns:
|
|
239
|
-
|
|
231
|
+
`True` if the tokens were successfully acquired, `False` otherwise.
|
|
240
232
|
"""
|
|
241
233
|
if not blocking:
|
|
242
234
|
return self._consume()
|
langchain_core/retrievers.py
CHANGED
|
@@ -3,25 +3,10 @@
|
|
|
3
3
|
It is more general than a vector store. A retriever does not need to be able to
|
|
4
4
|
store documents, only to return (or retrieve) it. Vector stores can be used as
|
|
5
5
|
the backbone of a retriever, but there are other types of retrievers as well.
|
|
6
|
-
|
|
7
|
-
**Class hierarchy:**
|
|
8
|
-
|
|
9
|
-
.. code-block::
|
|
10
|
-
|
|
11
|
-
BaseRetriever --> <name>Retriever # Examples: ArxivRetriever, MergerRetriever
|
|
12
|
-
|
|
13
|
-
**Main helpers:**
|
|
14
|
-
|
|
15
|
-
.. code-block::
|
|
16
|
-
|
|
17
|
-
RetrieverInput, RetrieverOutput, RetrieverLike, RetrieverOutputLike,
|
|
18
|
-
Document, Serializable, Callbacks,
|
|
19
|
-
CallbackManagerForRetrieverRun, AsyncCallbackManagerForRetrieverRun
|
|
20
6
|
"""
|
|
21
7
|
|
|
22
8
|
from __future__ import annotations
|
|
23
9
|
|
|
24
|
-
import warnings
|
|
25
10
|
from abc import ABC, abstractmethod
|
|
26
11
|
from inspect import signature
|
|
27
12
|
from typing import TYPE_CHECKING, Any
|
|
@@ -29,8 +14,6 @@ from typing import TYPE_CHECKING, Any
|
|
|
29
14
|
from pydantic import ConfigDict
|
|
30
15
|
from typing_extensions import Self, TypedDict, override
|
|
31
16
|
|
|
32
|
-
from langchain_core._api import deprecated
|
|
33
|
-
from langchain_core.callbacks import Callbacks
|
|
34
17
|
from langchain_core.callbacks.manager import AsyncCallbackManager, CallbackManager
|
|
35
18
|
from langchain_core.documents import Document
|
|
36
19
|
from langchain_core.runnables import (
|
|
@@ -87,48 +70,45 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
87
70
|
|
|
88
71
|
Example: A retriever that returns the first 5 documents from a list of documents
|
|
89
72
|
|
|
90
|
-
|
|
73
|
+
```python
|
|
74
|
+
from langchain_core.documents import Document
|
|
75
|
+
from langchain_core.retrievers import BaseRetriever
|
|
91
76
|
|
|
92
|
-
|
|
93
|
-
|
|
77
|
+
class SimpleRetriever(BaseRetriever):
|
|
78
|
+
docs: list[Document]
|
|
79
|
+
k: int = 5
|
|
94
80
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
81
|
+
def _get_relevant_documents(self, query: str) -> list[Document]:
|
|
82
|
+
\"\"\"Return the first k documents from the list of documents\"\"\"
|
|
83
|
+
return self.docs[:self.k]
|
|
98
84
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
async def _aget_relevant_documents(self, query: str) -> list[Document]:
|
|
104
|
-
\"\"\"(Optional) async native implementation.\"\"\"
|
|
105
|
-
return self.docs[:self.k]
|
|
85
|
+
async def _aget_relevant_documents(self, query: str) -> list[Document]:
|
|
86
|
+
\"\"\"(Optional) async native implementation.\"\"\"
|
|
87
|
+
return self.docs[:self.k]
|
|
88
|
+
```
|
|
106
89
|
|
|
107
90
|
Example: A simple retriever based on a scikit-learn vectorizer
|
|
108
91
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
from sklearn.metrics.pairwise import cosine_similarity
|
|
112
|
-
|
|
92
|
+
```python
|
|
93
|
+
from sklearn.metrics.pairwise import cosine_similarity
|
|
113
94
|
|
|
114
|
-
class TFIDFRetriever(BaseRetriever, BaseModel):
|
|
115
|
-
vectorizer: Any
|
|
116
|
-
docs: list[Document]
|
|
117
|
-
tfidf_array: Any
|
|
118
|
-
k: int = 4
|
|
119
95
|
|
|
120
|
-
|
|
121
|
-
|
|
96
|
+
class TFIDFRetriever(BaseRetriever, BaseModel):
|
|
97
|
+
vectorizer: Any
|
|
98
|
+
docs: list[Document]
|
|
99
|
+
tfidf_array: Any
|
|
100
|
+
k: int = 4
|
|
122
101
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
query_vec = self.vectorizer.transform([query])
|
|
126
|
-
# Op -- (n_docs,1) -- Cosine Sim with each doc
|
|
127
|
-
results = cosine_similarity(self.tfidf_array, query_vec).reshape(
|
|
128
|
-
(-1,)
|
|
129
|
-
)
|
|
130
|
-
return [self.docs[i] for i in results.argsort()[-self.k :][::-1]]
|
|
102
|
+
class Config:
|
|
103
|
+
arbitrary_types_allowed = True
|
|
131
104
|
|
|
105
|
+
def _get_relevant_documents(self, query: str) -> list[Document]:
|
|
106
|
+
# Ip -- (n_docs,x), Op -- (n_docs,n_Feats)
|
|
107
|
+
query_vec = self.vectorizer.transform([query])
|
|
108
|
+
# Op -- (n_docs,1) -- Cosine Sim with each doc
|
|
109
|
+
results = cosine_similarity(self.tfidf_array, query_vec).reshape((-1,))
|
|
110
|
+
return [self.docs[i] for i in results.argsort()[-self.k :][::-1]]
|
|
111
|
+
```
|
|
132
112
|
"""
|
|
133
113
|
|
|
134
114
|
model_config = ConfigDict(
|
|
@@ -138,14 +118,14 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
138
118
|
_new_arg_supported: bool = False
|
|
139
119
|
_expects_other_args: bool = False
|
|
140
120
|
tags: list[str] | None = None
|
|
141
|
-
"""Optional list of tags associated with the retriever.
|
|
121
|
+
"""Optional list of tags associated with the retriever.
|
|
142
122
|
These tags will be associated with each call to this retriever,
|
|
143
123
|
and passed as arguments to the handlers defined in `callbacks`.
|
|
144
124
|
You can use these to eg identify a specific instance of a retriever with its
|
|
145
125
|
use case.
|
|
146
126
|
"""
|
|
147
127
|
metadata: dict[str, Any] | None = None
|
|
148
|
-
"""Optional metadata associated with the retriever.
|
|
128
|
+
"""Optional metadata associated with the retriever.
|
|
149
129
|
This metadata will be associated with each call to this retriever,
|
|
150
130
|
and passed as arguments to the handlers defined in `callbacks`.
|
|
151
131
|
You can use these to eg identify a specific instance of a retriever with its
|
|
@@ -155,35 +135,6 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
155
135
|
@override
|
|
156
136
|
def __init_subclass__(cls, **kwargs: Any) -> None:
|
|
157
137
|
super().__init_subclass__(**kwargs)
|
|
158
|
-
# Version upgrade for old retrievers that implemented the public
|
|
159
|
-
# methods directly.
|
|
160
|
-
if cls.get_relevant_documents != BaseRetriever.get_relevant_documents:
|
|
161
|
-
warnings.warn(
|
|
162
|
-
"Retrievers must implement abstract `_get_relevant_documents` method"
|
|
163
|
-
" instead of `get_relevant_documents`",
|
|
164
|
-
DeprecationWarning,
|
|
165
|
-
stacklevel=4,
|
|
166
|
-
)
|
|
167
|
-
swap = cls.get_relevant_documents
|
|
168
|
-
cls.get_relevant_documents = ( # type: ignore[method-assign]
|
|
169
|
-
BaseRetriever.get_relevant_documents
|
|
170
|
-
)
|
|
171
|
-
cls._get_relevant_documents = swap # type: ignore[method-assign]
|
|
172
|
-
if (
|
|
173
|
-
hasattr(cls, "aget_relevant_documents")
|
|
174
|
-
and cls.aget_relevant_documents != BaseRetriever.aget_relevant_documents
|
|
175
|
-
):
|
|
176
|
-
warnings.warn(
|
|
177
|
-
"Retrievers must implement abstract `_aget_relevant_documents` method"
|
|
178
|
-
" instead of `aget_relevant_documents`",
|
|
179
|
-
DeprecationWarning,
|
|
180
|
-
stacklevel=4,
|
|
181
|
-
)
|
|
182
|
-
aswap = cls.aget_relevant_documents
|
|
183
|
-
cls.aget_relevant_documents = ( # type: ignore[method-assign]
|
|
184
|
-
BaseRetriever.aget_relevant_documents
|
|
185
|
-
)
|
|
186
|
-
cls._aget_relevant_documents = aswap # type: ignore[method-assign]
|
|
187
138
|
parameters = signature(cls._get_relevant_documents).parameters
|
|
188
139
|
cls._new_arg_supported = parameters.get("run_manager") is not None
|
|
189
140
|
if (
|
|
@@ -224,18 +175,16 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
224
175
|
|
|
225
176
|
Args:
|
|
226
177
|
input: The query string.
|
|
227
|
-
config: Configuration for the retriever.
|
|
228
|
-
kwargs: Additional arguments to pass to the retriever.
|
|
178
|
+
config: Configuration for the retriever.
|
|
179
|
+
**kwargs: Additional arguments to pass to the retriever.
|
|
229
180
|
|
|
230
181
|
Returns:
|
|
231
182
|
List of relevant documents.
|
|
232
183
|
|
|
233
184
|
Examples:
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
retriever.invoke("query")
|
|
238
|
-
|
|
185
|
+
```python
|
|
186
|
+
retriever.invoke("query")
|
|
187
|
+
```
|
|
239
188
|
"""
|
|
240
189
|
config = ensure_config(config)
|
|
241
190
|
inheritable_metadata = {
|
|
@@ -287,18 +236,16 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
287
236
|
|
|
288
237
|
Args:
|
|
289
238
|
input: The query string.
|
|
290
|
-
config: Configuration for the retriever.
|
|
291
|
-
kwargs: Additional arguments to pass to the retriever.
|
|
239
|
+
config: Configuration for the retriever.
|
|
240
|
+
**kwargs: Additional arguments to pass to the retriever.
|
|
292
241
|
|
|
293
242
|
Returns:
|
|
294
243
|
List of relevant documents.
|
|
295
244
|
|
|
296
245
|
Examples:
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
await retriever.ainvoke("query")
|
|
301
|
-
|
|
246
|
+
```python
|
|
247
|
+
await retriever.ainvoke("query")
|
|
248
|
+
```
|
|
302
249
|
"""
|
|
303
250
|
config = ensure_config(config)
|
|
304
251
|
inheritable_metadata = {
|
|
@@ -369,91 +316,3 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
369
316
|
query,
|
|
370
317
|
run_manager=run_manager.get_sync(),
|
|
371
318
|
)
|
|
372
|
-
|
|
373
|
-
@deprecated(since="0.1.46", alternative="invoke", removal="1.0")
|
|
374
|
-
def get_relevant_documents(
|
|
375
|
-
self,
|
|
376
|
-
query: str,
|
|
377
|
-
*,
|
|
378
|
-
callbacks: Callbacks = None,
|
|
379
|
-
tags: list[str] | None = None,
|
|
380
|
-
metadata: dict[str, Any] | None = None,
|
|
381
|
-
run_name: str | None = None,
|
|
382
|
-
**kwargs: Any,
|
|
383
|
-
) -> list[Document]:
|
|
384
|
-
"""Retrieve documents relevant to a query.
|
|
385
|
-
|
|
386
|
-
Users should favor using `.invoke` or `.batch` rather than
|
|
387
|
-
`get_relevant_documents directly`.
|
|
388
|
-
|
|
389
|
-
Args:
|
|
390
|
-
query: string to find relevant documents for.
|
|
391
|
-
callbacks: Callback manager or list of callbacks. Defaults to None.
|
|
392
|
-
tags: Optional list of tags associated with the retriever.
|
|
393
|
-
These tags will be associated with each call to this retriever,
|
|
394
|
-
and passed as arguments to the handlers defined in `callbacks`.
|
|
395
|
-
Defaults to None.
|
|
396
|
-
metadata: Optional metadata associated with the retriever.
|
|
397
|
-
This metadata will be associated with each call to this retriever,
|
|
398
|
-
and passed as arguments to the handlers defined in `callbacks`.
|
|
399
|
-
Defaults to None.
|
|
400
|
-
run_name: Optional name for the run. Defaults to None.
|
|
401
|
-
kwargs: Additional arguments to pass to the retriever.
|
|
402
|
-
|
|
403
|
-
Returns:
|
|
404
|
-
List of relevant documents.
|
|
405
|
-
"""
|
|
406
|
-
config: RunnableConfig = {}
|
|
407
|
-
if callbacks:
|
|
408
|
-
config["callbacks"] = callbacks
|
|
409
|
-
if tags:
|
|
410
|
-
config["tags"] = tags
|
|
411
|
-
if metadata:
|
|
412
|
-
config["metadata"] = metadata
|
|
413
|
-
if run_name:
|
|
414
|
-
config["run_name"] = run_name
|
|
415
|
-
return self.invoke(query, config, **kwargs)
|
|
416
|
-
|
|
417
|
-
@deprecated(since="0.1.46", alternative="ainvoke", removal="1.0")
|
|
418
|
-
async def aget_relevant_documents(
|
|
419
|
-
self,
|
|
420
|
-
query: str,
|
|
421
|
-
*,
|
|
422
|
-
callbacks: Callbacks = None,
|
|
423
|
-
tags: list[str] | None = None,
|
|
424
|
-
metadata: dict[str, Any] | None = None,
|
|
425
|
-
run_name: str | None = None,
|
|
426
|
-
**kwargs: Any,
|
|
427
|
-
) -> list[Document]:
|
|
428
|
-
"""Asynchronously get documents relevant to a query.
|
|
429
|
-
|
|
430
|
-
Users should favor using `.ainvoke` or `.abatch` rather than
|
|
431
|
-
`aget_relevant_documents directly`.
|
|
432
|
-
|
|
433
|
-
Args:
|
|
434
|
-
query: string to find relevant documents for.
|
|
435
|
-
callbacks: Callback manager or list of callbacks.
|
|
436
|
-
tags: Optional list of tags associated with the retriever.
|
|
437
|
-
These tags will be associated with each call to this retriever,
|
|
438
|
-
and passed as arguments to the handlers defined in `callbacks`.
|
|
439
|
-
Defaults to None.
|
|
440
|
-
metadata: Optional metadata associated with the retriever.
|
|
441
|
-
This metadata will be associated with each call to this retriever,
|
|
442
|
-
and passed as arguments to the handlers defined in `callbacks`.
|
|
443
|
-
Defaults to None.
|
|
444
|
-
run_name: Optional name for the run. Defaults to None.
|
|
445
|
-
kwargs: Additional arguments to pass to the retriever.
|
|
446
|
-
|
|
447
|
-
Returns:
|
|
448
|
-
List of relevant documents.
|
|
449
|
-
"""
|
|
450
|
-
config: RunnableConfig = {}
|
|
451
|
-
if callbacks:
|
|
452
|
-
config["callbacks"] = callbacks
|
|
453
|
-
if tags:
|
|
454
|
-
config["tags"] = tags
|
|
455
|
-
if metadata:
|
|
456
|
-
config["metadata"] = metadata
|
|
457
|
-
if run_name:
|
|
458
|
-
config["run_name"] = run_name
|
|
459
|
-
return await self.ainvoke(query, config, **kwargs)
|