GitHub

API reference for Service.

The client: the configured HttpClient and its calls. make builds it; Service(http_client, create_response, ...) makes one of four functions (a test double, say).

Every call goes through the client and then a provided OpenAiConfig's transform. 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
http_clientHttpClientService[HttpClientError] = http_client
max_response_bytesByteSize = max_response_bytes

Functions

__init__

__init__(http_client, create_response, create_response_stream, create_embedding, create_decisions, max_response_bytes=P.DEFAULT_MAX_RESPONSE_BYTES) -> None
ParameterTypeDescription
http_clientHttpClientService[HttpClientError]
create_responseCallable[[W.CreateResponse], Effect[tuple[W.Response, HttpClientResponse], AiError]]
create_response_streamCallable[[W.CreateResponse], Effect[ResponseStream, AiError]]
create_embeddingCallable[[W.CreateEmbeddingRequest], Effect[W.CreateEmbeddingResponse, AiError]]
create_decisionsCallable[[W.CreateDecisionsRequest], Effect[W.CreateDecisionsResponse, AiError]]
max_response_bytesByteSize = P.DEFAULT_MAX_RESPONSE_BYTES
def __init__(  # noqa: PLR0917
    self,
    http_client: HttpClientService[HttpClientError],
    create_response: Callable[
        [W.CreateResponse], Effect[tuple[W.Response, HttpClientResponse], AiError]
    ],
    create_response_stream: Callable[[W.CreateResponse], Effect[ResponseStream, AiError]],
    create_embedding: Callable[
        [W.CreateEmbeddingRequest], Effect[W.CreateEmbeddingResponse, AiError]
    ],
    create_decisions: Callable[
        [W.CreateDecisionsRequest], Effect[W.CreateDecisionsResponse, AiError]
    ],
    max_response_bytes: ByteSize = P.DEFAULT_MAX_RESPONSE_BYTES,
) -> None:
    self.http_client = http_client
    self._create_response = create_response
    self._create_response_stream = create_response_stream
    self._create_embedding = create_embedding
    self._create_decisions = create_decisions
    self.max_response_bytes = max_response_bytes

create_response

create_response(options) -> Effect[tuple[W.Response, HttpClientResponse], AiError]

Create a response: POST /responses with options. The decoded response and the HTTP response; fails with an AiError (see the class).

ParameterTypeDescription
optionsW.CreateResponse

Returns Effect[tuple[W.Response, HttpClientResponse], AiError]

def create_response(
    self, options: W.CreateResponse
) -> Effect[tuple[W.Response, HttpClientResponse], AiError]:
    """Create a response: ``POST /responses`` with ``options``. The
    decoded response and the HTTP response; fails with an ``AiError``
    (see the class)."""
    return self._create_response(options)

create_response_stream

create_response_stream(options) -> Effect[ResponseStream, AiError]

Create a response as server-sent events: POST /responses with "stream": true, or over the connection while OpenAiSocket (WebSocket mode) is provided. The HTTP response and the events, ending at response.completed, response.incomplete or response.failed (or [DONE]); the call and the stream fail with an AiError (see the class).

ParameterTypeDescription
optionsW.CreateResponse

Returns Effect[ResponseStream, AiError]

def create_response_stream(self, options: W.CreateResponse) -> Effect[ResponseStream, AiError]:
    """Create a response as server-sent events: ``POST /responses`` with
    ``"stream": true``, or over the connection while ``OpenAiSocket``
    (WebSocket mode) is provided. The HTTP response and the events,
    ending at ``response.completed``, ``response.incomplete`` or
    ``response.failed`` (or ``[DONE]``); the call and the stream fail
    with an ``AiError`` (see the class)."""
    return self._create_response_stream(options)

create_embedding

create_embedding(options) -> Effect[W.CreateEmbeddingResponse, AiError]

Create embeddings: POST /embeddings with options; fails with an AiError (see the class).

ParameterTypeDescription
optionsW.CreateEmbeddingRequest

Returns Effect[W.CreateEmbeddingResponse, AiError]

def create_embedding(
    self, options: W.CreateEmbeddingRequest
) -> Effect[W.CreateEmbeddingResponse, AiError]:
    """Create embeddings: ``POST /embeddings`` with ``options``; fails
    with an ``AiError`` (see the class)."""
    return self._create_embedding(options)

create_decisions

create_decisions(options) -> Effect[W.CreateDecisionsResponse, AiError]

Answer decision questions: POST /decisions with options; fails with an AiError (see the class).

ParameterTypeDescription
optionsW.CreateDecisionsRequest

Returns Effect[W.CreateDecisionsResponse, AiError]

def create_decisions(
    self, options: W.CreateDecisionsRequest
) -> Effect[W.CreateDecisionsResponse, AiError]:
    """Answer decision questions: ``POST /decisions`` with ``options``;
    fails with an ``AiError`` (see the class)."""
    return self._create_decisions(options)