Module: OllamaChat::Conversation
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/conversation.rb
Overview
A module that provides conversation persistence functionality for the OllamaChat::Chat class.
This module encapsulates the logic for saving and loading chat conversations
to/from JSON files. It delegates the actual file operations to the messages
object, which is expected to respond to save_conversation and
load_conversation methods.
Instance Method Summary collapse
-
#load_conversation(filename) ⇒ Object
private
Loads a conversation from a JSON file and replaces the current message history.
-
#save_conversation(filename, clean: false) ⇒ Object
private
Saves the current conversation to a JSON file.
Instance Method Details
#load_conversation(filename) ⇒ Object (private)
Loads a conversation from a JSON file and replaces the current message history.
This method delegates to the messages object's load_conversation
method, which handles deserialization of messages from JSON format. After
loading, if there are more than one message, it lists the last two messages
for confirmation.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ollama_chat/conversation.rb', line 55 def load_conversation(filename) success = .load_conversation(filename) if .size > 1 .list_conversation(2) end if success STDOUT.puts "Loaded conversation from #{filename.to_s.inspect}." else STDERR.puts "Loading conversation from #{filename.to_s.inspect} failed." end end |
#save_conversation(filename, clean: false) ⇒ Object (private)
Saves the current conversation to a JSON file.
This method delegates to the messages object's save_conversation
method, which handles the actual serialization of messages into JSON
format.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ollama_chat/conversation.rb', line 28 def save_conversation(filename, clean: false) if File.exist?(filename) confirm?( prompt: "🔔 File #{filename.to_s.inspect} already exists, overwrite? (y/n) ", yes: /\Ay/i ) or return end if .save_conversation(filename, messages: clean ? . : .) STDOUT.puts "Saved conversation to #{filename.to_s.inspect}." else STDERR.puts "Saving conversation to #{filename.to_s.inspect} failed." end end |