Class: OmniAI::Chat::File
- Defined in:
- lib/omniai/chat/file.rb
Overview
A file is media that can be sent to many LLMs.
Instance Attribute Summary collapse
-
#io ⇒ Object
Returns the value of attribute io.
Attributes inherited from Media
Instance Method Summary collapse
- #fetch! ⇒ String
- #filename ⇒ String
-
#initialize(io, type) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ String
- #serialize(context: nil) ⇒ Hash
Methods inherited from Media
#audio?, #data, #data_uri, #image?, #kind, #summarize, #text?, #video?
Methods inherited from Content
Constructor Details
#initialize(io, type) ⇒ File
Returns a new instance of File.
11 12 13 14 |
# File 'lib/omniai/chat/file.rb', line 11 def initialize(io, type) super(type) @io = io end |
Instance Attribute Details
#io ⇒ Object
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
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 |
#filename ⇒ 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 |
#inspect ⇒ String
17 18 19 |
# File 'lib/omniai/chat/file.rb', line 17 def inspect "#<#{self.class} io=#{@io.inspect}>" end |
#serialize(context: nil) ⇒ 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 |