Class: Ollama::Policies::CapabilityValidation

Inherits:
Base
  • Object
show all
Defined in:
lib/ollama/policies/capability_validation.rb

Overview

CapabilityValidation policy - validates model capabilities before request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: true, cache: nil, cache_ttl: 3600, hooks: {}) ⇒ CapabilityValidation

Returns a new instance of CapabilityValidation.

Parameters:

  • enabled (Boolean) (defaults to: true)

    Whether capability validation is enabled (default: true)

  • cache (Object) (defaults to: nil)

    Cache store for model profiles (must respond to read/write)

  • cache_ttl (Integer) (defaults to: 3600)

    Cache TTL in seconds (default: 3600)

  • hooks (Hash) (defaults to: {})

    Optional hooks: :capability_missing, :model_unknown



15
16
17
18
19
20
21
# File 'lib/ollama/policies/capability_validation.rb', line 15

def initialize(enabled: true, cache: nil, cache_ttl: 3600, hooks: {})
  super()
  @enabled = enabled
  @cache = cache
  @cache_ttl = cache_ttl
  @hooks = hooks
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



9
10
11
# File 'lib/ollama/policies/capability_validation.rb', line 9

def cache
  @cache
end

#enabledObject (readonly)

Returns the value of attribute enabled.



9
10
11
# File 'lib/ollama/policies/capability_validation.rb', line 9

def enabled
  @enabled
end

#hooksObject (readonly)

Returns the value of attribute hooks.



9
10
11
# File 'lib/ollama/policies/capability_validation.rb', line 9

def hooks
  @hooks
end

Instance Method Details

#around(request, env, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ollama/policies/capability_validation.rb', line 23

def around(request, env, &block)
  return block.call(request, env) unless @enabled

  # Skip if no model or endpoint doesn't require capabilities
  return block.call(request, env) unless validate_capabilities?(request)

  model = request_model(request)
  return block.call(request, env) unless model

  # Get model profile (with caching)
  profile = get_model_profile(model)

  # Check capabilities
  missing = check_capabilities(request, profile)
  return block.call(request, env) if missing.empty?

  @hooks[:capability_missing]&.call(missing, model, env)

  raise UnsupportedCapabilityError,
        "Model '#{model}' does not support: #{missing.join(", ")}"
end