Class: Kreator::Message

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, **options) ⇒ Message

Returns a new instance of Message.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kreator/message.rb', line 10

def initialize(role:, **options)
  validate_initialize_options!(options)
  role = role.to_s
  raise ArgumentError, "unknown role: #{role.inspect}" unless ROLES.include?(role)

  content = options.fetch(:content, "")
  tool_calls = options.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 = options.fetch(:tool_call_id, nil)
  @name = options.fetch(:name, nil)
  @metadata = options.fetch(:metadata, {}) || {}
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/kreator/message.rb', line 8

def content
  @content
end

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/kreator/message.rb', line 8

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/kreator/message.rb', line 8

def name
  @name
end

#roleObject (readonly)

Returns the value of attribute role.



8
9
10
# File 'lib/kreator/message.rb', line 8

def role
  @role
end

#tool_call_idObject (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_callsObject (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_hObject



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