Class: Common::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygpt/common/message.rb

Overview

Represents a Message object that is used in OpenAI Chat API requests This object is referenced by both ChatRequester and ChatCompletion objects

Constant Summary collapse

MESSAGE_REQUEST_KEYS =

The keys that are used in the request body for messages

%i[role content name tool_calls tool_call_id].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Message

Initializes the Message object

Parameters:

  • options (String, Hash) (defaults to: {})

    The message content



32
33
34
35
36
37
38
39
40
# File 'lib/rubygpt/common/message.rb', line 32

def initialize(options = {})
  if options.is_a?(String)
    @role = "system"
    @content = options
  else
    attributes_from_options(options)
  end
  json_content_parse
end

Instance Attribute Details

#contentObject (readonly)

The contents of the message.



18
19
20
# File 'lib/rubygpt/common/message.rb', line 18

def content
  @content
end

#nameObject (readonly)

Provides the model information to differentiate between participants of the same role.



21
22
23
# File 'lib/rubygpt/common/message.rb', line 21

def name
  @name
end

#roleObject (readonly)

The role of the author of this message. One of: user, assistant, system Default: system



15
16
17
# File 'lib/rubygpt/common/message.rb', line 15

def role
  @role
end

#tool_call_idObject (readonly)

Tool call that this message is responding to. (tool msg only)



27
28
29
# File 'lib/rubygpt/common/message.rb', line 27

def tool_call_id
  @tool_call_id
end

#tool_callsObject (readonly)

The tool calls generated by the model, such as function calls. (assistant msg only)



24
25
26
# File 'lib/rubygpt/common/message.rb', line 24

def tool_calls
  @tool_calls
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rubygpt/common/message.rb', line 46

def empty?
  content.nil? || content.empty?
end

#json?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rubygpt/common/message.rb', line 42

def json?
  !!@json_content
end

#to_hObject



50
51
52
# File 'lib/rubygpt/common/message.rb', line 50

def to_h
  { role:, content:, name:, tool_calls:, tool_call_id: }.compact
end