Class: Prescient::API

Inherits:
Object
  • Object
show all
Defined in:
lib/prescient/api.rb

Overview

Dependency-free Rack-compatible HTTP application for Prescient operations.

The application exposes only generic Prescient operations. It does not expose provider-specific methods, credentials, or raw provider responses.

Constant Summary collapse

DEFAULT_MAX_BODY_BYTES =

Returns Default maximum request body size in bytes.

Returns:

  • (Integer)

    Default maximum request body size in bytes

1_048_576
MAX_BATCH_SIZE =

Returns Maximum number of inputs accepted by batch embeddings.

Returns:

  • (Integer)

    Maximum number of inputs accepted by batch embeddings

32
API_VERSION =

Returns HTTP API version.

Returns:

  • (String)

    HTTP API version

'1'
ROUTES =

Returns Generic HTTP route handlers.

Returns:

  • (Hash<Array<String>, Symbol>)

    Generic HTTP route handlers

{
  ['GET',  '/healthz']             => :healthz_response,
  ['GET',  '/readyz']              => :readiness_response,
  ['GET',  '/v1/version']          => :version_response,
  ['GET',  '/v1/providers']        => :providers_response,
  ['GET',  '/v1/models']           => :models_response,
  ['GET',  '/v1/capabilities']     => :capabilities_response,
  ['GET',  '/v1/health']           => :health_response,
  ['POST', '/v1/generate']         => :generate_response,
  ['POST', '/v1/embeddings']       => :embeddings_response,
  ['POST', '/v1/embeddings/batch'] => :batch_embeddings_response,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(authentication: nil, max_body_bytes: DEFAULT_MAX_BODY_BYTES) ⇒ void

Parameters:

  • authentication (#call, nil) (defaults to: nil)

    Optional authentication hook

  • max_body_bytes (Integer) (defaults to: DEFAULT_MAX_BODY_BYTES)

    Maximum accepted request body size



37
38
39
40
# File 'lib/prescient/api.rb', line 37

def initialize(authentication: nil, max_body_bytes: DEFAULT_MAX_BODY_BYTES)
  @authentication = authentication
  @max_body_bytes = validate_body_limit(max_body_bytes)
end

Instance Method Details

#call(env) ⇒ Array(Integer, Hash, Array<String>)

Handle a Rack-style environment and return a Rack response tuple.

Parameters:

  • env (Hash)

    Rack-compatible request environment

Returns:

  • (Array(Integer, Hash, Array<String>))

    HTTP status, headers, body



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/prescient/api.rb', line 45

def call(env)
  request_id = request_id_for(env)
  public_path = request_target(env).first
  return dispatch(env, request_id) if ['/healthz', '/readyz'].include?(public_path)

  unless authenticated?(env)
    return response(401,
                    error_payload('authentication_required', 'authentication required',
                                  request_id))
  end

  dispatch(env, request_id)
rescue StandardError => e
  handle_exception(e, request_id)
end