Class: ChatAway

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questions, apikey: nil, filepath: '/tmp/chatgpt', debug: false) ⇒ ChatAway

Returns a new instance of ChatAway.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/chatgpt2023.rb', line 336

def initialize(questions, apikey: nil, filepath: '/tmp/chatgpt', debug: false)
  
  @debug = debug
  
  FileUtils.mkdir_p filepath
  idxfile = File.join(filepath, 'index.xml')
  cgfile =  File.join(filepath, 'chatgpt.xml')

  puts 'questions: ' + questions.inspect if @debug
  @dx = case questions.class.to_s.to_sym
  when :Dynarex
    questions
  when :String
    questions.lines.length < 2 ? Dynarex.new(questions) : import(questions)
  end
  
  @chat = CGRecorder.new(apikey: apikey, indexfile: idxfile, 
                         logfile: cgfile, attempts: 5, debug: @debug)
  @prompts = @chat.index.all.map(&:prompt)
  
  @mode = nil
  
end

Instance Attribute Details

#dxObject (readonly)

statement below used for debugging



334
335
336
# File 'lib/chatgpt2023.rb', line 334

def dx
  @dx
end

#promptsObject (readonly)

statement below used for debugging



334
335
336
# File 'lib/chatgpt2023.rb', line 334

def prompts
  @prompts
end

Instance Method Details

#startObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/chatgpt2023.rb', line 360

def start()        
  
  @dx.all.map do |rx|
    
    puts 'rx: ' + rx.inspect if @debug
    
    #if (@prompts.include?(rx.prompt) and rx.redo != 'true')      #    or @mode != :import
    #  next 
    #end
    
    type = rx.type == 'code' ? :code_completion : :completion
    
    prompt = rx.prompt
    
    puts 'prompt: ' + prompt

    attempts = 0
    reply = nil
    
    begin
      
      r = @chat.method(type).call prompt
      
      puts 'r: ' + r.inspect if @debug
      
      if r[:error] then
        
        puts r[:error][:text]
        sleep 2
        attempts += 1
        
        redo if attempts < 4             
        
      else
        reply = r[:text]
      end
      
    rescue
      
      puts 'Something not working! ' + ($!).inspect
      sleep 2
      attempts += 1
      
      retry if attempts < 4 
      
    ensure
      
      reply ||= ''
      
    end
    
    sleep 2
    
    reply
      
  end    
  
end