Sha256: 069f4cd79739bb9c3ab813fce4669fb097edf957ef696cce18ad6586af7d3550

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require_relative "quick_openai/version"
require_relative "quick_openai/dalle2"
require_relative "quick_openai/gpt3"
require_relative "quick_openai/extensions/string"

module QuickOpenAI
  class Error < StandardError; end

  class << self
    def client
      OpenAI::Client.new
    end

    def fetch_response_from_client
      ensure_access_token!

      begin
        response = yield client
      rescue StandardError
        raise QuickOpenAI::Error, "Unable to fetch response."
      end

      ensure_no_error!(response)

      response
    end

    private

    def ensure_access_token!
      return if ENV.key?("OPENAI_ACCESS_TOKEN")

      raise QuickOpenAI::Error, "Make sure OPENAI_ACCESS_TOKEN is present in ENV."
    end

    def ensure_no_error!(response)
      return unless (error = response.dig("error", "message"))

      raise QuickOpenAI::Error, "Unable to fetch response: #{error}"
    end
  end
end

require "down"
require "ruby/openai"
require "tempfile"

class String
  include QuickOpenAI::Extensions::String
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quick_openai-0.1.1 lib/quick_openai.rb