Current section

Files

Jump to
liter_llm lib liter_llm default_client.ex
Raw

lib/liter_llm/default_client.ex

# This file is auto-generated by alef — DO NOT EDIT.
# alef:hash:c892c5ad61c5b9376089e0d461b22e97ed9f5766653c5d3c2e5bf8cca5950f37
# To regenerate: alef generate
# To verify freshness: alef verify --exit-code
defmodule LiterLlm.DefaultClient do
@moduledoc """
Default client implementation backed by `reqwest`.
Sends requests to 143 LLM providers with automatic provider detection
and per-request routing. The provider is resolved at construction time
from `model_hint` (or defaults to OpenAI), but individual requests can
override the provider via model name prefix (e.g. `"anthropic/claude-3-5-sonnet"`
routes to Anthropic regardless of construction-time setting).
When the model prefix does not match any known provider, the construction-time
provider is used as the fallback. This enables seamless migration between
providers by changing only the model name.
The provider is stored behind an [`Arc`] so it can be shared cheaply into
async closures and streaming tasks. Pre-computed auth headers and extra
headers are cached at construction to avoid redundant encoding on every request.
"""
alias LiterLlm.Native
defstruct [:ref]
@typedoc "Default client implementation backed by `reqwest`."
@type t :: %__MODULE__{ref: reference()}
def chat(obj, req) do
Native.defaultclient_chat_async(obj.ref, req)
end
def chat_stream(obj, req) do
case Native.defaultclient_chat_stream_start(obj.ref, req) do
{:ok, handle} ->
stream =
Stream.unfold(handle, fn h ->
case Native.defaultclient_chat_stream_next(h) do
{:ok, nil} ->
nil
{:ok, chunk_json} when is_binary(chunk_json) ->
{Jason.decode!(chunk_json, keys: :atoms), h}
{:ok, chunk} ->
{chunk, h}
{:error, _} ->
nil
end
end)
{:ok, stream}
{:error, reason} ->
{:error, reason}
end
end
def embed(obj, req) do
Native.defaultclient_embed_async(obj.ref, req)
end
def list_models(obj) do
Native.defaultclient_list_models_async(obj.ref)
end
def image_generate(obj, req) do
Native.defaultclient_image_generate_async(obj.ref, req)
end
def speech(obj, req) do
Native.defaultclient_speech_async(obj.ref, req)
end
def transcribe(obj, req) do
Native.defaultclient_transcribe_async(obj.ref, req)
end
def moderate(obj, req) do
Native.defaultclient_moderate_async(obj.ref, req)
end
def rerank(obj, req) do
Native.defaultclient_rerank_async(obj.ref, req)
end
def search(obj, req) do
Native.defaultclient_search_async(obj.ref, req)
end
def ocr(obj, req) do
Native.defaultclient_ocr_async(obj.ref, req)
end
def create_file(obj, req) do
Native.defaultclient_create_file_async(obj.ref, req)
end
def retrieve_file(obj, file_id) do
Native.defaultclient_retrieve_file_async(obj.ref, file_id)
end
def delete_file(obj, file_id) do
Native.defaultclient_delete_file_async(obj.ref, file_id)
end
def list_files(obj, query) do
Native.defaultclient_list_files_async(obj.ref, query)
end
def file_content(obj, file_id) do
Native.defaultclient_file_content_async(obj.ref, file_id)
end
def create_batch(obj, req) do
Native.defaultclient_create_batch_async(obj.ref, req)
end
def retrieve_batch(obj, batch_id) do
Native.defaultclient_retrieve_batch_async(obj.ref, batch_id)
end
def list_batches(obj, query) do
Native.defaultclient_list_batches_async(obj.ref, query)
end
def cancel_batch(obj, batch_id) do
Native.defaultclient_cancel_batch_async(obj.ref, batch_id)
end
def fetch_batch_for_polling(obj, batch_id) do
Native.defaultclient_fetch_batch_for_polling_async(obj.ref, batch_id)
end
@doc "Poll a batch until it reaches a terminal status (Completed, Failed, Expired, Cancelled)."
def wait_for_batch(obj, batch_id, config) do
Native.defaultclient_wait_for_batch_async(obj.ref, batch_id, config)
end
def create_response(obj, req) do
Native.defaultclient_create_response_async(obj.ref, req)
end
def retrieve_response(obj, response_id) do
Native.defaultclient_retrieve_response_async(obj.ref, response_id)
end
def cancel_response(obj, response_id) do
Native.defaultclient_cancel_response_async(obj.ref, response_id)
end
end