Class: Ruboty::OpenAIChat::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/openai_chat/message.rb

Constant Summary collapse

ROLES =
%i[system user assistant].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, content:, expire_at: nil) ⇒ Message

Returns a new instance of Message.

Parameters:

  • role (:system, :user, :assistant)
  • content (String)
  • expire_at (Time, Integer, nil) (defaults to: nil)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
# File 'lib/ruboty/openai_chat/message.rb', line 24

def initialize(role:, content:, expire_at: nil)
  @role = role.to_sym
  raise ArgumentError, "role must be :system, :user, or :assistant" unless ROLES.include?(@role)

  @content = content
  @expire_at = expire_at&.yield_self { |t| Time.at(t) }
end

Instance Attribute Details

#contentString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/ruboty/openai_chat/message.rb', line 12

def content
  @content
end

#expire_atTime (readonly)

Returns:

  • (Time)


15
16
17
# File 'lib/ruboty/openai_chat/message.rb', line 15

def expire_at
  @expire_at
end

#role:system, ... (readonly)

Returns:

  • (:system, :user, :assistant)


9
10
11
# File 'lib/ruboty/openai_chat/message.rb', line 9

def role
  @role
end

Class Method Details

.from_hash(hash) ⇒ Object



17
18
19
# File 'lib/ruboty/openai_chat/message.rb', line 17

def self.from_hash(hash)
  new(**hash.transform_keys(&:to_sym))
end

Instance Method Details

#expired?(from = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ruboty/openai_chat/message.rb', line 42

def expired?(from = Time.now)
  expire_at && (expire_at <= from)
end

#to_api_hashObject



37
38
39
# File 'lib/ruboty/openai_chat/message.rb', line 37

def to_api_hash
  { role: role, content: content }
end

#to_hHash

Returns:

  • (Hash)


33
34
35
# File 'lib/ruboty/openai_chat/message.rb', line 33

def to_h
  { role: role, content: content, expire_at: expire_at&.to_i }
end