LLM.Dialect behaviour (LLM v0.1.0)

Copy Markdown View Source

Behaviour for wire format translation between normalized messages and provider APIs.

Each dialect handles encoding requests to provider-specific JSON format and decoding responses/chunks back to normalized LLM structs.

Built-in Dialects

Summary

Callbacks

Build the request body for the provider API.

Parse a single SSE data line into one or more chunk structs. Return :done when the stream is complete.

Decode a complete (non-streaming) response into a normalized LLM.Response.

Normalize the thinking option to provider-specific format.

Return extra headers needed for streaming requests.

Return the stream endpoint path (appended to base_url).

Types

Callbacks

build_request(t, keyword)

@callback build_request(
  LLM.Context.t(),
  keyword()
) :: map()

Build the request body for the provider API.

decode_chunk(t)

@callback decode_chunk(String.t()) :: [chunk_type()] | :done

Parse a single SSE data line into one or more chunk structs. Return :done when the stream is complete.

decode_response(map)

@callback decode_response(map()) :: {:ok, LLM.Response.t()} | {:error, term()}

Decode a complete (non-streaming) response into a normalized LLM.Response.

normalize_thinking(term)

(optional)
@callback normalize_thinking(term()) :: term()

Normalize the thinking option to provider-specific format.

stream_headers(keyword)

@callback stream_headers(keyword()) :: [{String.t(), String.t()}]

Return extra headers needed for streaming requests.

stream_path()

@callback stream_path() :: String.t()

Return the stream endpoint path (appended to base_url).