Module: OmniAI::Google::Chat::ChoiceSerializer

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

Overview

Overrides choice serialize / deserialize.

Constant Summary collapse

FINISH_REASONS =

Maps Gemini candidate ‘finishReason` values onto the normalized OmniAI::Chat::FinishReason symbols. The entire content-policy / safety family maps to `:filter`; unrecognized values (e.g. OTHER, MALFORMED_FUNCTION_CALL, FINISH_REASON_UNSPECIFIED) fall through to `:other`.

{
  "STOP" => OmniAI::Chat::FinishReason::STOP,
  "MAX_TOKENS" => OmniAI::Chat::FinishReason::LENGTH,
  "SAFETY" => OmniAI::Chat::FinishReason::FILTER,
  "RECITATION" => OmniAI::Chat::FinishReason::FILTER,
  "LANGUAGE" => OmniAI::Chat::FinishReason::FILTER,
  "BLOCKLIST" => OmniAI::Chat::FinishReason::FILTER,
  "PROHIBITED_CONTENT" => OmniAI::Chat::FinishReason::FILTER,
  "SPII" => OmniAI::Chat::FinishReason::FILTER,
  "IMAGE_SAFETY" => OmniAI::Chat::FinishReason::FILTER,
}.freeze

Class Method Summary collapse

Class Method Details

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

Parameters:

  • data (Hash)
  • context (Context)

Returns:

  • (OmniAI::Chat::Choice)


33
34
35
36
37
# File 'lib/omniai/google/chat/choice_serializer.rb', line 33

def self.deserialize(data, context:)
  message = OmniAI::Chat::Message.deserialize(data["content"], context:)
  finish_reason = OmniAI::Chat::FinishReason.deserialize(data["finishReason"], table: FINISH_REASONS)
  OmniAI::Chat::Choice.new(message:, finish_reason:)
end

.serialize(choice, context:) ⇒ Hash

Parameters:

  • choice (OmniAI::Chat::Choice)
  • context (Context)

Returns:

  • (Hash)


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

def self.serialize(choice, context:)
  content = choice.message.serialize(context:)
  { content: }
end