Class: OllamaChat::FollowChat
- Inherits:
-
Object
- Object
- OllamaChat::FollowChat
- Includes:
- Ollama, Ollama::Handlers::Concern, MessageType, Term::ANSIColor
- Defined in:
- lib/ollama_chat/follow_chat.rb
Instance Method Summary collapse
- #call(response) ⇒ Object
- #eval_stats(response) ⇒ Object
-
#initialize(chat:, messages:, voice: nil, output: STDOUT) ⇒ FollowChat
constructor
A new instance of FollowChat.
Methods included from MessageType
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 = @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&.&.role == 'assistant' if @messages&.last&.role != 'assistant' @messages << Message.new(role: 'assistant', content: '') @user = (@messages.last.images) + " " + bold { color(111) { 'assistant:' } } @output.puts @user unless @chat.markdown.on? end if content = response.&.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 |