GitHub

API reference for Service.

The client: the generated client of every operation and the calls the models make. make builds it; Service(client, create_decisions, ...) makes one of three functions (a test double, say).

Every failure is an AiError: an error status maps to its reason (a 429 to a RateLimitError), an unreadable or malformed body to an InvalidOutputError or InvalidResponseError, a transport failure to a NetworkError.

Attributes

AttributeTypeDescription
clientgenerated_client.OpenRouterClient = client
max_response_bytesByteSize = max_response_bytes

Functions

__init__

__init__(client, create_decisions, create_chat_completion, create_chat_completion_stream, max_response_bytes=P.DEFAULT_MAX_RESPONSE_BYTES) -> None
ParameterTypeDescription
clientgenerated_client.OpenRouterClient
create_decisionsCallable[[W.DecisionsRequest], Decisions]
create_chat_completionCallable[[GT.ChatRequest], ChatCompletion]
create_chat_completion_streamCallable[[GT.ChatRequest], ChatCompletionStream]
max_response_bytesByteSize = P.DEFAULT_MAX_RESPONSE_BYTES
def __init__(
    self,
    client: generated_client.OpenRouterClient,
    create_decisions: Callable[[W.DecisionsRequest], Decisions],
    create_chat_completion: Callable[[GT.ChatRequest], ChatCompletion],
    create_chat_completion_stream: Callable[[GT.ChatRequest], ChatCompletionStream],
    max_response_bytes: ByteSize = P.DEFAULT_MAX_RESPONSE_BYTES,
) -> None:
    self.client = client
    self._create_decisions = create_decisions
    self._create_chat_completion = create_chat_completion
    self._create_chat_completion_stream = create_chat_completion_stream
    self.max_response_bytes = max_response_bytes

create_decisions

create_decisions(options) -> Decisions

Answer decision questions: POST /alpha/decisions (next to the versioned API) with options, through an OpenRouterConfig's transform. The decoded decisions and the HTTP response; a body that cannot be encoded fails with an InvalidRequestError AiError, the rest as the class says.

ParameterTypeDescription
optionsW.DecisionsRequest

Returns Decisions

def create_decisions(self, options: W.DecisionsRequest) -> Decisions:
    """Answer decision questions: ``POST /alpha/decisions`` (next to the
    versioned API) with ``options``, through an ``OpenRouterConfig``'s
    transform. The decoded decisions and the HTTP response; a body that
    cannot be encoded fails with an ``InvalidRequestError`` ``AiError``,
    the rest as the class says."""
    return self._create_decisions(options)

create_chat_completion

create_chat_completion(options) -> ChatCompletion

Create a chat completion through the generated client (and an OpenRouterConfig's transform). The decoded result and the HTTP response; fails with an AiError (see the class).

ParameterTypeDescription
optionsGT.ChatRequest

Returns ChatCompletion

def create_chat_completion(self, options: GT.ChatRequest) -> ChatCompletion:
    """Create a chat completion through the generated client (and an
    ``OpenRouterConfig``'s transform). The decoded result and the HTTP
    response; fails with an ``AiError`` (see the class)."""
    return self._create_chat_completion(options)

create_chat_completion_stream

create_chat_completion_stream(options) -> ChatCompletionStream

Create a chat completion as server-sent events: POST /chat/completions with "stream": true and usage included, not through an OpenRouterConfig. The HTTP response and the chunks until [DONE] (a chunk may carry an error); data that is not a chunk fails the stream with an InvalidResponseError. The call and the stream fail with an AiError (see the class).

ParameterTypeDescription
optionsGT.ChatRequest

Returns ChatCompletionStream

def create_chat_completion_stream(self, options: GT.ChatRequest) -> ChatCompletionStream:
    """Create a chat completion as server-sent events: ``POST
    /chat/completions`` with ``"stream": true`` and usage included, not
    through an ``OpenRouterConfig``. The HTTP response and the chunks
    until ``[DONE]`` (a chunk may carry an ``error``); data that is not
    a chunk fails the stream with an ``InvalidResponseError``. The call
    and the stream fail with an ``AiError`` (see the class)."""
    return self._create_chat_completion_stream(options)