oai.client_utils
客户端类的实用工具
validate_parameter
def validate_parameter(params: Dict[str, Any], param_name: str,
allowed_types: Tuple, allow_None: bool,
default_value: Any, numerical_bound: Tuple,
allowed_values: list) -> Any
验证给定的配置参数,检查其类型、值,并设置默认值
参数:
params
Dict[str, Any] - 包含需要验证参数的字典。param_name
str - 要验证的参数的名称。allowed_types
Tuple - 参数可接受类型的元组。allow_None
bool - 该参数是否可以设置为None
。default_value
Any - 如果参数无效或缺失时使用的默认值。 numerical_bound (Optional[Tuple[Optional[float], Optional[float]]]): 一个元组,用于指定数值参数的下限和上限。 如果不适用,每个边界可以是None
。allowed_values
Optional[List[Any]] - 参数的允许值列表。 如果不需要特定值,可以为None
。
返回:
Any
- 验证后的参数值或如果验证失败则使用默认值。
引发:
-
TypeError
- 如果提供了allowed_values
但它不是一个列表。示例用法:
# Validating a numerical parameter within specific bounds
params = {"temperature": 0.5, "safety_model": "Meta-Llama/Llama-Guard-7b"}
temperature = validate_parameter(params, "temperature", (int, float), True, 0.7, (0, 1), None)
# Result: 0.5
# Validating a parameter that can be one of a list of allowed values
model = validate_parameter(
params, "safety_model", str, True, None, None, ["Meta-Llama/Llama-Guard-7b", "Meta-Llama/Llama-Guard-13b"]
)
# If "safety_model" is missing or invalid in params, defaults to "default"
should_hide_tools
def should_hide_tools(messages: List[Dict[str, Any]], tools: List[Dict[str,
Any]],
hide_tools_param: str) -> bool
确定是否应该隐藏工具。此函数用于在工具运行后隐藏它们,从而最小化LLM在不应选择它们时选择它们的可能性。
参数:
messages
List[Dict[str, Any]] - 消息列表tools
List[Dict[str, Any]] - 工具列表hide_tools_param
str - "hide_tools" 参数值。可以是 "if_all_run"(如果所有工具都已运行,则隐藏工具)、"if_any_run"(如果任何工具已运行,则隐藏工具)、"never"(从不隐藏工具)。默认值为 "never"。
返回:
-
bool
- 表示是否应排除工具在响应创建请求中示例用法:
# Validating a numerical parameter within specific bounds
messages = params.get("messages", [])
tools = params.get("tools", None)
hide_tools = should_hide_tools(messages, tools, params["hide_tools"])