Skip to content

Httpx Client

infra.llm.httpx_client

Summary

HTTP client ports for OpenAI-compatible chat completion endpoints.


Provide synchronous and asynchronous httpx wrappers that issue chat completion requests with bearer authentication.

Classes

AsyncClientPort

1
2
3
4
5
AsyncClientPort(
    api_key: str = "",
    endpoint: str = "",
    timeout: float = 60.0,
)

Asynchronous httpx client for chat completion APIs.

Attributes:

Name Type Description
api_key str

Bearer token used for authentication.

endpoint str

Base URL of the completions API.

timeout float

Request timeout in seconds.

_http_client AsyncClient

Underlying asynchronous HTTP client.

Initialize the asynchronous client.

Parameters:

Name Type Description Default
api_key str

Bearer token used for authentication.

''
endpoint str

Base URL of the completions API.

''
timeout float

Request timeout in seconds.

60.0
Functions
aclose async
aclose()

Close the underlying asynchronous HTTP client.

chat_completion async
1
2
3
4
5
6
chat_completion(
    model: str,
    messages: list,
    temperature: float = 0.0,
    max_tokens: int = 4096,
) -> dict

Send an asynchronous chat completion request.

Parameters:

Name Type Description Default
model str

Model identifier to invoke.

required
messages list

Chat messages in API format.

required
temperature float

Sampling temperature.

0.0
max_tokens int

Maximum tokens to generate.

4096

Returns:

Name Type Description
dict dict

Decoded JSON response body.

SyncClientPort

1
2
3
4
5
SyncClientPort(
    api_key: str = "",
    endpoint: str = "",
    timeout: float = 60.0,
)

Synchronous httpx client for chat completion APIs.

Attributes:

Name Type Description
api_key str

Bearer token used for authentication.

endpoint str

Base URL of the completions API.

timeout float

Request timeout in seconds.

_client Client

Underlying synchronous HTTP client.

Initialize the synchronous client.

Parameters:

Name Type Description Default
api_key str

Bearer token used for authentication.

''
endpoint str

Base URL of the completions API.

''
timeout float

Request timeout in seconds.

60.0
Functions
chat_completion
1
2
3
4
5
6
chat_completion(
    model: str,
    messages: list,
    temperature: float = 0.0,
    max_tokens: int = 4096,
) -> dict

Send a chat completion request.

Parameters:

Name Type Description Default
model str

Model identifier to invoke.

required
messages list

Chat messages in API format.

required
temperature float

Sampling temperature.

0.0
max_tokens int

Maximum tokens to generate.

4096

Returns:

Name Type Description
dict dict

Decoded JSON response body.

close
close()

Close the underlying HTTP client.