class Router

Selects a configured provider for each OpenAI-compatible HTTP request.

Definitions

def initialize(configuration)

Initialize a router for the given configuration.

Signature

parameter configuration Laiya::Configuration

The provider and model routes.

Implementation

def initialize(configuration)
	@configuration = configuration
end

def call(request)

Route an HTTP request to its configured provider.

Signature

parameter request Protocol::HTTP::Request

The incoming request.

returns Protocol::HTTP::Response

The provider response or a routing error.

Implementation

def call(request)
	provider = provider_for_model(model_for(request))
	
	unless provider
		return Protocol::HTTP::Response[
			404,
			{"content-type" => "application/json"},
			[JSON.dump(error: {message: "No provider is configured for this request", type: "not_found_error"})],
		]
	end
	
	return provider.call(request)
end

def models_response

Return the configured model catalog, if present.

Implementation

def models_response
	return if @configuration.models.empty?
	models = @configuration.models.each(default_provider: @configuration.default_provider).to_a
	
	Protocol::HTTP::Response[
		200,
		{"content-type" => "application/json"},
		[JSON.dump(object: "list", data: models)],
	]
end

def close

Close configured providers that support closing.

Implementation

def close
	@configuration.providers.values.uniq.each do |provider|
		provider.close if provider.respond_to?(:close)
	end
end