Sha256: d054676ba18ddb6908445fb99766bfd8fd5f0d6e982414b37039d0daae4b44f2

Contents?: true

Size: 1.21 KB

Versions: 28

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'openai'

module Boxcars
  module VectorStore
    class EmbedViaOpenAI
      include VectorStore

      def initialize(texts:, client:, model: 'text-embedding-ada-002')
        validate_params(texts, client)
        @texts = texts
        @client = client
        @model = model
      end

      def call
        texts.map do |text|
          embedding = embedding_with_retry(model: model, input: strip_new_lines(text))
          {
            embedding: embedding,
            dim: embedding.size
          }
        end
      end

      private

      attr_accessor :texts, :client, :model

      def validate_params(texts, client)
        raise_error 'texts must be an array of strings' unless texts.is_a?(Array) && texts.all? { |text| text.is_a?(String) }
        raise_error 'openai_connection must be an OpenAI::Client' unless client.is_a?(OpenAI::Client)
      end

      def embedding_with_retry(request)
        response = @client.embeddings(parameters: request)
        response['data'][0]['embedding']
      end

      def strip_new_lines(text)
        text.gsub("\n", ' ')
      end

      def raise_error(message)
        raise ::Boxcars::ValueError, message
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
boxcars-0.6.6 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.6.5 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.6.4 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.6.3 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.6.2 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.6.1 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.5.1 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.10 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.9 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.8 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.7 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.6 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.5 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.4 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.3 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.2 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.1 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.4.0 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.3.5 lib/boxcars/vector_store/embed_via_open_ai.rb
boxcars-0.3.4 lib/boxcars/vector_store/embed_via_open_ai.rb