The client: the configured HttpClient and its calls. make
builds it; Service(http_client, create_response, ...) makes one of
three 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#
| Attribute | Type | Description |
|---|---|---|
http_client | HttpClientService[HttpClientError] = http_client | |
max_response_bytes | ByteSize = max_response_bytes |
Functions#
__init__#
__init__(http_client, create_response, create_response_stream, create_embedding, max_response_bytes=P.DEFAULT_MAX_RESPONSE_BYTES) -> None| Parameter | Type | Description |
|---|---|---|
http_client | HttpClientService[HttpClientError] | |
create_response | Callable[[Payload], Effect[tuple[W.ChatCompletion, HttpClientResponse], AiError]] | |
create_response_stream | Callable[[Payload], Effect[tuple[HttpClientResponse, Stream[W.StreamEvent, AiError]], AiError]] | |
create_embedding | Callable[[Payload], Effect[W.EmbeddingResponse, AiError]] | |
max_response_bytes | ByteSize = P.DEFAULT_MAX_RESPONSE_BYTES |
def __init__(
self,
http_client: HttpClientService[HttpClientError],
create_response: Callable[
[Payload], Effect[tuple[W.ChatCompletion, HttpClientResponse], AiError]
],
create_response_stream: Callable[
[Payload], Effect[tuple[HttpClientResponse, Stream[W.StreamEvent, AiError]], AiError]
],
create_embedding: Callable[[Payload], Effect[W.EmbeddingResponse, 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.max_response_bytes = max_response_bytescreate_response#
create_response(options) -> Effect[tuple[W.ChatCompletion, HttpClientResponse], AiError]Create a chat completion: POST /chat/completions with
options. The decoded completion and the HTTP response; fails
with an AiError (see the class).
| Parameter | Type | Description |
|---|---|---|
options | Payload |
Returns Effect[tuple[W.ChatCompletion, HttpClientResponse], AiError]
def create_response(
self, options: Payload
) -> Effect[tuple[W.ChatCompletion, HttpClientResponse], AiError]:
"""Create a chat completion: ``POST /chat/completions`` with
``options``. The decoded completion and the HTTP response; fails
with an ``AiError`` (see the class)."""
return self._create_response(options)create_response_stream#
create_response_stream(options) -> Effect[tuple[HttpClientResponse, Stream[W.StreamEvent, AiError]], AiError]Create a chat completion as server-sent events: POST /chat/completions with "stream": true and usage included. The
HTTP response and the chunks, ending with "[DONE]"; an event
that is not a chunk arrives as an UnknownEvent, one that is not
JSON is skipped. The call and the stream fail with an AiError
(see the class).
| Parameter | Type | Description |
|---|---|---|
options | Payload |
Returns Effect[tuple[HttpClientResponse, Stream[W.StreamEvent, AiError]], AiError]
def create_response_stream(
self, options: Payload
) -> Effect[tuple[HttpClientResponse, Stream[W.StreamEvent, AiError]], AiError]:
"""Create a chat completion as server-sent events: ``POST
/chat/completions`` with ``"stream": true`` and usage included. The
HTTP response and the chunks, ending with ``"[DONE]"``; an event
that is not a chunk arrives as an ``UnknownEvent``, one that is not
JSON is skipped. 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.EmbeddingResponse, AiError]Create embeddings: POST /embeddings with options; fails
with an AiError (see the class).
| Parameter | Type | Description |
|---|---|---|
options | Payload |
Returns Effect[W.EmbeddingResponse, AiError]
def create_embedding(self, options: Payload) -> Effect[W.EmbeddingResponse, AiError]:
"""Create embeddings: ``POST /embeddings`` with ``options``; fails
with an ``AiError`` (see the class)."""
return self._create_embedding(options)