Class: Roast::Cogs::Chat::Session
- Inherits:
-
Object
- Object
- Roast::Cogs::Chat::Session
- 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
-
.from_chat(chat) ⇒ Object
Create a new session from a RubyLLM chat instance.
Instance Method Summary collapse
-
#apply!(chat) ⇒ Object
Apply this session’s messages to a RubyLLM chat instance.
-
#first(n = 2) ⇒ Object
Get a truncated session consisting only of the first N messages.
-
#initialize(messages) ⇒ Session
constructor
Initialize a new session with the given messages.
-
#last(n = 2) ⇒ Object
Get a truncated session consisting only of the last N messages.
Constructor Details
Class Method Details
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.first(n).deep_dup Session.new() 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.last(n).deep_dup Session.new() end |