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.



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

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

Instance Attribute Details

#ioIO, ...

Returns:

  • (IO, Pathname, String)


9
10
11
# File 'lib/omniai/chat/file.rb', line 9

def io
  @io
end

Instance Method Details

#fetch!String

Returns:

  • (String)


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

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

#filenameString

Returns:

  • (String)


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

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

#inspectString

Returns:

  • (String)


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

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

#serialize(context: nil) ⇒ Hash

Parameters:

  • context (Context) (defaults to: nil)

Returns:

  • (Hash)


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

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