Class: LumenLLM::Stores::MemoryStore

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

Instance Method Summary collapse

Constructor Details

#initializeMemoryStore

Returns a new instance of MemoryStore.



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

def initialize
  @values = {}
  @stats = Hash.new(0)
end

Instance Method Details

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



25
26
27
28
29
30
31
32
# File 'lib/lumen_llm/stores/memory_store.rb', line 25

def all_stats(pattern = "stats:*")
  prefix = pattern.sub(/\*$/, "")
  result = {}
  @stats.each do |key, value|
    result[key] = value if key.index(prefix) == 0
  end
  result
end

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



11
12
13
14
15
# File 'lib/lumen_llm/stores/memory_store.rb', line 11

def cache(key, _options = {})
  return @values[key] if @values.key?(key)

  @values[key] = yield
end

#incr_stat(metric, amount = 1) ⇒ Object



17
18
19
# File 'lib/lumen_llm/stores/memory_store.rb', line 17

def incr_stat(metric, amount = 1)
  @stats["stats:#{metric}"] += amount.to_i
end

#stat(key) ⇒ Object



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

def stat(key)
  @stats["stats:#{key}"].to_i
end

#track_usage(template_key, model, usage) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lumen_llm/stores/memory_store.rb', line 34

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