Class: ChatMessage

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/generators/langsmithrb_rails/demo/templates/chat_message.rb

Overview

Model for storing chat messages

Class Method Summary collapse

Class Method Details

.conversation_history(limit = 10) ⇒ Array<Hash>

Get the conversation history as an array of messages

Parameters:

  • limit (Integer) (defaults to: 10)

    Maximum number of messages to return

Returns:

  • (Array<Hash>)

    Array of messages with role and content



16
17
18
19
20
21
22
23
# File 'lib/generators/langsmithrb_rails/demo/templates/chat_message.rb', line 16

def self.conversation_history(limit = 10)
  recent.limit(limit).map do |message|
    {
      role: message.is_user? ? "user" : "assistant",
      content: message.content
    }
  end.reverse
end