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.

Examples:

Save a conversation

chat.save_conversation('my_chat.json')

Load a conversation

chat.load_conversation('my_chat.json')

Instance Method Summary collapse

Instance Method Details

#load_conversation(filename) ⇒ Object

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.

to load

Examples:

Load a conversation from a specific file

chat.load_conversation('conversations/2023-10-15_my_session.json')

Parameters:

  • filename (String)

    The path to the file containing the conversation



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ollama_chat/conversation.rb', line 47

def load_conversation(filename)
  success = messages.load_conversation(filename)
  if messages.size > 1
    messages.list_conversation(2)
  end
  if success
    STDOUT.puts "Loaded conversation from #{filename.inspect}."
  else
    STDERR.puts "Loading conversation from #{filename.inspect} failed."
  end
end

#save_conversation(filename) ⇒ Object

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.

be saved

Examples:

Save conversation with explicit filename

chat.save_conversation('conversations/2023-10-15_my_session.json')

Parameters:

  • filename (String)

    The path to the file where the conversation should



26
27
28
29
30
31
32
# File 'lib/ollama_chat/conversation.rb', line 26

def save_conversation(filename)
  if messages.save_conversation(filename)
    STDOUT.puts "Saved conversation to #{filename.inspect}."
  else
    STDERR.puts "Saving conversation to #{filename.inspect} failed."
  end
end