Base functionality shared across all LLM providers.
This module extracts common patterns to eliminate duplication across provider implementations. Providers can use these helpers directly or override as needed.
Usage
defmodule MyProvider do
alias ElixirLLM.Providers.Base
# Use shared helpers
defp parse_error(status, body), do: Base.parse_error(status, body, :my_provider)
defp accumulate_chunk(acc, chunk), do: Base.accumulate_chunk(acc, chunk)
end
Summary
Functions
Accumulates a chunk into the streaming state.
Builds standard Bearer token headers.
Builds the final Response from accumulated streaming state.
Creates a streaming fold function for NDJSON format. @deprecated Use create_ndjson_stream_fun/2 instead
Creates a streaming function for NDJSON format.
Creates a streaming fold function that processes SSE chunks. @deprecated Use create_sse_stream_fun/2 instead
Creates a streaming function that processes SSE chunks.
Formats messages for OpenAI-compatible APIs.
Formats tool parameters as JSON Schema.
Formats tools for OpenAI-compatible APIs.
Gets and clears the stream accumulator from the process dictionary. Returns the final accumulated state.
Gets the configured timeout for a provider.
Initializes the stream accumulator in the process dictionary. Must be called before starting a stream request.
Initial accumulator state for streaming responses.
Conditionally adds a key-value pair to a map if value is not nil.
Adds tools to request body if any are registered.
Merges tool calls from streaming chunks.
Parses a standard OpenAI-compatible chat completion response.
Parses an error response and returns a structured error.
Parses finish reason strings into atoms.
Parses newline-delimited JSON (NDJSON) format.
Parses Server-Sent Events (SSE) data format.
Parses a standard OpenAI-compatible streaming chunk.
Parses tool calls from OpenAI-compatible format.
Validates that an API key is configured and returns it.
Functions
@spec accumulate_chunk(map(), ElixirLLM.Chunk.t()) :: map()
Accumulates a chunk into the streaming state.
Builds standard Bearer token headers.
Validates that the API key is present and raises a clear error if not.
@spec build_final_response(map()) :: ElixirLLM.Response.t()
Builds the final Response from accumulated streaming state.
@spec create_ndjson_fold_fun((ElixirLLM.Chunk.t() -> any()), (map() -> ElixirLLM.Chunk.t() | nil)) :: (term(), {term(), term(), map()} -> {:cont, {term(), term(), map()}})
Creates a streaming fold function for NDJSON format. @deprecated Use create_ndjson_stream_fun/2 instead
@spec create_ndjson_stream_fun((ElixirLLM.Chunk.t() -> any()), (map() -> ElixirLLM.Chunk.t() | nil)) :: ({:data, binary()}, {term(), term()} -> {:cont, {term(), term()}})
Creates a streaming function for NDJSON format.
@spec create_sse_fold_fun((ElixirLLM.Chunk.t() -> any()), (map() -> ElixirLLM.Chunk.t() | nil)) :: (term(), {term(), term(), map()} -> {:cont, {term(), term(), map()}})
Creates a streaming fold function that processes SSE chunks. @deprecated Use create_sse_stream_fun/2 instead
@spec create_sse_stream_fun((ElixirLLM.Chunk.t() -> any()), (map() -> ElixirLLM.Chunk.t() | nil)) :: ({:data, binary()}, {term(), term()} -> {:cont, {term(), term()}})
Creates a streaming function that processes SSE chunks.
Uses process dictionary to accumulate state since Req 0.5.x into: option
only supports functions with {:data, data}, {req, resp} signature.
Formats messages for OpenAI-compatible APIs.
Formats tool parameters as JSON Schema.
Formats tools for OpenAI-compatible APIs.
Most providers accept this format directly.
@spec get_stream_accumulator() :: map()
Gets and clears the stream accumulator from the process dictionary. Returns the final accumulated state.
@spec get_timeout(atom()) :: pos_integer()
Gets the configured timeout for a provider.
@spec init_stream_accumulator() :: :ok
Initializes the stream accumulator in the process dictionary. Must be called before starting a stream request.
@spec initial_accumulator() :: map()
Initial accumulator state for streaming responses.
Conditionally adds a key-value pair to a map if value is not nil.
Adds tools to request body if any are registered.
Merges tool calls from streaming chunks.
@spec parse_chat_response(map(), (list() | nil -> [ElixirLLM.ToolCall.t()] | nil)) :: ElixirLLM.Response.t()
Parses a standard OpenAI-compatible chat completion response.
Parses an error response and returns a structured error.
This converts provider-specific error responses into ElixirLLM's structured error types for consistent error handling.
Parses finish reason strings into atoms.
Parses newline-delimited JSON (NDJSON) format.
Used by Ollama and some other providers that don't use SSE.
Parses Server-Sent Events (SSE) data format.
Handles the standard data: {...} format used by OpenAI, Anthropic, and most providers.
@spec parse_stream_chunk(map(), (list() | nil -> [ElixirLLM.ToolCall.t()] | nil)) :: ElixirLLM.Chunk.t() | nil
Parses a standard OpenAI-compatible streaming chunk.
@spec parse_tool_calls(list() | nil) :: [ElixirLLM.ToolCall.t()] | nil
Parses tool calls from OpenAI-compatible format.
Most providers (OpenAI, Groq, Mistral, Together, OpenRouter) use this format.
Validates that an API key is configured and returns it.
Raises ArgumentError with a helpful message if the key is missing.