The llm_wire.py module#
Summary#
Resolved chat endpoint configuration. |
|
Retry/backoff policy for LLM transport calls. |
|
Resolved provider and transport settings for an LLM call. |
|
A single resolved chat-model entry from the AALI models config. |
Raised when an LLM transport call fails. |
Read a boolean environment flag using common truthy and falsy strings. |
|
Resolve TLS verification settings for outbound LLM HTTP requests. |
|
Return the first model identifier from a comma-separated model list. |
|
Normalize an LLM endpoint URL by removing trailing slashes. |
|
Resolve provider, transport, endpoint, and model settings from the environment. |
|
Infer the LLM provider from an explicit provider, model name, or endpoint URL. |
|
Resolve the LiteLLM model route and API key for the selected provider. |
|
Return whether a native provider has enough configuration to run. |
|
Resolve the full LLM wire profile used for model calls. |
|
Resolve request-format quirks required by the selected model. |
|
Return the max-token request parameter name for a provider and model. |
|
Return whether temperature should be sent for a provider and model. |
|
Build an OpenAI-compatible chat-completions request body. |
|
Build keyword arguments for a LiteLLM completion call. |
|
Build HTTP authorization headers for an LLM provider profile. |
|
Extract assistant text from a chat-completion provider response. |
|
Send a synchronous chat-completion request through the configured LLM transport. |
|
Send an asynchronous chat-completion request through the configured LLM transport. |
|
Return the first usable chat model from the AALI config, or |
Description#
Wire helpers for LiteLLM/OpenAI-compatible chat endpoints.
Module detail#
- llm_wire.env_flag(name: str, *, default: bool, env=os.environ) bool#
Read a boolean environment flag using common truthy and falsy strings.
- llm_wire.resolve_tls_verify(env=os.environ) bool | str#
Resolve TLS verification settings for outbound LLM HTTP requests.
- llm_wire.first_model_token(raw: str | None) str | None#
Return the first model identifier from a comma-separated model list.
- llm_wire.normalize_endpoint(url: str) str#
Normalize an LLM endpoint URL by removing trailing slashes.
- llm_wire.resolve_model_config(*, endpoint: str | None = None, api_key: str | None = None, model: str | None = None, auth_style: str | None = None, env=os.environ) ModelConfig#
Resolve provider, transport, endpoint, and model settings from the environment.
- Parameters:
- endpoint
str|None,optional Provider endpoint used for the LLM request. Default is
None.- api_key
str|None,optional API key used to authenticate the provider request. Default is
None.- model
str|None,optional Model identifier selected for the request. Default is
None.- auth_style
str|None,optional Authorization-header style required by the provider. Default is
None.- env
Any,optional Environment mapping used to resolve LLM configuration. Default is
os.environ.
- endpoint
- Returns:
ModelConfigValue computed by the helper for the requested CFX workflow.
- llm_wire.detect_provider(model: str | None, endpoint: str | None, *, env=os.environ) str#
Infer the LLM provider from an explicit provider, model name, or endpoint URL.
- Parameters:
- Returns:
strString value produced for the requested CFX or provider operation.
- llm_wire.resolve_litellm_route(provider: str, model: str) str#
Resolve the LiteLLM model route and API key for the selected provider.
- llm_wire.native_provider_configured(model: str | None, *, env=os.environ) bool#
Return whether a native provider has enough configuration to run.
- llm_wire.resolve_profile(*, model: str | None = None, endpoint: str | None = None, auth_style: str | None = None, env=os.environ) LLMProfile#
Resolve the full LLM wire profile used for model calls.
- Parameters:
- model
str|None,optional Model identifier selected for the request. Default is
None.- endpoint
str|None,optional Provider endpoint used for the LLM request. Default is
None.- auth_style
str|None,optional Authorization-header style required by the provider. Default is
None.- env
Any,optional Environment mapping used to resolve LLM configuration. Default is
os.environ.
- model
- Returns:
LLMProfileValue computed by the helper for the requested CFX workflow.
- llm_wire.resolve_model_quirks(model: str) dict[str, Any]#
Resolve request-format quirks required by the selected model.
- llm_wire.max_tokens_param_for(model: str) str#
Return the max-token request parameter name for a provider and model.
- llm_wire.send_temperature_for(model: str) bool#
Return whether temperature should be sent for a provider and model.
- llm_wire.build_chat_body(*, model: str, messages: Sequence[dict[str, Any]], max_tokens: int | None = None, temperature: float | None = None) dict[str, Any]#
Build an OpenAI-compatible chat-completions request body.
- Parameters:
- model
str Model identifier selected for the request.
- messages
Sequence[dict[str,Any]] Chat messages to send to the model.
- max_tokens
int|None,optional Maximum number of tokens requested from the model. Default is
None.- temperature
float|None,optional Sampling temperature for providers that support it. Default is
None.
- model
- Returns:
- llm_wire.build_litellm_kwargs(profile: LLMProfile, messages: Sequence[dict[str, Any]], *, max_tokens: int | None = None, temperature: float | None = None, api_key: str | None = None, api_base: str | None = None, api_version: str | None = None) dict[str, Any]#
Build keyword arguments for a LiteLLM completion call.
- Parameters:
- profile
LLMProfile Resolved LLM provider profile used to build requests.
- messages
Sequence[dict[str,Any]] Chat messages to send to the model.
- max_tokens
int|None,optional Maximum number of tokens requested from the model. Default is
None.- temperature
float|None,optional Sampling temperature for providers that support it. Default is
None.- api_key
str|None,optional API key used to authenticate the provider request. Default is
None.- api_base
str|None,optional Provider API base URL override. Default is
None.- api_version
str|None,optional Provider API version override, when required. Default is
None.
- profile
- Returns:
- llm_wire.auth_headers(api_key: str | None, *, auth_style: str = 'bearer', base: dict[str, str] | None = None) dict[str, str]#
Build HTTP authorization headers for an LLM provider profile.
- Parameters:
- Returns:
- llm_wire.extract_chat_text(payload: dict[str, Any]) str#
Extract assistant text from a chat-completion provider response.
- llm_wire.call(profile: LLMProfile, messages: Sequence[dict[str, Any]], *, max_tokens: int | None = None, temperature: float | None = None, api_key: str | None = None, api_base: str | None = None, api_version: str | None = None) dict[str, Any]#
Send a synchronous chat-completion request through the configured LLM transport.
- Parameters:
- profile
LLMProfile Resolved LLM provider profile used to build requests.
- messages
Sequence[dict[str,Any]] Chat messages to send to the model.
- max_tokens
int|None,optional Maximum number of tokens requested from the model. Default is
None.- temperature
float|None,optional Sampling temperature for providers that support it. Default is
None.- api_key
str|None,optional API key used to authenticate the provider request. Default is
None.- api_base
str|None,optional Provider API base URL override. Default is
None.- api_version
str|None,optional Provider API version override, when required. Default is
None.
- profile
- Returns:
- async llm_wire.acall(profile: LLMProfile, messages: Sequence[dict[str, Any]], *, max_tokens: int | None = None, temperature: float | None = None, api_key: str | None = None, api_base: str | None = None, api_version: str | None = None) dict[str, Any]#
Send an asynchronous chat-completion request through the configured LLM transport.
- Parameters:
- profile
LLMProfile Resolved LLM provider profile used to build requests.
- messages
Sequence[dict[str,Any]] Chat messages to send to the model.
- max_tokens
int|None,optional Maximum number of tokens requested from the model. Default is
None.- temperature
float|None,optional Sampling temperature for providers that support it. Default is
None.- api_key
str|None,optional API key used to authenticate the provider request. Default is
None.- api_base
str|None,optional Provider API base URL override. Default is
None.- api_version
str|None,optional Provider API version override, when required. Default is
None.
- profile
- Returns:
- llm_wire.load_aali_chat_model() AaliChatModel | None#
Return the first usable chat model from the AALI config, or
None.Never raises: any I/O or parse error is logged and yields
Noneso the caller can fall back to its own defaults.- Returns:
AaliChatModel|NoneResult produced by the function.
- llm_wire.DEFAULT_MODEL = 'gpt-4o-mini'#
- llm_wire.PROVIDER_OPENAI = 'openai'#
- llm_wire.PROVIDER_AZURE = 'azure'#
- llm_wire.PROVIDER_ANTHROPIC = 'anthropic'#
- llm_wire.PROVIDER_GEMINI = 'gemini'#
- llm_wire.PROVIDER_COMPAT = 'compat'#
- llm_wire.TRANSPORT_LITELLM = 'litellm'#
- llm_wire.TRANSPORT_COMPAT = 'openai_compat'#
- llm_wire.logger#