Class: OmniAI::Chat::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/chat/content.rb

Overview

A placeholder for parts of a message. Any subclass must implement the serializable interface.

Direct Known Subclasses

Media, Text

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deserialize(data, context: nil) ⇒ Content

Parameters:

  • data (Hash, Array, String)
  • context (Context) (defaults to: nil)

    optional

Returns:

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/omniai/chat/content.rb', line 26

def self.deserialize(data, context: nil)
  return data if data.nil?
  return data.map { |data| deserialize(data, context:) } if data.is_a?(Array)

  deserialize = context&.deserializer(:content)
  return deserialize.call(data, context:) if deserialize

  return data if data.is_a?(String)

  raise ArgumentError, "untyped data=#{data.inspect}" unless data.key?('type')

  case data['type']
  when 'text' then Text.deserialize(data, context:)
  when /(.*)_url/ then URL.deserialize(data, context:)
  else raise ArgumentError, "unknown type=#{data['type'].inspect}"
  end
end

.summarize(content) ⇒ String

Returns:

  • (String)


8
9
10
11
12
13
# File 'lib/omniai/chat/content.rb', line 8

def self.summarize(content)
  return content.map { |entry| summarize(entry) }.join("\n\n") if content.is_a?(Array)
  return content if content.is_a?(String)

  content.summarize
end

Instance Method Details

#serialize(context: nil) ⇒ String

Parameters:

  • context (Context) (defaults to: nil)

    optional

Returns:

  • (String)

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/omniai/chat/content.rb', line 18

def serialize(context: nil)
  raise NotImplementedError, "#{self.class}#serialize undefined"
end