langchain-core 1.0.7__py3-none-any.whl → 1.2.1__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/callbacks/manager.py +14 -14
- langchain_core/callbacks/usage.py +1 -1
- langchain_core/indexing/api.py +2 -0
- langchain_core/language_models/__init__.py +15 -5
- langchain_core/language_models/_utils.py +1 -0
- langchain_core/language_models/chat_models.py +74 -94
- langchain_core/language_models/llms.py +5 -3
- langchain_core/language_models/model_profile.py +84 -0
- langchain_core/load/load.py +14 -1
- langchain_core/messages/ai.py +12 -4
- langchain_core/messages/base.py +6 -6
- langchain_core/messages/block_translators/anthropic.py +27 -8
- langchain_core/messages/block_translators/bedrock_converse.py +18 -8
- langchain_core/messages/block_translators/google_genai.py +25 -10
- langchain_core/messages/content.py +1 -1
- langchain_core/messages/tool.py +28 -27
- langchain_core/messages/utils.py +45 -18
- langchain_core/output_parsers/openai_tools.py +9 -7
- langchain_core/output_parsers/pydantic.py +1 -1
- langchain_core/output_parsers/string.py +27 -1
- langchain_core/prompts/chat.py +22 -17
- langchain_core/prompts/string.py +2 -59
- langchain_core/prompts/structured.py +7 -1
- langchain_core/runnables/base.py +174 -160
- langchain_core/runnables/branch.py +1 -1
- langchain_core/runnables/config.py +25 -20
- langchain_core/runnables/fallbacks.py +1 -2
- langchain_core/runnables/passthrough.py +2 -2
- langchain_core/tools/base.py +23 -4
- langchain_core/tools/convert.py +16 -0
- langchain_core/tools/retriever.py +29 -58
- langchain_core/tracers/event_stream.py +9 -4
- langchain_core/utils/aiter.py +3 -1
- langchain_core/utils/function_calling.py +7 -2
- langchain_core/utils/json_schema.py +29 -21
- langchain_core/utils/pydantic.py +7 -7
- langchain_core/utils/uuid.py +54 -0
- langchain_core/vectorstores/base.py +26 -18
- langchain_core/version.py +1 -1
- {langchain_core-1.0.7.dist-info → langchain_core-1.2.1.dist-info}/METADATA +2 -1
- {langchain_core-1.0.7.dist-info → langchain_core-1.2.1.dist-info}/RECORD +42 -40
- {langchain_core-1.0.7.dist-info → langchain_core-1.2.1.dist-info}/WHEEL +1 -1
langchain_core/tools/convert.py
CHANGED
|
@@ -23,6 +23,7 @@ def tool(
|
|
|
23
23
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
24
24
|
parse_docstring: bool = False,
|
|
25
25
|
error_on_invalid_docstring: bool = True,
|
|
26
|
+
extras: dict[str, Any] | None = None,
|
|
26
27
|
) -> Callable[[Callable | Runnable], BaseTool]: ...
|
|
27
28
|
|
|
28
29
|
|
|
@@ -38,6 +39,7 @@ def tool(
|
|
|
38
39
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
39
40
|
parse_docstring: bool = False,
|
|
40
41
|
error_on_invalid_docstring: bool = True,
|
|
42
|
+
extras: dict[str, Any] | None = None,
|
|
41
43
|
) -> BaseTool: ...
|
|
42
44
|
|
|
43
45
|
|
|
@@ -52,6 +54,7 @@ def tool(
|
|
|
52
54
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
53
55
|
parse_docstring: bool = False,
|
|
54
56
|
error_on_invalid_docstring: bool = True,
|
|
57
|
+
extras: dict[str, Any] | None = None,
|
|
55
58
|
) -> BaseTool: ...
|
|
56
59
|
|
|
57
60
|
|
|
@@ -66,6 +69,7 @@ def tool(
|
|
|
66
69
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
67
70
|
parse_docstring: bool = False,
|
|
68
71
|
error_on_invalid_docstring: bool = True,
|
|
72
|
+
extras: dict[str, Any] | None = None,
|
|
69
73
|
) -> Callable[[Callable | Runnable], BaseTool]: ...
|
|
70
74
|
|
|
71
75
|
|
|
@@ -80,6 +84,7 @@ def tool(
|
|
|
80
84
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
81
85
|
parse_docstring: bool = False,
|
|
82
86
|
error_on_invalid_docstring: bool = True,
|
|
87
|
+
extras: dict[str, Any] | None = None,
|
|
83
88
|
) -> BaseTool | Callable[[Callable | Runnable], BaseTool]:
|
|
84
89
|
"""Convert Python functions and `Runnables` to LangChain tools.
|
|
85
90
|
|
|
@@ -130,6 +135,15 @@ def tool(
|
|
|
130
135
|
parse parameter descriptions from Google Style function docstrings.
|
|
131
136
|
error_on_invalid_docstring: If `parse_docstring` is provided, configure
|
|
132
137
|
whether to raise `ValueError` on invalid Google Style docstrings.
|
|
138
|
+
extras: Optional provider-specific extra fields for the tool.
|
|
139
|
+
|
|
140
|
+
Used to pass configuration that doesn't fit into standard tool fields.
|
|
141
|
+
Chat models should process known extras when constructing model payloads.
|
|
142
|
+
|
|
143
|
+
!!! example
|
|
144
|
+
|
|
145
|
+
For example, Anthropic-specific fields like `cache_control`,
|
|
146
|
+
`defer_loading`, or `input_examples`.
|
|
133
147
|
|
|
134
148
|
Raises:
|
|
135
149
|
ValueError: If too many positional arguments are provided (e.g. violating the
|
|
@@ -292,6 +306,7 @@ def tool(
|
|
|
292
306
|
response_format=response_format,
|
|
293
307
|
parse_docstring=parse_docstring,
|
|
294
308
|
error_on_invalid_docstring=error_on_invalid_docstring,
|
|
309
|
+
extras=extras,
|
|
295
310
|
)
|
|
296
311
|
# If someone doesn't want a schema applied, we must treat it as
|
|
297
312
|
# a simple string->string function
|
|
@@ -308,6 +323,7 @@ def tool(
|
|
|
308
323
|
return_direct=return_direct,
|
|
309
324
|
coroutine=coroutine,
|
|
310
325
|
response_format=response_format,
|
|
326
|
+
extras=extras,
|
|
311
327
|
)
|
|
312
328
|
|
|
313
329
|
return _tool_factory
|
|
@@ -2,22 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from functools import partial
|
|
6
5
|
from typing import TYPE_CHECKING, Literal
|
|
7
6
|
|
|
8
7
|
from pydantic import BaseModel, Field
|
|
9
8
|
|
|
9
|
+
from langchain_core.callbacks import Callbacks
|
|
10
|
+
from langchain_core.documents import Document
|
|
10
11
|
from langchain_core.prompts import (
|
|
11
12
|
BasePromptTemplate,
|
|
12
13
|
PromptTemplate,
|
|
13
14
|
aformat_document,
|
|
14
15
|
format_document,
|
|
15
16
|
)
|
|
16
|
-
from langchain_core.tools.
|
|
17
|
+
from langchain_core.tools.structured import StructuredTool
|
|
17
18
|
|
|
18
19
|
if TYPE_CHECKING:
|
|
19
|
-
from langchain_core.callbacks import Callbacks
|
|
20
|
-
from langchain_core.documents import Document
|
|
21
20
|
from langchain_core.retrievers import BaseRetriever
|
|
22
21
|
|
|
23
22
|
|
|
@@ -27,43 +26,6 @@ class RetrieverInput(BaseModel):
|
|
|
27
26
|
query: str = Field(description="query to look up in retriever")
|
|
28
27
|
|
|
29
28
|
|
|
30
|
-
def _get_relevant_documents(
|
|
31
|
-
query: str,
|
|
32
|
-
retriever: BaseRetriever,
|
|
33
|
-
document_prompt: BasePromptTemplate,
|
|
34
|
-
document_separator: str,
|
|
35
|
-
callbacks: Callbacks = None,
|
|
36
|
-
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
37
|
-
) -> str | tuple[str, list[Document]]:
|
|
38
|
-
docs = retriever.invoke(query, config={"callbacks": callbacks})
|
|
39
|
-
content = document_separator.join(
|
|
40
|
-
format_document(doc, document_prompt) for doc in docs
|
|
41
|
-
)
|
|
42
|
-
if response_format == "content_and_artifact":
|
|
43
|
-
return (content, docs)
|
|
44
|
-
|
|
45
|
-
return content
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
async def _aget_relevant_documents(
|
|
49
|
-
query: str,
|
|
50
|
-
retriever: BaseRetriever,
|
|
51
|
-
document_prompt: BasePromptTemplate,
|
|
52
|
-
document_separator: str,
|
|
53
|
-
callbacks: Callbacks = None,
|
|
54
|
-
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
55
|
-
) -> str | tuple[str, list[Document]]:
|
|
56
|
-
docs = await retriever.ainvoke(query, config={"callbacks": callbacks})
|
|
57
|
-
content = document_separator.join(
|
|
58
|
-
[await aformat_document(doc, document_prompt) for doc in docs]
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
if response_format == "content_and_artifact":
|
|
62
|
-
return (content, docs)
|
|
63
|
-
|
|
64
|
-
return content
|
|
65
|
-
|
|
66
|
-
|
|
67
29
|
def create_retriever_tool(
|
|
68
30
|
retriever: BaseRetriever,
|
|
69
31
|
name: str,
|
|
@@ -72,7 +34,7 @@ def create_retriever_tool(
|
|
|
72
34
|
document_prompt: BasePromptTemplate | None = None,
|
|
73
35
|
document_separator: str = "\n\n",
|
|
74
36
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
75
|
-
) ->
|
|
37
|
+
) -> StructuredTool:
|
|
76
38
|
r"""Create a tool to do retrieval of documents.
|
|
77
39
|
|
|
78
40
|
Args:
|
|
@@ -93,22 +55,31 @@ def create_retriever_tool(
|
|
|
93
55
|
Returns:
|
|
94
56
|
Tool class to pass to an agent.
|
|
95
57
|
"""
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
58
|
+
document_prompt_ = document_prompt or PromptTemplate.from_template("{page_content}")
|
|
59
|
+
|
|
60
|
+
def func(
|
|
61
|
+
query: str, callbacks: Callbacks = None
|
|
62
|
+
) -> str | tuple[str, list[Document]]:
|
|
63
|
+
docs = retriever.invoke(query, config={"callbacks": callbacks})
|
|
64
|
+
content = document_separator.join(
|
|
65
|
+
format_document(doc, document_prompt_) for doc in docs
|
|
66
|
+
)
|
|
67
|
+
if response_format == "content_and_artifact":
|
|
68
|
+
return (content, docs)
|
|
69
|
+
return content
|
|
70
|
+
|
|
71
|
+
async def afunc(
|
|
72
|
+
query: str, callbacks: Callbacks = None
|
|
73
|
+
) -> str | tuple[str, list[Document]]:
|
|
74
|
+
docs = await retriever.ainvoke(query, config={"callbacks": callbacks})
|
|
75
|
+
content = document_separator.join(
|
|
76
|
+
[await aformat_document(doc, document_prompt_) for doc in docs]
|
|
77
|
+
)
|
|
78
|
+
if response_format == "content_and_artifact":
|
|
79
|
+
return (content, docs)
|
|
80
|
+
return content
|
|
81
|
+
|
|
82
|
+
return StructuredTool(
|
|
112
83
|
name=name,
|
|
113
84
|
description=description,
|
|
114
85
|
func=func,
|
|
@@ -12,7 +12,7 @@ from typing import (
|
|
|
12
12
|
TypeVar,
|
|
13
13
|
cast,
|
|
14
14
|
)
|
|
15
|
-
from uuid import UUID
|
|
15
|
+
from uuid import UUID
|
|
16
16
|
|
|
17
17
|
from typing_extensions import NotRequired, override
|
|
18
18
|
|
|
@@ -42,7 +42,8 @@ from langchain_core.tracers.log_stream import (
|
|
|
42
42
|
_astream_log_implementation,
|
|
43
43
|
)
|
|
44
44
|
from langchain_core.tracers.memory_stream import _MemoryStream
|
|
45
|
-
from langchain_core.utils.aiter import aclosing
|
|
45
|
+
from langchain_core.utils.aiter import aclosing
|
|
46
|
+
from langchain_core.utils.uuid import uuid7
|
|
46
47
|
|
|
47
48
|
if TYPE_CHECKING:
|
|
48
49
|
from collections.abc import AsyncIterator, Iterator, Sequence
|
|
@@ -188,7 +189,7 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
|
|
|
188
189
|
# atomic check and set
|
|
189
190
|
tap = self.is_tapped.setdefault(run_id, sentinel)
|
|
190
191
|
# wait for first chunk
|
|
191
|
-
first = await
|
|
192
|
+
first = await anext(output, sentinel)
|
|
192
193
|
if first is sentinel:
|
|
193
194
|
return
|
|
194
195
|
# get run info
|
|
@@ -1006,7 +1007,11 @@ async def _astream_events_implementation_v2(
|
|
|
1006
1007
|
|
|
1007
1008
|
# Assign the stream handler to the config
|
|
1008
1009
|
config = ensure_config(config)
|
|
1009
|
-
|
|
1010
|
+
if "run_id" in config:
|
|
1011
|
+
run_id = cast("UUID", config["run_id"])
|
|
1012
|
+
else:
|
|
1013
|
+
run_id = uuid7()
|
|
1014
|
+
config["run_id"] = run_id
|
|
1010
1015
|
callbacks = config.get("callbacks")
|
|
1011
1016
|
if callbacks is None:
|
|
1012
1017
|
config["callbacks"] = [event_streamer]
|
langchain_core/utils/aiter.py
CHANGED
|
@@ -26,13 +26,15 @@ from typing import (
|
|
|
26
26
|
|
|
27
27
|
from typing_extensions import override
|
|
28
28
|
|
|
29
|
+
from langchain_core._api.deprecation import deprecated
|
|
30
|
+
|
|
29
31
|
T = TypeVar("T")
|
|
30
32
|
|
|
31
33
|
_no_default = object()
|
|
32
34
|
|
|
33
35
|
|
|
34
36
|
# https://github.com/python/cpython/blob/main/Lib/test/test_asyncgen.py#L54
|
|
35
|
-
|
|
37
|
+
@deprecated(since="1.1.2", removal="2.0.0")
|
|
36
38
|
def py_anext(
|
|
37
39
|
iterator: AsyncIterator[T], default: T | Any = _no_default
|
|
38
40
|
) -> Awaitable[T | Any | None]:
|
|
@@ -8,6 +8,7 @@ import logging
|
|
|
8
8
|
import types
|
|
9
9
|
import typing
|
|
10
10
|
import uuid
|
|
11
|
+
from collections.abc import Mapping
|
|
11
12
|
from typing import (
|
|
12
13
|
TYPE_CHECKING,
|
|
13
14
|
Annotated,
|
|
@@ -327,7 +328,7 @@ def _format_tool_to_openai_function(tool: BaseTool) -> FunctionDescription:
|
|
|
327
328
|
|
|
328
329
|
|
|
329
330
|
def convert_to_openai_function(
|
|
330
|
-
function:
|
|
331
|
+
function: Mapping[str, Any] | type | Callable | BaseTool,
|
|
331
332
|
*,
|
|
332
333
|
strict: bool | None = None,
|
|
333
334
|
) -> dict[str, Any]:
|
|
@@ -353,6 +354,7 @@ def convert_to_openai_function(
|
|
|
353
354
|
ValueError: If function is not in a supported format.
|
|
354
355
|
|
|
355
356
|
!!! warning "Behavior changed in `langchain-core` 0.3.16"
|
|
357
|
+
|
|
356
358
|
`description` and `parameters` keys are now optional. Only `name` is
|
|
357
359
|
required and guaranteed to be part of the output.
|
|
358
360
|
"""
|
|
@@ -453,7 +455,7 @@ _WellKnownOpenAITools = (
|
|
|
453
455
|
|
|
454
456
|
|
|
455
457
|
def convert_to_openai_tool(
|
|
456
|
-
tool:
|
|
458
|
+
tool: Mapping[str, Any] | type[BaseModel] | Callable | BaseTool,
|
|
457
459
|
*,
|
|
458
460
|
strict: bool | None = None,
|
|
459
461
|
) -> dict[str, Any]:
|
|
@@ -477,15 +479,18 @@ def convert_to_openai_tool(
|
|
|
477
479
|
OpenAI tool-calling API.
|
|
478
480
|
|
|
479
481
|
!!! warning "Behavior changed in `langchain-core` 0.3.16"
|
|
482
|
+
|
|
480
483
|
`description` and `parameters` keys are now optional. Only `name` is
|
|
481
484
|
required and guaranteed to be part of the output.
|
|
482
485
|
|
|
483
486
|
!!! warning "Behavior changed in `langchain-core` 0.3.44"
|
|
487
|
+
|
|
484
488
|
Return OpenAI Responses API-style tools unchanged. This includes
|
|
485
489
|
any dict with `"type"` in `"file_search"`, `"function"`,
|
|
486
490
|
`"computer_use_preview"`, `"web_search_preview"`.
|
|
487
491
|
|
|
488
492
|
!!! warning "Behavior changed in `langchain-core` 0.3.63"
|
|
493
|
+
|
|
489
494
|
Added support for OpenAI's image generation built-in tool.
|
|
490
495
|
"""
|
|
491
496
|
# Import locally to prevent circular import
|
|
@@ -170,28 +170,33 @@ def dereference_refs(
|
|
|
170
170
|
full_schema: dict | None = None,
|
|
171
171
|
skip_keys: Sequence[str] | None = None,
|
|
172
172
|
) -> dict:
|
|
173
|
-
"""Resolve and inline JSON Schema
|
|
173
|
+
"""Resolve and inline JSON Schema `$ref` references in a schema object.
|
|
174
174
|
|
|
175
|
-
This function processes a JSON Schema and resolves all
|
|
176
|
-
them with the actual referenced content.
|
|
177
|
-
|
|
178
|
-
|
|
175
|
+
This function processes a JSON Schema and resolves all `$ref` references by
|
|
176
|
+
replacing them with the actual referenced content.
|
|
177
|
+
|
|
178
|
+
Handles both simple references and complex cases like circular references and mixed
|
|
179
|
+
`$ref` objects that contain additional properties alongside the `$ref`.
|
|
179
180
|
|
|
180
181
|
Args:
|
|
181
|
-
schema_obj: The JSON Schema object or fragment to process.
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
182
|
+
schema_obj: The JSON Schema object or fragment to process.
|
|
183
|
+
|
|
184
|
+
This can be a complete schema or just a portion of one.
|
|
185
|
+
full_schema: The complete schema containing all definitions that `$refs` might
|
|
186
|
+
point to.
|
|
187
|
+
|
|
188
|
+
If not provided, defaults to `schema_obj` (useful when the schema is
|
|
189
|
+
self-contained).
|
|
190
|
+
skip_keys: Controls recursion behavior and reference resolution depth.
|
|
191
|
+
|
|
192
|
+
- If `None` (Default): Only recurse under `'$defs'` and use shallow
|
|
193
|
+
reference resolution (break cycles but don't deep-inline nested refs)
|
|
194
|
+
- If provided (even as `[]`): Recurse under all keys and use deep reference
|
|
195
|
+
resolution (fully inline all nested references)
|
|
191
196
|
|
|
192
197
|
Returns:
|
|
193
|
-
A new dictionary with all $ref references resolved and inlined.
|
|
194
|
-
|
|
198
|
+
A new dictionary with all $ref references resolved and inlined.
|
|
199
|
+
The original `schema_obj` is not modified.
|
|
195
200
|
|
|
196
201
|
Examples:
|
|
197
202
|
Basic reference resolution:
|
|
@@ -203,7 +208,8 @@ def dereference_refs(
|
|
|
203
208
|
>>> result = dereference_refs(schema)
|
|
204
209
|
>>> result["properties"]["name"] # {"type": "string"}
|
|
205
210
|
|
|
206
|
-
Mixed
|
|
211
|
+
Mixed `$ref` with additional properties:
|
|
212
|
+
|
|
207
213
|
>>> schema = {
|
|
208
214
|
... "properties": {
|
|
209
215
|
... "name": {"$ref": "#/$defs/base", "description": "User name"}
|
|
@@ -215,6 +221,7 @@ def dereference_refs(
|
|
|
215
221
|
# {"type": "string", "minLength": 1, "description": "User name"}
|
|
216
222
|
|
|
217
223
|
Handling circular references:
|
|
224
|
+
|
|
218
225
|
>>> schema = {
|
|
219
226
|
... "properties": {"user": {"$ref": "#/$defs/User"}},
|
|
220
227
|
... "$defs": {
|
|
@@ -227,10 +234,11 @@ def dereference_refs(
|
|
|
227
234
|
>>> result = dereference_refs(schema) # Won't cause infinite recursion
|
|
228
235
|
|
|
229
236
|
!!! note
|
|
237
|
+
|
|
230
238
|
- Circular references are handled gracefully by breaking cycles
|
|
231
|
-
- Mixed
|
|
232
|
-
- Additional properties in mixed
|
|
233
|
-
- The
|
|
239
|
+
- Mixed `$ref` objects (with both `$ref` and other properties) are supported
|
|
240
|
+
- Additional properties in mixed `$refs` override resolved properties
|
|
241
|
+
- The `$defs` section is preserved in the output by default
|
|
234
242
|
"""
|
|
235
243
|
full = full_schema or schema_obj
|
|
236
244
|
keys_to_skip = list(skip_keys) if skip_keys is not None else ["$defs"]
|
langchain_core/utils/pydantic.py
CHANGED
|
@@ -88,18 +88,18 @@ def is_pydantic_v2_subclass(cls: type) -> bool:
|
|
|
88
88
|
"""Check if the given class is Pydantic v2-like.
|
|
89
89
|
|
|
90
90
|
Returns:
|
|
91
|
-
`True` if the given class is a subclass of Pydantic BaseModel 2.x.
|
|
91
|
+
`True` if the given class is a subclass of Pydantic `BaseModel` 2.x.
|
|
92
92
|
"""
|
|
93
93
|
return issubclass(cls, BaseModel)
|
|
94
94
|
|
|
95
95
|
|
|
96
96
|
def is_basemodel_subclass(cls: type) -> bool:
|
|
97
|
-
"""Check if the given class is a subclass of Pydantic BaseModel
|
|
97
|
+
"""Check if the given class is a subclass of Pydantic `BaseModel`.
|
|
98
98
|
|
|
99
99
|
Check if the given class is a subclass of any of the following:
|
|
100
100
|
|
|
101
|
-
* pydantic.BaseModel in Pydantic 2.x
|
|
102
|
-
* pydantic.v1.BaseModel in Pydantic 2.x
|
|
101
|
+
* `pydantic.BaseModel` in Pydantic 2.x
|
|
102
|
+
* `pydantic.v1.BaseModel` in Pydantic 2.x
|
|
103
103
|
|
|
104
104
|
Returns:
|
|
105
105
|
`True` if the given class is a subclass of Pydantic `BaseModel`.
|
|
@@ -112,12 +112,12 @@ def is_basemodel_subclass(cls: type) -> bool:
|
|
|
112
112
|
|
|
113
113
|
|
|
114
114
|
def is_basemodel_instance(obj: Any) -> bool:
|
|
115
|
-
"""Check if the given class is an instance of Pydantic BaseModel
|
|
115
|
+
"""Check if the given class is an instance of Pydantic `BaseModel`.
|
|
116
116
|
|
|
117
117
|
Check if the given class is an instance of any of the following:
|
|
118
118
|
|
|
119
|
-
* pydantic.BaseModel in Pydantic 2.x
|
|
120
|
-
* pydantic.v1.BaseModel in Pydantic 2.x
|
|
119
|
+
* `pydantic.BaseModel` in Pydantic 2.x
|
|
120
|
+
* `pydantic.v1.BaseModel` in Pydantic 2.x
|
|
121
121
|
|
|
122
122
|
Returns:
|
|
123
123
|
`True` if the given class is an instance of Pydantic `BaseModel`.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""UUID utility functions.
|
|
2
|
+
|
|
3
|
+
This module exports a uuid7 function to generate monotonic, time-ordered UUIDs
|
|
4
|
+
for tracing and similar operations.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import typing
|
|
10
|
+
from uuid import UUID
|
|
11
|
+
|
|
12
|
+
from uuid_utils.compat import uuid7 as _uuid_utils_uuid7
|
|
13
|
+
|
|
14
|
+
if typing.TYPE_CHECKING:
|
|
15
|
+
from uuid import UUID
|
|
16
|
+
|
|
17
|
+
_NANOS_PER_SECOND: typing.Final = 1_000_000_000
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _to_timestamp_and_nanos(nanoseconds: int) -> tuple[int, int]:
|
|
21
|
+
"""Split a nanosecond timestamp into seconds and remaining nanoseconds."""
|
|
22
|
+
seconds, nanos = divmod(nanoseconds, _NANOS_PER_SECOND)
|
|
23
|
+
return seconds, nanos
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def uuid7(nanoseconds: int | None = None) -> UUID:
|
|
27
|
+
"""Generate a UUID from a Unix timestamp in nanoseconds and random bits.
|
|
28
|
+
|
|
29
|
+
UUIDv7 objects feature monotonicity within a millisecond.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
nanoseconds: Optional ns timestamp. If not provided, uses current time.
|
|
33
|
+
"""
|
|
34
|
+
# --- 48 --- -- 4 -- --- 12 --- -- 2 -- --- 30 --- - 32 -
|
|
35
|
+
# unix_ts_ms | version | counter_hi | variant | counter_lo | random
|
|
36
|
+
#
|
|
37
|
+
# 'counter = counter_hi | counter_lo' is a 42-bit counter constructed
|
|
38
|
+
# with Method 1 of RFC 9562, §6.2, and its MSB is set to 0.
|
|
39
|
+
#
|
|
40
|
+
# 'random' is a 32-bit random value regenerated for every new UUID.
|
|
41
|
+
#
|
|
42
|
+
# If multiple UUIDs are generated within the same millisecond, the LSB
|
|
43
|
+
# of 'counter' is incremented by 1. When overflowing, the timestamp is
|
|
44
|
+
# advanced and the counter is reset to a random 42-bit integer with MSB
|
|
45
|
+
# set to 0.
|
|
46
|
+
|
|
47
|
+
# For now, just delegate to the uuid_utils implementation
|
|
48
|
+
if nanoseconds is None:
|
|
49
|
+
return _uuid_utils_uuid7()
|
|
50
|
+
seconds, nanos = _to_timestamp_and_nanos(nanoseconds)
|
|
51
|
+
return _uuid_utils_uuid7(timestamp=seconds, nanos=nanos)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
__all__ = ["uuid7"]
|
|
@@ -294,8 +294,9 @@ class VectorStore(ABC):
|
|
|
294
294
|
|
|
295
295
|
Args:
|
|
296
296
|
query: Input text.
|
|
297
|
-
search_type: Type of search to perform.
|
|
298
|
-
|
|
297
|
+
search_type: Type of search to perform.
|
|
298
|
+
|
|
299
|
+
Can be `'similarity'`, `'mmr'`, or `'similarity_score_threshold'`.
|
|
299
300
|
**kwargs: Arguments to pass to the search method.
|
|
300
301
|
|
|
301
302
|
Returns:
|
|
@@ -328,8 +329,9 @@ class VectorStore(ABC):
|
|
|
328
329
|
|
|
329
330
|
Args:
|
|
330
331
|
query: Input text.
|
|
331
|
-
search_type: Type of search to perform.
|
|
332
|
-
|
|
332
|
+
search_type: Type of search to perform.
|
|
333
|
+
|
|
334
|
+
Can be `'similarity'`, `'mmr'`, or `'similarity_score_threshold'`.
|
|
333
335
|
**kwargs: Arguments to pass to the search method.
|
|
334
336
|
|
|
335
337
|
Returns:
|
|
@@ -460,9 +462,10 @@ class VectorStore(ABC):
|
|
|
460
462
|
Args:
|
|
461
463
|
query: Input text.
|
|
462
464
|
k: Number of `Document` objects to return.
|
|
463
|
-
**kwargs:
|
|
464
|
-
|
|
465
|
-
|
|
465
|
+
**kwargs: Kwargs to be passed to similarity search.
|
|
466
|
+
|
|
467
|
+
Should include `score_threshold`, an optional floating point value
|
|
468
|
+
between `0` to `1` to filter the resulting set of retrieved docs.
|
|
466
469
|
|
|
467
470
|
Returns:
|
|
468
471
|
List of tuples of `(doc, similarity_score)`
|
|
@@ -487,9 +490,10 @@ class VectorStore(ABC):
|
|
|
487
490
|
Args:
|
|
488
491
|
query: Input text.
|
|
489
492
|
k: Number of `Document` objects to return.
|
|
490
|
-
**kwargs:
|
|
491
|
-
|
|
492
|
-
|
|
493
|
+
**kwargs: Kwargs to be passed to similarity search.
|
|
494
|
+
|
|
495
|
+
Should include `score_threshold`, an optional floating point value
|
|
496
|
+
between `0` to `1` to filter the resulting set of retrieved docs.
|
|
493
497
|
|
|
494
498
|
Returns:
|
|
495
499
|
List of tuples of `(doc, similarity_score)`
|
|
@@ -511,9 +515,10 @@ class VectorStore(ABC):
|
|
|
511
515
|
Args:
|
|
512
516
|
query: Input text.
|
|
513
517
|
k: Number of `Document` objects to return.
|
|
514
|
-
**kwargs:
|
|
515
|
-
|
|
516
|
-
|
|
518
|
+
**kwargs: Kwargs to be passed to similarity search.
|
|
519
|
+
|
|
520
|
+
Should include `score_threshold`, an optional floating point value
|
|
521
|
+
between `0` to `1` to filter the resulting set of retrieved docs.
|
|
517
522
|
|
|
518
523
|
Returns:
|
|
519
524
|
List of tuples of `(doc, similarity_score)`.
|
|
@@ -560,9 +565,10 @@ class VectorStore(ABC):
|
|
|
560
565
|
Args:
|
|
561
566
|
query: Input text.
|
|
562
567
|
k: Number of `Document` objects to return.
|
|
563
|
-
**kwargs:
|
|
564
|
-
|
|
565
|
-
|
|
568
|
+
**kwargs: Kwargs to be passed to similarity search.
|
|
569
|
+
|
|
570
|
+
Should include `score_threshold`, an optional floating point value
|
|
571
|
+
between `0` to `1` to filter the resulting set of retrieved docs.
|
|
566
572
|
|
|
567
573
|
Returns:
|
|
568
574
|
List of tuples of `(doc, similarity_score)`
|
|
@@ -900,13 +906,15 @@ class VectorStore(ABC):
|
|
|
900
906
|
|
|
901
907
|
Args:
|
|
902
908
|
**kwargs: Keyword arguments to pass to the search function.
|
|
909
|
+
|
|
903
910
|
Can include:
|
|
904
911
|
|
|
905
912
|
* `search_type`: Defines the type of search that the Retriever should
|
|
906
913
|
perform. Can be `'similarity'` (default), `'mmr'`, or
|
|
907
914
|
`'similarity_score_threshold'`.
|
|
908
|
-
* `search_kwargs`: Keyword arguments to pass to the search function.
|
|
909
|
-
|
|
915
|
+
* `search_kwargs`: Keyword arguments to pass to the search function.
|
|
916
|
+
|
|
917
|
+
Can include things like:
|
|
910
918
|
|
|
911
919
|
* `k`: Amount of documents to return (Default: `4`)
|
|
912
920
|
* `score_threshold`: Minimum relevance threshold
|
langchain_core/version.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: langchain-core
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.1
|
|
4
4
|
Summary: Building applications with LLMs through composability
|
|
5
5
|
Project-URL: Homepage, https://docs.langchain.com/
|
|
6
6
|
Project-URL: Documentation, https://reference.langchain.com/python/langchain_core/
|
|
@@ -18,6 +18,7 @@ Requires-Dist: pydantic<3.0.0,>=2.7.4
|
|
|
18
18
|
Requires-Dist: pyyaml<7.0.0,>=5.3.0
|
|
19
19
|
Requires-Dist: tenacity!=8.4.0,<10.0.0,>=8.1.0
|
|
20
20
|
Requires-Dist: typing-extensions<5.0.0,>=4.7.0
|
|
21
|
+
Requires-Dist: uuid-utils<1.0,>=0.12.0
|
|
21
22
|
Description-Content-Type: text/markdown
|
|
22
23
|
|
|
23
24
|
# 🦜🍎️ LangChain Core
|