langchain-core 0.4.0.dev0__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 +45 -70
- langchain_core/_api/deprecation.py +80 -80
- langchain_core/_api/path.py +22 -8
- langchain_core/_import_utils.py +10 -4
- langchain_core/agents.py +25 -21
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +341 -348
- langchain_core/callbacks/file.py +55 -44
- langchain_core/callbacks/manager.py +546 -683
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +35 -36
- langchain_core/callbacks/usage.py +65 -70
- langchain_core/chat_history.py +48 -55
- langchain_core/document_loaders/base.py +46 -21
- langchain_core/document_loaders/langsmith.py +39 -36
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +96 -74
- langchain_core/documents/compressor.py +12 -9
- langchain_core/documents/transformers.py +29 -28
- langchain_core/embeddings/fake.py +56 -57
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +15 -9
- langchain_core/globals.py +4 -163
- langchain_core/indexing/api.py +132 -125
- langchain_core/indexing/base.py +64 -67
- langchain_core/indexing/in_memory.py +26 -6
- langchain_core/language_models/__init__.py +15 -27
- langchain_core/language_models/_utils.py +267 -117
- langchain_core/language_models/base.py +92 -177
- langchain_core/language_models/chat_models.py +547 -407
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +72 -118
- langchain_core/language_models/llms.py +168 -242
- langchain_core/load/dump.py +8 -11
- langchain_core/load/load.py +32 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +50 -56
- langchain_core/messages/__init__.py +36 -51
- langchain_core/messages/ai.py +377 -150
- langchain_core/messages/base.py +239 -47
- 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 -3
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +7 -7
- langchain_core/messages/human.py +44 -38
- langchain_core/messages/modifier.py +3 -2
- langchain_core/messages/system.py +40 -27
- langchain_core/messages/tool.py +160 -58
- langchain_core/messages/utils.py +527 -638
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +68 -104
- langchain_core/output_parsers/json.py +13 -17
- langchain_core/output_parsers/list.py +11 -33
- langchain_core/output_parsers/openai_functions.py +56 -74
- langchain_core/output_parsers/openai_tools.py +68 -109
- langchain_core/output_parsers/pydantic.py +15 -13
- langchain_core/output_parsers/string.py +6 -2
- langchain_core/output_parsers/transform.py +17 -60
- langchain_core/output_parsers/xml.py +34 -44
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +26 -11
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +17 -6
- langchain_core/outputs/llm_result.py +15 -8
- langchain_core/prompt_values.py +29 -123
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -63
- langchain_core/prompts/chat.py +259 -288
- langchain_core/prompts/dict.py +19 -11
- langchain_core/prompts/few_shot.py +84 -90
- langchain_core/prompts/few_shot_with_templates.py +14 -12
- langchain_core/prompts/image.py +19 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +7 -8
- langchain_core/prompts/prompt.py +42 -43
- langchain_core/prompts/string.py +37 -16
- langchain_core/prompts/structured.py +43 -46
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +52 -192
- langchain_core/runnables/base.py +1727 -1683
- langchain_core/runnables/branch.py +52 -73
- langchain_core/runnables/config.py +89 -103
- langchain_core/runnables/configurable.py +128 -130
- langchain_core/runnables/fallbacks.py +93 -82
- langchain_core/runnables/graph.py +127 -127
- langchain_core/runnables/graph_ascii.py +63 -41
- langchain_core/runnables/graph_mermaid.py +87 -70
- langchain_core/runnables/graph_png.py +31 -36
- langchain_core/runnables/history.py +145 -161
- langchain_core/runnables/passthrough.py +141 -144
- langchain_core/runnables/retry.py +84 -68
- langchain_core/runnables/router.py +33 -37
- langchain_core/runnables/schema.py +79 -72
- langchain_core/runnables/utils.py +95 -139
- langchain_core/stores.py +85 -131
- langchain_core/structured_query.py +11 -15
- langchain_core/sys_info.py +31 -32
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +221 -247
- langchain_core/tools/convert.py +144 -161
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -19
- langchain_core/tools/simple.py +52 -29
- langchain_core/tools/structured.py +56 -60
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +103 -112
- langchain_core/tracers/context.py +29 -48
- langchain_core/tracers/core.py +142 -105
- langchain_core/tracers/evaluation.py +30 -34
- langchain_core/tracers/event_stream.py +162 -117
- langchain_core/tracers/langchain.py +34 -36
- langchain_core/tracers/log_stream.py +87 -49
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +18 -34
- langchain_core/tracers/run_collector.py +8 -20
- langchain_core/tracers/schemas.py +0 -125
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +47 -9
- langchain_core/utils/aiter.py +70 -66
- langchain_core/utils/env.py +12 -9
- langchain_core/utils/function_calling.py +139 -206
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +48 -45
- langchain_core/utils/json.py +14 -4
- langchain_core/utils/json_schema.py +159 -43
- langchain_core/utils/mustache.py +32 -25
- langchain_core/utils/pydantic.py +67 -40
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +104 -62
- langchain_core/vectorstores/base.py +131 -179
- langchain_core/vectorstores/in_memory.py +113 -182
- langchain_core/vectorstores/utils.py +23 -17
- langchain_core/version.py +1 -1
- langchain_core-1.0.0.dist-info/METADATA +68 -0
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.4.0.dev0.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 -448
- langchain_core/memory.py +0 -116
- langchain_core/messages/content_blocks.py +0 -1435
- langchain_core/prompts/pipeline.py +0 -133
- 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 -23
- langchain_core/utils/loading.py +0 -31
- langchain_core/v1/__init__.py +0 -1
- langchain_core/v1/chat_models.py +0 -1047
- langchain_core/v1/messages.py +0 -755
- langchain_core-0.4.0.dev0.dist-info/METADATA +0 -108
- langchain_core-0.4.0.dev0.dist-info/RECORD +0 -177
- langchain_core-0.4.0.dev0.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,15 +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
|
-
|
|
26
|
-
) -> Callable[[Union[Callable, Runnable]], BaseTool]: ...
|
|
26
|
+
) -> Callable[[Callable | Runnable], BaseTool]: ...
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
@overload
|
|
@@ -31,14 +31,13 @@ def tool(
|
|
|
31
31
|
name_or_callable: str,
|
|
32
32
|
runnable: Runnable,
|
|
33
33
|
*,
|
|
34
|
-
description:
|
|
34
|
+
description: str | None = None,
|
|
35
35
|
return_direct: bool = False,
|
|
36
|
-
args_schema:
|
|
36
|
+
args_schema: ArgsSchema | None = None,
|
|
37
37
|
infer_schema: bool = True,
|
|
38
38
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
39
39
|
parse_docstring: bool = False,
|
|
40
40
|
error_on_invalid_docstring: bool = True,
|
|
41
|
-
message_version: Literal["v0", "v1"] = "v0",
|
|
42
41
|
) -> BaseTool: ...
|
|
43
42
|
|
|
44
43
|
|
|
@@ -46,14 +45,13 @@ def tool(
|
|
|
46
45
|
def tool(
|
|
47
46
|
name_or_callable: Callable,
|
|
48
47
|
*,
|
|
49
|
-
description:
|
|
48
|
+
description: str | None = None,
|
|
50
49
|
return_direct: bool = False,
|
|
51
|
-
args_schema:
|
|
50
|
+
args_schema: ArgsSchema | None = None,
|
|
52
51
|
infer_schema: bool = True,
|
|
53
52
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
54
53
|
parse_docstring: bool = False,
|
|
55
54
|
error_on_invalid_docstring: bool = True,
|
|
56
|
-
message_version: Literal["v0", "v1"] = "v0",
|
|
57
55
|
) -> BaseTool: ...
|
|
58
56
|
|
|
59
57
|
|
|
@@ -61,34 +59,29 @@ def tool(
|
|
|
61
59
|
def tool(
|
|
62
60
|
name_or_callable: str,
|
|
63
61
|
*,
|
|
64
|
-
description:
|
|
62
|
+
description: str | None = None,
|
|
65
63
|
return_direct: bool = False,
|
|
66
|
-
args_schema:
|
|
64
|
+
args_schema: ArgsSchema | None = None,
|
|
67
65
|
infer_schema: bool = True,
|
|
68
66
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
69
67
|
parse_docstring: bool = False,
|
|
70
68
|
error_on_invalid_docstring: bool = True,
|
|
71
|
-
|
|
72
|
-
) -> Callable[[Union[Callable, Runnable]], BaseTool]: ...
|
|
69
|
+
) -> Callable[[Callable | Runnable], BaseTool]: ...
|
|
73
70
|
|
|
74
71
|
|
|
75
72
|
def tool(
|
|
76
|
-
name_or_callable:
|
|
77
|
-
runnable:
|
|
73
|
+
name_or_callable: str | Callable | None = None,
|
|
74
|
+
runnable: Runnable | None = None,
|
|
78
75
|
*args: Any,
|
|
79
|
-
description:
|
|
76
|
+
description: str | None = None,
|
|
80
77
|
return_direct: bool = False,
|
|
81
|
-
args_schema:
|
|
78
|
+
args_schema: ArgsSchema | None = None,
|
|
82
79
|
infer_schema: bool = True,
|
|
83
80
|
response_format: Literal["content", "content_and_artifact"] = "content",
|
|
84
81
|
parse_docstring: bool = False,
|
|
85
82
|
error_on_invalid_docstring: bool = True,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
BaseTool,
|
|
89
|
-
Callable[[Union[Callable, Runnable]], BaseTool],
|
|
90
|
-
]:
|
|
91
|
-
"""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.
|
|
92
85
|
|
|
93
86
|
Args:
|
|
94
87
|
name_or_callable: Optional name of the tool or the callable to be
|
|
@@ -97,140 +90,142 @@ def tool(
|
|
|
97
90
|
positional argument.
|
|
98
91
|
description: Optional description for the tool.
|
|
99
92
|
Precedence for the tool description value is as follows:
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
93
|
+
|
|
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
|
|
99
|
+
(used only if `description` / docstring are not provided)
|
|
106
100
|
*args: Extra positional arguments. Must be empty.
|
|
107
101
|
return_direct: Whether to return directly from the tool rather
|
|
108
|
-
than continuing the agent loop.
|
|
109
|
-
args_schema:
|
|
110
|
-
|
|
102
|
+
than continuing the agent loop.
|
|
103
|
+
args_schema: Optional argument schema for user to specify.
|
|
104
|
+
|
|
111
105
|
infer_schema: Whether to infer the schema of the arguments from
|
|
112
106
|
the function's signature. This also makes the resultant tool
|
|
113
107
|
accept a dictionary input to its `run()` function.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
the
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
Defaults to "content".
|
|
120
|
-
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
|
|
121
113
|
parse parameter descriptions from Google Style function docstrings.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
114
|
+
error_on_invalid_docstring: if `parse_docstring` is provided, configure
|
|
115
|
+
whether to raise `ValueError` on invalid Google Style docstrings.
|
|
116
|
+
|
|
117
|
+
Raises:
|
|
118
|
+
ValueError: If too many positional arguments are provided.
|
|
119
|
+
ValueError: If a runnable is provided without a string name.
|
|
120
|
+
ValueError: If the first argument is not a string or callable with
|
|
121
|
+
a `__name__` attribute.
|
|
122
|
+
ValueError: If the function does not have a docstring and description
|
|
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.
|
|
126
|
+
ValueError: If a Runnable is provided that does not have an object schema.
|
|
131
127
|
|
|
132
128
|
Returns:
|
|
133
129
|
The tool.
|
|
134
130
|
|
|
135
131
|
Requires:
|
|
136
|
-
- Function must be of type (str) -> str
|
|
132
|
+
- Function must be of type `(str) -> str`
|
|
137
133
|
- Function must have a docstring
|
|
138
134
|
|
|
139
135
|
Examples:
|
|
140
|
-
|
|
136
|
+
```python
|
|
137
|
+
@tool
|
|
138
|
+
def search_api(query: str) -> str:
|
|
139
|
+
# Searches the API for the query.
|
|
140
|
+
return
|
|
141
141
|
|
|
142
|
-
@tool
|
|
143
|
-
def search_api(query: str) -> str:
|
|
144
|
-
# Searches the API for the query.
|
|
145
|
-
return
|
|
146
142
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
143
|
+
@tool("search", return_direct=True)
|
|
144
|
+
def search_api(query: str) -> str:
|
|
145
|
+
# Searches the API for the query.
|
|
146
|
+
return
|
|
151
147
|
|
|
152
|
-
@tool(response_format="content_and_artifact")
|
|
153
|
-
def search_api(query: str) -> tuple[str, dict]:
|
|
154
|
-
return "partial json of results", {"full": "object of results"}
|
|
155
148
|
|
|
156
|
-
|
|
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
|
+
```
|
|
153
|
+
|
|
154
|
+
!!! version-added "Added in version 0.2.14"
|
|
157
155
|
|
|
158
156
|
Parse Google-style docstrings:
|
|
159
157
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
189
|
-
"type": "integer"
|
|
190
|
-
}
|
|
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",
|
|
182
|
+
},
|
|
183
|
+
"baz": {
|
|
184
|
+
"title": "Baz",
|
|
185
|
+
"description": "The baz.",
|
|
186
|
+
"type": "integer",
|
|
191
187
|
},
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
188
|
+
},
|
|
189
|
+
"required": ["bar", "baz"],
|
|
190
|
+
}
|
|
191
|
+
```
|
|
197
192
|
|
|
198
|
-
Note that parsing by default will raise
|
|
193
|
+
Note that parsing by default will raise `ValueError` if the docstring
|
|
199
194
|
is considered invalid. A docstring is considered invalid if it contains
|
|
200
195
|
arguments not in the function signature, or is unable to be parsed into
|
|
201
|
-
a summary and "Args:" blocks. Examples below:
|
|
202
|
-
|
|
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
|
-
""" # noqa: D214, D410, D411
|
|
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
|
+
```
|
|
224
|
+
""" # noqa: D214, D410, D411 # We're intentionally showing bad formatting in examples
|
|
230
225
|
|
|
231
226
|
def _create_tool_factory(
|
|
232
227
|
tool_name: str,
|
|
233
|
-
) -> Callable[[
|
|
228
|
+
) -> Callable[[Callable | Runnable], BaseTool]:
|
|
234
229
|
"""Create a decorator that takes a callable and returns a tool.
|
|
235
230
|
|
|
236
231
|
Args:
|
|
@@ -240,7 +235,7 @@ def tool(
|
|
|
240
235
|
A function that takes a callable or Runnable and returns a tool.
|
|
241
236
|
"""
|
|
242
237
|
|
|
243
|
-
def _tool_factory(dec_func:
|
|
238
|
+
def _tool_factory(dec_func: Callable | Runnable) -> BaseTool:
|
|
244
239
|
tool_description = description
|
|
245
240
|
if isinstance(dec_func, Runnable):
|
|
246
241
|
runnable = dec_func
|
|
@@ -250,18 +245,18 @@ def tool(
|
|
|
250
245
|
raise ValueError(msg)
|
|
251
246
|
|
|
252
247
|
async def ainvoke_wrapper(
|
|
253
|
-
callbacks:
|
|
248
|
+
callbacks: Callbacks | None = None, **kwargs: Any
|
|
254
249
|
) -> Any:
|
|
255
250
|
return await runnable.ainvoke(kwargs, {"callbacks": callbacks})
|
|
256
251
|
|
|
257
252
|
def invoke_wrapper(
|
|
258
|
-
callbacks:
|
|
253
|
+
callbacks: Callbacks | None = None, **kwargs: Any
|
|
259
254
|
) -> Any:
|
|
260
255
|
return runnable.invoke(kwargs, {"callbacks": callbacks})
|
|
261
256
|
|
|
262
257
|
coroutine = ainvoke_wrapper
|
|
263
258
|
func = invoke_wrapper
|
|
264
|
-
schema:
|
|
259
|
+
schema: ArgsSchema | None = runnable.input_schema
|
|
265
260
|
tool_description = description or repr(runnable)
|
|
266
261
|
elif inspect.iscoroutinefunction(dec_func):
|
|
267
262
|
coroutine = dec_func
|
|
@@ -284,7 +279,6 @@ def tool(
|
|
|
284
279
|
response_format=response_format,
|
|
285
280
|
parse_docstring=parse_docstring,
|
|
286
281
|
error_on_invalid_docstring=error_on_invalid_docstring,
|
|
287
|
-
message_version=message_version,
|
|
288
282
|
)
|
|
289
283
|
# If someone doesn't want a schema applied, we must treat it as
|
|
290
284
|
# a simple string->string function
|
|
@@ -301,7 +295,6 @@ def tool(
|
|
|
301
295
|
return_direct=return_direct,
|
|
302
296
|
coroutine=coroutine,
|
|
303
297
|
response_format=response_format,
|
|
304
|
-
message_version=message_version,
|
|
305
298
|
)
|
|
306
299
|
|
|
307
300
|
return _tool_factory
|
|
@@ -316,7 +309,7 @@ def tool(
|
|
|
316
309
|
|
|
317
310
|
if runnable is not None:
|
|
318
311
|
# tool is used as a function
|
|
319
|
-
# tool_from_runnable = tool("name", runnable)
|
|
312
|
+
# for instance tool_from_runnable = tool("name", runnable)
|
|
320
313
|
if not name_or_callable:
|
|
321
314
|
msg = "Runnable without name for tool constructor"
|
|
322
315
|
raise ValueError(msg)
|
|
@@ -353,7 +346,7 @@ def tool(
|
|
|
353
346
|
# @tool(parse_docstring=True)
|
|
354
347
|
# def my_tool():
|
|
355
348
|
# pass
|
|
356
|
-
def _partial(func:
|
|
349
|
+
def _partial(func: Callable | Runnable) -> BaseTool:
|
|
357
350
|
"""Partial function that takes a callable and returns a tool."""
|
|
358
351
|
name_ = func.get_name() if isinstance(func, Runnable) else func.__name__
|
|
359
352
|
tool_factory = _create_tool_factory(name_)
|
|
@@ -371,7 +364,7 @@ def _get_description_from_runnable(runnable: Runnable) -> str:
|
|
|
371
364
|
def _get_schema_from_runnable_and_arg_types(
|
|
372
365
|
runnable: Runnable,
|
|
373
366
|
name: str,
|
|
374
|
-
arg_types:
|
|
367
|
+
arg_types: dict[str, type] | None = None,
|
|
375
368
|
) -> type[BaseModel]:
|
|
376
369
|
"""Infer args_schema for tool."""
|
|
377
370
|
if arg_types is None:
|
|
@@ -390,30 +383,24 @@ def _get_schema_from_runnable_and_arg_types(
|
|
|
390
383
|
|
|
391
384
|
def convert_runnable_to_tool(
|
|
392
385
|
runnable: Runnable,
|
|
393
|
-
args_schema:
|
|
386
|
+
args_schema: type[BaseModel] | None = None,
|
|
394
387
|
*,
|
|
395
|
-
name:
|
|
396
|
-
description:
|
|
397
|
-
arg_types:
|
|
398
|
-
message_version: Literal["v0", "v1"] = "v0",
|
|
388
|
+
name: str | None = None,
|
|
389
|
+
description: str | None = None,
|
|
390
|
+
arg_types: dict[str, type] | None = None,
|
|
399
391
|
) -> BaseTool:
|
|
400
392
|
"""Convert a Runnable into a BaseTool.
|
|
401
393
|
|
|
402
394
|
Args:
|
|
403
395
|
runnable: The runnable to convert.
|
|
404
|
-
args_schema: The schema for the tool's input arguments.
|
|
405
|
-
name: The name of the tool.
|
|
406
|
-
description: The description of the tool.
|
|
407
|
-
arg_types: The types of the arguments.
|
|
408
|
-
message_version: Version of ToolMessage to return given
|
|
409
|
-
:class:`~langchain_core.messages.content_blocks.ToolCall` input.
|
|
410
|
-
|
|
411
|
-
If ``"v0"``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`.
|
|
412
|
-
If ``"v1"``, output will be a v1 :class:`~langchain_core.messages.v1.ToolMessage`.
|
|
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.
|
|
413
400
|
|
|
414
401
|
Returns:
|
|
415
402
|
The tool.
|
|
416
|
-
"""
|
|
403
|
+
"""
|
|
417
404
|
if args_schema:
|
|
418
405
|
runnable = runnable.with_types(input_type=args_schema)
|
|
419
406
|
description = description or _get_description_from_runnable(runnable)
|
|
@@ -426,15 +413,12 @@ def convert_runnable_to_tool(
|
|
|
426
413
|
func=runnable.invoke,
|
|
427
414
|
coroutine=runnable.ainvoke,
|
|
428
415
|
description=description,
|
|
429
|
-
message_version=message_version,
|
|
430
416
|
)
|
|
431
417
|
|
|
432
|
-
async def ainvoke_wrapper(
|
|
433
|
-
callbacks: Optional[Callbacks] = None, **kwargs: Any
|
|
434
|
-
) -> Any:
|
|
418
|
+
async def ainvoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
|
|
435
419
|
return await runnable.ainvoke(kwargs, config={"callbacks": callbacks})
|
|
436
420
|
|
|
437
|
-
def invoke_wrapper(callbacks:
|
|
421
|
+
def invoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
|
|
438
422
|
return runnable.invoke(kwargs, config={"callbacks": callbacks})
|
|
439
423
|
|
|
440
424
|
if (
|
|
@@ -454,5 +438,4 @@ def convert_runnable_to_tool(
|
|
|
454
438
|
coroutine=ainvoke_wrapper,
|
|
455
439
|
description=description,
|
|
456
440
|
args_schema=args_schema,
|
|
457
|
-
message_version=message_version,
|
|
458
441
|
)
|
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,10 +69,9 @@ 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
|
-
message_version: Literal["v0", "v1"] = "v1",
|
|
76
75
|
) -> Tool:
|
|
77
76
|
r"""Create a tool to do retrieval of documents.
|
|
78
77
|
|
|
@@ -82,22 +81,17 @@ def create_retriever_tool(
|
|
|
82
81
|
so should be unique and somewhat descriptive.
|
|
83
82
|
description: The description for the tool. This will be passed to the language
|
|
84
83
|
model, so should be descriptive.
|
|
85
|
-
document_prompt: The prompt to use for the document.
|
|
86
|
-
document_separator: The separator to use between documents.
|
|
87
|
-
response_format: The tool response format. If "content" then the output of
|
|
88
|
-
the tool is interpreted as the contents of a ToolMessage
|
|
89
|
-
"content_and_artifact" then the output is expected to be a two-tuple
|
|
90
|
-
corresponding to the (content, artifact) of a ToolMessage (artifact
|
|
91
|
-
being a list of documents in this case).
|
|
92
|
-
message_version: Version of ToolMessage to return given
|
|
93
|
-
:class:`~langchain_core.messages.content_blocks.ToolCall` input.
|
|
94
|
-
|
|
95
|
-
If ``"v0"``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`.
|
|
96
|
-
If ``"v1"``, output will be a v1 :class:`~langchain_core.messages.v1.ToolMessage`.
|
|
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).
|
|
97
91
|
|
|
98
92
|
Returns:
|
|
99
93
|
Tool class to pass to an agent.
|
|
100
|
-
"""
|
|
94
|
+
"""
|
|
101
95
|
document_prompt = document_prompt or PromptTemplate.from_template("{page_content}")
|
|
102
96
|
func = partial(
|
|
103
97
|
_get_relevant_documents,
|
|
@@ -120,5 +114,4 @@ def create_retriever_tool(
|
|
|
120
114
|
coroutine=afunc,
|
|
121
115
|
args_schema=RetrieverInput,
|
|
122
116
|
response_format=response_format,
|
|
123
|
-
message_version=message_version,
|
|
124
117
|
)
|