-
Notifications
You must be signed in to change notification settings - Fork 1
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
72 changed files
with
1,077 additions
and
831 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ source 'https://rubygems.org' | |
|
||
gemspec | ||
|
||
gem 'factory_bot' | ||
gem 'logger' | ||
gem 'rake' | ||
gem 'rspec' | ||
|
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
class Chat | ||
# For for. | ||
class Choice | ||
# @return [Integer] | ||
attr_accessor :index | ||
|
||
# @return [Message] | ||
attr_accessor :message | ||
|
||
# @param message [Message] | ||
def initialize(message:, index: 0) | ||
@message = message | ||
@index = index | ||
end | ||
|
||
# @return [String] | ||
def inspect | ||
"#<#{self.class.name} index=#{@index} message=#{@message.inspect}>" | ||
end | ||
|
||
# @param data [Hash] | ||
# @param context [OmniAI::Context] optional | ||
# | ||
# @return [Choice] | ||
def self.deserialize(data, context: nil) | ||
deserialize = context&.deserializers&.[](:choice) | ||
return deserialize.call(data, context:) if deserialize | ||
|
||
index = data['index'] | ||
message = Message.deserialize(data['message'] || data['delta'], context:) | ||
new(message:, index:) | ||
end | ||
|
||
# @param context [OmniAI::Context] optional | ||
# @return [Hash] | ||
def serialize(context: nil) | ||
serialize = context&.serializers&.[](:choice) | ||
return serialize.call(self, context:) if serialize | ||
|
||
{ | ||
index:, | ||
message: message.serialize(context:), | ||
} | ||
end | ||
|
||
# @return [Array<Content>, String] | ||
def content | ||
message.content | ||
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
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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
class Chat | ||
# A function that includes a name / arguments. | ||
class Function | ||
# @return [String] | ||
attr_accessor :name | ||
|
||
# @return [Hash] | ||
attr_accessor :arguments | ||
|
||
# @param name [String] | ||
# @param arguments [Hash] | ||
def initialize(name:, arguments:) | ||
@name = name | ||
@arguments = arguments | ||
end | ||
|
||
# @return [String] | ||
def inspect | ||
"#<#{self.class.name} name=#{name.inspect} arguments=#{arguments.inspect}>" | ||
end | ||
|
||
# @param data [Hash] | ||
# @param context [Context] optional | ||
# | ||
# @return [Function] | ||
def self.deserialize(data, context: nil) | ||
deserialize = context&.deserializers&.[](:function) | ||
return deserialize.call(data, context:) if deserialize | ||
|
||
name = data['name'] | ||
arguments = JSON.parse(data['arguments']) if data['arguments'] | ||
|
||
new(name:, arguments:) | ||
end | ||
|
||
# @param context [Context] optional | ||
# | ||
# @return [Hash] | ||
def serialize(context: nil) | ||
serializer = context&.serializers&.[](:function) | ||
return serializer.call(self, context:) if serializer | ||
|
||
{ | ||
name: @name, | ||
arguments: (JSON.generate(@arguments) if @arguments), | ||
} | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.