Class: Leann::Embedding::OpenAI
- Defined in:
- lib/leann/embedding/openai.rb
Overview
OpenAI Embeddings API provider
Constant Summary collapse
- BASE_URL =
"https://api.openai.com/v1/embeddings"- MAX_BATCH_SIZE =
2048- MAX_RETRIES =
3- RETRY_DELAY =
1.0- DIMENSIONS =
Model dimensions lookup
{ "text-embedding-3-small" => 1536, "text-embedding-3-large" => 3072, "text-embedding-ada-002" => 1536 }.freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#compute(texts) ⇒ Array<Array<Float>>
Compute embeddings for texts.
-
#initialize(model: "text-embedding-3-small", api_key: nil, base_url: nil) ⇒ OpenAI
constructor
A new instance of OpenAI.
Methods inherited from Base
Constructor Details
#initialize(model: "text-embedding-3-small", api_key: nil, base_url: nil) ⇒ OpenAI
Returns a new instance of OpenAI.
32 33 34 35 36 37 38 39 40 |
# File 'lib/leann/embedding/openai.rb', line 32 def initialize(model: "text-embedding-3-small", api_key: nil, base_url: nil) super(model: model) @api_key = api_key || Leann.configuration.openai_api_key || ENV["OPENAI_API_KEY"] @base_url = base_url || Leann.configuration.openai_base_url || BASE_URL @dimensions = DIMENSIONS[model] validate_configuration! end |
Instance Method Details
#compute(texts) ⇒ Array<Array<Float>>
Compute embeddings for texts
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/leann/embedding/openai.rb', line 46 def compute(texts) return [] if texts.empty? = [] in_batches(texts, MAX_BATCH_SIZE) do |batch| = compute_batch(batch) .concat() print "." # Progress indicator end puts " Done! (#{.size} embeddings)" unless texts.size < MAX_BATCH_SIZE end |