工具#

langchain_core.tools.convert.tool(*, return_direct: bool = False, args_schema: type | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = 'content', parse_docstring: bool = False, error_on_invalid_docstring: bool = True) Callable[[Callable | Runnable], BaseTool][source]#
langchain_core.tools.convert.tool(name_or_callable: str, runnable: Runnable, *, return_direct: bool = False, args_schema: type | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = 'content', parse_docstring: bool = False, error_on_invalid_docstring: bool = True) BaseTool
langchain_core.tools.convert.tool(name_or_callable: Callable, *, return_direct: bool = False, args_schema: type | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = 'content', parse_docstring: bool = False, error_on_invalid_docstring: bool = True) BaseTool
langchain_core.tools.convert.tool(name_or_callable: str, *, return_direct: bool = False, args_schema: type | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = 'content', parse_docstring: bool = False, error_on_invalid_docstring: bool = True) Callable[[Callable | Runnable], BaseTool]

将函数制作成工具,可以带参数或不带参数使用。

Parameters:
  • name_or_callable – 工具的可选名称或可调用对象,将被转换为工具。必须作为位置参数提供。

  • runnable – 可选的runnable,用于转换为工具。必须作为位置参数提供。

  • return_direct – 是否直接从工具返回,而不是继续代理循环。默认为 False。

  • args_schema – 用户可选的参数模式。 默认为 None。

  • infer_schema – 是否从函数的签名中推断参数的模式。这也使得生成的工具在其run()函数中接受字典输入。默认为True。

  • response_format – 工具响应格式。如果为“content”,则工具的输出被解释为ToolMessage的内容。如果为“content_and_artifact”,则输出应为对应于ToolMessage的(content, artifact)的二元组。默认为“content”。

  • parse_docstring – 如果 infer_schemaparse_docstring,将尝试从Google风格函数文档字符串中解析参数描述。默认为False。

  • error_on_invalid_docstring – 如果提供了 parse_docstring,配置是否在无效的 Google 风格文档字符串上引发 ValueError。默认为 True。

Returns:

该工具。

Requires:
  • 函数类型必须为 (str) -> str

  • 函数必须有一个文档字符串

示例

@tool
def search_api(query: str) -> str:
    # Searches the API for the query.
    return

@tool("search", return_direct=True)
def search_api(query: str) -> str:
    # Searches the API for the query.
    return

@tool(response_format="content_and_artifact")
def search_api(query: str) -> Tuple[str, dict]:
    return "partial json of results", {"full": "object of results"}

在版本0.2.14中添加。

解析Google风格的文档字符串:

@tool(parse_docstring=True)
def foo(bar: str, baz: int) -> str:
    """The foo.

    Args:
        bar: The bar.
        baz: The baz.
    """
    return bar

foo.args_schema.model_json_schema()
{
    "title": "foo",
    "description": "The foo.",
    "type": "object",
    "properties": {
        "bar": {
            "title": "Bar",
            "description": "The bar.",
            "type": "string"
        },
        "baz": {
            "title": "Baz",
            "description": "The baz.",
            "type": "integer"
        }
    },
    "required": [
        "bar",
        "baz"
    ]
}

请注意,默认情况下,如果文档字符串被视为无效,解析将引发 ValueError。如果文档字符串包含函数签名中不存在的参数,或者无法解析为摘要和“Args:”块,则被视为无效。示例如下:

# No args section
def invalid_docstring_1(bar: str, baz: int) -> str:
    """The foo."""
    return bar

# Improper whitespace between summary and args section
def invalid_docstring_2(bar: str, baz: int) -> str:
    """The foo.
    Args:
        bar: The bar.
        baz: The baz.
    """
    return bar

# Documented args absent from function signature
def invalid_docstring_3(bar: str, baz: int) -> str:
    """The foo.

    Args:
        banana: The bar.
        monkey: The baz.
    """
    return bar

使用工具的示例