class Application < Protocol::HTTP::Middleware
- Inherits from
Protocol::HTTP::Middleware
OpenAI-compatible HTTP API application.
Definitions
def initialize(configuration:)
Initialize the API middleware with a configuration.
Signature
-
parameter
configurationLaiya::Configuration Provider and model routing settings.
Implementation
def initialize(configuration:)
@configuration = configuration
@router = Laiya::Router.new(configuration)
end
def call(request)
Handle a model-list request or proxy a request to its provider.
Signature
-
parameter
requestProtocol::HTTP::Request The incoming HTTP request.
-
returns
Protocol::HTTP::Response The API response.
Implementation
def call(request)
if request.method == "GET" && request.path == "/v1/models" && (response = @router.models_response)
return response
end
return @router.call(request)
rescue StandardError
return Protocol::HTTP::Response[
502,
{"content-type" => "application/json"},
[JSON.dump(error: {message: "Upstream provider request failed", type: "server_error"})],
]
end
def close
Close all providers owned by the application.
Implementation
def close
@router.close
end