Ragex. AI. Provider. Shared
(Ragex v0.32.1)
View Source
Helpers shared by the AI provider implementations
(Ragex.AI.Provider.Anthropic, Ragex.AI.Provider.OpenAI,
Ragex.AI.Provider.DeepSeekR1).
Extracted because mix credo --strict (Credo.Check.Design.DuplicatedCode)
found each of these pieces of logic copy-pasted near-identically across two
or three provider modules:
resolve_config/3/resolve_api_key/2: theopts > provider config > defaultresolution used by Anthropic and OpenAI's privateget_config/1andget_api_key/0functions.to_api_message/1,format_api_tool_calls/1: the OpenAI-compatible chat message/tool-call formatting duplicated byte-for-byte between OpenAI and DeepSeekR1.start_streaming_task/3: theTask.async+Req.post(..., into: ...)SSE launcher duplicated across all three providers'stream_api/stream_generateimplementations.
Each provider still owns its own event-parsing loop (Anthropic, OpenAI, and DeepSeek use different SSE payload shapes), so only the genuinely identical plumbing lives here.
Summary
Functions
Formats tool-call structs (atom- or string-keyed) into OpenAI-compatible API format.
Resolves this provider's API key: runtime :ai_keys config first, falling
back to the given environment variable.
Resolves provider request config with opts > application provider config > defaults precedence, matching the pattern previously duplicated in each
provider's private get_config/1.
Launches a background Task that POSTs an SSE request and forwards each
chunk to the calling process as {:stream_chunk, data}, followed by either
:stream_done or {:stream_error, reason}.
Converts a generic (atom- or string-keyed) message map into OpenAI-compatible API format.
Types
@type provider_defaults() :: %{ endpoint: String.t(), model: String.t(), temperature: float() | number(), max_tokens: pos_integer() }
Functions
Formats tool-call structs (atom- or string-keyed) into OpenAI-compatible API format.
Resolves this provider's API key: runtime :ai_keys config first, falling
back to the given environment variable.
@spec resolve_config(atom(), keyword(), provider_defaults()) :: provider_config()
Resolves provider request config with opts > application provider config > defaults precedence, matching the pattern previously duplicated in each
provider's private get_config/1.
Launches a background Task that POSTs an SSE request and forwards each
chunk to the calling process as {:stream_chunk, data}, followed by either
:stream_done or {:stream_error, reason}.
Callers are expected to build their own Stream.resource/3 around
receive, since each provider parses a different SSE event shape.
Converts a generic (atom- or string-keyed) message map into OpenAI-compatible API format.