langchain-core 1.0.0a7__py3-none-any.whl → 1.0.0rc1__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 +7 -6
- langchain_core/caches.py +4 -10
- 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 +76 -55
- langchain_core/document_loaders/langsmith.py +21 -21
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +37 -40
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +46 -52
- langchain_core/exceptions.py +5 -5
- langchain_core/indexing/api.py +11 -11
- langchain_core/indexing/base.py +24 -24
- langchain_core/language_models/__init__.py +0 -2
- langchain_core/language_models/_utils.py +51 -53
- langchain_core/language_models/base.py +23 -24
- langchain_core/language_models/chat_models.py +121 -144
- langchain_core/language_models/fake_chat_models.py +5 -5
- langchain_core/language_models/llms.py +10 -12
- langchain_core/load/dump.py +1 -1
- langchain_core/load/load.py +16 -16
- langchain_core/load/serializable.py +35 -34
- langchain_core/messages/__init__.py +1 -16
- langchain_core/messages/ai.py +105 -104
- langchain_core/messages/base.py +26 -26
- langchain_core/messages/block_translators/__init__.py +17 -17
- langchain_core/messages/block_translators/anthropic.py +2 -2
- langchain_core/messages/block_translators/bedrock_converse.py +2 -2
- langchain_core/messages/block_translators/google_genai.py +2 -2
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +2 -2
- langchain_core/messages/block_translators/openai.py +4 -4
- langchain_core/messages/chat.py +1 -1
- langchain_core/messages/content.py +189 -193
- langchain_core/messages/function.py +5 -5
- langchain_core/messages/human.py +15 -17
- langchain_core/messages/modifier.py +1 -1
- langchain_core/messages/system.py +12 -14
- langchain_core/messages/tool.py +45 -49
- langchain_core/messages/utils.py +384 -396
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +22 -23
- langchain_core/output_parsers/json.py +3 -3
- langchain_core/output_parsers/list.py +1 -1
- langchain_core/output_parsers/openai_functions.py +46 -44
- langchain_core/output_parsers/openai_tools.py +7 -7
- langchain_core/output_parsers/pydantic.py +10 -11
- langchain_core/output_parsers/string.py +1 -1
- 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 +5 -5
- langchain_core/outputs/llm_result.py +5 -5
- langchain_core/prompt_values.py +5 -5
- langchain_core/prompts/__init__.py +3 -23
- langchain_core/prompts/base.py +32 -37
- langchain_core/prompts/chat.py +216 -222
- langchain_core/prompts/dict.py +2 -2
- langchain_core/prompts/few_shot.py +76 -83
- langchain_core/prompts/few_shot_with_templates.py +6 -8
- langchain_core/prompts/image.py +11 -13
- langchain_core/prompts/loading.py +1 -1
- langchain_core/prompts/message.py +2 -2
- langchain_core/prompts/prompt.py +14 -16
- langchain_core/prompts/string.py +19 -7
- langchain_core/prompts/structured.py +24 -25
- langchain_core/rate_limiters.py +36 -38
- langchain_core/retrievers.py +41 -182
- langchain_core/runnables/base.py +565 -590
- langchain_core/runnables/branch.py +7 -7
- langchain_core/runnables/config.py +37 -44
- langchain_core/runnables/configurable.py +8 -9
- langchain_core/runnables/fallbacks.py +8 -8
- langchain_core/runnables/graph.py +28 -27
- 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 +20 -20
- langchain_core/runnables/passthrough.py +8 -8
- langchain_core/runnables/retry.py +3 -3
- langchain_core/runnables/router.py +1 -1
- 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 +63 -63
- langchain_core/tools/convert.py +92 -92
- langchain_core/tools/render.py +9 -9
- langchain_core/tools/retriever.py +1 -1
- langchain_core/tools/simple.py +6 -7
- langchain_core/tools/structured.py +17 -18
- 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 +2 -2
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/utils/aiter.py +31 -31
- langchain_core/utils/env.py +5 -5
- langchain_core/utils/function_calling.py +48 -120
- langchain_core/utils/html.py +4 -4
- langchain_core/utils/input.py +2 -2
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +19 -19
- 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 +4 -4
- langchain_core/utils/utils.py +25 -28
- langchain_core/vectorstores/base.py +43 -64
- langchain_core/vectorstores/in_memory.py +83 -85
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a7.dist-info → langchain_core-1.0.0rc1.dist-info}/METADATA +23 -11
- langchain_core-1.0.0rc1.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.0a7.dist-info/RECORD +0 -176
- {langchain_core-1.0.0a7.dist-info → langchain_core-1.0.0rc1.dist-info}/WHEEL +0 -0
langchain_core/tools/base.py
CHANGED
|
@@ -92,7 +92,7 @@ def _is_annotated_type(typ: type[Any]) -> bool:
|
|
|
92
92
|
typ: The type to check.
|
|
93
93
|
|
|
94
94
|
Returns:
|
|
95
|
-
True if the type is an Annotated type, False otherwise.
|
|
95
|
+
`True` if the type is an Annotated type, `False` otherwise.
|
|
96
96
|
"""
|
|
97
97
|
return get_origin(typ) is typing.Annotated
|
|
98
98
|
|
|
@@ -226,7 +226,7 @@ def _is_pydantic_annotation(annotation: Any, pydantic_version: str = "v2") -> bo
|
|
|
226
226
|
pydantic_version: The Pydantic version to check against ("v1" or "v2").
|
|
227
227
|
|
|
228
228
|
Returns:
|
|
229
|
-
True if the annotation is a Pydantic model, False otherwise.
|
|
229
|
+
`True` if the annotation is a Pydantic model, `False` otherwise.
|
|
230
230
|
"""
|
|
231
231
|
base_model_class = BaseModelV1 if pydantic_version == "v1" else BaseModel
|
|
232
232
|
try:
|
|
@@ -245,7 +245,7 @@ def _function_annotations_are_pydantic_v1(
|
|
|
245
245
|
func: The function being checked.
|
|
246
246
|
|
|
247
247
|
Returns:
|
|
248
|
-
True if all Pydantic annotations are from V1, False otherwise.
|
|
248
|
+
True if all Pydantic annotations are from V1, `False` otherwise.
|
|
249
249
|
|
|
250
250
|
Raises:
|
|
251
251
|
NotImplementedError: If the function contains mixed V1 and V2 annotations.
|
|
@@ -285,24 +285,24 @@ def create_schema_from_function(
|
|
|
285
285
|
error_on_invalid_docstring: bool = False,
|
|
286
286
|
include_injected: bool = True,
|
|
287
287
|
) -> type[BaseModel]:
|
|
288
|
-
"""Create a
|
|
288
|
+
"""Create a Pydantic schema from a function's signature.
|
|
289
289
|
|
|
290
290
|
Args:
|
|
291
|
-
model_name: Name to assign to the generated
|
|
291
|
+
model_name: Name to assign to the generated Pydantic schema.
|
|
292
292
|
func: Function to generate the schema from.
|
|
293
293
|
filter_args: Optional list of arguments to exclude from the schema.
|
|
294
|
-
Defaults to FILTERED_ARGS
|
|
294
|
+
Defaults to `FILTERED_ARGS`.
|
|
295
295
|
parse_docstring: Whether to parse the function's docstring for descriptions
|
|
296
|
-
for each argument. Defaults to False
|
|
297
|
-
error_on_invalid_docstring: if
|
|
298
|
-
whether to raise ValueError on invalid Google Style docstrings.
|
|
299
|
-
Defaults to False
|
|
296
|
+
for each argument. Defaults to `False`.
|
|
297
|
+
error_on_invalid_docstring: if `parse_docstring` is provided, configure
|
|
298
|
+
whether to raise `ValueError` on invalid Google Style docstrings.
|
|
299
|
+
Defaults to `False`.
|
|
300
300
|
include_injected: Whether to include injected arguments in the schema.
|
|
301
|
-
Defaults to True
|
|
301
|
+
Defaults to `True`, since we want to include them in the schema
|
|
302
302
|
when *validating* tool inputs.
|
|
303
303
|
|
|
304
304
|
Returns:
|
|
305
|
-
A
|
|
305
|
+
A Pydantic model with the same arguments as the function.
|
|
306
306
|
"""
|
|
307
307
|
sig = inspect.signature(func)
|
|
308
308
|
|
|
@@ -312,7 +312,7 @@ def create_schema_from_function(
|
|
|
312
312
|
# https://docs.pydantic.dev/latest/usage/validation_decorator/
|
|
313
313
|
with warnings.catch_warnings():
|
|
314
314
|
# We are using deprecated functionality here.
|
|
315
|
-
# This code should be re-written to simply construct a
|
|
315
|
+
# This code should be re-written to simply construct a Pydantic model
|
|
316
316
|
# using inspect.signature and create_model.
|
|
317
317
|
warnings.simplefilter("ignore", category=PydanticDeprecationWarning)
|
|
318
318
|
validated = validate_arguments(func, config=_SchemaConfig) # type: ignore[operator]
|
|
@@ -460,13 +460,13 @@ class ChildTool(BaseTool):
|
|
|
460
460
|
"""Callbacks to be called during tool execution."""
|
|
461
461
|
|
|
462
462
|
tags: list[str] | None = None
|
|
463
|
-
"""Optional list of tags associated with the tool.
|
|
463
|
+
"""Optional list of tags associated with the tool.
|
|
464
464
|
These tags will be associated with each call to this tool,
|
|
465
465
|
and passed as arguments to the handlers defined in `callbacks`.
|
|
466
466
|
You can use these to eg identify a specific instance of a tool with its use case.
|
|
467
467
|
"""
|
|
468
468
|
metadata: dict[str, Any] | None = None
|
|
469
|
-
"""Optional metadata associated with the tool.
|
|
469
|
+
"""Optional metadata associated with the tool.
|
|
470
470
|
This metadata will be associated with each call to this tool,
|
|
471
471
|
and passed as arguments to the handlers defined in `callbacks`.
|
|
472
472
|
You can use these to eg identify a specific instance of a tool with its use case.
|
|
@@ -492,7 +492,7 @@ class ChildTool(BaseTool):
|
|
|
492
492
|
"""Initialize the tool.
|
|
493
493
|
|
|
494
494
|
Raises:
|
|
495
|
-
TypeError: If
|
|
495
|
+
TypeError: If `args_schema` is not a subclass of pydantic `BaseModel` or
|
|
496
496
|
dict.
|
|
497
497
|
"""
|
|
498
498
|
if (
|
|
@@ -517,7 +517,7 @@ class ChildTool(BaseTool):
|
|
|
517
517
|
"""Check if the tool accepts only a single input argument.
|
|
518
518
|
|
|
519
519
|
Returns:
|
|
520
|
-
True if the tool has only one input argument, False otherwise.
|
|
520
|
+
`True` if the tool has only one input argument, `False` otherwise.
|
|
521
521
|
"""
|
|
522
522
|
keys = {k for k in self.args if k != "kwargs"}
|
|
523
523
|
return len(keys) == 1
|
|
@@ -616,10 +616,10 @@ class ChildTool(BaseTool):
|
|
|
616
616
|
The parsed and validated input.
|
|
617
617
|
|
|
618
618
|
Raises:
|
|
619
|
-
ValueError: If string input is provided with JSON schema
|
|
620
|
-
ValueError: If InjectedToolCallId is required but
|
|
619
|
+
ValueError: If string input is provided with JSON schema `args_schema`.
|
|
620
|
+
ValueError: If InjectedToolCallId is required but `tool_call_id` is not
|
|
621
621
|
provided.
|
|
622
|
-
TypeError: If args_schema is not a Pydantic
|
|
622
|
+
TypeError: If args_schema is not a Pydantic `BaseModel` or dict.
|
|
623
623
|
"""
|
|
624
624
|
input_args = self.args_schema
|
|
625
625
|
if isinstance(tool_input, str):
|
|
@@ -686,8 +686,8 @@ class ChildTool(BaseTool):
|
|
|
686
686
|
def _run(self, *args: Any, **kwargs: Any) -> Any:
|
|
687
687
|
"""Use the tool.
|
|
688
688
|
|
|
689
|
-
Add run_manager:
|
|
690
|
-
|
|
689
|
+
Add `run_manager: CallbackManagerForToolRun | None = None` to child
|
|
690
|
+
implementations to enable tracing.
|
|
691
691
|
|
|
692
692
|
Returns:
|
|
693
693
|
The result of the tool execution.
|
|
@@ -696,8 +696,8 @@ class ChildTool(BaseTool):
|
|
|
696
696
|
async def _arun(self, *args: Any, **kwargs: Any) -> Any:
|
|
697
697
|
"""Use the tool asynchronously.
|
|
698
698
|
|
|
699
|
-
Add run_manager:
|
|
700
|
-
|
|
699
|
+
Add `run_manager: AsyncCallbackManagerForToolRun | None = None` to child
|
|
700
|
+
implementations to enable tracing.
|
|
701
701
|
|
|
702
702
|
Returns:
|
|
703
703
|
The result of the tool execution.
|
|
@@ -767,17 +767,17 @@ class ChildTool(BaseTool):
|
|
|
767
767
|
|
|
768
768
|
Args:
|
|
769
769
|
tool_input: The input to the tool.
|
|
770
|
-
verbose: Whether to log the tool's progress.
|
|
770
|
+
verbose: Whether to log the tool's progress.
|
|
771
771
|
start_color: The color to use when starting the tool. Defaults to 'green'.
|
|
772
772
|
color: The color to use when ending the tool. Defaults to 'green'.
|
|
773
|
-
callbacks: Callbacks to be called during tool execution.
|
|
774
|
-
tags: Optional list of tags associated with the tool.
|
|
775
|
-
metadata: Optional metadata associated with the tool.
|
|
776
|
-
run_name: The name of the run.
|
|
777
|
-
run_id: The id of the run.
|
|
778
|
-
config: The configuration for the tool.
|
|
779
|
-
tool_call_id: The id of the tool call.
|
|
780
|
-
kwargs: Keyword arguments to be passed to tool callbacks (event handler)
|
|
773
|
+
callbacks: Callbacks to be called during tool execution.
|
|
774
|
+
tags: Optional list of tags associated with the tool.
|
|
775
|
+
metadata: Optional metadata associated with the tool.
|
|
776
|
+
run_name: The name of the run.
|
|
777
|
+
run_id: The id of the run.
|
|
778
|
+
config: The configuration for the tool.
|
|
779
|
+
tool_call_id: The id of the tool call.
|
|
780
|
+
**kwargs: Keyword arguments to be passed to tool callbacks (event handler)
|
|
781
781
|
|
|
782
782
|
Returns:
|
|
783
783
|
The output of the tool.
|
|
@@ -879,17 +879,17 @@ class ChildTool(BaseTool):
|
|
|
879
879
|
|
|
880
880
|
Args:
|
|
881
881
|
tool_input: The input to the tool.
|
|
882
|
-
verbose: Whether to log the tool's progress.
|
|
882
|
+
verbose: Whether to log the tool's progress.
|
|
883
883
|
start_color: The color to use when starting the tool. Defaults to 'green'.
|
|
884
884
|
color: The color to use when ending the tool. Defaults to 'green'.
|
|
885
|
-
callbacks: Callbacks to be called during tool execution.
|
|
886
|
-
tags: Optional list of tags associated with the tool.
|
|
887
|
-
metadata: Optional metadata associated with the tool.
|
|
888
|
-
run_name: The name of the run.
|
|
889
|
-
run_id: The id of the run.
|
|
890
|
-
config: The configuration for the tool.
|
|
891
|
-
tool_call_id: The id of the tool call.
|
|
892
|
-
kwargs: Keyword arguments to be passed to tool callbacks
|
|
885
|
+
callbacks: Callbacks to be called during tool execution.
|
|
886
|
+
tags: Optional list of tags associated with the tool.
|
|
887
|
+
metadata: Optional metadata associated with the tool.
|
|
888
|
+
run_name: The name of the run.
|
|
889
|
+
run_id: The id of the run.
|
|
890
|
+
config: The configuration for the tool.
|
|
891
|
+
tool_call_id: The id of the tool call.
|
|
892
|
+
**kwargs: Keyword arguments to be passed to tool callbacks
|
|
893
893
|
|
|
894
894
|
Returns:
|
|
895
895
|
The output of the tool.
|
|
@@ -981,7 +981,7 @@ def _is_tool_call(x: Any) -> bool:
|
|
|
981
981
|
x: The input to check.
|
|
982
982
|
|
|
983
983
|
Returns:
|
|
984
|
-
True if the input is a tool call, False otherwise.
|
|
984
|
+
`True` if the input is a tool call, `False` otherwise.
|
|
985
985
|
"""
|
|
986
986
|
return isinstance(x, dict) and x.get("type") == "tool_call"
|
|
987
987
|
|
|
@@ -1128,7 +1128,7 @@ def _is_message_content_type(obj: Any) -> bool:
|
|
|
1128
1128
|
obj: The object to check.
|
|
1129
1129
|
|
|
1130
1130
|
Returns:
|
|
1131
|
-
True if the object is valid message content, False otherwise.
|
|
1131
|
+
`True` if the object is valid message content, `False` otherwise.
|
|
1132
1132
|
"""
|
|
1133
1133
|
return isinstance(obj, str) or (
|
|
1134
1134
|
isinstance(obj, list) and all(_is_message_content_block(e) for e in obj)
|
|
@@ -1144,7 +1144,7 @@ def _is_message_content_block(obj: Any) -> bool:
|
|
|
1144
1144
|
obj: The object to check.
|
|
1145
1145
|
|
|
1146
1146
|
Returns:
|
|
1147
|
-
True if the object is a valid content block, False otherwise.
|
|
1147
|
+
`True` if the object is a valid content block, `False` otherwise.
|
|
1148
1148
|
"""
|
|
1149
1149
|
if isinstance(obj, str):
|
|
1150
1150
|
return True
|
|
@@ -1217,24 +1217,24 @@ class InjectedToolCallId(InjectedToolArg):
|
|
|
1217
1217
|
This annotation is used to mark a tool parameter that should receive
|
|
1218
1218
|
the tool call ID at runtime.
|
|
1219
1219
|
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
)
|
|
1220
|
+
```python
|
|
1221
|
+
from typing import Annotated
|
|
1222
|
+
from langchain_core.messages import ToolMessage
|
|
1223
|
+
from langchain_core.tools import tool, InjectedToolCallId
|
|
1224
|
+
|
|
1225
|
+
@tool
|
|
1226
|
+
def foo(
|
|
1227
|
+
x: int, tool_call_id: Annotated[str, InjectedToolCallId]
|
|
1228
|
+
) -> ToolMessage:
|
|
1229
|
+
\"\"\"Return x.\"\"\"
|
|
1230
|
+
return ToolMessage(
|
|
1231
|
+
str(x),
|
|
1232
|
+
artifact=x,
|
|
1233
|
+
name="foo",
|
|
1234
|
+
tool_call_id=tool_call_id
|
|
1235
|
+
)
|
|
1237
1236
|
|
|
1237
|
+
```
|
|
1238
1238
|
"""
|
|
1239
1239
|
|
|
1240
1240
|
|
|
@@ -1248,7 +1248,7 @@ def _is_injected_arg_type(
|
|
|
1248
1248
|
injected_type: The specific injected type to check for.
|
|
1249
1249
|
|
|
1250
1250
|
Returns:
|
|
1251
|
-
True if the type is an injected argument, False otherwise.
|
|
1251
|
+
`True` if the type is an injected argument, `False` otherwise.
|
|
1252
1252
|
"""
|
|
1253
1253
|
injected_type = injected_type or InjectedToolArg
|
|
1254
1254
|
return any(
|
langchain_core/tools/convert.py
CHANGED
|
@@ -91,42 +91,42 @@ def tool(
|
|
|
91
91
|
description: Optional description for the tool.
|
|
92
92
|
Precedence for the tool description value is as follows:
|
|
93
93
|
|
|
94
|
-
-
|
|
95
|
-
(used even if docstring and/or
|
|
94
|
+
- `description` argument
|
|
95
|
+
(used even if docstring and/or `args_schema` are provided)
|
|
96
96
|
- tool function docstring
|
|
97
|
-
(used even if
|
|
98
|
-
-
|
|
97
|
+
(used even if `args_schema` is provided)
|
|
98
|
+
- `args_schema` description
|
|
99
99
|
(used only if `description` / docstring are not provided)
|
|
100
100
|
*args: Extra positional arguments. Must be empty.
|
|
101
101
|
return_direct: Whether to return directly from the tool rather
|
|
102
|
-
than continuing the agent loop. Defaults to False
|
|
102
|
+
than continuing the agent loop. Defaults to `False`.
|
|
103
103
|
args_schema: optional argument schema for user to specify.
|
|
104
|
-
|
|
104
|
+
|
|
105
105
|
infer_schema: Whether to infer the schema of the arguments from
|
|
106
106
|
the function's signature. This also makes the resultant tool
|
|
107
107
|
accept a dictionary input to its `run()` function.
|
|
108
|
-
Defaults to True
|
|
108
|
+
Defaults to `True`.
|
|
109
109
|
response_format: The tool response format. If "content" then the output of
|
|
110
110
|
the tool is interpreted as the contents of a ToolMessage. If
|
|
111
111
|
"content_and_artifact" then the output is expected to be a two-tuple
|
|
112
112
|
corresponding to the (content, artifact) of a ToolMessage.
|
|
113
113
|
Defaults to "content".
|
|
114
|
-
parse_docstring: if
|
|
114
|
+
parse_docstring: if `infer_schema` and `parse_docstring`, will attempt to
|
|
115
115
|
parse parameter descriptions from Google Style function docstrings.
|
|
116
|
-
Defaults to False
|
|
117
|
-
error_on_invalid_docstring: if
|
|
116
|
+
Defaults to `False`.
|
|
117
|
+
error_on_invalid_docstring: if `parse_docstring` is provided, configure
|
|
118
118
|
whether to raise ValueError on invalid Google Style docstrings.
|
|
119
|
-
Defaults to True
|
|
119
|
+
Defaults to `True`.
|
|
120
120
|
|
|
121
121
|
Raises:
|
|
122
122
|
ValueError: If too many positional arguments are provided.
|
|
123
123
|
ValueError: If a runnable is provided without a string name.
|
|
124
124
|
ValueError: If the first argument is not a string or callable with
|
|
125
|
-
a
|
|
125
|
+
a `__name__` attribute.
|
|
126
126
|
ValueError: If the function does not have a docstring and description
|
|
127
|
-
is not provided and
|
|
128
|
-
ValueError: If
|
|
129
|
-
Google-style docstring and
|
|
127
|
+
is not provided and `infer_schema` is False.
|
|
128
|
+
ValueError: If `parse_docstring` is True and the function has an invalid
|
|
129
|
+
Google-style docstring and `error_on_invalid_docstring` is True.
|
|
130
130
|
ValueError: If a Runnable is provided that does not have an object schema.
|
|
131
131
|
|
|
132
132
|
Returns:
|
|
@@ -137,94 +137,94 @@ def tool(
|
|
|
137
137
|
- Function must have a docstring
|
|
138
138
|
|
|
139
139
|
Examples:
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return
|
|
140
|
+
```python
|
|
141
|
+
@tool
|
|
142
|
+
def search_api(query: str) -> str:
|
|
143
|
+
# Searches the API for the query.
|
|
144
|
+
return
|
|
146
145
|
|
|
147
146
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
147
|
+
@tool("search", return_direct=True)
|
|
148
|
+
def search_api(query: str) -> str:
|
|
149
|
+
# Searches the API for the query.
|
|
150
|
+
return
|
|
152
151
|
|
|
153
152
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
@tool(response_format="content_and_artifact")
|
|
154
|
+
def search_api(query: str) -> tuple[str, dict]:
|
|
155
|
+
return "partial json of results", {"full": "object of results"}
|
|
156
|
+
```
|
|
157
157
|
|
|
158
158
|
!!! version-added "Added in version 0.2.14"
|
|
159
159
|
|
|
160
160
|
Parse Google-style docstrings:
|
|
161
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
|
-
"type": "string",
|
|
187
|
-
},
|
|
188
|
-
"baz": {
|
|
189
|
-
"title": "Baz",
|
|
190
|
-
"description": "The baz.",
|
|
191
|
-
"type": "integer",
|
|
192
|
-
},
|
|
162
|
+
```python
|
|
163
|
+
@tool(parse_docstring=True)
|
|
164
|
+
def foo(bar: str, baz: int) -> str:
|
|
165
|
+
\"\"\"The foo.
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
bar: The bar.
|
|
169
|
+
baz: The baz.
|
|
170
|
+
\"\"\"
|
|
171
|
+
return bar
|
|
172
|
+
|
|
173
|
+
foo.args_schema.model_json_schema()
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
{
|
|
178
|
+
"title": "foo",
|
|
179
|
+
"description": "The foo.",
|
|
180
|
+
"type": "object",
|
|
181
|
+
"properties": {
|
|
182
|
+
"bar": {
|
|
183
|
+
"title": "Bar",
|
|
184
|
+
"description": "The bar.",
|
|
185
|
+
"type": "string",
|
|
193
186
|
},
|
|
194
|
-
"
|
|
195
|
-
|
|
187
|
+
"baz": {
|
|
188
|
+
"title": "Baz",
|
|
189
|
+
"description": "The baz.",
|
|
190
|
+
"type": "integer",
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
"required": ["bar", "baz"],
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
196
|
|
|
197
|
-
Note that parsing by default will raise
|
|
197
|
+
Note that parsing by default will raise `ValueError` if the docstring
|
|
198
198
|
is considered invalid. A docstring is considered invalid if it contains
|
|
199
199
|
arguments not in the function signature, or is unable to be parsed into
|
|
200
200
|
a summary and "Args:" blocks. Examples below:
|
|
201
201
|
|
|
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
|
-
|
|
202
|
+
```python
|
|
203
|
+
# No args section
|
|
204
|
+
def invalid_docstring_1(bar: str, baz: int) -> str:
|
|
205
|
+
\"\"\"The foo.\"\"\"
|
|
206
|
+
return bar
|
|
207
|
+
|
|
208
|
+
# Improper whitespace between summary and args section
|
|
209
|
+
def invalid_docstring_2(bar: str, baz: int) -> str:
|
|
210
|
+
\"\"\"The foo.
|
|
211
|
+
Args:
|
|
212
|
+
bar: The bar.
|
|
213
|
+
baz: The baz.
|
|
214
|
+
\"\"\"
|
|
215
|
+
return bar
|
|
216
|
+
|
|
217
|
+
# Documented args absent from function signature
|
|
218
|
+
def invalid_docstring_3(bar: str, baz: int) -> str:
|
|
219
|
+
\"\"\"The foo.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
banana: The bar.
|
|
223
|
+
monkey: The baz.
|
|
224
|
+
\"\"\"
|
|
225
|
+
return bar
|
|
226
|
+
|
|
227
|
+
```
|
|
228
228
|
""" # noqa: D214, D410, D411 # We're intentionally showing bad formatting in examples
|
|
229
229
|
|
|
230
230
|
def _create_tool_factory(
|
|
@@ -397,10 +397,10 @@ def convert_runnable_to_tool(
|
|
|
397
397
|
|
|
398
398
|
Args:
|
|
399
399
|
runnable: The runnable to convert.
|
|
400
|
-
args_schema: The schema for the tool's input arguments.
|
|
401
|
-
name: The name of the tool.
|
|
402
|
-
description: The description of the tool.
|
|
403
|
-
arg_types: The types of the arguments.
|
|
400
|
+
args_schema: The schema for the tool's input arguments.
|
|
401
|
+
name: The name of the tool.
|
|
402
|
+
description: The description of the tool.
|
|
403
|
+
arg_types: The types of the arguments.
|
|
404
404
|
|
|
405
405
|
Returns:
|
|
406
406
|
The tool.
|
langchain_core/tools/render.py
CHANGED
|
@@ -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:
|
|
@@ -81,7 +81,7 @@ 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.
|
|
84
|
+
document_prompt: The prompt to use for the document.
|
|
85
85
|
document_separator: The separator to use between documents. Defaults to "\n\n".
|
|
86
86
|
response_format: The tool response format. If "content" then the output of
|
|
87
87
|
the tool is interpreted as the contents of a ToolMessage. If
|
langchain_core/tools/simple.py
CHANGED
|
@@ -69,7 +69,7 @@ class Tool(BaseTool):
|
|
|
69
69
|
def _to_args_and_kwargs(
|
|
70
70
|
self, tool_input: str | dict, tool_call_id: str | None
|
|
71
71
|
) -> tuple[tuple, dict]:
|
|
72
|
-
"""Convert tool input to
|
|
72
|
+
"""Convert tool input to Pydantic model.
|
|
73
73
|
|
|
74
74
|
Args:
|
|
75
75
|
tool_input: The input to the tool.
|
|
@@ -79,8 +79,7 @@ class Tool(BaseTool):
|
|
|
79
79
|
ToolException: If the tool input is invalid.
|
|
80
80
|
|
|
81
81
|
Returns:
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
The Pydantic model args and kwargs.
|
|
84
83
|
"""
|
|
85
84
|
args, kwargs = super()._to_args_and_kwargs(tool_input, tool_call_id)
|
|
86
85
|
# For backwards compatibility. The tool must be run with a single input
|
|
@@ -177,10 +176,10 @@ class Tool(BaseTool):
|
|
|
177
176
|
func: The function to create the tool from.
|
|
178
177
|
name: The name of the tool.
|
|
179
178
|
description: The description of the tool.
|
|
180
|
-
return_direct: Whether to return the output directly. Defaults to False
|
|
181
|
-
args_schema: The schema of the tool's input arguments.
|
|
182
|
-
coroutine: The asynchronous version of the function.
|
|
183
|
-
kwargs: Additional arguments to pass to the tool.
|
|
179
|
+
return_direct: Whether to return the output directly. Defaults to `False`.
|
|
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.
|
|
184
183
|
|
|
185
184
|
Returns:
|
|
186
185
|
The tool.
|
|
@@ -149,22 +149,22 @@ class StructuredTool(BaseTool):
|
|
|
149
149
|
description: The description of the tool.
|
|
150
150
|
Defaults to the function docstring.
|
|
151
151
|
return_direct: Whether to return the result directly or as a callback.
|
|
152
|
-
Defaults to False
|
|
153
|
-
args_schema: The schema of the tool's input arguments.
|
|
152
|
+
Defaults to `False`.
|
|
153
|
+
args_schema: The schema of the tool's input arguments.
|
|
154
154
|
infer_schema: Whether to infer the schema from the function's signature.
|
|
155
|
-
Defaults to True
|
|
155
|
+
Defaults to `True`.
|
|
156
156
|
response_format: The tool response format. If "content" then the output of
|
|
157
157
|
the tool is interpreted as the contents of a ToolMessage. If
|
|
158
158
|
"content_and_artifact" then the output is expected to be a two-tuple
|
|
159
159
|
corresponding to the (content, artifact) of a ToolMessage.
|
|
160
160
|
Defaults to "content".
|
|
161
|
-
parse_docstring: if
|
|
161
|
+
parse_docstring: if `infer_schema` and `parse_docstring`, will attempt
|
|
162
162
|
to parse parameter descriptions from Google Style function docstrings.
|
|
163
|
-
Defaults to False
|
|
164
|
-
error_on_invalid_docstring: if
|
|
163
|
+
Defaults to `False`.
|
|
164
|
+
error_on_invalid_docstring: if `parse_docstring` is provided, configure
|
|
165
165
|
whether to raise ValueError on invalid Google Style docstrings.
|
|
166
|
-
Defaults to False
|
|
167
|
-
kwargs: Additional arguments to pass to the tool
|
|
166
|
+
Defaults to `False`.
|
|
167
|
+
**kwargs: Additional arguments to pass to the tool
|
|
168
168
|
|
|
169
169
|
Returns:
|
|
170
170
|
The tool.
|
|
@@ -173,18 +173,17 @@ class StructuredTool(BaseTool):
|
|
|
173
173
|
ValueError: If the function is not provided.
|
|
174
174
|
ValueError: If the function does not have a docstring and description
|
|
175
175
|
is not provided.
|
|
176
|
-
TypeError: If the
|
|
176
|
+
TypeError: If the `args_schema` is not a `BaseModel` or dict.
|
|
177
177
|
|
|
178
178
|
Examples:
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
179
|
+
```python
|
|
180
|
+
def add(a: int, b: int) -> int:
|
|
181
|
+
\"\"\"Add two numbers\"\"\"
|
|
182
|
+
return a + b
|
|
183
|
+
tool = StructuredTool.from_function(add)
|
|
184
|
+
tool.run(1, 2) # 3
|
|
185
|
+
|
|
186
|
+
```
|
|
188
187
|
"""
|
|
189
188
|
if func is not None:
|
|
190
189
|
source_function = func
|