Skip to content

Commit

Permalink
Merge pull request alexrudall#119 from alexrudall/dalle
Browse files Browse the repository at this point in the history
Add DALL·E Images#generate endpoint!
  • Loading branch information
alexrudall authored Nov 13, 2022
2 parents 37b52ce + 233f543 commit e4105c1
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2022-11-13

### Added

- Add Images#generate endpoint to generate images with DALL·E!

## [2.0.1] - 2022-10-22

### Removed
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ This fine-tuned model name can then be used in classifications:
JSON.parse(response.body)["choices"].map { |c| c["text"] }
```

### Images

Generate an image using DALL·E!

```ruby
response = client.images.generate(parameters: { prompt: "A baby sea otter cooking pasta wearing a hat of some sort" })
puts response.dig("data", 0, "url") }
=> "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
```

![Otter Chef](https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhhQPMiIQ0Es8OwrH/user-jxM65ijkZc1qRfHC0IJ8mOIc/img-UrDvFC4tDnuhTieF7TrTJ2gq.png?st=2022-11-13T15%3A55%3A34Z&se=2022-11-13T17%3A55%3A34Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-11-13T01%3A32%3A30Z&ske=2022-11-14T01%3A32%3A30Z&sks=b&skv=2021-08-06&sig=tLdggckHl20CnnpCleoeiAEQjy4zMjuZJiUdovmkoF0%3D)

### Moderations

Pass a string to check if it violates OpenAI's Content Policy:
Expand Down
1 change: 1 addition & 0 deletions lib/ruby/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "ruby/openai/engines"
require "ruby/openai/files"
require "ruby/openai/finetunes"
require "ruby/openai/images"
require "ruby/openai/models"
require "ruby/openai/client"
require "ruby/openai/version"
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby/openai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def finetunes
@finetunes ||= OpenAI::Finetunes.new(access_token: @access_token)
end

def images
@images ||= OpenAI::Images.new(access_token: @access_token)
end

def models
@models ||= OpenAI::Models.new(access_token: @access_token)
end
Expand Down
27 changes: 27 additions & 0 deletions lib/ruby/openai/images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module OpenAI
class Images
include HTTParty
base_uri "https://api.openai.com"

def initialize(access_token: nil)
@access_token = access_token || ENV.fetch("OPENAI_ACCESS_TOKEN")
end

def generate(version: default_version, parameters: {})
self.class.post(
"/#{version}/images/generations",
headers: {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{@access_token}"
},
body: parameters.to_json
)
end

private

def default_version
"v1".freeze
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby/openai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Ruby
module OpenAI
VERSION = "2.0.1".freeze
VERSION = "2.1.0".freeze
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions spec/ruby/openai/client/images_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RSpec.describe OpenAI::Client do
describe "#images" do
describe "#generate", :vcr do
let(:response) do
OpenAI::Client.new.images.generate(
parameters: {
prompt: prompt,
size: size
}
)
end
let(:cassette) { "images generate #{prompt}" }
let(:prompt) { "A baby sea otter cooking pasta wearing a hat of some sort" }
let(:size) { "256x256" }

it "succeeds" do
VCR.use_cassette(cassette) do
r = JSON.parse(response.body)
expect(r.dig("data", 0, "url")).to include("dalle")
end
end
end
end
end

0 comments on commit e4105c1

Please sign in to comment.