Class: Ruboty::OpenAIChat::Message
- Inherits:
-
Object
- Object
- Ruboty::OpenAIChat::Message
- Defined in:
- lib/ruboty/openai_chat/message.rb
Constant Summary collapse
- ROLES =
%i[system user assistant].freeze
Instance Attribute Summary collapse
- #content ⇒ String readonly
- #expire_at ⇒ Time readonly
- #role ⇒ :system, ... readonly
Class Method Summary collapse
Instance Method Summary collapse
- #expired?(from = Time.now) ⇒ Boolean
-
#initialize(role:, content:, expire_at: nil) ⇒ Message
constructor
A new instance of Message.
- #to_api_hash ⇒ Object
- #to_h ⇒ Hash
Constructor Details
#initialize(role:, content:, expire_at: nil) ⇒ Message
Returns a new instance of Message.
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
#content ⇒ String (readonly)
12 13 14 |
# File 'lib/ruboty/openai_chat/message.rb', line 12 def content @content end |
#expire_at ⇒ Time (readonly)
15 16 17 |
# File 'lib/ruboty/openai_chat/message.rb', line 15 def expire_at @expire_at end |
#role ⇒ :system, ... (readonly)
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
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_hash ⇒ Object
37 38 39 |
# File 'lib/ruboty/openai_chat/message.rb', line 37 def to_api_hash { role: role, content: content } end |
#to_h ⇒ 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 |