Module: OmniAI::Google::Chat::ResponseSerializer

Defined in:
lib/omniai/google/chat/response_serializer.rb

Overview

Overrides response serialize / deserialize.

Class Method Summary collapse

Class Method Details

.deserialize(data, context:) ⇒ OmniAI::Chat::Response

Parameters:

  • data (Hash)
  • context (OmniAI::Context)

Returns:

  • (OmniAI::Chat::Response)


26
27
28
29
30
31
# File 'lib/omniai/google/chat/response_serializer.rb', line 26

def self.deserialize(data, context:)
  choices = data["candidates"].map { |candidate| OmniAI::Chat::Choice.deserialize(candidate, context:) }
  usage = OmniAI::Chat::Usage.deserialize(data["usageMetadata"], context:) if data["usageMetadata"]

  OmniAI::Chat::Response.new(data:, choices:, usage:)
end

.serialize(response, context:) ⇒ Hash

Parameters:

  • response (OmniAI::Chat::Response)
  • context (OmniAI::Context)

Returns:

  • (Hash)


12
13
14
15
16
17
18
19
20
# File 'lib/omniai/google/chat/response_serializer.rb', line 12

def self.serialize(response, context:)
  candidates = response.choices.map { |choice| choice.serialize(context:) }
   = response.usage&.serialize(context:)

  {
    candidates:,
    usageMetadata: ,
  }
end