Class: SemanticTextChunker::Embedders::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_text_chunker/embedders/openai.rb

Constant Summary collapse

BATCH_SIZE =
100
ENDPOINT =
"https://api.openai.com/v1/embeddings"

Instance Method Summary collapse

Methods inherited from Base

#cosine_similarity

Constructor Details

#initialize(api_key:, model: "text-embedding-3-small") ⇒ OpenAI

Returns a new instance of OpenAI.



11
12
13
14
# File 'lib/semantic_text_chunker/embedders/openai.rb', line 11

def initialize(api_key:, model: "text-embedding-3-small")
  @api_key = api_key
  @model   = model
end

Instance Method Details

#embed(texts) ⇒ Object



16
17
18
19
20
21
# File 'lib/semantic_text_chunker/embedders/openai.rb', line 16

def embed(texts)
  texts.each_slice(BATCH_SIZE).flat_map do |batch|
    response = post(batch)
    response["data"].map { |d| d["embedding"] }
  end
end