Class: RubyLLM::SemanticCache::Entry

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, response:, embedding:, metadata: {}, id: nil, created_at: nil) ⇒ Entry

Returns a new instance of Entry.



11
12
13
14
15
16
17
18
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 11

def initialize(query:, response:, embedding:, metadata: {}, id: nil, created_at: nil)
  @id = id || SecureRandom.uuid
  @query = query
  @response = response
  @embedding = embedding
   = 
  @created_at = created_at || Time.now
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



9
10
11
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 9

def created_at
  @created_at
end

#embeddingObject (readonly)

Returns the value of attribute embedding.



9
10
11
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 9

def embedding
  @embedding
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 9

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 9

def 
  
end

#queryObject (readonly)

Returns the value of attribute query.



9
10
11
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 9

def query
  @query
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 9

def response
  @response
end

Class Method Details

.from_h(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 30

def self.from_h(hash)
  new(
    id: hash[:id] || hash["id"],
    query: hash[:query] || hash["query"],
    response: hash[:response] || hash["response"],
    embedding: hash[:embedding] || hash["embedding"],
    metadata: hash[:metadata] || hash["metadata"] || {},
    created_at: parse_time(hash[:created_at] || hash["created_at"])
  )
end

.parse_time(value) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 41

def self.parse_time(value)
  case value
  when Time then value
  when String then Time.parse(value)
  when nil then Time.now
  else value
  end
end

Instance Method Details

#to_hObject



20
21
22
23
24
25
26
27
28
# File 'lib/ruby_llm/semantic_cache/entry.rb', line 20

def to_h
  {
    id: @id,
    query: @query,
    response: @response,
    metadata: ,
    created_at: @created_at.iso8601
  }
end