AI.Endpoint behaviour (fnord v0.8.92)

View Source

API endpoint abstraction.

This behavior encapsulates the common mechanics for calling API endpoints via Http.post_json/3 and applying API-level retry semantics.

In particular, OpenAI rate limiting is surfaced as HTTP 429 with a JSON body containing an error code ("rate_limit_exceeded").

Callers implement endpoint_path/0 and then call AI.Endpoint.post_json/3.

Summary

Functions

Perform a JSON POST request against the endpoint module's endpoint_path/0. Retries up to 3 times when the server indicates throttling.

The fixed retry limit for API calls.

Types

endpoint()

@type endpoint() :: module()

headers()

@type headers() :: [{String.t(), String.t()}]

http_error()

@type http_error() :: {:http_error, {http_status(), String.t()}}

http_status()

@type http_status() :: integer()

payload()

@type payload() :: map()

response()

@type response() :: success() | http_error() | transport_error()

success()

@type success() :: {:ok, map()}

transport_error()

@type transport_error() :: {:transport_error, any()}

Callbacks

endpoint_path()

@callback endpoint_path() :: String.t()

Functions

post_json(endpoint_module, headers, payload)

@spec post_json(endpoint(), headers(), payload()) :: response()

Perform a JSON POST request against the endpoint module's endpoint_path/0. Retries up to 3 times when the server indicates throttling.

retry_limit()

@spec retry_limit() :: pos_integer()

The fixed retry limit for API calls.