Class: Llmemory::VectorStore::OpenAIEmbeddings
- Defined in:
- lib/llmemory/vector_store/openai_embeddings.rb
Constant Summary collapse
- DEFAULT_MODEL =
"text-embedding-3-small"- DEFAULT_DIMS =
1536
Instance Attribute Summary collapse
-
#last_usage ⇒ Object
readonly
Returns the value of attribute last_usage.
Instance Method Summary collapse
- #embed(text) ⇒ Object
-
#initialize(api_key: nil, model: nil) ⇒ OpenAIEmbeddings
constructor
A new instance of OpenAIEmbeddings.
Constructor Details
#initialize(api_key: nil, model: nil) ⇒ OpenAIEmbeddings
Returns a new instance of OpenAIEmbeddings.
17 18 19 20 21 22 23 |
# File 'lib/llmemory/vector_store/openai_embeddings.rb', line 17 def initialize(api_key: nil, model: nil) @api_key = api_key || Llmemory.configuration.llm_api_key @model = model || DEFAULT_MODEL @cache = {} @cache_order = [] @last_usage = Llmemory::LLM::Usage.zero end |
Instance Attribute Details
#last_usage ⇒ Object (readonly)
Returns the value of attribute last_usage.
15 16 17 |
# File 'lib/llmemory/vector_store/openai_embeddings.rb', line 15 def last_usage @last_usage end |
Instance Method Details
#embed(text) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/llmemory/vector_store/openai_embeddings.rb', line 25 def (text) return Array.new(DEFAULT_DIMS, 0.0) if text.to_s.strip.empty? if Llmemory.configuration. key = cache_key(text) if @cache.key?(key) @last_usage = Llmemory::LLM::Usage.zero return @cache[key].dup end end result = (text) if Llmemory.configuration. evict_if_needed @cache[cache_key(text)] = result.dup @cache_order << cache_key(text) end result end |