gen_ai_hub.proxy.core.proxy_clients module

class gen_ai_hub.proxy.core.proxy_clients.ProxyClients

Bases: object

Catalog for proxy client classes.

__init__() None

Initializes a ProxyClients instance.

register(name: str)

Decorator to register a proxy client class.

Parameters:

name (str) – The name to register the proxy client class under.

Raises:

ValueError – If the provided class is not a subclass of BaseProxyClient.

Returns:

A wrapper function.

Return type:

Callable

get_proxy_cls(proxy_version: str | None = None) type[BaseProxyClient]

Get the proxy client class for the given version.

Parameters:

proxy_version (str | None, optional) – The proxy version.

Returns:

The proxy client class.

Return type:

type[BaseProxyClient]

get_proxy_cls_name(proxy_client_cls: type[BaseProxyClient] | BaseProxyClient) str

Get the name of the proxy client class.

Parameters:

proxy_client_cls (type[BaseProxyClient] | BaseProxyClient) – The proxy client class or instance.

Raises:
  • ValueError – If the provided class is not a subclass of BaseProxyClient.

  • KeyError – If the class is not registered.

Returns:

The name of the proxy client class.

Return type:

str

gen_ai_hub.proxy.core.proxy_clients.proxy_version_context(proxy_version: str, catalog: ProxyClients | None = None) None

Context manager to set a thread-local proxy version.

Parameters:
  • proxy_version (str) – The proxy version to set.

  • catalog (Optional[ProxyClients], optional) – The catalog for which the proxy version is to be set. If none is provided, the proxy version is set for the default proxy_clients catalog.

Raises:

ValueError – If proxy_version is not a string.

gen_ai_hub.proxy.core.proxy_clients.set_proxy_version(proxy_version: str, catalog: ProxyClients | None = None) None

Set the global proxy version.

Parameters:
  • proxy_version (str) – The proxy version to set.

  • catalog (Optional[ProxyClients], optional) – The catalog for which the proxy version is to be set. If none is provided, the proxy version is set for the default proxy_clients catalog.

Raises:

ValueError – If proxy_version is not a string.

gen_ai_hub.proxy.core.proxy_clients.get_proxy_version(catalog: ProxyClients | None = None) str

Get the current proxy version. The version is selected in the following order:

  • thread-local overwrite (set with proxy_version_context)

  • global overwrite (set with set_proxy_version)

  • environment variable (LLM_PROXY_VERSION)

  • first registered proxy version

Parameters:

catalog (Optional[ProxyClients], optional) – The catalog from which to get the proxy version. If none is provided,

Raises:

ValueError – If no proxy version is set.

Returns:

The current proxy version.

Return type:

str

gen_ai_hub.proxy.core.proxy_clients.get_proxy_client(proxy_version: str | None = None, catalog: ProxyClients | None = None, **kwargs) BaseProxyClient

Get a proxy client for the given proxy version.

Parameters:
  • proxy_version (str | None, optional) – The version of the proxy client to retrieve. If not provided, the function will attempt to retrieve the version using the get_proxy_version function.

  • catalog (Optional[ProxyClients], optional) – The catalog from which to retrieve the proxy client. If not provided, the function will default to the proxy_clients catalog

  • kwargs (dict) – Arbitrary keyword arguments that will be passed to the constructor of the proxy client class.

Returns:

An instance of the proxy client.

Return type:

BaseProxyClient