Class: PatientLLM::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_llm/configuration.rb

Overview

Configuration for provider registry.

Providers can be registered from a built-in preset, which supplies the vendor's base URL, serializer, and authentication header details, so that registering a provider and wiring up its API key is a single step:

Examples:

Using presets

PatientLLM.configure do |config|
  config.provider :openai, preset: :openai, api_key: -> { ENV["OPENAI_API_KEY"] }
  config.provider :anthropic, preset: :anthropic, api_key: -> { ENV["ANTHROPIC_API_KEY"] }
end

Fully custom provider

PatientLLM.configure do |config|
  config.provider :local, url: "http://localhost:1234", headers: {}
end

Constant Summary collapse

SECRET_NAME_PREFIX =

Prefix for secret names derived from provider names by the api_key option.

"patient_llm"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



31
32
33
34
# File 'lib/patient_llm/configuration.rb', line 31

def initialize
  @providers = {}
  @session_offload_options = nil
end

Instance Attribute Details

#session_offload_optionsObject (readonly)

Returns the value of attribute session_offload_options.



29
30
31
# File 'lib/patient_llm/configuration.rb', line 29

def session_offload_options
  @session_offload_options
end

Instance Method Details

#lookup(name) ⇒ Hash?

Look up a registered provider by name. A provider registered with callable arguments is resolved on every lookup: each callable is called and the provider config is rebuilt from the results.

Parameters:

  • name (Symbol, String)

    Provider name

Returns:

  • (Hash, nil)

    Provider config hash



97
98
99
100
101
102
103
104
105
# File 'lib/patient_llm/configuration.rb', line 97

def lookup(name)
  entry = @providers[name&.to_s]
  return entry unless entry&.key?(:deferred)

  resolved = entry[:deferred].transform_values do |value|
    value.respond_to?(:call) ? value.call : value
  end
  build_provider_config(resolved)
end

#provider(name, preset: nil, url: nil, api_key: nil, region: nil, headers: nil, serializer: nil, path: nil, params: {}, preprocessors: nil, timeout: nil, max_tool_iterations: nil) ⇒ void

This method returns an undefined value.

Register a provider.

When a preset is given, its values are used as defaults; any explicit keyword argument overrides the preset. When api_key is given, the key is registered as a PatientHttp secret named patient_llm.<name>.api_key and referenced from the provider's authentication header, so no separate register_secret call is needed. The key value itself is never serialized into requests; only the secret name travels with them.

Arguments passed as a callable object (responds to :call) are evaluated at runtime on every request.

Parameters:

  • name (Symbol, String)

    Provider name

  • preset (Symbol, nil) (defaults to: nil)

    Built-in preset name (:openai, :anthropic, :gemini, :bedrock_runtime)

  • url (String, #call, nil) (defaults to: nil)

    Base URL for the provider API

  • api_key (#call, String, PatientHttp::SecretReference, nil) (defaults to: nil)

    The API key for the provider. A Proc or callable object is resolved at dispatch time (preferred). A String is captured into the secret registry and never serialized. A SecretReference uses your own registered secret as-is. This option can only be used in conjunction with a preset.

  • region (String, #call, nil) (defaults to: nil)

    Region for presets with region-based URLs (e.g. :bedrock_runtime)

  • headers (Hash, #call, nil) (defaults to: nil)

    Default headers for requests (merged over preset headers)

  • serializer (Symbol, #call, nil) (defaults to: nil)

    API format (:chat_completion, :open_responses, :messages, :converse, :gemini)

  • path (String, #call, nil) (defaults to: nil)

    Override the default endpoint path

  • params (Hash, #call) (defaults to: {})

    Additional parameters to merge into every request payload

  • preprocessors (String, Symbol, Array<String, Symbol>, #call, nil) (defaults to: nil)

    Names of request preprocessors to apply to every request (e.g. for request signing). Names must be registered on the PatientHttp configuration with register_preprocessor.

  • timeout (Numeric, #call, nil) (defaults to: nil)

    Request timeout in seconds for this provider's requests

  • max_tool_iterations (Integer, #call, nil) (defaults to: nil)

    Maximum automatic tool-execution rounds for this provider's requests (default: PatientLLM::Callback::MAX_TOOL_ITERATIONS)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/patient_llm/configuration.rb', line 67

def provider(name, preset: nil, url: nil, api_key: nil, region: nil, headers: nil, serializer: nil, path: nil, params: {}, preprocessors: nil, timeout: nil, max_tool_iterations: nil)
  preset_config = preset ? Presets.fetch(preset) : nil
  options = {
    preset_config: preset_config,
    url: url,
    region: region,
    headers: headers,
    serializer: serializer,
    path: path,
    params: params,
    preprocessors: preprocessors,
    timeout: timeout,
    max_tool_iterations: max_tool_iterations
  }

  options[:api_key_header] = api_key_header(name, api_key, preset_config || {}) if api_key

  @providers[name.to_s] = if options.values.any? { |value| value.respond_to?(:call) }
    {deferred: options}
  else
    build_provider_config(options)
  end
end

#provider_namesArray<String>

All registered provider names.

Returns:

  • (Array<String>)

    the provider names



110
111
112
# File 'lib/patient_llm/configuration.rb', line 110

def provider_names
  @providers.keys
end

#session_offload(payload_store:, threshold: 65_536) ⇒ void

This method returns an undefined value.

Configure automatic offloading of large sessions to a PatientHttp payload store instead of carrying them through the job queue inline. Sessions whose serialized size exceeds the threshold are written to the store and passed by reference.

Parameters:

  • payload_store (Symbol, String)

    Name of a payload store registered on the PatientHttp configuration

  • threshold (Integer) (defaults to: 65_536)

    Size in bytes above which sessions are offloaded

Raises:

  • (ArgumentError)


123
124
125
126
127
# File 'lib/patient_llm/configuration.rb', line 123

def session_offload(payload_store:, threshold: 65_536)
  raise ArgumentError, "threshold must be a positive Integer" unless threshold.is_a?(Integer) && threshold.positive?

  @session_offload_options = {payload_store: payload_store.to_s, threshold: threshold}
end