Class: RubyLLM::SemanticCache::Configuration

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

Constant Summary collapse

VALID_VECTOR_STORES =
i[memory redis].freeze
VALID_CACHE_STORES =
i[memory redis].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 51

def initialize
  @vector_store = :memory
  @cache_store = :memory
  @redis_url = nil
  @redis_client = nil
  @embedding_model = "text-embedding-3-small"
  @embedding_dimensions = 1536
  @similarity_threshold = 0.92
  @ttl = nil
  @namespace = "ruby_llm_semantic_cache"
  @instrumentation_callback = nil
  @max_messages = 1
end

Instance Attribute Details

#cache_storeObject

Cache store backend: :redis, :memory



17
18
19
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 17

def cache_store
  @cache_store
end

#embedding_dimensionsObject

Embedding dimensions (default: 1536 for text-embedding-3-small)



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

def embedding_dimensions
  @embedding_dimensions
end

#embedding_modelObject

Embedding model to use (default: text-embedding-3-small)



26
27
28
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 26

def embedding_model
  @embedding_model
end

#instrumentation_callbackObject

Instrumentation callback for metrics/observability Called with event_name and payload hash



44
45
46
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 44

def instrumentation_callback
  @instrumentation_callback
end

#max_messagesObject

Maximum conversation messages before skipping cache (excluding system messages)

  • Integer: skip cache after N messages (default: 1, only first message cached)

  • :unlimited or false: cache all messages regardless of conversation length



49
50
51
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 49

def max_messages
  @max_messages
end

#namespaceObject

Namespace for cache keys (useful for multi-tenant apps)



40
41
42
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 40

def namespace
  @namespace
end

#redis_clientObject

Redis client instance (alternative to redis_url)



23
24
25
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 23

def redis_client
  @redis_client
end

#redis_urlObject

Redis connection URL (if using Redis backend)



20
21
22
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 20

def redis_url
  @redis_url
end

#similarity_thresholdObject

Similarity threshold (0.0 to 1.0) Higher = stricter matching, fewer cache hits Lower = looser matching, more cache hits but potential mismatches



34
35
36
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 34

def similarity_threshold
  @similarity_threshold
end

#ttlObject

TTL for cached entries in seconds (nil = no expiration)



37
38
39
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 37

def ttl
  @ttl
end

#vector_storeObject

Vector store backend: :redis, :memory



14
15
16
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 14

def vector_store
  @vector_store
end

Instance Method Details

#ttl_secondsObject



65
66
67
68
69
70
71
72
73
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 65

def ttl_seconds
  return nil if @ttl.nil?

  case @ttl
  when Numeric then @ttl.to_i
  when ->(t) { t.respond_to?(:to_i) } then @ttl.to_i
  else nil
  end
end

#valid?Boolean

Check if configuration is valid without raising

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 87

def valid?
  validate!
  true
rescue ConfigurationError
  false
end

#validate!Object

Validate the configuration and raise errors for invalid settings

Raises:



77
78
79
80
81
82
83
# File 'lib/ruby_llm/semantic_cache/configuration.rb', line 77

def validate!
  validate_stores!
  validate_threshold!
  validate_dimensions!
  validate_redis_config!
  true
end