Class: OmniAI::Chat::Media

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

Overview

An abstract class that represents audio / image / video and is used for both files and urls.

Direct Known Subclasses

File, URL

Defined Under Namespace

Classes: TypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Content

deserialize, #serialize, summarize

Constructor Details

#initialize(type) ⇒ Media

Returns a new instance of Media.

Parameters:

  • type (String)

    “audio/flac”, “image/jpeg”, “video/mpeg”, “application/pdf”, etc.



13
14
15
16
# File 'lib/omniai/chat/media.rb', line 13

def initialize(type)
  super()
  @type = type
end

Instance Attribute Details

#typeSymbol, String

Returns:

  • (Symbol, String)


10
11
12
# File 'lib/omniai/chat/media.rb', line 10

def type
  @type
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/omniai/chat/media.rb', line 29

def audio?
  @type.match?(%r{^audio/})
end

#dataString

e.g. “Hello” -> “SGVsbG8h”

Returns:

  • (String)


63
64
65
# File 'lib/omniai/chat/media.rb', line 63

def data
  Base64.strict_encode64(fetch!)
end

#data_uriString

e.g. “data:text/html;base64,…”

Returns:

  • (String)


70
71
72
# File 'lib/omniai/chat/media.rb', line 70

def data_uri
  "data:#{@type};base64,#{data}"
end

#document?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/omniai/chat/media.rb', line 44

def document?
  @type.match?(%r{^application/pdf$})
end

#fetch!String

Returns:

  • (String)

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/omniai/chat/media.rb', line 75

def fetch!
  raise NotImplementedError, "#{self.class}#fetch! undefined"
end

#image?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/omniai/chat/media.rb', line 34

def image?
  @type.match?(%r{^image/})
end

#kind:text, ...

Returns:

  • (:text, :audio, :image, :video :document)


49
50
51
52
53
54
55
56
57
58
# File 'lib/omniai/chat/media.rb', line 49

def kind
  if text? then :text
  elsif audio? then :audio
  elsif image? then :image
  elsif video? then :video
  elsif document? then :document
  else
    raise(TypeError, "unsupported type=#{@type}")
  end
end

#summarizeString

Returns:

  • (String)


19
20
21
# File 'lib/omniai/chat/media.rb', line 19

def summarize
  "[#{filename}]"
end

#text?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/omniai/chat/media.rb', line 24

def text?
  @type.match?(%r{^text/})
end

#video?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/omniai/chat/media.rb', line 39

def video?
  @type.match?(%r{^video/})
end