artkit.model.llm.base.HTTPXChatConnector#
- class artkit.model.llm.base.HTTPXChatConnector(*, model_id, api_key_env=None, initial_delay=None, exponential_base=None, jitter=None, max_retries=None, system_prompt=None, httpx_client_kwargs=None, **model_params)[source]#
ABC that represents connecting to a custom endpoint.
- Bases:
ChatModelConnector
[AsyncClient
]- Metaclasses:
- Parameters:
model_id (
str
) – the ID of the model to useapi_key_env (
Optional
[str
]) – the environment variable that holds the API key; if not specified, use the default API key environment variable for the model as returned byget_default_api_key_env()
initial_delay (
Optional
[float
]) – the initial delay in seconds between client requests (defaults to1.0
)exponential_base (
Optional
[float
]) – the base for the exponential backoff (defaults to2.0
)jitter (
Optional
[bool
]) – whether to add jitter to the delay (defaults toTrue
)max_retries (
Optional
[int
]) – the maximum number of retries for client requests (defaults to5
) parameters withNone
values are considered unset and will be ignoredsystem_prompt (
Optional
[str
]) – the system prompt to initialise the chat system with (optional)httpx_client_kwargs (
Optional
[dict
[str
,Any
]]) – the kwargs to use for making thehttpx.AsyncClient
(optional):param model_params: additional model parameters, passed with every request;
Method summary
This method is responsible for formatting the input to the LLM chat system.
Get the API key from the environment variable specified by
api_key_env
.Get the client to use for making requests.
Get the default name of the environment variable that holds the API key.
Get the parameters of the model as a mapping.
Get a response, or multiple alternative responses, from the chat system.
This method is responsible for formatting the
httpx.Response
after having made the request.Render this object as an expression.
Set the system prompt for the LLM system.
Attribute summary
The ID of the model to use.
The system prompt used to set up the LLM system.
The environment variable that holds the API key.
The initial delay in seconds between client requests.
The base for the exponential backoff.
Whether to add jitter to the delay.
The maximum number of retries for client requests.
Additional model parameters, passed with every request.
Definitions
- abstract build_request_arguments(message, *, history=None, **model_params)[source]#
This method is responsible for formatting the input to the LLM chat system. For argument options see
httpx.AsyncClient.request
.
- get_api_key()#
Get the API key from the environment variable specified by
api_key_env
.- Return type:
- Returns:
the API key
- Raises:
ValueError – if the environment variable is not set
- get_client()[source]#
Get the client to use for making requests. Allows us to create a new async client every time we’re in a new event loop. Internally, we can cache the client using a weak key dictionary that maps event loop objects to async clients
- Return type:
AsyncClient
- Returns:
The client to use for making requests.
- abstract classmethod get_default_api_key_env()#
Get the default name of the environment variable that holds the API key.
- Return type:
- Returns:
the default name of the api key environment variable
- get_model_params()#
Get the parameters of the model as a mapping.
This includes all parameters that influence the model’s behavior, but not parameters that determine the model itself or are are specific to the client such as the model ID or the API key.
- async get_response(message, *, history=None, **model_params)[source]#
Get a response, or multiple alternative responses, from the chat system.
- Parameters:
- Return type:
- Returns:
the response or alternative responses generated by the chat system
- Raises:
RequestLimitException – if an error occurs while communicating with the chat system
- abstract parse_httpx_response(response)[source]#
This method is responsible for formatting the
httpx.Response
after having made the request.
- to_expression()#
Render this object as an expression.
- Return type:
Expression
- Returns:
the expression representing this object