Backends

OpenAI

OpenAI backend.

class LLMOpenAI.LLMOpenAI.LLMOpenAI(model, params=None, config=None, config_file=None, examples=None)[source]

Bases: BaseLLM

OpenAI chat-completions backend.

Configuration keys:

LLM_VERSION: Model name.
API_KEY_NAME: Environment variable holding the API key.
BASE_URL: Optional custom base URL (also accepts ``BASE_URL_ENV``).
ORGANIZATION / PROJECT: Optional OpenAI account scoping.
IMAGE_DETAIL: Detail level sent with images ("auto", "low", "high").
LLM_CONFIG: Request parameters, forwarded as-is. Use the names the model expects,
    e.g. ``max_completion_tokens`` for the gpt-5 family and ``max_tokens`` before it.
Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

image_part(image)[source]

Encode an image as an OpenAI image_url content part.

Parameters:

image (Any) – A PIL.Image.Image, or the path of an image file.

Returns:

An image_url content part holding a data: URL and the configured detail level.

Return type:

Dict[str, Any]

LLMOpenAI.LLMOpenAI.LLM

alias of LLMOpenAI

Azure OpenAI

Azure OpenAI backend.

class LLMAzureOpenAI.LLMAzureOpenAI.LLMAzureOpenAI(model, params=None, config=None, config_file=None, examples=None)[source]

Bases: BaseLLM

Azure OpenAI chat-completions backend.

Configuration keys:

LLM_VERSION: Model name.
DEPLOYMENT: Azure deployment name. Defaults to ``LLM_VERSION``.
API_KEY_NAME: Environment variable holding the API key.
ENDPOINT / ENDPOINT_ENV: Endpoint URL, or the environment variable holding it.
API_VERSION: Azure API version.
IMAGE_DETAIL: Detail level sent with images ("auto", "low", "high").
LLM_CONFIG: Request parameters, forwarded as-is. Use the names the deployment expects,
    e.g. ``max_completion_tokens`` for the gpt-5 family and ``max_tokens`` before it.
Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

image_part(image)[source]

Encode an image as an OpenAI image_url content part.

Parameters:

image (Any) – A PIL.Image.Image, or the path of an image file.

Returns:

An image_url content part holding a data: URL and the configured detail level.

Return type:

Dict[str, Any]

LLMAzureOpenAI.LLMAzureOpenAI.LLM

alias of LLMAzureOpenAI

Azure OpenAI (Responses API)

Azure OpenAI backend speaking the Responses API.

class LLMAzureOpenAIResponses.LLMAzureOpenAIResponses.LLMAzureOpenAIResponses(model, params=None, config=None, config_file=None, examples=None)[source]

Bases: LLMAzureOpenAI

Azure OpenAI backend for deployments served through the Responses API.

The reasoning-heavy deployments (the -pro tier, and the o*-pro models before it) do not expose /chat/completions at all: every request to it comes back as 400 The requested operation is unsupported. They are only reachable through /responses, which differs from chat completions in more than the URL:

  • max_completion_tokens is named max_output_tokens, and seed does not exist.

  • Content parts are input_text / input_image, and input_image carries the URL directly rather than nesting it under an image_url object.

  • The answer is one flat output_text, and usage is reported as input_tokens / output_tokens.

Connection settings are read by LLMAzureOpenAI and are unchanged, except that API_VERSION must be one that serves the Responses API (2025-04-01-preview or later). LLM_CONFIG is still forwarded as-is, so it has to use the Responses names.

Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

build_content(message, images=None)[source]

Build the content of a single message, using Responses content parts.

Parameters:
  • message (str) – The text of the message.

  • images (Optional[Sequence[ImageInput]], optional) – Images to attach.

Returns:

The plain string without images, and a list of input_text / input_image parts with them. The base class builds a text part instead, which the Responses API rejects.

Return type:

Any

image_part(image)[source]

Encode an image as a Responses input_image content part.

Parameters:

image (ImageInput) – A PIL.Image.Image, or the path of an image file.

Returns:

An input_image part holding a data: URL and the configured detail level. Unlike the chat-completions part, the URL is a plain string field.

Return type:

Dict[str, Any]

LLMAzureOpenAIResponses.LLMAzureOpenAIResponses.LLM

alias of LLMAzureOpenAIResponses

Anthropic

Anthropic backend.

class LLMAnthropic.LLMAnthropic.LLMAnthropic(model, params=None, config=None, config_file=None, examples=None)[source]

Bases: BaseLLM

Anthropic Messages API backend.

Configuration keys:

LLM_VERSION: Model name.
API_KEY_NAME: Environment variable holding the API key.
BASE_URL: Optional base URL. When set, the Foundry client is used.
LLM_CONFIG: Request parameters, forwarded as-is. ``max_tokens`` is required by the API,
    so it is always sent.
Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

image_part(image)[source]

Encode an image as an Anthropic base64 image block.

Parameters:

image (Any) – A PIL.Image.Image, or the path of an image file.

Returns:

An image content block carrying the base64 payload.

Return type:

Dict[str, Any]

LLMAnthropic.LLMAnthropic.LLM

alias of LLMAnthropic

Google Gemini

Google Gemini backend.

class LLMGemini.LLMGemini.LLMGemini(model, params=None, config=None, config_file=None, examples=None)[source]

Bases: BaseLLM

Gemini backend built on the google-genai SDK.

Configuration keys:

LLM_VERSION: Model name.
API_KEY_NAME: Environment variable holding the API key.
BASE_URL: Optional custom endpoint.
LLM_CONFIG: Request parameters, forwarded to ``GenerateContentConfig``. Generic names are
    translated, so ``max_tokens`` reaches the API as ``max_output_tokens``.
Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

build_content(message, images=None)[source]

Build message content as Gemini parts.

Parameters:
  • message (str) – The text of the message.

  • images (Optional[Sequence[ImageInput]], optional) – Images to attach.

Returns:

The text part followed by one inline data part per image.

Return type:

List[types.Part]

image_part(image)[source]

Encode an image as a Gemini inline data part.

Parameters:

image (Any) – A PIL.Image.Image, or the path of an image file.

Returns:

A part holding the raw image bytes and their MIME type.

Return type:

types.Part

LLMGemini.LLMGemini.LLM

alias of LLMGemini

GLM

GLM backend (OpenAI-compatible API).

class LLMGLM.LLMGLM.LLMGLM(model, params=None, config=None, config_file=None, examples=None)[source]

Bases: BaseLLM

GLM chat-completions backend, served through the OpenAI-compatible API.

Configuration keys:

LLM_VERSION: Model name.
API_KEY_NAME: Environment variable holding the API key. ``ZHIPUAI_API_KEY`` is used as a
    fallback when the named variable is unset.
BASE_URL: API base URL. Defaults to the BigModel endpoint.
LLM_CONFIG: Request parameters, forwarded as-is.
Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

image_part(image)[source]

Encode an image as an OpenAI-style image_url content part.

Parameters:

image (Any) – A PIL.Image.Image, or the path of an image file.

Returns:

An image_url content part holding a data: URL.

Return type:

Dict[str, Any]

LLMGLM.LLMGLM.LLM

alias of LLMGLM

Hugging Face

Hugging Face LLM wrapper with optional quantization and config loading.

class LLMHuggingFace.LLMHuggingFace.LLMHuggingFace(model, params=None, config=None, config_file=None, examples=None, device=None, quantize=None, cache_dir=None, device_map=None)[source]

Bases: BaseLLM

Text-only Hugging Face backend, running the model in-process.

The weights are loaded on the first query() (through connect()), so building the instance from a configuration file stays cheap.

Configuration keys:

MODEL_NAME: Hugging Face model identifier.
DEVICE: Device to run on. Defaults to CUDA when available.
QUANTIZE: 8, 4, or 0 (no quantization). Defaults to 8.
DEVICE_MAP / MULTI_GPU: Model sharding across GPUs.
CACHE_DIR: Where weights are downloaded.
DISABLE_SYSTEM: Rewrite system messages for templates without a system role.
ENABLE_THINKING: Passed to chat templates that support it (Qwen3 family).
LLM_CONFIG: Generation parameters (``max_tokens``, ``temperature``, ``top_p``, ``stop``,
    ``use_cache``), consumed locally rather than sent to a server.
Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

  • device (str | None)

  • quantize (int | None)

  • cache_dir (str | None)

  • device_map (Any | None)

format_prompt(examples, query)[source]

Format a simple Q/A few-shot prompt.

Parameters:
  • examples (List[Dict[str, str]]) – Few-shot examples with “question”/”answer”.

  • query (str) – User question to append.

Returns:

Rendered prompt string.

Return type:

str

generate_response(examples, query, max_length=512)[source]

Generate a response from Q/A few-shot examples.

Parameters:
  • examples (List[Dict[str, str]]) – Few-shot examples with “question”/”answer”.

  • query (str) – User question to answer.

  • max_length (int) – Maximum number of generated tokens.

Returns:

Assistant response.

Return type:

str

image_part(image)[source]

Wrap an image as the content part the chat templates expect.

Where the remote backends encode the image into the request, here the image travels as an object: the processor reads it when it renders the template, so it is kept as a PIL image rather than base64.

Parameters:

image (Any) – A PIL.Image.Image, or the path of an image file.

Returns:

An image content part holding the opened image.

Return type:

Dict[str, Any]

Raises:

NotImplementedError – If this model does not take images.

count_prompt_tokens(messages)[source]

Count prompt tokens locally using the model tokenizer.

Parameters:

messages (List[Dict[str, Any]])

Return type:

int

close()[source]

Release model/tokenizer references and free CUDA cache.

Return type:

None

LLMHuggingFace.LLMHuggingFace.LLM

alias of LLMHuggingFace

vLLM

vLLM offline backend with process-local shared engine reuse.

class LLMVLLM.LLMVLLM.LLMVLLM(model, params=None, config=None, config_file=None, examples=None, **overrides)[source]

Bases: BaseLLM

Offline vLLM backend.

Model weights and KV runtime are kept in a process-level cache, so creating multiple instances with the same engine config does not reload the model.

Configuration keys:

MODEL_NAME: Model identifier or local path.
DTYPE, TENSOR_PARALLEL_SIZE, MAX_MODEL_LEN, MAX_NUM_SEQS, MAX_NUM_BATCHED_TOKENS,
GPU_MEMORY_UTILIZATION, SWAP_SPACE, TRUST_REMOTE_CODE, ENABLE_PREFIX_CACHING,
DISABLE_LOG_STATS, ENFORCE_EAGER: vLLM engine settings.
DOWNLOAD_DIR / DOWNLOAD_WORKERS: Where and how weights are fetched.
DISABLE_SYSTEM: Rewrite system messages for templates without a system role.
LLM_CONFIG: Sampling parameters, consumed locally rather than sent to a server.
Parameters:
  • model (str)

  • params (Dict[str, Any] | None)

  • config (Dict[str, Any] | None)

  • config_file (str | Path | None)

  • examples (str | Path | None)

  • overrides (Any)

count_prompt_tokens(messages)[source]

Count prompt tokens using the backend tokenizer.

Parameters:

messages (List[Dict[str, Any]])

Return type:

int

close()[source]

Release this instance while keeping the shared engine warm.

The shared engine is intentionally retained until process exit so multiple phases in a single script execution reuse loaded weights and prefix cache.

Return type:

None

LLMVLLM.LLMVLLM.LLM

alias of LLMVLLM