Module: Rogalik::Plugins::OpenAIEmbeddings::EmbeddingsMethods
- Defined in:
- lib/rogalik/plugins/open_ai_embeddings.rb
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
OpenAI API key.
-
#model ⇒ String
readonly
Embedding model name.
Instance Method Summary collapse
-
#client ⇒ OpenAI::Client
Returns a memoized OpenAI client.
-
#embed_documents(documents) ⇒ Array<Pipeline::Document>
Generates embeddings for each document and assigns them in place.
-
#embed_text(text) ⇒ Array<Float>
Generates an embedding vector for a single string.
- #initialize(config = Rogalik.configuration.embeddings) ⇒ Object
Instance Attribute Details
#api_key ⇒ String (readonly)
Returns OpenAI API key.
11 12 13 |
# File 'lib/rogalik/plugins/open_ai_embeddings.rb', line 11 def api_key @api_key end |
#model ⇒ String (readonly)
Returns embedding model name.
14 15 16 |
# File 'lib/rogalik/plugins/open_ai_embeddings.rb', line 14 def model @model end |
Instance Method Details
#client ⇒ OpenAI::Client
Returns a memoized OpenAI client.
29 30 31 |
# File 'lib/rogalik/plugins/open_ai_embeddings.rb', line 29 def client @client ||= OpenAI::Client.new(api_key: api_key) end |
#embed_documents(documents) ⇒ Array<Pipeline::Document>
Generates embeddings for each document and assigns them in place.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rogalik/plugins/open_ai_embeddings.rb', line 37 def (documents) response = client..create(input: documents.map(&:page_content), model: model) = response.data documents.each_with_index do |doc, index| doc. = [index]. end documents end |
#embed_text(text) ⇒ Array<Float>
Generates an embedding vector for a single string.
52 53 54 55 |
# File 'lib/rogalik/plugins/open_ai_embeddings.rb', line 52 def (text) response = client..create(input: text, model: model) response.data.first. end |
#initialize(config = Rogalik.configuration.embeddings) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/rogalik/plugins/open_ai_embeddings.rb', line 17 def initialize(config = Rogalik.configuration.) api_key = config.api_key || ENV["OPENAI_API_KEY"] raise ArgumentError, "OpenAI API key is required, set through env OPENAI_API_KEY or config" if api_key.nil? raise ArgumentError, "OpenAI model name is required" if config.model.nil? @api_key = api_key @model = config.model end |