Sha256: f5ab3e1200c54edfbccdef703bcafe00627e5fe50ccf9541d5730582b4c92dfc

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module OpenAI
  class Client
    include HTTParty
    base_uri "https://api.openai.com"

    def initialize(access_token: nil)
      @access_token = access_token || ENV["OPENAI_ACCESS_TOKEN"]
    end

    def completions(engine:, version: default_version, parameters: {})
      self.class.post(
        "/#{version}/engines/#{engine}/completions",
        headers: {
          "Content-Type" => "application/json",
          "Authorization" => "Bearer #{@access_token}"
        },
        body: parameters.to_json
      )
    end

    def files
      @files ||= OpenAI::Files.new(access_token: @access_token)
    end

    def search(engine:, query:, documents: nil, file: nil, version: default_version)
      self.class.post(
        "/#{version}/engines/#{engine}/search",
        headers: {
          "Content-Type" => "application/json",
          "Authorization" => "Bearer #{@access_token}"
        },
        body: { query: query }.merge(documents_or_file(documents: documents, file: file)).to_json
      )
    end

    private

    def default_version
      "v1".freeze
    end

    def documents_or_file(documents: nil, file: nil)
      documents ? { documents: documents } : { file: file }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-openai-1.1.0 lib/ruby/openai/client.rb