gen_ai_hub.proxy.core.utils module

gen_ai_hub.proxy.core.utils.lru_cache_extended(timeout: int | None = None, maxsize: int | None = None, typed: bool = False, first_arg_self: bool = False)

Decorator to add LRU caching with optional timeout to methods. Handles ‘self’ as a weak reference for instance methods if required.

Parameters:
  • timeout (Optional[int], optional) – time in seconds after which the cache will be refreshed. If None, never expires.

  • maxsize (Optional[int], optional) – maximum size of the cache.

  • typed (bool, optional) – if True, arguments of different types will be cached separately.

  • first_arg_self (bool, optional) – if True, treats the first argument as ‘self’ and uses its id for caching.

Returns:

Decorated method with cache and optional timeout.

Return type:

Callable

gen_ai_hub.proxy.core.utils.if_set(value, alternative=NOT_GIVEN)

Check if a value is set (not NotGiven and not None), otherwise return an alternative.

Parameters:
  • value (any) – the value to check.

  • alternative (any) – the alternative value to return if the original value is not set.

Returns:

The original value if set, otherwise the alternative.

Return type:

any

gen_ai_hub.proxy.core.utils.if_str_set(value: str, alternative: str = '')

Check if a string value is set (not empty), otherwise return an alternative.

Parameters:
  • value (str) – the string value to check.

  • alternative (str, optional) – the alternative string to return if the original value is empty.

Returns:

The original string if not empty, otherwise the alternative.

Return type:

str

gen_ai_hub.proxy.core.utils.kwargs_if_set(**kwargs)

Filter keyword arguments to include only those that are set (not NotGiven and not None).

Returns:

A dictionary of keyword arguments that are set.

Return type:

Dict[str, any]

gen_ai_hub.proxy.core.utils.warn_once(msg, category=None)

Issue a warning only once for a given message.

Parameters:
  • msg (str) – the warning message.

  • category (Optional[Warning], optional) – the warning category.