Client

Client for the OpenRouter.ai API. This module contains the Client object, which stores configuration for openrouter.ai API requrests, as well as the new function, and functions to set various configuration options.

Client

The record used to store configuration for the OpenRouter API client.

Client : {
    api : Api,
    api_key : Str,
    model : Str,
    timeout_ms : TimeoutConfig,
    provider_order : Option (List Str),
    temperature : F32,
    top_p : F32,
    top_k : U64,
    frequency_penalty : F32,
    presence_penalty : F32,
    repetition_penalty : F32,
    min_p : F32,
    top_a : F32,
    seed : Option U64,
    max_tokens : Option U64,
    models : Option (List Str),
    route : Option Str,
    tools: Option (List Tool),
    system: Option Str,
    messages: List Message,
    stream: Bool,
}

default_model

Default model to use for API requests. This defaults to the openrouter/auto model router.

default_url

The default URL for the OpenRouter API. Currently the only supported URL is the openrouter.ai API url.

new : { api ? Api, api_key : Str, model ? Str, timeout_ms ? TimeoutConfig, provider_order ? List Str, temperature ? F32, top_p ? F32, top_k ? U64, frequency_penalty ? F32, presence_penalty ? F32, repetition_penalty ? F32, min_p ? F32, top_a ? F32, seed ? U64, max_tokens ? U64, models ? List Str, route ? [ UseFallback, NoFallback ], tools ? List Tool, system ? Str, stream ? Bool } -> Client

Initialize the OpenRouter API client with the required API key. All parameters besides apiKey are completely optional, and may be set during newialization, assigned later, or left as their defaults.

client = Client.new { apiKey: "your_openrouter_api_key" }

Api

ApiTarget : [OpenRouter, OpenAI, Anthropic, OpenAICompliant { url: Str }]

get_api_url : Client -> Str

set_model : Client, Str -> Client

Set the model to be used for the API requests. Default: "openrouter/auto"

set_api : Client, Api -> Client

Set the URL to be used for the API requests. (Change with care - while the openrouter.ai API is similar to OpenAI's, there may be some unexpected differences.)

set_api_key : Client, Str -> Client

set_timeout_ms : Client, TimeoutConfig -> Client

Set the request timeout for the API requests. Default: NoTimeout

set_provider_order : Client, List Str -> Client

Set the provider order for the API requests. Default: [] - use all providers.

set_temperature : Client, F32 -> Client

Set the temperature for the API requests. Range: [0.0, 2.0] Default: 1.0

set_top_p : Client, F32 -> Client

Set the top_p for the API requests. Range: [0.0, 1.0] Default: 1.0

set_top_k : Client, U64 -> Client

Set the top_k for the API requests. Range: [0, Num.maxU64] Default: 0

set_frequency_penalty : Client, F32 -> Client

Set the frequency penalty for the API requests. Range: [-2.0, 2.0] Default: 0.0

set_presence_penalty : Client, F32 -> Client

Set the presence penalty for the API requests. Range: [-2.0, 2.0] Default: 0.0

set_repetition_penalty : Client, F32 -> Client

Set the repetition penalty for the API requests. Range: [0.0, 2.0] Default: 1.0

set_min_p : Client, F32 -> Client

Set the min_p for the API requests. Range: [0.0, 1.0] Default: 0.0

set_top_a : Client, F32 -> Client

Set the top_a for the API requests. Range: [0.0, 1.0] Default: 0.0

set_seed : Client, U64 -> Client

Set the seed for the API requests. (This is for OpenAI models only) Default: 0 - random seed

set_max_tokens : Client, U64 -> Client

Set the max_tokens for the API requests. Range: [1, contextLength] Default: 0 == no limit

set_models : Client, List Str -> Client

Set the models for the auto router to choose from. If not set, the auto router will choose from a small selection of the top performing models. https://openrouter.ai/models/openrouter/auto Default: []

set_route : Client, [ UseFallback, NoFallback ] -> Client

Set the parameter which determines whether to use a fallback model if the primary model fails. OpenRouter will use the models provided in models, or if no models are provided, will try a similarly priced model to the primary. https://openrouter.ai/docs#model-routing Default: NoFallback

set_tools : Client, List Tool -> Client

Set the list of tools available for models to use to handle requests. Default: []

set_system : Client, Str -> Client

Set the system message to be used in for all requests. This is specific to anthropic's API, since it does not use system role messages. Default: ""

set_messages : Client, List Message -> Client

set_stream : Client, Bool -> Client