Class: Llmemory::VectorStore::OpenAIEmbeddings
- Includes:
- LLM::HttpClient
- Defined in:
- lib/llmemory/vector_store/openai_embeddings.rb
Constant Summary collapse
- DEFAULT_MODEL =
"text-embedding-3-small"- DEFAULT_DIMS =
1536
Constants included from LLM::HttpClient
LLM::HttpClient::RETRYABLE_STATUSES
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, base_url: nil) ⇒ OpenAIEmbeddings
constructor
A new instance of OpenAIEmbeddings.
Methods inherited from Base
Constructor Details
#initialize(api_key: nil, model: nil, base_url: nil) ⇒ OpenAIEmbeddings
Returns a new instance of OpenAIEmbeddings.
19 20 21 22 23 24 25 26 |
# File 'lib/llmemory/vector_store/openai_embeddings.rb', line 19 def initialize(api_key: nil, model: nil, base_url: nil) @api_key = api_key || Llmemory.configuration.llm_api_key @model = model || DEFAULT_MODEL @base_url = base_url || Llmemory.configuration.llm_base_url || "https://api.openai.com/v1" @cache = {} @cache_order = [] @last_usage = Llmemory::LLM::Usage.zero end |
Instance Attribute Details
#last_usage ⇒ Object (readonly)
Returns the value of attribute last_usage.
17 18 19 |
# File 'lib/llmemory/vector_store/openai_embeddings.rb', line 17 def last_usage @last_usage end |
Instance Method Details
#embed(text) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/llmemory/vector_store/openai_embeddings.rb', line 28 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 |