Class: Rack::AI::Configuration
- Inherits:
-
Object
- Object
- Rack::AI::Configuration
- Extended by:
- Dry::Configurable
- Defined in:
- lib/rack/ai/configuration.rb
Constant Summary collapse
- ConfigSchema =
Validation schema
Dry::Validation.Contract do params do required(:provider).filled(:symbol) optional(:api_key).maybe(:string) optional(:api_url).maybe(:string) required(:timeout).filled(:integer) required(:retries).filled(:integer) required(:features).filled(:array) required(:fail_safe).filled(:bool) end rule(:provider) do key.failure("must be one of: openai, huggingface, local") unless [:openai, :huggingface, :local].include?(value) end rule(:timeout) do key.failure("must be positive") if value <= 0 end rule(:retries) do key.failure("must be non-negative") if value < 0 end end
Instance Attribute Summary collapse
-
#allowed_data_types ⇒ Object
Instance methods for configuration access.
-
#api_key ⇒ Object
Instance methods for configuration access.
-
#api_url ⇒ Object
Instance methods for configuration access.
-
#async_processing ⇒ Object
Instance methods for configuration access.
-
#batch_size ⇒ Object
Instance methods for configuration access.
-
#blocked_data_types ⇒ Object
Instance methods for configuration access.
-
#cache_enabled ⇒ Object
Instance methods for configuration access.
-
#cache_ttl ⇒ Object
Instance methods for configuration access.
-
#explain_decisions ⇒ Object
Instance methods for configuration access.
-
#fail_safe ⇒ Object
Instance methods for configuration access.
-
#features ⇒ Object
Instance methods for configuration access.
-
#log_level ⇒ Object
Instance methods for configuration access.
-
#metrics_enabled ⇒ Object
Instance methods for configuration access.
-
#provider ⇒ Object
Instance methods for configuration access.
-
#rate_limit ⇒ Object
Instance methods for configuration access.
-
#retries ⇒ Object
Instance methods for configuration access.
-
#sanitize_logs ⇒ Object
Instance methods for configuration access.
-
#timeout ⇒ Object
Instance methods for configuration access.
Instance Method Summary collapse
- #caching ⇒ Object
- #classification ⇒ Object
-
#config ⇒ Object
For compatibility with middleware.
- #feature_enabled?(feature) ⇒ Boolean
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #moderation ⇒ Object
- #provider_config ⇒ Object
- #routing ⇒ Object
- #to_h ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rack/ai/configuration.rb', line 88 def initialize # Set default values @provider = :openai @api_key = nil @api_url = nil @timeout = 30 @retries = 3 @features = [:classification, :moderation] @fail_safe = true @async_processing = true @sanitize_logs = true @allowed_data_types = [:headers, :query_params] @blocked_data_types = [:body, :cookies] @cache_enabled = true @cache_ttl = 3600 @rate_limit = 1000 @batch_size = 10 @log_level = :info @metrics_enabled = true @explain_decisions = false # Nested configuration objects with OpenStruct-like behavior end |
Instance Attribute Details
#allowed_data_types ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def allowed_data_types @allowed_data_types end |
#api_key ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def api_key @api_key end |
#api_url ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def api_url @api_url end |
#async_processing ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def async_processing @async_processing end |
#batch_size ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def batch_size @batch_size end |
#blocked_data_types ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def blocked_data_types @blocked_data_types end |
#cache_enabled ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def cache_enabled @cache_enabled end |
#cache_ttl ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def cache_ttl @cache_ttl end |
#explain_decisions ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def explain_decisions @explain_decisions end |
#fail_safe ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def fail_safe @fail_safe end |
#features ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def features @features end |
#log_level ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def log_level @log_level end |
#metrics_enabled ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def metrics_enabled @metrics_enabled end |
#provider ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def provider @provider end |
#rate_limit ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def rate_limit @rate_limit end |
#retries ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def retries @retries end |
#sanitize_logs ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def sanitize_logs @sanitize_logs end |
#timeout ⇒ Object
Instance methods for configuration access
83 84 85 |
# File 'lib/rack/ai/configuration.rb', line 83 def timeout @timeout end |
Instance Method Details
#caching ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/rack/ai/configuration.rb', line 127 def caching @caching ||= OpenStruct.new( predictive_enabled: true, prefetch_threshold: 0.9, redis_url: "redis://localhost:6379" ) end |
#classification ⇒ Object
112 113 114 115 116 117 |
# File 'lib/rack/ai/configuration.rb', line 112 def classification @classification ||= OpenStruct.new( confidence_threshold: 0.8, categories: [:spam, :bot, :human, :suspicious] ) end |
#config ⇒ Object
For compatibility with middleware
144 145 146 |
# File 'lib/rack/ai/configuration.rb', line 144 def config self end |
#feature_enabled?(feature) ⇒ Boolean
204 205 206 |
# File 'lib/rack/ai/configuration.rb', line 204 def feature_enabled?(feature) @features.include?(feature.to_sym) end |
#moderation ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/rack/ai/configuration.rb', line 119 def moderation @moderation ||= OpenStruct.new( toxicity_threshold: 0.7, check_response: false, block_on_violation: true ) end |
#provider_config ⇒ Object
208 209 210 211 212 213 214 215 216 |
# File 'lib/rack/ai/configuration.rb', line 208 def provider_config { provider: provider, api_key: api_key, api_url: api_url, timeout: timeout, retries: retries } end |
#routing ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/rack/ai/configuration.rb', line 135 def routing @routing ||= OpenStruct.new( smart_routing_enabled: true, suspicious_route: "/captcha", bot_route: "/api/bot" ) end |
#to_h ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/rack/ai/configuration.rb', line 181 def to_h { provider: provider, api_key: api_key, api_url: api_url, timeout: timeout, retries: retries, features: @features, fail_safe: @fail_safe, async_processing: @async_processing, sanitize_logs: @sanitize_logs, allowed_data_types: @allowed_data_types, blocked_data_types: @blocked_data_types, cache_enabled: @cache_enabled, cache_ttl: @cache_ttl, rate_limit: @rate_limit, batch_size: @batch_size, log_level: @log_level, metrics_enabled: @metrics_enabled, explain_decisions: @explain_decisions } end |
#validate! ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/rack/ai/configuration.rb', line 173 def validate! result = ConfigSchema.call(to_h) return true if result.success? errors = result.errors.to_h raise ConfigurationError, "Invalid configuration: #{errors}" end |