tooluniverse.base_tool module¶
- class tooluniverse.base_tool.BaseTool[源代码]¶
基类:
object- STATIC_CACHE_VERSION = '1'¶
- classmethod get_default_config_file()[源代码]¶
获取此工具类型的默认配置文件路径。
该方法采用了一种稳健的路径解析策略,适用于不同的安装场景:
已安装的软件包:使用 importlib.resources 以实现正确的软件包资源访问
开发模式:回退到基于文件的路径解析
遗留版 Python:处理 importlib.resources 和 importlib_resources
在子类中重写此方法以指定自定义默认值文件。
- 退货
指向默认文件的路径或资源对象
- run(arguments=None, stream_callback=None, use_cache=False, validate=True)[源代码]¶
执行该工具。
默认的 BaseTool 实现接受一个可选的参数映射,以便与大多数具体工具实现保持一致,这些工具通常期望输入为字典形式。
- 参数:
arguments (
dict, optional) – 工具专用参数stream_callback (
callable, optional) – 用于流式响应的回调use_cache (
bool, optional) – 是否启用了结果缓存validate (
bool, optional) – 是否已执行参数验证
备注
这些附加参数(stream_callback、use_cache、validate)由 run_one_function() 传递,用于提供有关执行的上下文。工具可以利用这些参数进行优化或特殊处理。
为了向后兼容,不接受这些参数的工具仍然可以正常工作——它们只会接收到 arguments 参数。
- get_schema_const_operation()[源代码]¶
Return the operation value from the tool’s parameter schema, or empty string.
Checks const first (single fixed value), then falls back to the first value in enum (single-value enum is equivalent to const).
- get_required_parameters()[源代码]¶
Retrieve required parameters from the endpoint definition. Returns list: List of required parameters for the given endpoint.
- validate_parameters(arguments)[源代码]¶
根据工具架构验证参数。
此方法使用 jsonschema 提供标准参数验证。子类可以重写此方法以实现自定义验证逻辑。
- 退货
验证失败时返回 ToolError,验证通过时返回 None
- _ERROR_CLASSIFICATION = [({'401', '403', 'api key', 'auth', 'token', 'unauthorized'}, <class 'tooluniverse.exceptions.ToolAuthError'>, 'Authentication failed'), ({'429', 'limit exceeded', 'quota', 'rate limit'}, <class 'tooluniverse.exceptions.ToolRateLimitError'>, 'Rate limit exceeded'), ({'404', 'connection', 'network', 'not found', 'timeout', 'unavailable'}, <class 'tooluniverse.exceptions.ToolUnavailableError'>, 'Tool unavailable'), ({'invalid', 'parameter', 'schema', 'validation'}, <class 'tooluniverse.exceptions.ToolValidationError'>, 'Validation error'), ({'config', 'configuration', 'setup'}, <class 'tooluniverse.exceptions.ToolConfigError'>, 'Configuration error'), ({'dependency', 'import', 'module', 'package'}, <class 'tooluniverse.exceptions.ToolDependencyError'>, 'Dependency error')]¶
- handle_error(exception)[源代码]¶
将原始异常分类为结构化的 ToolError。
此方法提供标准错误分类。子类可以重写此方法以实现自定义错误处理逻辑。
- 参数:
exception (Exception) – 用于分类的原始异常
- 退货
结构化 ToolError 实例