Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion lib/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
require_relative "openai/internal/type/base_page"
require_relative "openai/internal/type/base_stream"
require_relative "openai/internal/type/request_parameters"
require_relative "openai/aliases"
require_relative "openai/internal"
require_relative "openai/request_options"
require_relative "openai/errors"
require_relative "openai/internal/transport/base_client"
Expand Down
19 changes: 0 additions & 19 deletions lib/openai/aliases.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/openai/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,32 +225,4 @@ class InternalServerError < OpenAI::Errors::APIStatusError
HTTP_STATUS = (500..)
end
end

Error = OpenAI::Errors::Error

ConversionError = OpenAI::Errors::ConversionError

APIError = OpenAI::Errors::APIError

APIStatusError = OpenAI::Errors::APIStatusError

APIConnectionError = OpenAI::Errors::APIConnectionError

APITimeoutError = OpenAI::Errors::APITimeoutError

BadRequestError = OpenAI::Errors::BadRequestError

AuthenticationError = OpenAI::Errors::AuthenticationError

PermissionDeniedError = OpenAI::Errors::PermissionDeniedError

NotFoundError = OpenAI::Errors::NotFoundError

ConflictError = OpenAI::Errors::ConflictError

UnprocessableEntityError = OpenAI::Errors::UnprocessableEntityError

RateLimitError = OpenAI::Errors::RateLimitError

InternalServerError = OpenAI::Errors::InternalServerError
end
8 changes: 8 additions & 0 deletions lib/openai/internal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module OpenAI
# @api private
module Internal
OMIT = Object.new.freeze
end
end
6 changes: 3 additions & 3 deletions lib/openai/internal/transport/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def initialize(

begin
status, response, stream = @requester.execute(input)
rescue OpenAI::APIConnectionError => e
rescue OpenAI::Errors::APIConnectionError => e
status = e
end

Expand All @@ -361,7 +361,7 @@ def initialize(
retry_count: retry_count,
send_retry_header: send_retry_header
)
in OpenAI::APIConnectionError if retry_count >= max_retries
in OpenAI::Errors::APIConnectionError if retry_count >= max_retries
raise status
in (400..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: response)
decoded = Kernel.then do
Expand Down Expand Up @@ -421,7 +421,7 @@ def initialize(
# @return [Object]
def request(req)
self.class.validate!(req)
model = req.fetch(:model) { OpenAI::Unknown }
model = req.fetch(:model) { OpenAI::Internal::Type::Unknown }
opts = req[:options].to_h
OpenAI::RequestOptions.validate!(opts)
request = build_request(req.except(:options), opts)
Expand Down
4 changes: 3 additions & 1 deletion lib/openai/internal/type/array_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def ===(other) = other.is_a?(Array) && other.all?(item_type)
#
# @return [Boolean]
def ==(other)
other.is_a?(OpenAI::ArrayOf) && other.nilable? == nilable? && other.item_type == item_type
# rubocop:disable Layout/LineLength
other.is_a?(OpenAI::Internal::Type::ArrayOf) && other.nilable? == nilable? && other.item_type == item_type
# rubocop:enable Layout/LineLength
end

# @api private
Expand Down
31 changes: 12 additions & 19 deletions lib/openai/internal/type/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class << self
#
# @return [Hash{Symbol=>Hash{Symbol=>Object}}]
def known_fields
@known_fields ||= (self < OpenAI::BaseModel ? superclass.known_fields.dup : {})
@known_fields ||= (self < OpenAI::Internal::Type::BaseModel ? superclass.known_fields.dup : {})
end

# @api private
Expand Down Expand Up @@ -64,14 +64,7 @@ def fields
setter = "#{name_sym}="
api_name = info.fetch(:api_name, name_sym)
nilable = info[:nil?]
const = if required && !nilable
info.fetch(
:const,
OpenAI::Internal::Util::OMIT
)
else
OpenAI::Internal::Util::OMIT
end
const = required && !nilable ? info.fetch(:const, OpenAI::Internal::OMIT) : OpenAI::Internal::OMIT

[name_sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)

Expand All @@ -89,7 +82,7 @@ def fields

define_method(name_sym) do
target = type_fn.call
value = @data.fetch(name_sym) { const == OpenAI::Internal::Util::OMIT ? nil : const }
value = @data.fetch(name_sym) { const == OpenAI::Internal::OMIT ? nil : const }
state = {strictness: :strong, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
if (nilable || !required) && value.nil?
nil
Expand All @@ -105,7 +98,7 @@ def fields
# rubocop:disable Layout/LineLength
message = "Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}. To get the unparsed API response, use #{cls}[:#{__method__}]."
# rubocop:enable Layout/LineLength
raise OpenAI::ConversionError.new(message)
raise OpenAI::Errors::ConversionError.new(message)
end
end

Expand Down Expand Up @@ -175,7 +168,7 @@ def optional(name_sym, type_info, spec = {})
# @param other [Object]
#
# @return [Boolean]
def ==(other) = other.is_a?(Class) && other <= OpenAI::BaseModel && other.fields == fields
def ==(other) = other.is_a?(Class) && other <= OpenAI::Internal::Type::BaseModel && other.fields == fields
end

# @param other [Object]
Expand All @@ -186,7 +179,7 @@ def ==(other) = self.class == other.class && @data == other.to_h
class << self
# @api private
#
# @param value [OpenAI::BaseModel, Hash{Object=>Object}, Object]
# @param value [OpenAI::Internal::Type::BaseModel, Hash{Object=>Object}, Object]
#
# @param state [Hash{Symbol=>Object}] .
#
Expand All @@ -196,7 +189,7 @@ class << self
#
# @option state [Integer] :branched
#
# @return [OpenAI::BaseModel, Object]
# @return [OpenAI::Internal::Type::BaseModel, Object]
def coerce(value, state:)
exactness = state.fetch(:exactness)

Expand All @@ -221,7 +214,7 @@ def coerce(value, state:)
api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)

unless val.key?(api_name)
if required && mode != :dump && const == OpenAI::Internal::Util::OMIT
if required && mode != :dump && const == OpenAI::Internal::OMIT
exactness[nilable ? :maybe : :no] += 1
else
exactness[:yes] += 1
Expand Down Expand Up @@ -255,7 +248,7 @@ def coerce(value, state:)

# @api private
#
# @param value [OpenAI::BaseModel, Object]
# @param value [OpenAI::Internal::Type::BaseModel, Object]
#
# @return [Hash{Object=>Object}, Object]
def dump(value)
Expand Down Expand Up @@ -284,7 +277,7 @@ def dump(value)

known_fields.each_value do |field|
mode, api_name, const = field.fetch_values(:mode, :api_name, :const)
next if mode == :coerce || acc.key?(api_name) || const == OpenAI::Internal::Util::OMIT
next if mode == :coerce || acc.key?(api_name) || const == OpenAI::Internal::OMIT
acc.store(api_name, const)
end

Expand Down Expand Up @@ -351,13 +344,13 @@ def to_yaml(*a) = self.class.dump(self).to_yaml(*a)

# Create a new instance of a model.
#
# @param data [Hash{Symbol=>Object}, OpenAI::BaseModel]
# @param data [Hash{Symbol=>Object}, OpenAI::Internal::Type::BaseModel]
def initialize(data = {})
case OpenAI::Internal::Util.coerce_hash(data)
in Hash => coerced
@data = coerced
else
raise ArgumentError.new("Expected a #{Hash} or #{OpenAI::BaseModel}, got #{data.inspect}")
raise ArgumentError.new("Expected a #{Hash} or #{OpenAI::Internal::Type::BaseModel}, got #{data.inspect}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/openai/internal/type/boolean_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.===(other) = other == true || other == false
# @param other [Object]
#
# @return [Boolean]
def self.==(other) = other.is_a?(Class) && other <= OpenAI::BooleanModel
def self.==(other) = other.is_a?(Class) && other <= OpenAI::Internal::Type::BooleanModel

class << self
# @api private
Expand Down
10 changes: 5 additions & 5 deletions lib/openai/internal/type/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def coerce(value, state:) = (raise NotImplementedError)
def dump(value)
case value
in Array
value.map { OpenAI::Unknown.dump(_1) }
value.map { OpenAI::Internal::Type::Unknown.dump(_1) }
in Hash
value.transform_values { OpenAI::Unknown.dump(_1) }
in OpenAI::BaseModel
value.transform_values { OpenAI::Internal::Type::Unknown.dump(_1) }
in OpenAI::Internal::Type::BaseModel
value.class.dump(value)
else
value
Expand Down Expand Up @@ -64,7 +64,7 @@ def type_info(spec)
in Hash
type_info(spec.slice(:const, :enum, :union).first&.last)
in true | false
-> { OpenAI::BooleanModel }
-> { OpenAI::Internal::Type::BooleanModel }
in OpenAI::Internal::Type::Converter | Class | Symbol
-> { spec }
in NilClass | Integer | Float
Expand Down Expand Up @@ -209,7 +209,7 @@ def coerce(
#
# @return [Object]
def dump(target, value)
target.is_a?(OpenAI::Internal::Type::Converter) ? target.dump(value) : OpenAI::Unknown.dump(value)
target.is_a?(OpenAI::Internal::Type::Converter) ? target.dump(value) : OpenAI::Internal::Type::Unknown.dump(value)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openai/internal/type/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def ===(other) = values.include?(other)
#
# @return [Boolean]
def ==(other)
other.is_a?(Module) && other.singleton_class <= OpenAI::Enum && other.values.to_set == values.to_set
other.is_a?(Module) && other.singleton_class <= OpenAI::Internal::Type::Enum && other.values.to_set == values.to_set
end

# @api private
Expand Down
4 changes: 3 additions & 1 deletion lib/openai/internal/type/hash_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def ===(other)
#
# @return [Boolean]
def ==(other)
other.is_a?(OpenAI::HashOf) && other.nilable? == nilable? && other.item_type == item_type
# rubocop:disable Layout/LineLength
other.is_a?(OpenAI::Internal::Type::HashOf) && other.nilable? == nilable? && other.item_type == item_type
# rubocop:enable Layout/LineLength
end

# @api private
Expand Down
2 changes: 1 addition & 1 deletion lib/openai/internal/type/request_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module RequestParameters

# @param mod [Module]
def self.included(mod)
return unless mod <= OpenAI::BaseModel
return unless mod <= OpenAI::Internal::Type::BaseModel

mod.extend(OpenAI::Internal::Type::RequestParameters::Converter)
mod.optional(:request_options, OpenAI::RequestOptions)
Expand Down
8 changes: 4 additions & 4 deletions lib/openai/internal/type/union.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ def variants = derefed_variants.map(&:last)
# @return [OpenAI::Internal::Type::Converter, Class, nil]
private def resolve_variant(value)
case [@discriminator, value]
in [_, OpenAI::BaseModel]
in [_, OpenAI::Internal::Type::BaseModel]
value.class
in [Symbol, Hash]
key = value.fetch(@discriminator) do
value.fetch(@discriminator.to_s, OpenAI::Internal::Util::OMIT)
value.fetch(@discriminator.to_s, OpenAI::Internal::OMIT)
end

return nil if key == OpenAI::Internal::Util::OMIT
return nil if key == OpenAI::Internal::OMIT

key = key.to_sym if key.is_a?(String)
known_variants.find { |k,| k == key }&.last&.call
Expand All @@ -126,7 +126,7 @@ def ===(other)
# @return [Boolean]
def ==(other)
# rubocop:disable Layout/LineLength
other.is_a?(Module) && other.singleton_class <= OpenAI::Union && other.derefed_variants == derefed_variants
other.is_a?(Module) && other.singleton_class <= OpenAI::Internal::Type::Union && other.derefed_variants == derefed_variants
# rubocop:enable Layout/LineLength
end

Expand Down
2 changes: 1 addition & 1 deletion lib/openai/internal/type/unknown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.===(other) = true
# @param other [Object]
#
# @return [Boolean]
def self.==(other) = other.is_a?(Class) && other <= OpenAI::Unknown
def self.==(other) = other.is_a?(Class) && other <= OpenAI::Internal::Type::Unknown

class << self
# @api private
Expand Down
11 changes: 2 additions & 9 deletions lib/openai/internal/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ def coerce_hash(input)
end
end

# Use this to indicate that a value should be explicitly removed from a data
# structure when using `OpenAI::Internal::Util.deep_merge`.
#
# e.g. merging `{a: 1}` and `{a: OMIT}` should produce `{}`, where merging
# `{a: 1}` and `{}` would produce `{a: 1}`.
OMIT = Object.new.freeze

class << self
# @api private
#
Expand All @@ -166,9 +159,9 @@ class << self
private def deep_merge_lr(lhs, rhs, concat: false)
case [lhs, rhs, concat]
in [Hash, Hash, _]
rhs_cleaned = rhs.reject { _2 == OpenAI::Internal::Util::OMIT }
rhs_cleaned = rhs.reject { _2 == OpenAI::Internal::OMIT }
lhs
.reject { |key, _| rhs[key] == OpenAI::Internal::Util::OMIT }
.reject { |key, _| rhs[key] == OpenAI::Internal::OMIT }
.merge(rhs_cleaned) do |_, old_val, new_val|
deep_merge_lr(old_val, new_val, concat: concat)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openai/models/all_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module OpenAI
module Models
module AllModels
extend OpenAI::Union
extend OpenAI::Internal::Type::Union

variant String

Expand Down
Loading