validate_parameter

validate_parameter(
    params: dict[str, Any],
    param_name: str,
    allowed_types: tuple[Any, ...],
    allow_None: bool,
    default_value: Any,
    numerical_bound: tuple[float | None, float | None] | None,
    allowed_values: list[Any] | None
) -> Any

Validates a given config parameter, checking its type, values, and setting defaults Parameters:
params (Dict[str, Any]): Dictionary containing parameters to validate.
param_name (str): The name of the parameter to validate.
allowed_types (Tuple): Tuple of acceptable types for the parameter.
allow_None (bool): Whether the parameter can be None.
default_value (Any): The default value to use if the parameter is invalid or missing.
numerical_bound (Optional[Tuple[Optional[float], Optional[float]]]):
A tuple specifying the lower and upper bounds for numerical parameters.
Each bound can be None if not applicable.
allowed_values (Optional[List[Any]]): A list of acceptable values for the parameter.
Can be None if no specific values are required.
Returns:
Any: The validated parameter value or the default value if validation fails.
Raises:
TypeError: If allowed_values is provided but is not a list.
Example Usage:

    # 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"
Parameters:
NameDescription
paramsType: dict[str, typing.Any]
param_nameType: str
allowed_typesType: tuple[typing.Any, …]
allow_NoneType: bool
default_valueType: Any
numerical_boundType: tuple[float | None, float | None] | None
allowed_valuesType: list[typing.Any] | None
Returns:
TypeDescription
AnyAny: The validated parameter value or the default value if validation fails.