qfa.adapters.llm_client#
LLM client adapter using LiteLLM for unified provider access.
Classes
|
LLM adapter satisfying LLMPort via LiteLLM. |
- class qfa.adapters.llm_client.LiteLLMClient(model: str, api_key: str, api_base: str, api_version: str, chars_per_token: int, max_total_tokens: int)[source]#
Bases:
LLMPortLLM adapter satisfying LLMPort via LiteLLM.
Routes to any LLM provider based on the model string prefix (e.g.
"azure/gpt-4","azure_ai/mistral-large-2411"). Calculates per-call cost using LiteLLM’s built-in cost map or custom pricing registered vialitellm.register_model().- Parameters:
- async complete(system_message: str, user_message: str, tenant_id: str, response_model: type[T_Response], timeout: float = 40.0) LLMResponse[source]#
Send a completion request via LiteLLM, retrying transient failures.
timeoutis the budget for a single attempt. Transient failures (timeout, rate-limit) and content-policy rejections are retried with exponential backoff up to a total wall-clock budget ofLLM_RETRY_BUDGET_MULTIPLIER * timeout; the retry wraps only the provider call, so injection/token checks and response parsing happen exactly once. Callers that enforce a deadline must sizetimeoutso this worst-case budget still fits (the orchestrator does this in_check_deadline_and_get_timeout). Content-policy rejections are retried because Azure’s filter severity classification is not guaranteed deterministic for identical input (#293); other bad-request and generic API errors are not retried — they are not transient. Azure signals a rejection two ways, both mapped toLLMContentPolicyViolationErrorand both retried: a synchronousBadRequestError(sniffed by_to_domain_error), or a200response whosechoices[0].message.contentisNonewith itscontent_filter_resultsflagging a category (Azure’s asynchronous filter, which lets the call through and blocks the completion after generation). The asynchronous path bills a completion before rejecting it, so usage from every discarded attempt is accumulated and folded into whichever outcome this call ultimately produces: the returnedLLMResponse’s token/cost fields on eventual success, or the raisedLLMContentPolicyViolationError’sdiscarded_*fields if every attempt is blocked.- Parameters:
- Returns:
The model’s response including token usage and cost.
- Return type:
- Raises:
LLMTimeoutError – When the provider does not respond in time on every attempt.
LLMRateLimitError – When the provider rate-limits on every attempt.
LLMContentPolicyViolationError – When the provider rejects the request under its content policy on every attempt.
LLMBadRequestError – When the provider rejects the request for any other reason.
PromptInjectionDetectedError – When the input matches a known prompt-injection pattern.
LLMError – For any other provider error or empty response.