Sha256: b8da085a9c7a1d67768eafa3dca7af0893066c7a9090a7c84230d40011e86e92

Contents?: true

Size: 855 Bytes

Versions: 2

Compression:

Stored size: 855 Bytes

Contents

# frozen_string_literal: true

require_relative "chatgpt/version"
require "openai"

module Ruby
  module Chatgpt
    class MissingApiKeyError < StandardError; end

    unless ENV["OPENAI_API_KEY"]
      raise MissingApiKeyError,
            "Please set a value for the OPENAI_API_KEY variable"
    end

    client = OpenAI::Client.new(access_token: ENV["OPENAI_API_KEY"])

    loop do
      print 'Enter a prompt (or "exit" to quit): '
      input = gets.chomp

      break if input.downcase == "exit"

      client.chat(
        parameters: {
          model: "gpt-3.5-turbo", # Required.
          messages: [{ role: "user", content: input }], # Required.
          temperature: 0.7,
          stream: proc do |chunk, _bytesize|
            print chunk.dig("choices", 0, "delta", "content")
          end
        }
      )

      puts
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-chatgpt-0.1.2 lib/ruby/chatgpt.rb
ruby-chatgpt-0.1.1 lib/ruby/chatgpt.rb