Skip to content

Add multi-image upload #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions lib/openai/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ def variations(parameters: {})
private

def open_files(parameters)
parameters = parameters.merge(image: File.open(parameters[:image]))
parameters = parameters.merge(mask: File.open(parameters[:mask])) if parameters[:mask]
parameters
params = parameters.dup

if params[:image].is_a?(Array)
process_image_array(params)
else
params[:image] = File.open(params[:image])
end

params[:mask] = File.open(params[:mask]) if params[:mask]
params
end

def process_image_array(params)
params[:image].each_with_index do |img_path, index|
params[:"image[#{index}]"] = File.open(img_path)
end
params.delete(:image)
end
end
end
83 changes: 83 additions & 0 deletions spec/fixtures/cassettes/images_edit_multiple.yml

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions spec/openai/client/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,26 @@
end
end
end

describe "with multiple images", :vcr do
let(:response) do
OpenAI::Client.new.images.edit(
parameters: {
image: [
File.join(RSPEC_ROOT, "fixtures/files", "image.png"),
File.join(RSPEC_ROOT, "fixtures/files", "mask.png")
],
prompt: "Create a collage from these images",
model: "gpt-image-1" # Required for multiple images
}
)
end

it "converts array of images to image[] parameter" do
VCR.use_cassette("images edit multiple") do
expect(response.dig("data", 0, "b64_json")).to be_a(String)
end
end
end
end
end