Class: Roast::Cogs::Chat::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/cogs/chat/session.rb

Overview

Container for chat session information needed to resume a conversation

Holds the messages from a chat conversation and provides methods to truncate or restore the session.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages) ⇒ Session

Initialize a new session with the given messages

: (Array) -> void



25
26
27
# File 'lib/roast/cogs/chat/session.rb', line 25

def initialize(messages)
  @messages = messages
end

Class Method Details

.from_chat(chat) ⇒ Object

Create a new session from a RubyLLM chat instance

: (RubyLLM::Chat) -> Session



16
17
18
19
# File 'lib/roast/cogs/chat/session.rb', line 16

def from_chat(chat)
  messages = chat.messages.deep_dup
  Session.new(messages)
end

Instance Method Details

#apply!(chat) ⇒ Object

Apply this session’s messages to a RubyLLM chat instance

Replaces the chat’s messages with this session’s messages, effectively restoring the conversation state.

: (RubyLLM::Chat) -> void



59
60
61
62
# File 'lib/roast/cogs/chat/session.rb', line 59

def apply!(chat)
  chat.instance_variable_set(:@messages, @messages.deep_dup)
  chat.with_temperature(@temperature) if @temperature
end

#first(n = 2) ⇒ Object

Get a truncated session consisting only of the first N messages

Each full turn in a conversation consists of two messages (a prompt and a response), so to include N full turns you should pass ‘2 * N` as the argument. The default value is `2`, which returns only the first full turn.

: (?Integer) -> Session



36
37
38
39
# File 'lib/roast/cogs/chat/session.rb', line 36

def first(n = 2)
  messages = @messages.first(n).deep_dup
  Session.new(messages)
end

#last(n = 2) ⇒ Object

Get a truncated session consisting only of the last N messages

Each full turn in a conversation consists of two messages (a prompt and a response), so to include N full turns you should pass ‘2 * N` as the argument. The default value is `2`, which returns only the last full turn.

: (?Integer) -> Session



48
49
50
51
# File 'lib/roast/cogs/chat/session.rb', line 48

def last(n = 2)
  messages = @messages.last(n).deep_dup
  Session.new(messages)
end