Sha256: b054bf0646bceea4f1934c2f14bbed5a159a6fd73857d8bcf3dd4dd0f4991a3e

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

require "uri"
require "net/http"
require "json"
require "rainbow/ext/string"

module Blade
  class Gpt
    def initialize
      @questions = []
    end

    def ask(question)
      @questions << { role: "user", content: question }
      uri = URI.parse("https://api.thunderjava.com/v1/chat/completions")
      request = Net::HTTP::Post.new(uri)
      request.content_type = "application/json"
      request["Authorization"] = "Bearer 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"

      request.body = JSON.dump({
                                 "model": "gpt-3.5-turbo",
                                 "messages": @questions
                               })

      req_options = {
        use_ssl: uri.scheme == "https",
      }
      response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
        http.request(request)
      end
      res = JSON.parse(response.body)
      (res["choices"] || [] ).each do |choice|
        puts  Rainbow("GPT: ").yellow + Rainbow(choice["message"]["content"]).cyan
        @questions << choice["message"]
      end
      return res["choices"]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blade-gpt-1.0.0 lib/blade/gpt.rb
blade-gpt-0.1.1 lib/blade/gpt.rb