Sha256: 282644f86955893262abd06f3f444439df647cd0f57a913d41a0428fa95ee937

Contents?: true

Size: 1.65 KB

Versions: 23

Compression:

Stored size: 1.65 KB

Contents

require 'openai'
require 'fileutils'

module OpenAi
  class << self
    def client
      @client ||= create_client
    end

    def create_client
      configure_client
      OpenAI::Client.new
    end

    def configure_client
      OpenAI.configure do |config|
        config.access_token = ENV.fetch('OPENAI_ACCESS_TOKEN')
        config.organization_id = ENV.fetch('OPENAI_ORGANIZATION_ID', nil)
      end
    end

    def input(request, model = 'gpt-3.5-turbo', temperature = 0.7)
      client.chat(
        parameters: {
          model: model,
          messages: [{ role: 'user', content: request }],
          temperature: temperature
        })
    end

    def create_file(options)
      path, request, choice = options.values_at(:path, :request, :choice)
      File.write(path, output(request: request, choice: choice))
    end

    def output(options)
      request, choice = options.values_at(:request, :choice)
      choice ||= 0
      extract_text(input(request), 'choices', choice, 'message', 'content')
    end

    def edit_file(options)
      path, request, choice = options.values_at(:path, :request, :choice)
      content = File.read(path)
      response = edit(content: content, request: request)
      File.write(path, extract_text(response, 'choices', choice, 'text'))
    end

    def edit(options)
      content, request, model = options.values_at(:content, :request, :model)
      model ||= 'text-davinci-edit-001'
      client.edits(
        parameters: {
          model: model,
          input: content,
          instruction: request
        }
      )
    end

    private

    def extract_text(response, *keys)
      response.dig(*keys)
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
ruby_raider-0.8.3 lib/open_ai/open_ai.rb
ruby_raider-0.8.2 lib/open_ai/open_ai.rb
ruby_raider-0.8.1 lib/open_ai/open_ai.rb
ruby_raider-0.8.0 lib/open_ai/open_ai.rb
ruby_raider-0.7.9 lib/open_ai/open_ai.rb
ruby_raider-0.7.8 lib/open_ai/open_ai.rb
ruby_raider-0.7.7 lib/open_ai/open_ai.rb
ruby_raider-0.7.6 lib/open_ai/open_ai.rb
ruby_raider-0.7.5 lib/open_ai/open_ai.rb
ruby_raider-0.7.4 lib/open_ai/open_ai.rb
ruby_raider-0.7.3 lib/open_ai/open_ai.rb
ruby_raider-0.7.2 lib/open_ai/open_ai.rb
ruby_raider-0.7.1 lib/open_ai/open_ai.rb
ruby_raider-0.7.0 lib/open_ai/open_ai.rb
ruby_raider-0.6.9 lib/open_ai/open_ai.rb
ruby_raider-0.6.8 lib/open_ai/open_ai.rb
ruby_raider-0.6.7 lib/open_ai/open_ai.rb
ruby_raider-0.6.6 lib/open_ai/open_ai.rb
ruby_raider-0.6.5 lib/open_ai/open_ai.rb
ruby_raider-0.6.4 lib/open_ai/open_ai.rb