ExLLM.Adapters.OpenAICompatible behaviour (ex_llm v0.5.0)

View Source

Base implementation for OpenAI-compatible API providers.

Many LLM providers follow OpenAI's API format, allowing for shared implementation. This module provides common functionality that can be reused across providers.

Usage

Create a new adapter by using this module:

defmodule ExLLM.Adapters.MyProvider do
  use ExLLM.Adapters.OpenAICompatible,
    provider: :my_provider,
    base_url: "https://api.myprovider.com/v1",
    models: ["model-1", "model-2"]

  # Override any functions as needed
  defp transform_request(request, _options) do
    # Custom request transformation
    request
  end
end

Summary

Functions

Default model transformer for configuration data.

Formats a model ID into a human-readable name.

Callbacks

filter_model(model)

@callback filter_model(model :: map()) :: boolean()

get_api_key(config)

@callback get_api_key(config :: map()) :: String.t() | nil

get_base_url(config)

@callback get_base_url(config :: map()) :: String.t()

Defines callbacks for OpenAI-compatible adapters.

get_headers(api_key, options)

@callback get_headers(api_key :: String.t(), options :: keyword()) :: [
  {String.t(), String.t()}
]

parse_error(response)

@callback parse_error(response :: map()) :: {:error, term()}

parse_model(model)

@callback parse_model(model :: map()) :: ExLLM.Types.Model.t()

transform_request(request, options)

@callback transform_request(request :: map(), options :: keyword()) :: map()

transform_response(response, options)

@callback transform_response(response :: map(), options :: keyword()) :: map()

Functions

default_model_transformer(model_id, config)

Default model transformer for configuration data.

format_model_name(model_id)

Formats a model ID into a human-readable name.

Delegates to shared ModelUtils for consistency.