omniai-google 1.9.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b894f099b440d516869aeeb9590d9c6c9386ff33111bccdefff5111ae005695
4
- data.tar.gz: 20595ba59b4ecdde932f645954a05923be8337a03e01a00efe668d937c46613c
3
+ metadata.gz: 920c87f6c8f91437a1a5580caf2c838521047d734413762776c5d3e7415bda12
4
+ data.tar.gz: be11205e829fbf12049d9a14b65b3432bdfdb38a377d6ed1958294a8c04d8ef1
5
5
  SHA512:
6
- metadata.gz: b9845d667026a8121c4fc06388c7686e69860d2d22097fa3c49153c3d3fab9e867b0aa4fd47691df07f6d505de15ed0d06e29b973ac28d1d4d83e37bdfe4dd56
7
- data.tar.gz: bdeeb06f52c05d4e955ad77eee2bdaf7a28a1987b5c1ad4333b8748b7ec61c215419f5d36768a03fa774fd821c5466694b3258cac00759df735a65c450805212
6
+ metadata.gz: 9001c4da34477f01fd0a0fe410885f7be12e2cc5fe575f31071d5716605567ef55a7184af34d9c758266bbac46a42fddff53884f9c51cf309cd7f51ae671149f
7
+ data.tar.gz: b6938cfad64524232b79053b37c59bf15151c381e1ad0c09283369ea9829e47a9686f2177ad782cfb5cf71db1a5e0ab6085c7c8ac7c3edd1e38758496b4c3a26
@@ -3,14 +3,15 @@
3
3
  module OmniAI
4
4
  module Google
5
5
  class Chat
6
- # Overrides payload serialize / deserialize.
7
- module PayloadSerializer
8
- # @param payload [OmniAI::Chat::Payload]
6
+ # Overrides response serialize / deserialize.
7
+ module ResponseSerializer
8
+ # @param response [OmniAI::Chat::Response]
9
9
  # @param context [OmniAI::Context]
10
+ #
10
11
  # @return [Hash]
11
- def self.serialize(payload, context:)
12
- candidates = payload.choices.map { |choice| choice.serialize(context:) }
13
- usage_metadata = payload.usage&.serialize(context:)
12
+ def self.serialize(response, context:)
13
+ candidates = response.choices.map { |choice| choice.serialize(context:) }
14
+ usage_metadata = response.usage&.serialize(context:)
14
15
 
15
16
  {
16
17
  candidates:,
@@ -20,12 +21,13 @@ module OmniAI
20
21
 
21
22
  # @param data [Hash]
22
23
  # @param context [OmniAI::Context]
23
- # @return [OmniAI::Chat::Payload]
24
+ #
25
+ # @return [OmniAI::Chat::Response]
24
26
  def self.deserialize(data, context:)
25
27
  choices = data["candidates"].map { |candidate| OmniAI::Chat::Choice.deserialize(candidate, context:) }
26
28
  usage = OmniAI::Chat::Usage.deserialize(data["usageMetadata"], context:) if data["usageMetadata"]
27
29
 
28
- OmniAI::Chat::Payload.new(choices:, usage:)
30
+ OmniAI::Chat::Response.new(data:, choices:, usage:)
29
31
  end
30
32
  end
31
33
  end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module Google
5
+ class Chat
6
+ # Combine chunks into a hash. For each chunk yield the text (delta) if a block is given and the chunk is text.
7
+ class Stream < OmniAI::Chat::Stream
8
+ # @yield [delta]
9
+ # @yieldparam delta [OmniAI::Chat::Delta]
10
+ #
11
+ # @return [Hash]
12
+ def stream!(&block)
13
+ @data = { "candidates" => [] }
14
+
15
+ @chunks.each do |chunk|
16
+ parser.feed(chunk) do |type, data, id|
17
+ process!(type, data, id, &block)
18
+ end
19
+ end
20
+
21
+ @data
22
+ end
23
+
24
+ protected
25
+
26
+ # @yield [delta]
27
+ # @yieldparam delta [OmniAI::Chat::Delta]
28
+ #
29
+ # @param type [String]
30
+ # @param data [String]
31
+ # @param id [String]
32
+ def process!(type, data, id, &)
33
+ log(type, data, id)
34
+
35
+ process_data!(data: JSON.parse(data), &)
36
+ end
37
+
38
+ # @yield [delta]
39
+ # @yieldparam delta [OmniAI::Chat::Delta]
40
+ #
41
+ # @param data [Hash]
42
+ def process_data!(data:, &block)
43
+ data.each do |key, value|
44
+ @data[key] = value unless key.eql?("candidates")
45
+ end
46
+
47
+ data["candidates"].each_with_index do |candidate, index|
48
+ candidate["content"]["parts"].each do |part|
49
+ block&.call(OmniAI::Chat::Delta.new(text: part["text"])) if part["text"]
50
+ end
51
+
52
+ merge_candidate!(candidate:, index:, &block)
53
+ end
54
+ end
55
+
56
+ # @param candidate [Hash]
57
+ # @param index [Integer]
58
+ def merge_candidate!(candidate:, index:)
59
+ if @data["candidates"][index].nil?
60
+ @data["candidates"][index] = candidate
61
+ else
62
+ merge_parts!(content: @data["candidates"][index]["content"], parts: candidate["content"]["parts"])
63
+ end
64
+ end
65
+
66
+ # @param content [Hash]
67
+ # @param parts [Array<Hash>]
68
+ def merge_parts!(content:, parts:)
69
+ parts.each_with_index do |part, index|
70
+ if content["parts"][index].nil?
71
+ content["parts"][index] = part
72
+ else
73
+ content["parts"][index]["text"] += part["text"]
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -45,8 +45,8 @@ module OmniAI
45
45
  context.serializers[:usage] = UsageSerializer.method(:serialize)
46
46
  context.deserializers[:usage] = UsageSerializer.method(:deserialize)
47
47
 
48
- context.serializers[:payload] = PayloadSerializer.method(:serialize)
49
- context.deserializers[:payload] = PayloadSerializer.method(:deserialize)
48
+ context.serializers[:response] = ResponseSerializer.method(:serialize)
49
+ context.deserializers[:response] = ResponseSerializer.method(:deserialize)
50
50
 
51
51
  context.serializers[:choice] = ChoiceSerializer.method(:serialize)
52
52
  context.deserializers[:choice] = ChoiceSerializer.method(:deserialize)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Google
5
- VERSION = "1.9.7"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai-google
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.7
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-28 00:00:00.000000000 Z
10
+ date: 2025-03-03 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: event_stream_parser
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: '1.9'
32
+ version: '2.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '1.9'
39
+ version: '2.0'
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: zeitwerk
42
42
  requirement: !ruby/object:Gem::Requirement
@@ -67,7 +67,8 @@ files:
67
67
  - lib/omniai/google/chat/function_serializer.rb
68
68
  - lib/omniai/google/chat/media_serializer.rb
69
69
  - lib/omniai/google/chat/message_serializer.rb
70
- - lib/omniai/google/chat/payload_serializer.rb
70
+ - lib/omniai/google/chat/response_serializer.rb
71
+ - lib/omniai/google/chat/stream.rb
71
72
  - lib/omniai/google/chat/text_serializer.rb
72
73
  - lib/omniai/google/chat/tool_call_result_serializer.rb
73
74
  - lib/omniai/google/chat/tool_call_serializer.rb