Class: OmniAI::Chat::File

Inherits:
Media show all
Defined in:
lib/omniai/chat/file.rb

Overview

A file is media that can be sent to many LLMs.

Instance Attribute Summary collapse

Attributes inherited from Media

#type

Instance Method Summary collapse

Methods inherited from Media

#audio?, #data, #data_uri, #image?, #kind, #summarize, #text?, #video?

Methods inherited from Content

deserialize, summarize

Constructor Details

#initialize(io, type) ⇒ File

Returns a new instance of File.

Parameters:

  • io (IO, Pathname, String)
  • type (Symbol, String)

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



11
12
13
14
# File 'lib/omniai/chat/file.rb', line 11

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

Instance Attribute Details

#ioObject

Returns the value of attribute io.



7
8
9
# File 'lib/omniai/chat/file.rb', line 7

def io
  @io
end

Instance Method Details

#fetch!String

Returns:

  • (String)


22
23
24
25
26
27
# File 'lib/omniai/chat/file.rb', line 22

def fetch!
  case @io
  when String then ::File.binread(@io)
  else @io.read
  end
end

#filenameString

Returns:

  • (String)


44
45
46
47
48
49
# File 'lib/omniai/chat/file.rb', line 44

def filename
  case @io
  when Tempfile, File, String then ::File.basename(@io)
  else 'DATA'
  end
end

#inspectString

Returns:

  • (String)


17
18
19
# File 'lib/omniai/chat/file.rb', line 17

def inspect
  "#<#{self.class} io=#{@io.inspect}>"
end

#serialize(context: nil) ⇒ Hash

Parameters:

  • context (Context) (defaults to: nil)

Returns:

  • (Hash)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/omniai/chat/file.rb', line 31

def serialize(context: nil)
  if text?
    content = fetch!
    Text.new("<file>#{filename}: #{content}</file>").serialize(context:)
  else
    serializer = context&.serializer(:file)
    return serializer.call(self, context:) if serializer

    { type: "#{kind}_url", "#{kind}_url": { url: data_uri } }
  end
end