Class: OllamaChat::MessageList
- Inherits:
-
Object
- Object
- OllamaChat::MessageList
- Includes:
- MessageFormat, Term::ANSIColor
- Defined in:
- lib/ollama_chat/message_list.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#system ⇒ Object
readonly
Returns the value of attribute system.
Instance Method Summary collapse
-
#<<(message) ⇒ OllamaChat::MessageList
The << operator appends a message to the list of messages and returns self.
-
#at_location ⇒ String
The at_location method returns the location/time/units information as a string if location is enabled.
-
#clear ⇒ OllamaChat::MessageList
The clear method removes all non-system messages from the message list.
-
#drop(n) ⇒ Integer
Removes the last ‘n` exchanges from the message list.
-
#initialize(chat) ⇒ MessageList
constructor
The initialize method sets up the message list for an OllamaChat session.
-
#last ⇒ Ollama::Message
Returns the last message from the conversation.
-
#list_conversation(last = nil) ⇒ OllamaChat::MessageList
The list_conversation method displays the last n messages from the conversation.
-
#load_conversation(filename) ⇒ OllamaChat::MessageList
The load_conversation method loads a conversation from a file and populates the message list.
-
#save_conversation(filename) ⇒ OllamaChat::MessageList
The save_conversation method saves the current conversation to a file.
-
#second_last ⇒ Ollama::Message
The second_last method returns the second-to-last message from the conversation if there are more than one non-system messages.
-
#set_system_prompt(system) ⇒ OllamaChat::MessageList
Sets the system prompt for the chat session.
-
#show_last ⇒ OllamaChat::MessageList
The show_last method displays the text of the last message if it is not from the user.
-
#show_system_prompt ⇒ self, NilClass
The show_system_prompt method displays the system prompt configured for the chat session.
-
#size ⇒ Integer
Returns the number of messages stored in the message list.
-
#to_ary ⇒ Array
The to_ary method converts the message list into an array of Ollama::Message objects.
Methods included from MessageFormat
#message_type, #talk_annotate, #think_annotate
Constructor Details
#initialize(chat) ⇒ MessageList
The initialize method sets up the message list for an OllamaChat session.
belongs to
9 10 11 12 |
# File 'lib/ollama_chat/message_list.rb', line 9 def initialize(chat) @chat = chat = [] end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
16 17 18 |
# File 'lib/ollama_chat/message_list.rb', line 16 def end |
#system ⇒ Object (readonly)
Returns the value of attribute system.
14 15 16 |
# File 'lib/ollama_chat/message_list.rb', line 14 def system @system end |
Instance Method Details
#<<(message) ⇒ OllamaChat::MessageList
The << operator appends a message to the list of messages and returns self.
38 39 40 41 |
# File 'lib/ollama_chat/message_list.rb', line 38 def <<() << self end |
#at_location ⇒ String
The at_location method returns the location/time/units information as a string if location is enabled.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/ollama_chat/message_list.rb', line 239 def at_location if @chat.location.on? location_name = config.location.name location_decimal_degrees = config.location.decimal_degrees * ', ' localtime = Time.now.iso8601 units = config.location.units config.prompts.location % { location_name:, location_decimal_degrees:, localtime:, units:, } end.to_s end |
#clear ⇒ OllamaChat::MessageList
The clear method removes all non-system messages from the message list.
28 29 30 31 |
# File 'lib/ollama_chat/message_list.rb', line 28 def clear .delete_if { _1.role != 'system' } self end |
#drop(n) ⇒ Integer
-
System messages are preserved and not considered part of an exchange.
-
If only one incomplete exchange (a single user message) exists, it will be dropped first before removing complete exchanges.
Removes the last ‘n` exchanges from the message list. An exchange consists of a user and an assistant message. If only a single user message is present at the end, it will be removed first before proceeding with complete exchanges.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ollama_chat/message_list.rb', line 136 def drop(n) n = n.to_i.clamp(1, Float::INFINITY) = .reject { _1.role == 'system' } if &.last&.role == 'user' .pop n -= 1 end if n == 0 STDOUT.puts "Dropped the last exchange." return 1 end if .empty? STDOUT.puts "No more exchanges can be dropped." return 0 end m = 0 while .size > 1 && n > 0 .pop(2) m += 1 n -= 1 end STDOUT.puts "Dropped the last #{m} exchanges." m end |
#last ⇒ Ollama::Message
Returns the last message from the conversation.
47 48 49 |
# File 'lib/ollama_chat/message_list.rb', line 47 def last .last end |
#list_conversation(last = nil) ⇒ OllamaChat::MessageList
The list_conversation method displays the last n messages from the conversation.
100 101 102 103 104 105 106 107 108 |
# File 'lib/ollama_chat/message_list.rb', line 100 def list_conversation(last = nil) last = (last || .size).clamp(0, .size) use_pager do |output| [-last..-1].to_a.each do || output.puts () end end self end |
#load_conversation(filename) ⇒ OllamaChat::MessageList
The load_conversation method loads a conversation from a file and populates the message list.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ollama_chat/message_list.rb', line 67 def load_conversation(filename) unless File.exist?(filename) STDERR.puts "File #{filename.inspect} doesn't exist. Choose another filename." return end = File.open(filename, 'r') do |output| JSON(output.read).map { Ollama::Message.from_hash(_1) } end self end |
#save_conversation(filename) ⇒ OllamaChat::MessageList
The save_conversation method saves the current conversation to a file.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ollama_chat/message_list.rb', line 84 def save_conversation(filename) if File.exist?(filename) STDERR.puts "File #{filename.inspect} already exists. Choose another filename." return end File.open(filename, ?w) do |output| output.puts JSON() end self end |
#second_last ⇒ Ollama::Message
The second_last method returns the second-to-last message from the conversation if there are more than one non-system messages.
55 56 57 58 59 |
# File 'lib/ollama_chat/message_list.rb', line 55 def second_last if .reject { _1.role == 'system' }.size > 1 [-2] end end |
#set_system_prompt(system) ⇒ OllamaChat::MessageList
This method:
-
Removes all existing system prompts from the message list
-
Adds the new system prompt to the beginning of the message list if provided
-
Handles edge cases such as clearing prompts when ‘system` is `nil` or `false`
Sets the system prompt for the chat session.
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ollama_chat/message_list.rb', line 171 def set_system_prompt(system) .reject! { |msg| msg.role == 'system' } if new_system_prompt = system.full?(:to_s) @system = new_system_prompt .unshift( Ollama::Message.new(role: 'system', content: self.system) ) else @system = nil end self end |
#show_last ⇒ OllamaChat::MessageList
The show_last method displays the text of the last message if it is not from the user. It uses a pager for output and returns the instance itself.
114 115 116 117 118 119 120 121 |
# File 'lib/ollama_chat/message_list.rb', line 114 def show_last = last &.role == 'user' and return use_pager do |output| output.puts () end self end |
#show_system_prompt ⇒ self, NilClass
The show_system_prompt method displays the system prompt configured for the chat session.
It retrieves the system prompt from the @system instance variable, parses it using Kramdown::ANSI, and removes any trailing newlines. If the resulting string is empty, the method returns immediately.
Otherwise, it prints a formatted message to the console, including the configured system prompt and its length in characters.
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/ollama_chat/message_list.rb', line 195 def show_system_prompt system_prompt = Kramdown::ANSI.parse(system.to_s).gsub(/\n+\z/, '').full? system_prompt or return STDOUT.puts " Configured system prompt is:\n \#{system_prompt}\n\n System prompt length: \#{bold{system_prompt.size}} characters.\n EOT\n self\nend\n" |
#size ⇒ Integer
Returns the number of messages stored in the message list.
21 22 23 |
# File 'lib/ollama_chat/message_list.rb', line 21 def size .size end |
#to_ary ⇒ Array
The to_ary method converts the message list into an array of Ollama::Message objects. If location support was enabled and the message list contains a system message, the system messages is decorated with the curent location, time, and unit preferences.
messages in the list.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/ollama_chat/message_list.rb', line 214 def to_ary location = at_location.full? add_system = !!location result = .map do || if .role == 'system' && location add_system = false content = .content + "\n\n#{location}" Ollama::Message.new(role: .role, content:) else end end if add_system prompt = @chat.config.system_prompts.assistant? content = [ prompt, location ].compact * "\n\n" = Ollama::Message.new(role: 'system', content:) result.unshift end result end |