Class: OmniAI::Chat::URL

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

Overview

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

Defined Under Namespace

Classes: FetchError

Instance Attribute Summary collapse

Attributes inherited from Media

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Media

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

Methods inherited from Content

summarize

Constructor Details

#initialize(uri, type = nil) ⇒ URL

Returns a new instance of URL.

Parameters:

  • uri (URI, String)

  • type (Symbol, String) (defaults to: nil)

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



14
15
16
17
# File 'lib/omniai/chat/url.rb', line 14

def initialize(uri, type = nil)
  super(type)
  @uri = uri
end

Instance Attribute Details

#uriURI, String

Returns:

  • (URI, String)


8
9
10
# File 'lib/omniai/chat/url.rb', line 8

def uri
  @uri
end

Class Method Details

.deserialize(data, context: nil) ⇒ Object

Parameters:

  • data (Hash)


30
31
32
33
34
35
36
37
38
# File 'lib/omniai/chat/url.rb', line 30

def self.deserialize(data, context: nil)
  deserialize = context&.deserializer(:url)
  return deserialize.call(data, context:) if deserialize

  type = /(?<type>\w+)_url/.match(data['type'])[:type]
  uri = data["#{type}_url"]['url']

  new(uri, type)
end

Instance Method Details

#fetch!String

Returns:

  • (String)

Raises:



61
62
63
64
# File 'lib/omniai/chat/url.rb', line 61

def fetch!
  response = request!
  String(response.body)
end

#filenameString

Returns:

  • (String)


67
68
69
# File 'lib/omniai/chat/url.rb', line 67

def filename
  ::File.basename(@uri)
end

#inspectString

Returns:

  • (String)


25
26
27
# File 'lib/omniai/chat/url.rb', line 25

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

#serialize(context: nil) ⇒ Hash

Parameters:

  • context (Context) (defaults to: nil)

    optional

Returns:

  • (Hash)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/omniai/chat/url.rb', line 43

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

    {
      type: "#{kind}_url",
      "#{kind}_url": { url: @uri },
    }
  end
end

#summarizeString

Returns:

  • (String)


20
21
22
# File 'lib/omniai/chat/url.rb', line 20

def summarize
  "[#{filename}]"
end