Module: OmniAI::Google::Chat::PayloadSerializer

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

Overview

Overrides payload serialize / deserialize.

Class Method Summary collapse

Class Method Details

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

Parameters:

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

Returns:

  • (OmniAI::Chat::Payload)


24
25
26
27
28
29
# File 'lib/omniai/google/chat/payload_serializer.rb', line 24

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::Payload.new(choices:, usage:)
end

.serialize(payload, context:) ⇒ Hash

Parameters:

  • payload (OmniAI::Chat::Payload)
  • context (OmniAI::Context)

Returns:

  • (Hash)


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

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

  {
    candidates:,
    usageMetadata: ,
  }
end