omniai 1.6.4 → 1.6.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3c40ccbf59a89846731fb7e3b1a47c4d2ecc944ea2cb45e56b4911bdc301c13
4
- data.tar.gz: fdcd923f1ccb0db66630e48d0230c406ac1d72eb8602bee2872e4d02d10ce081
3
+ metadata.gz: 0bc92ec17e5ca87590eca2181453b6a3e54c2d15bb6dda0c80c604a443e59660
4
+ data.tar.gz: e4eb4ad22437711fbac2c538edc644861107985662f24a236b317d229802f7b7
5
5
  SHA512:
6
- metadata.gz: d1cdb9d2b81d286a593c0f2becff6249c4e33e8143b4d158f3d48204dc4ca0fc6e627804722a52db69121bd5b223ae7d407eeac912d4830d7093eddfae089304
7
- data.tar.gz: 1b0f10832178feb1304da5d12abe1bd9613df52ffa8eb2edf1678668cdb3425487e90323774ca8e092b95002f83001eb4b560bc0c8536bc133222842b67986ff
6
+ metadata.gz: 75b7b695353ec0899b786ee057beaedcd8ab21ba15efca6f3a21c4e22730dc73b7676fbdfece11f624a1dce77638ad2d7cc4a6ff054c2180edb8f423b20ca6ee
7
+ data.tar.gz: 9c7ab97db7e4d8085774c9fd76803b8abfe53905b6d9b6d141caed311d019347d3f0e880e957597b529de8e479d1b69415213e82ca1daba34bf00d7049eddbae
@@ -4,6 +4,14 @@ module OmniAI
4
4
  class Chat
5
5
  # A placeholder for parts of a message. Any subclass must implement the serializable interface.
6
6
  class Content
7
+ # @return [String]
8
+ def self.summarize(content)
9
+ return content.map { |entry| summarize(entry) }.join("\n\n") if content.is_a?(Array)
10
+ return content if content.is_a?(String)
11
+
12
+ content.summarize
13
+ end
14
+
7
15
  # @param context [Context] optional
8
16
  #
9
17
  # @return [String]
@@ -29,10 +29,23 @@ module OmniAI
29
29
  # @param context [Context]
30
30
  # @return [Hash]
31
31
  def serialize(context: nil)
32
- serializer = context&.serializers&.[](:file)
33
- return serializer.call(self, context:) if serializer
32
+ if text?
33
+ content = fetch!
34
+ Text.new("<file>#{filename}: #{content}</file>").serialize(context:)
35
+ else
36
+ serializer = context&.serializers&.[](:file)
37
+ return serializer.call(self, context:) if serializer
34
38
 
35
- { type: "#{kind}_url", "#{kind}_url": { url: data_uri } }
39
+ { type: "#{kind}_url", "#{kind}_url": { url: data_uri } }
40
+ end
41
+ end
42
+
43
+ # @return [String]
44
+ def filename
45
+ case @io
46
+ when Tempfile, File, String then ::File.basename(@io)
47
+ else 'DATA'
48
+ end
36
49
  end
37
50
  end
38
51
  end
@@ -15,6 +15,11 @@ module OmniAI
15
15
  @type = type
16
16
  end
17
17
 
18
+ # @return [String]
19
+ def summarize
20
+ "[#{filename}]"
21
+ end
22
+
18
23
  # @return [Boolean]
19
24
  def text?
20
25
  @type.match?(%r{^text/})
@@ -31,6 +31,14 @@ module OmniAI
31
31
  "#<#{self.class} role=#{@role.inspect} content=#{@content.inspect}>"
32
32
  end
33
33
 
34
+ # @return [String]
35
+ def summarize
36
+ <<~TEXT
37
+ #{@role}
38
+ #{Content.summarize(@content)}
39
+ TEXT
40
+ end
41
+
34
42
  # Usage:
35
43
  #
36
44
  # Message.deserialize({ role: :user, content: 'Hello!' }) # => #<Message ...>
@@ -62,6 +62,11 @@ module OmniAI
62
62
  "#<#{self.class.name} messages=#{@messages.inspect}>"
63
63
  end
64
64
 
65
+ # @return [String]
66
+ def summarize
67
+ @messages.map(&:summarize).join("\n\n")
68
+ end
69
+
65
70
  # Usage:
66
71
  #
67
72
  # prompt.serialize # => [{ content: "What is the capital of Canada?", role: :user }]
@@ -18,6 +18,11 @@ module OmniAI
18
18
  "#<#{self.class} text=#{@text.inspect}>"
19
19
  end
20
20
 
21
+ # @return [String]
22
+ def summarize
23
+ @text
24
+ end
25
+
21
26
  # @param data [Hash]
22
27
  def self.deserialize(data, context: nil)
23
28
  deserialize = context&.deserializers&.[](:text)
@@ -16,6 +16,11 @@ module OmniAI
16
16
  @uri = uri
17
17
  end
18
18
 
19
+ # @return [String]
20
+ def summarize
21
+ "[#{filename}]"
22
+ end
23
+
19
24
  # @return [String]
20
25
  def inspect
21
26
  "#<#{self.class} uri=#{@uri.inspect}>"
@@ -36,13 +41,18 @@ module OmniAI
36
41
  #
37
42
  # @return [Hash]
38
43
  def serialize(context: nil)
39
- serializer = context&.serializers&.[](:url)
40
- return serializer.call(self, context:) if serializer
44
+ if text?
45
+ content = fetch!
46
+ Text.new("<file>#{filename}: #{content}</file>").serialize(context:)
47
+ else
48
+ serializer = context&.serializers&.[](:url)
49
+ return serializer.call(self, context:) if serializer
41
50
 
42
- {
43
- type: "#{kind}_url",
44
- "#{kind}_url": { url: @uri },
45
- }
51
+ {
52
+ type: "#{kind}_url",
53
+ "#{kind}_url": { url: @uri },
54
+ }
55
+ end
46
56
  end
47
57
 
48
58
  # @raise [FetchError]
@@ -53,6 +63,11 @@ module OmniAI
53
63
  String(response.body)
54
64
  end
55
65
 
66
+ # @return [String]
67
+ def filename
68
+ ::File.basename(@uri)
69
+ end
70
+
56
71
  protected
57
72
 
58
73
  # @raise [FetchError]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniAI
4
- VERSION = '1.6.4'
4
+ VERSION = '1.6.6'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.4
4
+ version: 1.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre