Provider-agnostic LLM client used by codergen nodes and agent sessions.
The client resolves providers, applies middleware, delegates to adapter modules, and supports both request/response and streaming flows.
Beyond the low-level complete/2 and stream/2 APIs, the module also exposes:
from_env/1for runtime construction from application config- module-level default-client helpers
- spec-aligned convenience wrappers such as
generate/2 - stream accumulation helpers for callers that want a final normalized response
- JSON object generation helpers layered on top of the normalized response surface
Summary
Functions
Consumes a raw event stream and returns a normalized final response.
Consumes a raw event stream and returns a normalized final response.
Consumes a raw event stream and returns the accumulated response plus resolved request.
Clears the module-level default client.
Executes a completion request via the configured module-level default client.
Executes a completion request and returns either a response or an error tuple.
Executes a completion request and also returns the resolved request.
Returns the configured module-level default client, or nil.
Builds a client from application config, with direct opts taking precedence.
Spec-style completion alias for complete/2.
Spec-style completion alias for complete/2.
Generates a JSON object via the configured module-level default client.
Generates a JSON object from a non-streaming response.
Generates a JSON object from a non-streaming response and also returns transport data.
Spec-style completion alias for complete_with_request/2.
Builds a client from keyword options.
Stores a module-level default client used by the arity-1 helpers.
Executes a streaming request via the configured module-level default client.
Executes a streaming request and returns the event stream or an error tuple.
Generates a JSON object from a streamed response via the default client.
Generates a JSON object by first accumulating a streamed response.
Streams normalized events with inserted :object_delta JSON events.
Streams normalized events with inserted :object_delta JSON events.
Streams normalized events with inserted :object_delta JSON events and returns the
resolved request.
Generates a JSON object by first accumulating a streamed response and also returns the normalized response plus resolved request.
Executes a streaming request and also returns the resolved request.
Types
@type middleware() :: (AttractorEx.LLM.Request.t(), (AttractorEx.LLM.Request.t() -> any()) -> any())
@type t() :: %AttractorEx.LLM.Client{ default_provider: String.t() | nil, middleware: [middleware()], providers: %{optional(String.t()) => module()}, retry_policy: AttractorEx.LLM.RetryPolicy.t() | nil, streaming_middleware: [middleware()] }
Functions
@spec accumulate_stream(AttractorEx.LLM.Request.t()) :: AttractorEx.LLM.Response.t() | {:error, term()}
Consumes a raw event stream and returns a normalized final response.
This is useful for callers that want provider streaming for latency but still need a
single accumulated AttractorEx.LLM.Response.
@spec accumulate_stream(t(), AttractorEx.LLM.Request.t()) :: AttractorEx.LLM.Response.t() | {:error, term()}
Consumes a raw event stream and returns a normalized final response.
@spec accumulate_stream_with_request(t(), AttractorEx.LLM.Request.t()) :: {:ok, AttractorEx.LLM.Response.t(), AttractorEx.LLM.Request.t()} | {:error, term()}
Consumes a raw event stream and returns the accumulated response plus resolved request.
@spec clear_default() :: :ok
Clears the module-level default client.
@spec complete(AttractorEx.LLM.Request.t()) :: AttractorEx.LLM.Response.t() | {:error, term()}
Executes a completion request via the configured module-level default client.
Executes a completion request and returns either a response or an error tuple.
Executes a completion request and also returns the resolved request.
@spec default() :: t() | nil
Returns the configured module-level default client, or nil.
Builds a client from application config, with direct opts taking precedence.
Supported config shape:
config :attractor_phoenix, :attractor_ex_llm,
providers: %{"openai" => MyApp.OpenAIAdapter},
default_provider: "openai"
@spec generate(AttractorEx.LLM.Request.t()) :: AttractorEx.LLM.Response.t() | {:error, term()}
Spec-style completion alias for complete/2.
@spec generate(t(), AttractorEx.LLM.Request.t()) :: AttractorEx.LLM.Response.t() | {:error, term()}
Spec-style completion alias for complete/2.
@spec generate_object(AttractorEx.LLM.Request.t()) :: {:ok, map() | list()} | {:error, term()}
Generates a JSON object via the configured module-level default client.
@spec generate_object(t(), AttractorEx.LLM.Request.t()) :: {:ok, map() | list()} | {:error, term()}
Generates a JSON object from a non-streaming response.
The response body is decoded from response.text.
@spec generate_object_with_request(t(), AttractorEx.LLM.Request.t()) :: {:ok, map() | list(), AttractorEx.LLM.Response.t(), AttractorEx.LLM.Request.t()} | {:error, term()}
Generates a JSON object from a non-streaming response and also returns transport data.
@spec generate_with_request(t(), AttractorEx.LLM.Request.t()) :: {:ok, AttractorEx.LLM.Response.t(), AttractorEx.LLM.Request.t()} | {:error, term()}
Spec-style completion alias for complete_with_request/2.
Builds a client from keyword options.
Stores a module-level default client used by the arity-1 helpers.
@spec stream(AttractorEx.LLM.Request.t()) :: Enumerable.t() | {:error, term()}
Executes a streaming request via the configured module-level default client.
Executes a streaming request and returns the event stream or an error tuple.
@spec stream_object(AttractorEx.LLM.Request.t()) :: {:ok, map() | list()} | {:error, term()}
Generates a JSON object from a streamed response via the default client.
@spec stream_object(t(), AttractorEx.LLM.Request.t()) :: {:ok, map() | list()} | {:error, term()}
Generates a JSON object by first accumulating a streamed response.
@spec stream_object_deltas(AttractorEx.LLM.Request.t()) :: Enumerable.t() | {:error, term()}
Streams normalized events with inserted :object_delta JSON events.
@spec stream_object_deltas(t(), AttractorEx.LLM.Request.t()) :: Enumerable.t() | {:error, term()}
Streams normalized events with inserted :object_delta JSON events.
@spec stream_object_deltas_with_request(t(), AttractorEx.LLM.Request.t()) :: {:ok, Enumerable.t(), AttractorEx.LLM.Request.t()} | {:error, term()}
Streams normalized events with inserted :object_delta JSON events and returns the
resolved request.
@spec stream_object_with_request(t(), AttractorEx.LLM.Request.t()) :: {:ok, map() | list(), AttractorEx.LLM.Response.t(), AttractorEx.LLM.Request.t()} | {:error, term()}
Generates a JSON object by first accumulating a streamed response and also returns the normalized response plus resolved request.
Executes a streaming request and also returns the resolved request.