Class: Kreator::Message
- Inherits:
-
Object
- Object
- Kreator::Message
- Defined in:
- lib/kreator/message.rb
Constant Summary collapse
- ROLES =
%w[system user assistant tool].freeze
- INITIALIZE_OPTIONS =
%i[content tool_calls tool_call_id name metadata].freeze
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#tool_call_id ⇒ Object
readonly
Returns the value of attribute tool_call_id.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
Class Method Summary collapse
- .assistant(content, tool_calls: []) ⇒ Object
- .from_h(hash) ⇒ Object
- .system(content) ⇒ Object
- .tool(content:, tool_call_id:, name: nil) ⇒ Object
- .user(content) ⇒ Object
Instance Method Summary collapse
-
#initialize(role:, **options) ⇒ Message
constructor
A new instance of Message.
- #to_h ⇒ Object
Constructor Details
#initialize(role:, **options) ⇒ Message
Returns a new instance of Message.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kreator/message.rb', line 10 def initialize(role:, **) () role = role.to_s raise ArgumentError, "unknown role: #{role.inspect}" unless ROLES.include?(role) content = .fetch(:content, "") tool_calls = .fetch(:tool_calls, []) @role = role @content = content.to_s @tool_calls = tool_calls.map { |call| call.is_a?(ToolCall) ? call : ToolCall.from_h(call) } @tool_call_id = .fetch(:tool_call_id, nil) @name = .fetch(:name, nil) @metadata = .fetch(:metadata, {}) || {} end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/kreator/message.rb', line 8 def content @content end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
8 9 10 |
# File 'lib/kreator/message.rb', line 8 def @metadata end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/kreator/message.rb', line 8 def name @name end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
8 9 10 |
# File 'lib/kreator/message.rb', line 8 def role @role end |
#tool_call_id ⇒ Object (readonly)
Returns the value of attribute tool_call_id.
8 9 10 |
# File 'lib/kreator/message.rb', line 8 def tool_call_id @tool_call_id end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
8 9 10 |
# File 'lib/kreator/message.rb', line 8 def tool_calls @tool_calls end |
Class Method Details
.assistant(content, tool_calls: []) ⇒ Object
29 30 31 |
# File 'lib/kreator/message.rb', line 29 def self.assistant(content, tool_calls: []) new(role: "assistant", content: content, tool_calls: tool_calls) end |
.from_h(hash) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kreator/message.rb', line 41 def self.from_h(hash) new( role: hash.fetch("role", hash[:role]), content: hash.fetch("content", hash[:content] || ""), tool_calls: hash.fetch("tool_calls", hash[:tool_calls] || []), tool_call_id: hash.fetch("tool_call_id", hash[:tool_call_id] || nil), name: hash.fetch("name", hash[:name] || nil), metadata: hash.fetch("metadata", hash[:metadata] || {}) ) end |
.system(content) ⇒ Object
33 34 35 |
# File 'lib/kreator/message.rb', line 33 def self.system(content) new(role: "system", content: content) end |
.tool(content:, tool_call_id:, name: nil) ⇒ Object
37 38 39 |
# File 'lib/kreator/message.rb', line 37 def self.tool(content:, tool_call_id:, name: nil) new(role: "tool", content: content, tool_call_id: tool_call_id, name: name) end |
.user(content) ⇒ Object
25 26 27 |
# File 'lib/kreator/message.rb', line 25 def self.user(content) new(role: "user", content: content) end |
Instance Method Details
#to_h ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kreator/message.rb', line 52 def to_h { "role" => role, "content" => content, "tool_calls" => tool_calls.map(&:to_h), "tool_call_id" => tool_call_id, "name" => name, "metadata" => }.compact end |