langchain-core 0.3.79__py3-none-any.whl → 1.0.0__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 +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +52 -65
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +19 -19
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +323 -334
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +441 -507
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +48 -63
- langchain_core/document_loaders/base.py +23 -23
- langchain_core/document_loaders/langsmith.py +37 -37
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +62 -65
- langchain_core/documents/compressor.py +4 -4
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +10 -11
- langchain_core/globals.py +3 -151
- langchain_core/indexing/api.py +61 -66
- langchain_core/indexing/base.py +58 -58
- langchain_core/indexing/in_memory.py +3 -3
- langchain_core/language_models/__init__.py +14 -27
- langchain_core/language_models/_utils.py +270 -84
- langchain_core/language_models/base.py +55 -162
- langchain_core/language_models/chat_models.py +442 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +61 -39
- langchain_core/language_models/llms.py +123 -231
- langchain_core/load/dump.py +4 -5
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +39 -40
- langchain_core/messages/__init__.py +61 -22
- langchain_core/messages/ai.py +368 -163
- langchain_core/messages/base.py +214 -43
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +41 -38
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +38 -28
- langchain_core/messages/tool.py +96 -103
- langchain_core/messages/utils.py +478 -504
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +58 -61
- langchain_core/output_parsers/json.py +7 -8
- langchain_core/output_parsers/list.py +5 -7
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +14 -19
- langchain_core/output_parsers/pydantic.py +12 -13
- langchain_core/output_parsers/string.py +2 -2
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +8 -10
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +8 -8
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +12 -12
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +45 -55
- langchain_core/prompts/chat.py +254 -313
- langchain_core/prompts/dict.py +5 -5
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +4 -4
- langchain_core/prompts/structured.py +42 -50
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +49 -190
- langchain_core/runnables/base.py +1484 -1709
- langchain_core/runnables/branch.py +45 -61
- langchain_core/runnables/config.py +80 -88
- langchain_core/runnables/configurable.py +117 -134
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +85 -95
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +38 -50
- langchain_core/runnables/graph_png.py +15 -16
- langchain_core/runnables/history.py +135 -148
- langchain_core/runnables/passthrough.py +124 -150
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +25 -30
- langchain_core/runnables/schema.py +79 -74
- langchain_core/runnables/utils.py +62 -68
- langchain_core/stores.py +81 -115
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +179 -187
- langchain_core/tools/convert.py +131 -139
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +11 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +30 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +50 -60
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +115 -70
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +43 -43
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +46 -8
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -191
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +37 -42
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +8 -8
- langchain_core/utils/mustache.py +9 -11
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +80 -54
- langchain_core/vectorstores/base.py +129 -164
- langchain_core/vectorstores/in_memory.py +99 -174
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -447
- langchain_core/memory.py +0 -120
- langchain_core/messages/content_blocks.py +0 -176
- langchain_core/prompts/pipeline.py +0 -138
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-0.3.79.dist-info/RECORD +0 -174
- langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
langchain_core/tools/convert.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""Convert functions and runnables to tools."""
|
|
2
2
|
|
|
3
3
|
import inspect
|
|
4
|
-
from
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from typing import Any, Literal, get_type_hints, overload
|
|
5
6
|
|
|
6
7
|
from pydantic import BaseModel, Field, create_model
|
|
7
8
|
|
|
@@ -15,14 +16,14 @@ from langchain_core.tools.structured import StructuredTool
|
|
|
15
16
|
@overload
|
|
16
17
|
def tool(
|
|
17
18
|
*,
|
|
18
|
-
description:
|
|
19
|
+
description: str | None = None,
|
|
19
20
|
return_direct: bool = False,
|
|
20
|
-
args_schema:
|
|
21
|
+
args_schema: ArgsSchema | None = None,
|
|
21
22
|
infer_schema: bool = True,
|
|
22
23
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
23
24
|
parse_docstring: bool = False,
|
|
24
25
|
error_on_invalid_docstring: bool = True,
|
|
25
|
-
) -> Callable[[
|
|
26
|
+
) -> Callable[[Callable | Runnable], BaseTool]: ...
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
@overload
|
|
@@ -30,9 +31,9 @@ def tool(
|
|
|
30
31
|
name_or_callable: str,
|
|
31
32
|
runnable: Runnable,
|
|
32
33
|
*,
|
|
33
|
-
description:
|
|
34
|
+
description: str | None = None,
|
|
34
35
|
return_direct: bool = False,
|
|
35
|
-
args_schema:
|
|
36
|
+
args_schema: ArgsSchema | None = None,
|
|
36
37
|
infer_schema: bool = True,
|
|
37
38
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
38
39
|
parse_docstring: bool = False,
|
|
@@ -44,9 +45,9 @@ def tool(
|
|
|
44
45
|
def tool(
|
|
45
46
|
name_or_callable: Callable,
|
|
46
47
|
*,
|
|
47
|
-
description:
|
|
48
|
+
description: str | None = None,
|
|
48
49
|
return_direct: bool = False,
|
|
49
|
-
args_schema:
|
|
50
|
+
args_schema: ArgsSchema | None = None,
|
|
50
51
|
infer_schema: bool = True,
|
|
51
52
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
52
53
|
parse_docstring: bool = False,
|
|
@@ -58,32 +59,29 @@ def tool(
|
|
|
58
59
|
def tool(
|
|
59
60
|
name_or_callable: str,
|
|
60
61
|
*,
|
|
61
|
-
description:
|
|
62
|
+
description: str | None = None,
|
|
62
63
|
return_direct: bool = False,
|
|
63
|
-
args_schema:
|
|
64
|
+
args_schema: ArgsSchema | None = None,
|
|
64
65
|
infer_schema: bool = True,
|
|
65
66
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
66
67
|
parse_docstring: bool = False,
|
|
67
68
|
error_on_invalid_docstring: bool = True,
|
|
68
|
-
) -> Callable[[
|
|
69
|
+
) -> Callable[[Callable | Runnable], BaseTool]: ...
|
|
69
70
|
|
|
70
71
|
|
|
71
72
|
def tool(
|
|
72
|
-
name_or_callable:
|
|
73
|
-
runnable:
|
|
73
|
+
name_or_callable: str | Callable | None = None,
|
|
74
|
+
runnable: Runnable | None = None,
|
|
74
75
|
*args: Any,
|
|
75
|
-
description:
|
|
76
|
+
description: str | None = None,
|
|
76
77
|
return_direct: bool = False,
|
|
77
|
-
args_schema:
|
|
78
|
+
args_schema: ArgsSchema | None = None,
|
|
78
79
|
infer_schema: bool = True,
|
|
79
80
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
80
81
|
parse_docstring: bool = False,
|
|
81
82
|
error_on_invalid_docstring: bool = True,
|
|
82
|
-
) ->
|
|
83
|
-
|
|
84
|
-
Callable[[Union[Callable, Runnable]], BaseTool],
|
|
85
|
-
]:
|
|
86
|
-
"""Make tools out of functions, can be used with or without arguments.
|
|
83
|
+
) -> BaseTool | Callable[[Callable | Runnable], BaseTool]:
|
|
84
|
+
"""Make tools out of Python functions, can be used with or without arguments.
|
|
87
85
|
|
|
88
86
|
Args:
|
|
89
87
|
name_or_callable: Optional name of the tool or the callable to be
|
|
@@ -93,145 +91,141 @@ def tool(
|
|
|
93
91
|
description: Optional description for the tool.
|
|
94
92
|
Precedence for the tool description value is as follows:
|
|
95
93
|
|
|
96
|
-
-
|
|
97
|
-
(used even if docstring and/or
|
|
98
|
-
-
|
|
99
|
-
(used even if
|
|
100
|
-
-
|
|
94
|
+
- `description` argument
|
|
95
|
+
(used even if docstring and/or `args_schema` are provided)
|
|
96
|
+
- Tool function docstring
|
|
97
|
+
(used even if `args_schema` is provided)
|
|
98
|
+
- `args_schema` description
|
|
101
99
|
(used only if `description` / docstring are not provided)
|
|
102
100
|
*args: Extra positional arguments. Must be empty.
|
|
103
101
|
return_direct: Whether to return directly from the tool rather
|
|
104
|
-
than continuing the agent loop.
|
|
105
|
-
args_schema:
|
|
106
|
-
|
|
102
|
+
than continuing the agent loop.
|
|
103
|
+
args_schema: Optional argument schema for user to specify.
|
|
104
|
+
|
|
107
105
|
infer_schema: Whether to infer the schema of the arguments from
|
|
108
106
|
the function's signature. This also makes the resultant tool
|
|
109
107
|
accept a dictionary input to its `run()` function.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
the
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
Defaults to "content".
|
|
116
|
-
parse_docstring: if ``infer_schema`` and ``parse_docstring``, will attempt to
|
|
108
|
+
response_format: The tool response format. If `"content"` then the output of
|
|
109
|
+
the tool is interpreted as the contents of a `ToolMessage`. If
|
|
110
|
+
`"content_and_artifact"` then the output is expected to be a two-tuple
|
|
111
|
+
corresponding to the `(content, artifact)` of a `ToolMessage`.
|
|
112
|
+
parse_docstring: if `infer_schema` and `parse_docstring`, will attempt to
|
|
117
113
|
parse parameter descriptions from Google Style function docstrings.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
whether to raise ValueError on invalid Google Style docstrings.
|
|
121
|
-
Defaults to True.
|
|
114
|
+
error_on_invalid_docstring: if `parse_docstring` is provided, configure
|
|
115
|
+
whether to raise `ValueError` on invalid Google Style docstrings.
|
|
122
116
|
|
|
123
117
|
Raises:
|
|
124
118
|
ValueError: If too many positional arguments are provided.
|
|
125
119
|
ValueError: If a runnable is provided without a string name.
|
|
126
120
|
ValueError: If the first argument is not a string or callable with
|
|
127
|
-
a
|
|
121
|
+
a `__name__` attribute.
|
|
128
122
|
ValueError: If the function does not have a docstring and description
|
|
129
|
-
is not provided and
|
|
130
|
-
ValueError: If
|
|
131
|
-
Google-style docstring and
|
|
123
|
+
is not provided and `infer_schema` is `False`.
|
|
124
|
+
ValueError: If `parse_docstring` is `True` and the function has an invalid
|
|
125
|
+
Google-style docstring and `error_on_invalid_docstring` is True.
|
|
132
126
|
ValueError: If a Runnable is provided that does not have an object schema.
|
|
133
127
|
|
|
134
128
|
Returns:
|
|
135
129
|
The tool.
|
|
136
130
|
|
|
137
131
|
Requires:
|
|
138
|
-
- Function must be of type (str) -> str
|
|
132
|
+
- Function must be of type `(str) -> str`
|
|
139
133
|
- Function must have a docstring
|
|
140
134
|
|
|
141
135
|
Examples:
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return
|
|
136
|
+
```python
|
|
137
|
+
@tool
|
|
138
|
+
def search_api(query: str) -> str:
|
|
139
|
+
# Searches the API for the query.
|
|
140
|
+
return
|
|
148
141
|
|
|
149
142
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
143
|
+
@tool("search", return_direct=True)
|
|
144
|
+
def search_api(query: str) -> str:
|
|
145
|
+
# Searches the API for the query.
|
|
146
|
+
return
|
|
154
147
|
|
|
155
148
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
149
|
+
@tool(response_format="content_and_artifact")
|
|
150
|
+
def search_api(query: str) -> tuple[str, dict]:
|
|
151
|
+
return "partial json of results", {"full": "object of results"}
|
|
152
|
+
```
|
|
159
153
|
|
|
160
|
-
|
|
154
|
+
!!! version-added "Added in version 0.2.14"
|
|
161
155
|
|
|
162
156
|
Parse Google-style docstrings:
|
|
163
157
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
"
|
|
185
|
-
"
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
"type": "string",
|
|
189
|
-
},
|
|
190
|
-
"baz": {
|
|
191
|
-
"title": "Baz",
|
|
192
|
-
"description": "The baz.",
|
|
193
|
-
"type": "integer",
|
|
194
|
-
},
|
|
158
|
+
```python
|
|
159
|
+
@tool(parse_docstring=True)
|
|
160
|
+
def foo(bar: str, baz: int) -> str:
|
|
161
|
+
\"\"\"The foo.
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
bar: The bar.
|
|
165
|
+
baz: The baz.
|
|
166
|
+
\"\"\"
|
|
167
|
+
return bar
|
|
168
|
+
|
|
169
|
+
foo.args_schema.model_json_schema()
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
{
|
|
174
|
+
"title": "foo",
|
|
175
|
+
"description": "The foo.",
|
|
176
|
+
"type": "object",
|
|
177
|
+
"properties": {
|
|
178
|
+
"bar": {
|
|
179
|
+
"title": "Bar",
|
|
180
|
+
"description": "The bar.",
|
|
181
|
+
"type": "string",
|
|
195
182
|
},
|
|
196
|
-
"
|
|
197
|
-
|
|
183
|
+
"baz": {
|
|
184
|
+
"title": "Baz",
|
|
185
|
+
"description": "The baz.",
|
|
186
|
+
"type": "integer",
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
"required": ["bar", "baz"],
|
|
190
|
+
}
|
|
191
|
+
```
|
|
198
192
|
|
|
199
|
-
Note that parsing by default will raise
|
|
193
|
+
Note that parsing by default will raise `ValueError` if the docstring
|
|
200
194
|
is considered invalid. A docstring is considered invalid if it contains
|
|
201
195
|
arguments not in the function signature, or is unable to be parsed into
|
|
202
|
-
a summary and "Args:" blocks. Examples below:
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
196
|
+
a summary and `"Args:"` blocks. Examples below:
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
# No args section
|
|
200
|
+
def invalid_docstring_1(bar: str, baz: int) -> str:
|
|
201
|
+
\"\"\"The foo.\"\"\"
|
|
202
|
+
return bar
|
|
203
|
+
|
|
204
|
+
# Improper whitespace between summary and args section
|
|
205
|
+
def invalid_docstring_2(bar: str, baz: int) -> str:
|
|
206
|
+
\"\"\"The foo.
|
|
207
|
+
Args:
|
|
208
|
+
bar: The bar.
|
|
209
|
+
baz: The baz.
|
|
210
|
+
\"\"\"
|
|
211
|
+
return bar
|
|
212
|
+
|
|
213
|
+
# Documented args absent from function signature
|
|
214
|
+
def invalid_docstring_3(bar: str, baz: int) -> str:
|
|
215
|
+
\"\"\"The foo.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
banana: The bar.
|
|
219
|
+
monkey: The baz.
|
|
220
|
+
\"\"\"
|
|
221
|
+
return bar
|
|
222
|
+
|
|
223
|
+
```
|
|
230
224
|
""" # noqa: D214, D410, D411 # We're intentionally showing bad formatting in examples
|
|
231
225
|
|
|
232
226
|
def _create_tool_factory(
|
|
233
227
|
tool_name: str,
|
|
234
|
-
) -> Callable[[
|
|
228
|
+
) -> Callable[[Callable | Runnable], BaseTool]:
|
|
235
229
|
"""Create a decorator that takes a callable and returns a tool.
|
|
236
230
|
|
|
237
231
|
Args:
|
|
@@ -241,7 +235,7 @@ def tool(
|
|
|
241
235
|
A function that takes a callable or Runnable and returns a tool.
|
|
242
236
|
"""
|
|
243
237
|
|
|
244
|
-
def _tool_factory(dec_func:
|
|
238
|
+
def _tool_factory(dec_func: Callable | Runnable) -> BaseTool:
|
|
245
239
|
tool_description = description
|
|
246
240
|
if isinstance(dec_func, Runnable):
|
|
247
241
|
runnable = dec_func
|
|
@@ -251,18 +245,18 @@ def tool(
|
|
|
251
245
|
raise ValueError(msg)
|
|
252
246
|
|
|
253
247
|
async def ainvoke_wrapper(
|
|
254
|
-
callbacks:
|
|
248
|
+
callbacks: Callbacks | None = None, **kwargs: Any
|
|
255
249
|
) -> Any:
|
|
256
250
|
return await runnable.ainvoke(kwargs, {"callbacks": callbacks})
|
|
257
251
|
|
|
258
252
|
def invoke_wrapper(
|
|
259
|
-
callbacks:
|
|
253
|
+
callbacks: Callbacks | None = None, **kwargs: Any
|
|
260
254
|
) -> Any:
|
|
261
255
|
return runnable.invoke(kwargs, {"callbacks": callbacks})
|
|
262
256
|
|
|
263
257
|
coroutine = ainvoke_wrapper
|
|
264
258
|
func = invoke_wrapper
|
|
265
|
-
schema:
|
|
259
|
+
schema: ArgsSchema | None = runnable.input_schema
|
|
266
260
|
tool_description = description or repr(runnable)
|
|
267
261
|
elif inspect.iscoroutinefunction(dec_func):
|
|
268
262
|
coroutine = dec_func
|
|
@@ -352,7 +346,7 @@ def tool(
|
|
|
352
346
|
# @tool(parse_docstring=True)
|
|
353
347
|
# def my_tool():
|
|
354
348
|
# pass
|
|
355
|
-
def _partial(func:
|
|
349
|
+
def _partial(func: Callable | Runnable) -> BaseTool:
|
|
356
350
|
"""Partial function that takes a callable and returns a tool."""
|
|
357
351
|
name_ = func.get_name() if isinstance(func, Runnable) else func.__name__
|
|
358
352
|
tool_factory = _create_tool_factory(name_)
|
|
@@ -370,7 +364,7 @@ def _get_description_from_runnable(runnable: Runnable) -> str:
|
|
|
370
364
|
def _get_schema_from_runnable_and_arg_types(
|
|
371
365
|
runnable: Runnable,
|
|
372
366
|
name: str,
|
|
373
|
-
arg_types:
|
|
367
|
+
arg_types: dict[str, type] | None = None,
|
|
374
368
|
) -> type[BaseModel]:
|
|
375
369
|
"""Infer args_schema for tool."""
|
|
376
370
|
if arg_types is None:
|
|
@@ -389,20 +383,20 @@ def _get_schema_from_runnable_and_arg_types(
|
|
|
389
383
|
|
|
390
384
|
def convert_runnable_to_tool(
|
|
391
385
|
runnable: Runnable,
|
|
392
|
-
args_schema:
|
|
386
|
+
args_schema: type[BaseModel] | None = None,
|
|
393
387
|
*,
|
|
394
|
-
name:
|
|
395
|
-
description:
|
|
396
|
-
arg_types:
|
|
388
|
+
name: str | None = None,
|
|
389
|
+
description: str | None = None,
|
|
390
|
+
arg_types: dict[str, type] | None = None,
|
|
397
391
|
) -> BaseTool:
|
|
398
392
|
"""Convert a Runnable into a BaseTool.
|
|
399
393
|
|
|
400
394
|
Args:
|
|
401
395
|
runnable: The runnable to convert.
|
|
402
|
-
args_schema: The schema for the tool's input arguments.
|
|
403
|
-
name: The name of the tool.
|
|
404
|
-
description: The description of the tool.
|
|
405
|
-
arg_types: The types of the arguments.
|
|
396
|
+
args_schema: The schema for the tool's input arguments.
|
|
397
|
+
name: The name of the tool.
|
|
398
|
+
description: The description of the tool.
|
|
399
|
+
arg_types: The types of the arguments.
|
|
406
400
|
|
|
407
401
|
Returns:
|
|
408
402
|
The tool.
|
|
@@ -421,12 +415,10 @@ def convert_runnable_to_tool(
|
|
|
421
415
|
description=description,
|
|
422
416
|
)
|
|
423
417
|
|
|
424
|
-
async def ainvoke_wrapper(
|
|
425
|
-
callbacks: Optional[Callbacks] = None, **kwargs: Any
|
|
426
|
-
) -> Any:
|
|
418
|
+
async def ainvoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
|
|
427
419
|
return await runnable.ainvoke(kwargs, config={"callbacks": callbacks})
|
|
428
420
|
|
|
429
|
-
def invoke_wrapper(callbacks:
|
|
421
|
+
def invoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
|
|
430
422
|
return runnable.invoke(kwargs, config={"callbacks": callbacks})
|
|
431
423
|
|
|
432
424
|
if (
|
langchain_core/tools/render.py
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
from collections.abc import Callable
|
|
5
6
|
from inspect import signature
|
|
6
|
-
from typing import Callable
|
|
7
7
|
|
|
8
8
|
from langchain_core.tools.base import BaseTool
|
|
9
9
|
|
|
@@ -21,10 +21,10 @@ def render_text_description(tools: list[BaseTool]) -> str:
|
|
|
21
21
|
|
|
22
22
|
Output will be in the format of:
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
```txt
|
|
25
|
+
search: This tool is used for search
|
|
26
|
+
calculator: This tool is used for math
|
|
27
|
+
```
|
|
28
28
|
"""
|
|
29
29
|
descriptions = []
|
|
30
30
|
for tool in tools:
|
|
@@ -49,11 +49,11 @@ def render_text_description_and_args(tools: list[BaseTool]) -> str:
|
|
|
49
49
|
|
|
50
50
|
Output will be in the format of:
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
```txt
|
|
53
|
+
search: This tool is used for search, args: {"query": {"type": "string"}}
|
|
54
|
+
calculator: This tool is used for math, \
|
|
55
|
+
args: {"expression": {"type": "string"}}
|
|
56
|
+
```
|
|
57
57
|
"""
|
|
58
58
|
tool_strings = []
|
|
59
59
|
for tool in tools:
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from functools import partial
|
|
6
|
-
from typing import TYPE_CHECKING, Literal
|
|
6
|
+
from typing import TYPE_CHECKING, Literal
|
|
7
7
|
|
|
8
8
|
from pydantic import BaseModel, Field
|
|
9
9
|
|
|
@@ -34,7 +34,7 @@ def _get_relevant_documents(
|
|
|
34
34
|
document_separator: str,
|
|
35
35
|
callbacks: Callbacks = None,
|
|
36
36
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
37
|
-
) ->
|
|
37
|
+
) -> str | tuple[str, list[Document]]:
|
|
38
38
|
docs = retriever.invoke(query, config={"callbacks": callbacks})
|
|
39
39
|
content = document_separator.join(
|
|
40
40
|
format_document(doc, document_prompt) for doc in docs
|
|
@@ -52,7 +52,7 @@ async def _aget_relevant_documents(
|
|
|
52
52
|
document_separator: str,
|
|
53
53
|
callbacks: Callbacks = None,
|
|
54
54
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
55
|
-
) ->
|
|
55
|
+
) -> str | tuple[str, list[Document]]:
|
|
56
56
|
docs = await retriever.ainvoke(query, config={"callbacks": callbacks})
|
|
57
57
|
content = document_separator.join(
|
|
58
58
|
[await aformat_document(doc, document_prompt) for doc in docs]
|
|
@@ -69,7 +69,7 @@ def create_retriever_tool(
|
|
|
69
69
|
name: str,
|
|
70
70
|
description: str,
|
|
71
71
|
*,
|
|
72
|
-
document_prompt:
|
|
72
|
+
document_prompt: BasePromptTemplate | None = None,
|
|
73
73
|
document_separator: str = "\n\n",
|
|
74
74
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
75
75
|
) -> Tool:
|
|
@@ -81,13 +81,13 @@ def create_retriever_tool(
|
|
|
81
81
|
so should be unique and somewhat descriptive.
|
|
82
82
|
description: The description for the tool. This will be passed to the language
|
|
83
83
|
model, so should be descriptive.
|
|
84
|
-
document_prompt: The prompt to use for the document.
|
|
85
|
-
document_separator: The separator to use between documents.
|
|
86
|
-
response_format: The tool response format. If "content" then the output of
|
|
87
|
-
the tool is interpreted as the contents of a ToolMessage
|
|
88
|
-
"content_and_artifact" then the output is expected to be a two-tuple
|
|
89
|
-
corresponding to the (content, artifact) of a ToolMessage (artifact
|
|
90
|
-
being a list of documents in this case).
|
|
84
|
+
document_prompt: The prompt to use for the document.
|
|
85
|
+
document_separator: The separator to use between documents.
|
|
86
|
+
response_format: The tool response format. If `"content"` then the output of
|
|
87
|
+
the tool is interpreted as the contents of a `ToolMessage`. If
|
|
88
|
+
`"content_and_artifact"` then the output is expected to be a two-tuple
|
|
89
|
+
corresponding to the `(content, artifact)` of a `ToolMessage` (artifact
|
|
90
|
+
being a list of documents in this case).
|
|
91
91
|
|
|
92
92
|
Returns:
|
|
93
93
|
Tool class to pass to an agent.
|
langchain_core/tools/simple.py
CHANGED
|
@@ -2,14 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from collections.abc import Awaitable
|
|
5
|
+
from collections.abc import Awaitable, Callable
|
|
6
6
|
from inspect import signature
|
|
7
7
|
from typing import (
|
|
8
8
|
TYPE_CHECKING,
|
|
9
9
|
Any,
|
|
10
|
-
Callable,
|
|
11
|
-
Optional,
|
|
12
|
-
Union,
|
|
13
10
|
)
|
|
14
11
|
|
|
15
12
|
from typing_extensions import override
|
|
@@ -34,9 +31,9 @@ class Tool(BaseTool):
|
|
|
34
31
|
"""Tool that takes in function or coroutine directly."""
|
|
35
32
|
|
|
36
33
|
description: str = ""
|
|
37
|
-
func:
|
|
34
|
+
func: Callable[..., str] | None
|
|
38
35
|
"""The function to run when the tool is called."""
|
|
39
|
-
coroutine:
|
|
36
|
+
coroutine: Callable[..., Awaitable[str]] | None = None
|
|
40
37
|
"""The asynchronous version of the function."""
|
|
41
38
|
|
|
42
39
|
# --- Runnable ---
|
|
@@ -44,8 +41,8 @@ class Tool(BaseTool):
|
|
|
44
41
|
@override
|
|
45
42
|
async def ainvoke(
|
|
46
43
|
self,
|
|
47
|
-
input:
|
|
48
|
-
config:
|
|
44
|
+
input: str | dict | ToolCall,
|
|
45
|
+
config: RunnableConfig | None = None,
|
|
49
46
|
**kwargs: Any,
|
|
50
47
|
) -> Any:
|
|
51
48
|
if not self.coroutine:
|
|
@@ -70,9 +67,9 @@ class Tool(BaseTool):
|
|
|
70
67
|
return {"tool_input": {"type": "string"}}
|
|
71
68
|
|
|
72
69
|
def _to_args_and_kwargs(
|
|
73
|
-
self, tool_input:
|
|
70
|
+
self, tool_input: str | dict, tool_call_id: str | None
|
|
74
71
|
) -> tuple[tuple, dict]:
|
|
75
|
-
"""Convert tool input to
|
|
72
|
+
"""Convert tool input to Pydantic model.
|
|
76
73
|
|
|
77
74
|
Args:
|
|
78
75
|
tool_input: The input to the tool.
|
|
@@ -82,8 +79,7 @@ class Tool(BaseTool):
|
|
|
82
79
|
ToolException: If the tool input is invalid.
|
|
83
80
|
|
|
84
81
|
Returns:
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
The Pydantic model args and kwargs.
|
|
87
83
|
"""
|
|
88
84
|
args, kwargs = super()._to_args_and_kwargs(tool_input, tool_call_id)
|
|
89
85
|
# For backwards compatibility. The tool must be run with a single input
|
|
@@ -101,7 +97,7 @@ class Tool(BaseTool):
|
|
|
101
97
|
self,
|
|
102
98
|
*args: Any,
|
|
103
99
|
config: RunnableConfig,
|
|
104
|
-
run_manager:
|
|
100
|
+
run_manager: CallbackManagerForToolRun | None = None,
|
|
105
101
|
**kwargs: Any,
|
|
106
102
|
) -> Any:
|
|
107
103
|
"""Use the tool.
|
|
@@ -128,7 +124,7 @@ class Tool(BaseTool):
|
|
|
128
124
|
self,
|
|
129
125
|
*args: Any,
|
|
130
126
|
config: RunnableConfig,
|
|
131
|
-
run_manager:
|
|
127
|
+
run_manager: AsyncCallbackManagerForToolRun | None = None,
|
|
132
128
|
**kwargs: Any,
|
|
133
129
|
) -> Any:
|
|
134
130
|
"""Use the tool asynchronously.
|
|
@@ -157,7 +153,7 @@ class Tool(BaseTool):
|
|
|
157
153
|
|
|
158
154
|
# TODO: this is for backwards compatibility, remove in future
|
|
159
155
|
def __init__(
|
|
160
|
-
self, name: str, func:
|
|
156
|
+
self, name: str, func: Callable | None, description: str, **kwargs: Any
|
|
161
157
|
) -> None:
|
|
162
158
|
"""Initialize tool."""
|
|
163
159
|
super().__init__(name=name, func=func, description=description, **kwargs)
|
|
@@ -165,14 +161,13 @@ class Tool(BaseTool):
|
|
|
165
161
|
@classmethod
|
|
166
162
|
def from_function(
|
|
167
163
|
cls,
|
|
168
|
-
func:
|
|
164
|
+
func: Callable | None,
|
|
169
165
|
name: str, # We keep these required to support backwards compatibility
|
|
170
166
|
description: str,
|
|
171
167
|
return_direct: bool = False, # noqa: FBT001,FBT002
|
|
172
|
-
args_schema:
|
|
173
|
-
coroutine:
|
|
174
|
-
|
|
175
|
-
] = None, # This is last for compatibility, but should be after func
|
|
168
|
+
args_schema: ArgsSchema | None = None,
|
|
169
|
+
coroutine: Callable[..., Awaitable[Any]]
|
|
170
|
+
| None = None, # This is last for compatibility, but should be after func
|
|
176
171
|
**kwargs: Any,
|
|
177
172
|
) -> Tool:
|
|
178
173
|
"""Initialize tool from a function.
|
|
@@ -181,10 +176,10 @@ class Tool(BaseTool):
|
|
|
181
176
|
func: The function to create the tool from.
|
|
182
177
|
name: The name of the tool.
|
|
183
178
|
description: The description of the tool.
|
|
184
|
-
return_direct: Whether to return the output directly.
|
|
185
|
-
args_schema: The schema of the tool's input arguments.
|
|
186
|
-
coroutine: The asynchronous version of the function.
|
|
187
|
-
kwargs: Additional arguments to pass to the tool.
|
|
179
|
+
return_direct: Whether to return the output directly.
|
|
180
|
+
args_schema: The schema of the tool's input arguments.
|
|
181
|
+
coroutine: The asynchronous version of the function.
|
|
182
|
+
**kwargs: Additional arguments to pass to the tool.
|
|
188
183
|
|
|
189
184
|
Returns:
|
|
190
185
|
The tool.
|