Class: Askcii::ChatSession

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

Instance Method Summary collapse

Constructor Details

#initialize(options, selected_config) ⇒ ChatSession



7
8
9
10
# File 'lib/askcii/chat_session.rb', line 7

def initialize(options, selected_config)
  @options = options
  @selected_config = selected_config
end

Instance Method Details

#create_chatObject



29
30
31
32
33
34
35
# File 'lib/askcii/chat_session.rb', line 29

def create_chat
  if @options[:private]
    create_private_chat
  else
    create_persistent_chat
  end
end

#execute_chat(prompt, input = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/askcii/chat_session.rb', line 37

def execute_chat(prompt, input = nil)
  chat = create_chat

  chat.with_instructions 'You are a command line application. Your responses should be suitable to be read in a terminal. Your responses should only include the necessary text. Do not include any explanations unless prompted for it.'

  full_prompt = input ? "With the following text:\n\n#{input}\n\n#{prompt}" : prompt

  chat.ask(full_prompt) do |chunk|
    print chunk.content
  end
  puts ''
end

#handle_last_responseObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/askcii/chat_session.rb', line 12

def handle_last_response
  return unless @options[:last_response]

  context = ENV['ASKCII_SESSION'] || SecureRandom.hex(8)
  model_id = @selected_config['model_id']
  chat_record = Askcii::Chat.find_or_create(context: context, model_id: model_id)

  last_message = chat_record.messages.select { |msg| msg.role == 'assistant' }.last
  if last_message
    puts last_message.content
    exit 0
  else
    puts 'No previous response found.'
    exit 1
  end
end