Class: SemanticCache::Entry
- Inherits:
-
Object
- Object
- SemanticCache::Entry
- Defined in:
- lib/semantic_cache/entry.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#embedding ⇒ Object
readonly
Returns the value of attribute embedding.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Class Method Summary collapse
Instance Method Summary collapse
- #expired? ⇒ Boolean
-
#initialize(query:, embedding:, response:, model: nil, tags: [], ttl: nil, metadata: {}) ⇒ Entry
constructor
A new instance of Entry.
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(query:, embedding:, response:, model: nil, tags: [], ttl: nil, metadata: {}) ⇒ Entry
Returns a new instance of Entry.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/semantic_cache/entry.rb', line 9 def initialize(query:, embedding:, response:, model: nil, tags: [], ttl: nil, metadata: {}) @query = query = @response = response @model = model = Array() @created_at = Time.now.to_f @ttl = ttl = end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def created_at @created_at end |
#embedding ⇒ Object (readonly)
Returns the value of attribute embedding.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def model @model end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def query @query end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def response @response end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
7 8 9 |
# File 'lib/semantic_cache/entry.rb', line 7 def ttl @ttl end |
Class Method Details
.from_h(hash) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/semantic_cache/entry.rb', line 43 def self.from_h(hash) hash = hash.transform_keys(&:to_sym) if hash.is_a?(Hash) entry = new( query: hash[:query], embedding: hash[:embedding], response: hash[:response], model: hash[:model], tags: hash[:tags] || [], ttl: hash[:ttl], metadata: hash[:metadata] || {} ) entry.instance_variable_set(:@created_at, hash[:created_at]) if hash[:created_at] entry end |
.from_json(json_string) ⇒ Object
58 59 60 |
# File 'lib/semantic_cache/entry.rb', line 58 def self.from_json(json_string) from_h(JSON.parse(json_string)) end |
Instance Method Details
#expired? ⇒ Boolean
20 21 22 23 24 |
# File 'lib/semantic_cache/entry.rb', line 20 def expired? return false if @ttl.nil? Time.now.to_f - @created_at > @ttl end |
#to_h ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/semantic_cache/entry.rb', line 26 def to_h { query: @query, embedding: , response: @response, model: @model, tags: , created_at: @created_at, ttl: @ttl, metadata: } end |
#to_json(*args) ⇒ Object
39 40 41 |
# File 'lib/semantic_cache/entry.rb', line 39 def to_json(*args) to_h.to_json(*args) end |