Class: OllamaChat::FollowChat

Inherits:
Object
  • Object
show all
Includes:
Ollama, Ollama::Handlers::Concern, MessageType, Term::ANSIColor
Defined in:
lib/ollama_chat/follow_chat.rb

Instance Method Summary collapse

Methods included from MessageType

#message_type

Constructor Details

#initialize(chat:, messages:, voice: nil, output: STDOUT) ⇒ FollowChat

Returns a new instance of FollowChat.



7
8
9
10
11
12
13
14
# File 'lib/ollama_chat/follow_chat.rb', line 7

def initialize(chat:, messages:, voice: nil, output: STDOUT)
  super(output:)
  @chat        = chat
  @output.sync = true
  @say         = voice ? Handlers::Say.new(voice:) : NOP
  @messages    = messages
  @user        = nil
end

Instance Method Details

#call(response) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ollama_chat/follow_chat.rb', line 16

def call(response)
  OllamaChat::Chat.config.debug and jj response
  if response&.message&.role == 'assistant'
    if @messages&.last&.role != 'assistant'
      @messages << Message.new(role: 'assistant', content: '')
      @user = message_type(@messages.last.images) + " " +
        bold { color(111) { 'assistant:' } }
      @output.puts @user unless @chat.markdown.on?
    end
    if content = response.message&.content
      content = content.gsub(%r(<think>), "šŸ’­\n").gsub(%r(</think>), "\nšŸ’¬")
    end
    @messages.last.content << content
    if @chat.markdown.on? and content = @messages.last.content.full?
      markdown_content = Kramdown::ANSI.parse(content)
      @output.print clear_screen, move_home, @user, ?\n, markdown_content
    else
      @output.print content
    end
    @say.call(response)
  end
  if response.done
    @output.puts "", eval_stats(response)
  end
  self
end

#eval_stats(response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ollama_chat/follow_chat.rb', line 43

def eval_stats(response)
  eval_duration = response.eval_duration / 1e9
  prompt_eval_duration = response.prompt_eval_duration / 1e9
  stats_text = {
    eval_duration:        Tins::Duration.new(eval_duration),
    eval_count:           response.eval_count.to_i,
    eval_rate:            bold { "%.2f c/s" % (response.eval_count.to_i / eval_duration) } + color(111),
    prompt_eval_duration: Tins::Duration.new(prompt_eval_duration),
    prompt_eval_count:    response.prompt_eval_count.to_i,
    prompt_eval_rate:     bold { "%.2f c/s" % (response.prompt_eval_count.to_i / prompt_eval_duration) } + color(111),
    total_duration:       Tins::Duration.new(response.total_duration / 1e9),
    load_duration:        Tins::Duration.new(response.load_duration / 1e9),
  }.map { _1 * ?= } * ' '
  'šŸ“Š ' + color(111) {
    Kramdown::ANSI::Width.wrap(stats_text, percentage: 90).gsub(/(?<!\A)^/, '   ')
  }
end