Class: Patogpt

Inherits:
Object
  • Object
show all
Defined in:
lib/patogpt.rb

Class Method Summary collapse

Class Method Details

.ask(q, key) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/patogpt.rb', line 7

def self.ask(q, key)
  payload = {
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: q }]
  }

  uri = URI("https://api.openai.com/v1/chat/completions")

  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true

  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Bearer #{key}"
  request.body = JSON.generate(payload)

  response = https.request(request)

  resp_json = JSON.parse(response.read_body)

  resp_json['choices'][0].dig('message', 'content')
end

.start(key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/patogpt.rb', line 30

def self.start(key)
  key = key || ENV['OPENAI_API_KEY']
  q = ''

  pastel = Pastel.new

  while true
    puts '..........................................................................................'
    puts ''
    print "@You: "

    q = gets.chomp

    break if q == "exit"

    if q.strip.length > 0
      a = ask(q, key)

      puts ''
      print '@PatoGtp: '
      puts TTY::Markdown.parse(a)
    end
  end
end