Class: LumenLLM::Stores::RedisStore

Inherits:
Object
  • Object
show all
Defined in:
lib/lumen_llm/stores/redis_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ RedisStore

Returns a new instance of RedisStore.



7
8
9
# File 'lib/lumen_llm/stores/redis_store.rb', line 7

def initialize(redis)
  @redis = redis
end

Instance Method Details

#all_stats(pattern = "stats:*") ⇒ Object



29
30
31
32
33
34
35
# File 'lib/lumen_llm/stores/redis_store.rb', line 29

def all_stats(pattern = "stats:*")
  result = {}
  @redis.keys(pattern).each do |key|
    result[key] = @redis.get(key).to_i
  end
  result
end

#cache(key, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/lumen_llm/stores/redis_store.rb', line 11

def cache(key, options = {})
  ttl = options[:ttl] || 3600
  value = @redis.get(key)
  return JSON.parse(value) if value

  result = yield
  @redis.setex(key, ttl, result.to_json)
  result
end

#incr_stat(metric, amount = 1) ⇒ Object



21
22
23
# File 'lib/lumen_llm/stores/redis_store.rb', line 21

def incr_stat(metric, amount = 1)
  @redis.incrby("stats:#{metric}", amount.to_i)
end

#stat(key) ⇒ Object



25
26
27
# File 'lib/lumen_llm/stores/redis_store.rb', line 25

def stat(key)
  @redis.get("stats:#{key}").to_i
end

#track_usage(template_key, model, usage) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lumen_llm/stores/redis_store.rb', line 37

def track_usage(template_key, model, usage)
  date = Date.today.strftime("%Y-%m-%d")
  [
    "usage:total",
    "usage:#{template_key}",
    "usage:model:#{model}",
    "usage:#{template_key}:model:#{model}",
    "usage:date:#{date}",
    "usage:#{template_key}:date:#{date}"
  ].each do |prefix|
    incr_stat("#{prefix}:prompt_tokens", usage["prompt_tokens"])
    incr_stat("#{prefix}:completion_tokens", usage["completion_tokens"])
    incr_stat("#{prefix}:total_tokens", usage["total_tokens"])
  end
end