function_utils
get_typed_annotation
def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any
获取参数的类型注解。
参数:
annotation
- 参数的注解globalns
- 函数的全局命名空间
返回值:
参数的类型注解
get_typed_signature
def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature
获取带有类型注解的函数的签名。
参数:
call
- 要获取签名的函数
返回值:
带有类型注解的函数的签名
get_typed_return_annotation
def get_typed_return_annotation(call: Callable[..., Any]) -> Any
获取函数的返回注解。
参数:
call
- 要获取返回注解的函数
返回值:
函数的返回注解
get_param_annotations
def get_param_annotations(
typed_signature: inspect.Signature
) -> Dict[str, Union[Annotated[Type[Any], str], Type[Any]]]
获取函数参数的类型注解。
参数:
typed_signature
- 带有类型注解的函数的签名
返回值:
函数参数的类型注解字典
Parameters
class Parameters(BaseModel)
由 OpenAI API 定义的函数参数
Function
class Function(BaseModel)
由 OpenAI API 定义的函数
ToolFunction
class ToolFunction(BaseModel)
由 OpenAI API 定义的工具函数。
get_parameter_json_schema
def get_parameter_json_schema(
k: str, v: Any, default_values: Dict[str, Any]) -> JsonSchemaValue
根据 OpenAI API 定义,获取参数的 JSON 模式。
参数:
k
- 参数的名称v
- 参数的类型default_values
- 函数参数的默认值
返回值:
参数的 Pydantic 模型
get_required_params
def get_required_params(typed_signature: inspect.Signature) -> List[str]
获取函数的必需参数。
参数:
signature
- 使用 inspect.signature 返回的函数签名
返回值:
函数的必需参数列表
get_default_values
def get_default_values(typed_signature: inspect.Signature) -> Dict[str, Any]
获取函数参数的默认值。
参数:
signature
- 使用 inspect.signature 返回的函数签名
返回值:
函数参数的默认值字典
get_parameters
def get_parameters(required: List[str],
param_annotations: Dict[str, Union[Annotated[Type[Any],
str],
Type[Any]]],
default_values: Dict[str, Any]) -> Parameters
def get_parameters(required: List[str],
param_annotations: Dict[str, Union[Annotated[Type[Any],
str],
Type[Any]]],
default_values: Dict[str, Any]) -> Parameters
这是一个名为 get_parameters
的函数,它接受三个参数:required
、param_annotations
和 default_values
,并返回一个 Parameters
对象。
required
是一个字符串列表,表示必需的参数。param_annotations
是一个字典,用于存储参数的注释信息。它的键是参数的名称,值是一个联合类型,可以是带注释的参数类型(Annotated[Type[Any], str]
)或普通的参数类型(Type[Any]
)。default_values
是一个字典,用于存储参数的默认值。它的键是参数的名称,值是参数的默认值。
函数的返回类型是 Parameters
。
请注意,List
、Dict
、Union
和 Annotated
是 Python 中的类型注解,用于指定参数和返回值的类型。
获取由OpenAI API定义的函数的参数
参数:
required
- 函数的必需参数hints
- 函数的类型提示,由typing.get_type_hints返回
返回值:
函数参数的Pydantic模型
get_missing_annotations
def get_missing_annotations(typed_signature: inspect.Signature,
required: List[str]) -> Tuple[Set[str], Set[str]]
获取函数的缺失注释
忽略具有默认值的参数,因为它们不需要注释,但会记录警告。
参数:
typed_signature
- 带有类型注释的函数签名required
- 函数的必需参数
返回值:
函数的缺失注释的集合
get_function_schema
def get_function_schema(f: Callable[..., Any],
*,
name: Optional[str] = None,
description: str) -> Dict[str, Any]
获取由OpenAI API定义的函数的JSON模式
参数:
f
- 要获取JSON模式的函数name
- 函数的名称description
- 函数 的描述
返回值:
函数的JSON模式
抛出:
TypeError
- 如果函数没有注释
示例:
def f(a: Annotated[str, "Parameter a"], b: int = 2, c: Annotated[float, "Parameter c"] = 0.1) -> None:
pass
get_function_schema(f, description="function f")
# {'type': 'function',
# 'function': {'description': 'function f',
# 'name': 'f',
# 'parameters': {'type': 'object',
# 'properties': {'a': {'type': 'str', 'description': 'Parameter a'},
# 'b': {'type': 'int', 'description': 'b'},
# 'c': {'type': 'float', 'description': 'Parameter c'}},
# 'required': ['a']}}}
get_load_param_if_needed_function
def get_load_param_if_needed_function(
t: Any
) -> Optional[Callable[[Dict[str, Any], Type[BaseModel]], BaseModel]]
获取一个函数,用于加载参数,如果参数是Pydantic模型的话
参数:
t
- 参数的类型注释
返回值:
如果参数是Pydantic模型,则返回一个加载参数的函数,否则返回None
load_basemodels_if_needed
def load_basemodels_if_needed(func: Callable[..., Any]) -> Callable[..., Any]
一个装饰器,用于在参数是Pydantic模型时加载函数的参数
参数:
func
- 带有注释参数的函数
返回值:
在调用原始函数之前加载参数的函数