Class: OmniAI::Chat::URL
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.
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
#uri ⇒ 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
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
61
62
63
64
|
# File 'lib/omniai/chat/url.rb', line 61
def fetch!
response = request!
String(response.body)
end
|
#filename ⇒ String
67
68
69
|
# File 'lib/omniai/chat/url.rb', line 67
def filename
::File.basename(@uri)
end
|
#inspect ⇒ String
25
26
27
|
# File 'lib/omniai/chat/url.rb', line 25
def inspect
"#<#{self.class} uri=#{@uri.inspect}>"
end
|
#serialize(context: nil) ⇒ 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
|
#summarize ⇒ String
20
21
22
|
# File 'lib/omniai/chat/url.rb', line 20
def summarize
"[#{filename}]"
end
|