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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ Naming/ClassAndModuleCamelCase:
- "**/*.rbi"

Naming/MethodParameterName:
Exclude:
- "**/*.rbi"
Enabled: false

Naming/PredicateName:
Exclude:
Expand Down
10 changes: 10 additions & 0 deletions lib/openai/type/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ def deconstruct_keys(keys)
.to_h
end

# @param a [Object]
#
# @return [String]
def to_json(*a) = self.class.dump(self).to_json(*a)

# @param a [Object]
#
# @return [String]
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]
Expand Down
16 changes: 6 additions & 10 deletions lib/openai/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class << self
#
# @yieldparam [Enumerator::Yielder]
# @return [Enumerable]
def string_io(&blk)
def writable_enum(&blk)
Enumerator.new do |y|
y.define_singleton_method(:write) do
self << _1.clone
Expand All @@ -454,15 +454,13 @@ def string_io(&blk)
end

class << self
# rubocop:disable Naming/MethodParameterName

# @api private
#
# @param y [Enumerator::Yielder]
# @param boundary [String]
# @param key [Symbol, String]
# @param val [Object]
private def encode_multipart_formdata(y, boundary:, key:, val:)
private def write_multipart_chunk(y, boundary:, key:, val:)
y << "--#{boundary}\r\n"
y << "Content-Disposition: form-data"
unless key.nil?
Expand Down Expand Up @@ -494,8 +492,6 @@ class << self
y << "\r\n"
end

# rubocop:enable Naming/MethodParameterName

# @api private
#
# @param body [Object]
Expand All @@ -504,21 +500,21 @@ class << self
private def encode_multipart_streaming(body)
boundary = SecureRandom.urlsafe_base64(60)

strio = string_io do |y|
strio = writable_enum do |y|
case body
in Hash
body.each do |key, val|
case val
in Array if val.all? { primitive?(_1) }
val.each do |v|
encode_multipart_formdata(y, boundary: boundary, key: key, val: v)
write_multipart_chunk(y, boundary: boundary, key: key, val: v)
end
else
encode_multipart_formdata(y, boundary: boundary, key: key, val: val)
write_multipart_chunk(y, boundary: boundary, key: key, val: val)
end
end
else
encode_multipart_formdata(y, boundary: boundary, key: nil, val: body)
write_multipart_chunk(y, boundary: boundary, key: nil, val: body)
end
y << "--#{boundary}--\r\n"
end
Expand Down
8 changes: 8 additions & 0 deletions rbi/lib/openai/type/base_model.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ module OpenAI
def deconstruct_keys(keys)
end

sig { params(a: T.anything).returns(String) }
def to_json(*a)
end

sig { params(a: T.anything).returns(String) }
def to_yaml(*a)
end

# Create a new instance of a model.
sig { params(data: T.any(T::Hash[Symbol, T.anything], T.self_type)).returns(T.attached_class) }
def self.new(data = {})
Expand Down
4 changes: 2 additions & 2 deletions rbi/lib/openai/util.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module OpenAI

class << self
sig { params(blk: T.proc.params(y: Enumerator::Yielder).void).returns(T::Enumerable[String]) }
def string_io(&blk)
def writable_enum(&blk)
end
end

Expand All @@ -207,7 +207,7 @@ module OpenAI
sig do
params(y: Enumerator::Yielder, boundary: String, key: T.any(Symbol, String), val: T.anything).void
end
private def encode_multipart_formdata(y, boundary:, key:, val:)
private def write_multipart_chunk(y, boundary:, key:, val:)
end

# @api private
Expand Down
4 changes: 4 additions & 0 deletions sig/openai/type/base_model.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module OpenAI

def deconstruct_keys: (::Array[Symbol]? keys) -> ::Hash[Symbol, top]

def to_json: (*top a) -> String

def to_yaml: (*top a) -> String

def initialize: (?::Hash[Symbol, top] | self data) -> void

def inspect: -> String
Expand Down
4 changes: 2 additions & 2 deletions sig/openai/util.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ module OpenAI
} -> void
end

def self?.string_io: {
def self?.writable_enum: {
(Enumerator::Yielder y) -> void
} -> Enumerable[String]

def self?.encode_multipart_formdata: (
def self?.write_multipart_chunk: (
Enumerator::Yielder y,
boundary: String,
key: Symbol | String,
Expand Down
2 changes: 1 addition & 1 deletion test/openai/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_copy_write
StringIO.new("abc") => "abc"
}
cases.each do |input, expected|
enum = OpenAI::Util.string_io do |y|
enum = OpenAI::Util.writable_enum do |y|
IO.copy_stream(input, y)
end
assert_equal(expected, enum.to_a.join)
Expand Down