|
|
|
@ -249,7 +249,16 @@ def _infer_arg_descriptions(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _SchemaConfig:
|
|
|
|
|
"""Configuration for the pydantic model."""
|
|
|
|
|
"""Configuration for the pydantic model.
|
|
|
|
|
|
|
|
|
|
This is used to configure the pydantic model created from
|
|
|
|
|
a function's signature.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
extra: Whether to allow extra fields in the model.
|
|
|
|
|
arbitrary_types_allowed: Whether to allow arbitrary types in the model.
|
|
|
|
|
Defaults to True.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
extra: Any = Extra.forbid
|
|
|
|
|
arbitrary_types_allowed: bool = True
|
|
|
|
@ -265,15 +274,18 @@ def create_schema_from_function(
|
|
|
|
|
) -> Type[BaseModel]:
|
|
|
|
|
"""Create a pydantic schema from a function's signature.
|
|
|
|
|
Args:
|
|
|
|
|
model_name: Name to assign to the generated pydandic schema
|
|
|
|
|
func: Function to generate the schema from
|
|
|
|
|
filter_args: Optional list of arguments to exclude from the schema
|
|
|
|
|
model_name: Name to assign to the generated pydantic schema.
|
|
|
|
|
func: Function to generate the schema from.
|
|
|
|
|
filter_args: Optional list of arguments to exclude from the schema.
|
|
|
|
|
Defaults to FILTERED_ARGS.
|
|
|
|
|
parse_docstring: Whether to parse the function's docstring for descriptions
|
|
|
|
|
for each argument.
|
|
|
|
|
error_on_invalid_docstring: if ``parse_docstring`` is provided, configures
|
|
|
|
|
for each argument. Defaults to False.
|
|
|
|
|
error_on_invalid_docstring: if ``parse_docstring`` is provided, configure
|
|
|
|
|
whether to raise ValueError on invalid Google Style docstrings.
|
|
|
|
|
Defaults to False.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
A pydantic model with the same arguments as the function
|
|
|
|
|
A pydantic model with the same arguments as the function.
|
|
|
|
|
"""
|
|
|
|
|
# https://docs.pydantic.dev/latest/usage/validation_decorator/
|
|
|
|
|
validated = validate_arguments(func, config=_SchemaConfig) # type: ignore
|
|
|
|
@ -348,8 +360,9 @@ class ChildTool(BaseTool):
|
|
|
|
|
args_schema: Optional[Type[BaseModel]] = None
|
|
|
|
|
"""Pydantic model class to validate and parse the tool's input arguments."""
|
|
|
|
|
return_direct: bool = False
|
|
|
|
|
"""Whether to return the tool's output directly. Setting this to True means
|
|
|
|
|
"""Whether to return the tool's output directly.
|
|
|
|
|
|
|
|
|
|
Setting this to True means
|
|
|
|
|
that after the tool is called, the AgentExecutor will stop looping.
|
|
|
|
|
"""
|
|
|
|
|
verbose: bool = False
|
|
|
|
@ -360,13 +373,13 @@ class ChildTool(BaseTool):
|
|
|
|
|
callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
|
|
|
|
|
"""Deprecated. Please use callbacks instead."""
|
|
|
|
|
tags: Optional[List[str]] = None
|
|
|
|
|
"""Optional list of tags associated with the tool. Defaults to None
|
|
|
|
|
"""Optional list of tags associated with the tool. Defaults to None.
|
|
|
|
|
These tags will be associated with each call to this tool,
|
|
|
|
|
and passed as arguments to the handlers defined in `callbacks`.
|
|
|
|
|
You can use these to eg identify a specific instance of a tool with its use case.
|
|
|
|
|
"""
|
|
|
|
|
metadata: Optional[Dict[str, Any]] = None
|
|
|
|
|
"""Optional metadata associated with the tool. Defaults to None
|
|
|
|
|
"""Optional metadata associated with the tool. Defaults to None.
|
|
|
|
|
This metadata will be associated with each call to this tool,
|
|
|
|
|
and passed as arguments to the handlers defined in `callbacks`.
|
|
|
|
|
You can use these to eg identify a specific instance of a tool with its use case.
|
|
|
|
@ -383,7 +396,7 @@ class ChildTool(BaseTool):
|
|
|
|
|
"""Handle the content of the ValidationError thrown."""
|
|
|
|
|
|
|
|
|
|
response_format: Literal["content", "content_and_artifact"] = "content"
|
|
|
|
|
"""The tool response format.
|
|
|
|
|
"""The tool response format. Defaults to 'content'.
|
|
|
|
|
|
|
|
|
|
If "content" then the output of the tool is interpreted as the contents of a
|
|
|
|
|
ToolMessage. If "content_and_artifact" then the output is expected to be a
|
|
|
|
@ -414,7 +427,14 @@ class ChildTool(BaseTool):
|
|
|
|
|
def get_input_schema(
|
|
|
|
|
self, config: Optional[RunnableConfig] = None
|
|
|
|
|
) -> Type[BaseModel]:
|
|
|
|
|
"""The tool's input schema."""
|
|
|
|
|
"""The tool's input schema.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
config: The configuration for the tool.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The input schema for the tool.
|
|
|
|
|
"""
|
|
|
|
|
if self.args_schema is not None:
|
|
|
|
|
return self.args_schema
|
|
|
|
|
else:
|
|
|
|
@ -441,7 +461,11 @@ class ChildTool(BaseTool):
|
|
|
|
|
# --- Tool ---
|
|
|
|
|
|
|
|
|
|
def _parse_input(self, tool_input: Union[str, Dict]) -> Union[str, Dict[str, Any]]:
|
|
|
|
|
"""Convert tool input to pydantic model."""
|
|
|
|
|
"""Convert tool input to a pydantic model.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tool_input: The input to the tool.
|
|
|
|
|
"""
|
|
|
|
|
input_args = self.args_schema
|
|
|
|
|
if isinstance(tool_input, str):
|
|
|
|
|
if input_args is not None:
|
|
|
|
@ -460,7 +484,14 @@ class ChildTool(BaseTool):
|
|
|
|
|
|
|
|
|
|
@root_validator(pre=True)
|
|
|
|
|
def raise_deprecation(cls, values: Dict) -> Dict:
|
|
|
|
|
"""Raise deprecation warning if callback_manager is used."""
|
|
|
|
|
"""Raise deprecation warning if callback_manager is used.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
values: The values to validate.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The validated values.
|
|
|
|
|
"""
|
|
|
|
|
if values.get("callback_manager") is not None:
|
|
|
|
|
warnings.warn(
|
|
|
|
|
"callback_manager is deprecated. Please use callbacks instead.",
|
|
|
|
@ -514,7 +545,28 @@ class ChildTool(BaseTool):
|
|
|
|
|
tool_call_id: Optional[str] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> Any:
|
|
|
|
|
"""Run the tool."""
|
|
|
|
|
"""Run the tool.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tool_input: The input to the tool.
|
|
|
|
|
verbose: Whether to log the tool's progress. Defaults to None.
|
|
|
|
|
start_color: The color to use when starting the tool. Defaults to 'green'.
|
|
|
|
|
color: The color to use when ending the tool. Defaults to 'green'.
|
|
|
|
|
callbacks: Callbacks to be called during tool execution. Defaults to None.
|
|
|
|
|
tags: Optional list of tags associated with the tool. Defaults to None.
|
|
|
|
|
metadata: Optional metadata associated with the tool. Defaults to None.
|
|
|
|
|
run_name: The name of the run. Defaults to None.
|
|
|
|
|
run_id: The id of the run. Defaults to None.
|
|
|
|
|
config: The configuration for the tool. Defaults to None.
|
|
|
|
|
tool_call_id: The id of the tool call. Defaults to None.
|
|
|
|
|
kwargs: Additional arguments to pass to the tool
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The output of the tool.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ToolException: If an error occurs during tool execution.
|
|
|
|
|
"""
|
|
|
|
|
callback_manager = CallbackManager.configure(
|
|
|
|
|
callbacks,
|
|
|
|
|
self.callbacks,
|
|
|
|
@ -600,7 +652,28 @@ class ChildTool(BaseTool):
|
|
|
|
|
tool_call_id: Optional[str] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> Any:
|
|
|
|
|
"""Run the tool asynchronously."""
|
|
|
|
|
"""Run the tool asynchronously.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tool_input: The input to the tool.
|
|
|
|
|
verbose: Whether to log the tool's progress. Defaults to None.
|
|
|
|
|
start_color: The color to use when starting the tool. Defaults to 'green'.
|
|
|
|
|
color: The color to use when ending the tool. Defaults to 'green'.
|
|
|
|
|
callbacks: Callbacks to be called during tool execution. Defaults to None.
|
|
|
|
|
tags: Optional list of tags associated with the tool. Defaults to None.
|
|
|
|
|
metadata: Optional metadata associated with the tool. Defaults to None.
|
|
|
|
|
run_name: The name of the run. Defaults to None.
|
|
|
|
|
run_id: The id of the run. Defaults to None.
|
|
|
|
|
config: The configuration for the tool. Defaults to None.
|
|
|
|
|
tool_call_id: The id of the tool call. Defaults to None.
|
|
|
|
|
kwargs: Additional arguments to pass to the tool
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The output of the tool.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ToolException: If an error occurs during tool execution.
|
|
|
|
|
"""
|
|
|
|
|
callback_manager = AsyncCallbackManager.configure(
|
|
|
|
|
callbacks,
|
|
|
|
|
self.callbacks,
|
|
|
|
@ -709,7 +782,11 @@ class Tool(BaseTool):
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def args(self) -> dict:
|
|
|
|
|
"""The tool's input arguments."""
|
|
|
|
|
"""The tool's input arguments.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The input arguments for the tool.
|
|
|
|
|
"""
|
|
|
|
|
if self.args_schema is not None:
|
|
|
|
|
return self.args_schema.schema()["properties"]
|
|
|
|
|
# For backwards compatibility, if the function signature is ambiguous,
|
|
|
|
@ -788,7 +865,23 @@ class Tool(BaseTool):
|
|
|
|
|
] = None, # This is last for compatibility, but should be after func
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> Tool:
|
|
|
|
|
"""Initialize tool from a function."""
|
|
|
|
|
"""Initialize tool from a function.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
func: The function to create the tool from.
|
|
|
|
|
name: The name of the tool.
|
|
|
|
|
description: The description of the tool.
|
|
|
|
|
return_direct: Whether to return the output directly. Defaults to False.
|
|
|
|
|
args_schema: The schema of the tool's input arguments. Defaults to None.
|
|
|
|
|
coroutine: The asynchronous version of the function. Defaults to None.
|
|
|
|
|
**kwargs: Additional arguments to pass to the tool.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The tool.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ValueError: If the function is not provided.
|
|
|
|
|
"""
|
|
|
|
|
if func is None and coroutine is None:
|
|
|
|
|
raise ValueError("Function and/or coroutine must be provided")
|
|
|
|
|
return cls(
|
|
|
|
@ -893,25 +986,34 @@ class StructuredTool(BaseTool):
|
|
|
|
|
A classmethod that helps to create a tool from a function.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
func: The function from which to create a tool
|
|
|
|
|
coroutine: The async function from which to create a tool
|
|
|
|
|
name: The name of the tool. Defaults to the function name
|
|
|
|
|
description: The description of the tool. Defaults to the function docstring
|
|
|
|
|
return_direct: Whether to return the result directly or as a callback
|
|
|
|
|
args_schema: The schema of the tool's input arguments
|
|
|
|
|
infer_schema: Whether to infer the schema from the function's signature
|
|
|
|
|
func: The function from which to create a tool.
|
|
|
|
|
coroutine: The async function from which to create a tool.
|
|
|
|
|
name: The name of the tool. Defaults to the function name.
|
|
|
|
|
description: The description of the tool.
|
|
|
|
|
Defaults to the function docstring.
|
|
|
|
|
return_direct: Whether to return the result directly or as a callback.
|
|
|
|
|
Defaults to False.
|
|
|
|
|
args_schema: The schema of the tool's input arguments. Defaults to None.
|
|
|
|
|
infer_schema: Whether to infer the schema from the function's signature.
|
|
|
|
|
Defaults to True.
|
|
|
|
|
response_format: The tool response format. If "content" then the output of
|
|
|
|
|
the tool is interpreted as the contents of a ToolMessage. If
|
|
|
|
|
"content_and_artifact" then the output is expected to be a two-tuple
|
|
|
|
|
corresponding to the (content, artifact) of a ToolMessage.
|
|
|
|
|
Defaults to "content".
|
|
|
|
|
parse_docstring: if ``infer_schema`` and ``parse_docstring``, will attempt
|
|
|
|
|
to parse parameter descriptions from Google Style function docstrings.
|
|
|
|
|
error_on_invalid_docstring: if ``parse_docstring`` is provided, configures
|
|
|
|
|
Defaults to False.
|
|
|
|
|
error_on_invalid_docstring: if ``parse_docstring`` is provided, configure
|
|
|
|
|
whether to raise ValueError on invalid Google Style docstrings.
|
|
|
|
|
Defaults to False.
|
|
|
|
|
**kwargs: Additional arguments to pass to the tool
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The tool
|
|
|
|
|
The tool.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ValueError: If the function is not provided.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
@ -989,19 +1091,27 @@ def tool(
|
|
|
|
|
Args:
|
|
|
|
|
*args: The arguments to the tool.
|
|
|
|
|
return_direct: Whether to return directly from the tool rather
|
|
|
|
|
than continuing the agent loop.
|
|
|
|
|
args_schema: optional argument schema for user to specify
|
|
|
|
|
than continuing the agent loop. Defaults to False.
|
|
|
|
|
args_schema: optional argument schema for user to specify.
|
|
|
|
|
Defaults to None.
|
|
|
|
|
infer_schema: Whether to infer the schema of the arguments from
|
|
|
|
|
the function's signature. This also makes the resultant tool
|
|
|
|
|
accept a dictionary input to its `run()` function.
|
|
|
|
|
Defaults to True.
|
|
|
|
|
response_format: The tool response format. If "content" then the output of
|
|
|
|
|
the tool is interpreted as the contents of a ToolMessage. If
|
|
|
|
|
"content_and_artifact" then the output is expected to be a two-tuple
|
|
|
|
|
corresponding to the (content, artifact) of a ToolMessage.
|
|
|
|
|
Defaults to "content".
|
|
|
|
|
parse_docstring: if ``infer_schema`` and ``parse_docstring``, will attempt to
|
|
|
|
|
parse parameter descriptions from Google Style function docstrings.
|
|
|
|
|
error_on_invalid_docstring: if ``parse_docstring`` is provided, configures
|
|
|
|
|
Defaults to False.
|
|
|
|
|
error_on_invalid_docstring: if ``parse_docstring`` is provided, configure
|
|
|
|
|
whether to raise ValueError on invalid Google Style docstrings.
|
|
|
|
|
Defaults to True.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The tool.
|
|
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
- Function must be of type (str) -> str
|
|
|
|
@ -1230,9 +1340,11 @@ def create_retriever_tool(
|
|
|
|
|
so should be unique and somewhat descriptive.
|
|
|
|
|
description: The description for the tool. This will be passed to the language
|
|
|
|
|
model, so should be descriptive.
|
|
|
|
|
document_prompt: The prompt to use for the document. Defaults to None.
|
|
|
|
|
document_separator: The separator to use between documents. Defaults to "\n\n".
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Tool class to pass to an agent
|
|
|
|
|
Tool class to pass to an agent.
|
|
|
|
|
"""
|
|
|
|
|
document_prompt = document_prompt or PromptTemplate.from_template("{page_content}")
|
|
|
|
|
func = partial(
|
|
|
|
@ -1262,6 +1374,12 @@ ToolsRenderer = Callable[[List[BaseTool]], str]
|
|
|
|
|
def render_text_description(tools: List[BaseTool]) -> str:
|
|
|
|
|
"""Render the tool name and description in plain text.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tools: The tools to render.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The rendered text.
|
|
|
|
|
|
|
|
|
|
Output will be in the format of:
|
|
|
|
|
|
|
|
|
|
.. code-block:: markdown
|
|
|
|
@ -1284,6 +1402,12 @@ def render_text_description(tools: List[BaseTool]) -> str:
|
|
|
|
|
def render_text_description_and_args(tools: List[BaseTool]) -> str:
|
|
|
|
|
"""Render the tool name, description, and args in plain text.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tools: The tools to render.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The rendered text.
|
|
|
|
|
|
|
|
|
|
Output will be in the format of:
|
|
|
|
|
|
|
|
|
|
.. code-block:: markdown
|
|
|
|
@ -1444,7 +1568,18 @@ def convert_runnable_to_tool(
|
|
|
|
|
description: Optional[str] = None,
|
|
|
|
|
arg_types: Optional[Dict[str, Type]] = None,
|
|
|
|
|
) -> BaseTool:
|
|
|
|
|
"""Convert a Runnable into a BaseTool."""
|
|
|
|
|
"""Convert a Runnable into a BaseTool.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
runnable: The runnable to convert.
|
|
|
|
|
args_schema: The schema for the tool's input arguments. Defaults to None.
|
|
|
|
|
name: The name of the tool. Defaults to None.
|
|
|
|
|
description: The description of the tool. Defaults to None.
|
|
|
|
|
arg_types: The types of the arguments. Defaults to None.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The tool.
|
|
|
|
|
"""
|
|
|
|
|
if args_schema:
|
|
|
|
|
runnable = runnable.with_types(input_type=args_schema)
|
|
|
|
|
description = description or _get_description_from_runnable(runnable)
|
|
|
|
|