-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly implement serialize / deserialize
- Loading branch information
Showing
20 changed files
with
459 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
module Anthropic | ||
class Chat | ||
# Overrides choice serialize / deserialize. | ||
module ChoiceSerializer | ||
# @param choice [OmniAI::Chat::Choice] | ||
# @param context [Context] | ||
# @return [Hash] | ||
def self.serialize(choice, context:) | ||
choice.message.serialize(context:) | ||
end | ||
|
||
# @param data [Hash] | ||
# @param context [Context] | ||
# @return [OmniAI::Chat::Choice] | ||
def self.deserialize(data, context:) | ||
message = OmniAI::Chat::Message.deserialize(data, context:) | ||
OmniAI::Chat::Choice.new(message:) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
module Anthropic | ||
class Chat | ||
# Overrides content serialize / deserialize. | ||
module ContentSerializer | ||
# @param data [Hash] | ||
# @param context [Context] | ||
# @return [OmniAI::Chat::Text, OmniAI::Chat::ToolCall] | ||
def self.deserialize(data, context:) | ||
case data['type'] | ||
when 'text' then OmniAI::Chat::Text.deserialize(data, context:) | ||
when 'tool_use' then OmniAI::Chat::ToolCall.deserialize(data, context:) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
module Anthropic | ||
class Chat | ||
# Overrides function serialize / deserialize. | ||
module FunctionSerializer | ||
# @param function [OmniAI::Chat::Function] | ||
# @return [Hash] | ||
def self.serialize(function, *) | ||
{ | ||
name: function.name, | ||
input: function.arguments, | ||
} | ||
end | ||
|
||
# @param data [Hash] | ||
# @return [OmniAI::Chat::Function] | ||
def self.deserialize(data, *) | ||
name = data['name'] | ||
arguments = data['input'] | ||
OmniAI::Chat::Function.new(name:, arguments:) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
module Anthropic | ||
class Chat | ||
# Overrides media serialize / deserialize. | ||
module MediaSerializer | ||
# @param payload [OmniAI::Chat::Media] | ||
# @return [Hash] | ||
def self.serialize(media, *) | ||
{ | ||
type: media.kind, # i.e. 'image' / 'video' / 'audio' / ... | ||
source: { | ||
type: 'base64', | ||
media_type: media.type, # i.e. 'image/jpeg' / 'video/ogg' / 'audio/mpeg' / ... | ||
data: media.data, | ||
}, | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
module Anthropic | ||
class Chat | ||
# Overrides payload serialize / deserialize. | ||
module PayloadSerializer | ||
# @param payload [OmniAI::Chat::Payload] | ||
# @param context [OmniAI::Context] | ||
# @return [Hash] | ||
def self.serialize(payload, context:) | ||
usage = payload.usage.serialize(context:) | ||
choice = payload.choice.serialize(context:) | ||
|
||
choice.merge({ usage: }) | ||
end | ||
|
||
# @param data [Hash] | ||
# @param context [OmniAI::Context] | ||
# @return [OmniAI::Chat::Payload] | ||
def self.deserialize(data, context:) | ||
usage = OmniAI::Chat::Usage.deserialize(data['usage'], context:) if data['usage'] | ||
choice = OmniAI::Chat::Choice.deserialize(data, context:) | ||
|
||
OmniAI::Chat::Payload.new(choices: [choice], usage:) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.