ahvn.utils.basic.func_utils module¶
- ahvn.utils.basic.func_utils.code2func(code, func_name=None, env=None)[源代码]¶
Extract a callable function from a code snippet.
警告
This function uses exec() to execute the provided code snippet. Executing arbitrary code is a security risk and can lead to remote code execution. Only use this function with trusted code sources. Do not use it to process untrusted user input.
- 参数:
code (str) -- The code snippet containing the function definition.
func_name (Optional[str], optional) -- The name of the function to extract. Defaults to None. If None, and only one callable is found, that function will be used.
env (Optional[Dict], optional) -- The environment in which to execute the code. Defaults to None.
- 返回:
The extracted callable function.
- 返回类型:
Callable
- 抛出:
ValueError -- If no callable is found or multiple callables without specifying func_name.
- ahvn.utils.basic.func_utils.funcwrap(exec_func, sig_func)[源代码]¶
Create a wrapper function that calls exec_func but has the signature and metadata of sig_func.
- ahvn.utils.basic.func_utils.parse_docstring(func)[源代码]¶
Parse the docstring of a Python function.
- 参数:
func (Callable) -- The Python function whose docstring is to be parsed.
- 返回:
A dictionary containing the parsed components of the docstring.
- 返回类型:
Dict
- ahvn.utils.basic.func_utils.synthesize_docstring(description=None, input_schema=None, output_schema=None, style='google')[源代码]¶
Synthesize a docstring from tool specification attributes.
- 参数:
description (Optional[str], optional) -- Tool description. Defaults to None.
input_schema (Optional[Dict[str, Any]], optional) -- Parameters schema (JSON schema object). Defaults to None.
output_schema (Optional[Dict[str, Any]], optional) -- Output schema (JSON schema object). Defaults to None.
style (str, optional) -- Docstring style ('google', 'numpy', 'rest'). Defaults to 'google'.
- 返回:
The synthesized docstring.
- 返回类型:
- ahvn.utils.basic.func_utils.synthesize_def(name, input_schema=None, output_schema=None, docstring=None, code='pass')[源代码]¶
Generate a Python function definition from schema metadata.
- ahvn.utils.basic.func_utils.synthesize_signature(name, input_schema=None, arguments=None)[源代码]¶
Generate a Python function call signature with provided arguments and default values.
- 参数:
name (
str) -- The function name.input_schema (
Optional[Dict[str,Any]]) -- JSON schema for function input_schema (same format as synthesize_def).arguments (
Optional[Dict[str,Any]]) -- Dict of argument values to include in the signature. Missing arguments will use their default values from the schema.
- 返回:
The function call signature, e.g., "f(a=1, b=5)".
- 返回类型:
示例
>>> synthesize_signature("f", {"type": "object", "properties": {"a": {"type": "int"}, "b": {"type": "int", "default": 5}}}, {"a": 1}) 'f(a=1, b=5)'