Class: Vellum::ChatMessage
- Inherits:
-
Object
- Object
- Vellum::ChatMessage
- Defined in:
- lib/vellum_ai/types/chat_message.rb
Instance Attribute Summary collapse
-
#additional_properties ⇒ Object
readonly
Returns the value of attribute additional_properties.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Class Method Summary collapse
-
.from_json(json_object:) ⇒ ChatMessage
Deserialize a JSON object to an instance of ChatMessage.
-
.validate_raw(obj:) ⇒ Void
Leveraged for Union-type generation, validate_raw attempts to parse the given hash and check each fields type against the current object’s property definitions.
Instance Method Summary collapse
- #initialize(role:, text: nil, content: nil, source: nil, additional_properties: nil) ⇒ ChatMessage constructor
-
#to_json(*_args) ⇒ JSON
Serialize an instance of ChatMessage to a JSON object.
Constructor Details
#initialize(role:, text: nil, content: nil, source: nil, additional_properties: nil) ⇒ ChatMessage
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/vellum_ai/types/chat_message.rb', line 17 def initialize(role:, text: nil, content: nil, source: nil, additional_properties: nil) # @type [String] @text = text # @type [CHAT_MESSAGE_ROLE] @role = role # @type [ChatMessageContent] @content = content # @type [String] An optional identifier representing who or what generated this message. @source = source # @type [OpenStruct] Additional properties unmapped to the current class definition @additional_properties = additional_properties end |
Instance Attribute Details
#additional_properties ⇒ Object (readonly)
Returns the value of attribute additional_properties.
9 10 11 |
# File 'lib/vellum_ai/types/chat_message.rb', line 9 def additional_properties @additional_properties end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
9 10 11 |
# File 'lib/vellum_ai/types/chat_message.rb', line 9 def content @content end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
9 10 11 |
# File 'lib/vellum_ai/types/chat_message.rb', line 9 def role @role end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
9 10 11 |
# File 'lib/vellum_ai/types/chat_message.rb', line 9 def source @source end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
9 10 11 |
# File 'lib/vellum_ai/types/chat_message.rb', line 9 def text @text end |
Class Method Details
.from_json(json_object:) ⇒ ChatMessage
Deserialize a JSON object to an instance of ChatMessage
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/vellum_ai/types/chat_message.rb', line 34 def self.from_json(json_object:) struct = JSON.parse(json_object, object_class: OpenStruct) parsed_json = JSON.parse(json_object) text = struct.text role = CHAT_MESSAGE_ROLE.key(parsed_json["role"]) || parsed_json["role"] if parsed_json["content"].nil? content = nil else content = parsed_json["content"].to_json content = ChatMessageContent.from_json(json_object: content) end source = struct.source new(text: text, role: role, content: content, source: source, additional_properties: struct) end |
.validate_raw(obj:) ⇒ Void
Leveraged for Union-type generation, validate_raw attempts to parse the given hash and check each fields type against the current object’s property definitions.
60 61 62 63 64 65 |
# File 'lib/vellum_ai/types/chat_message.rb', line 60 def self.validate_raw(obj:) obj.text&.is_a?(String) != false || raise("Passed value for field obj.text is not the expected type, validation failed.") obj.role.is_a?(CHAT_MESSAGE_ROLE) != false || raise("Passed value for field obj.role is not the expected type, validation failed.") obj.content.nil? || ChatMessageContent.validate_raw(obj: obj.content) obj.source&.is_a?(String) != false || raise("Passed value for field obj.source is not the expected type, validation failed.") end |
Instance Method Details
#to_json(*_args) ⇒ JSON
Serialize an instance of ChatMessage to a JSON object
52 53 54 |
# File 'lib/vellum_ai/types/chat_message.rb', line 52 def to_json(*_args) { "text": @text, "role": CHAT_MESSAGE_ROLE[@role] || @role, "content": @content, "source": @source }.to_json end |