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. rubocop:disable Metrics/ClassLength

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/search"] => :search_response,
  ["POST", "/v1/search/generate"] => :search_generate_response,
  ["POST", "/v1/agent"] => :agent_response,
  ["POST", "/v1/embeddings"] => :embeddings_response,
  ["POST", "/v1/embeddings/batch"] => :batch_embeddings_response
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(authentication: nil, authorization: nil, request_context: nil, telemetry: 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



42
43
44
45
46
47
48
49
# File 'lib/prescient/api.rb', line 42

def initialize(authentication: nil, authorization: nil, request_context: nil,
               telemetry: nil, max_body_bytes: DEFAULT_MAX_BODY_BYTES)
  @authentication = authentication
  @authorization = authorization
  @request_context = request_context
  @telemetry = telemetry
  @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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/prescient/api.rb', line 54

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)

  authentication = authentication_result(env)
  unless authentication
    return response(401,
                    error_payload("authentication_required", "authentication required",
                                  request_id))
  end

  dispatch(env, request_id, principal: authentication == true ? nil : authentication)
rescue StandardError => e
  handle_exception(e, request_id)
end