Skip to content

Commit

Permalink
WIP: image generation with Dall-e
Browse files Browse the repository at this point in the history
  • Loading branch information
ceicke committed Jun 11, 2024
1 parent b56f1de commit 195ebc4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@
/.idea/*
/client/.idea/*
vendor/bundle

# Ignore downloaded images
/public/dalle_images
1 change: 1 addition & 0 deletions app/services/toolbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def self.descendants
[
Toolbox::HelloWorld,
Toolbox::OpenMeteo,
Toolbox::Dalle
]
end

Expand Down
48 changes: 48 additions & 0 deletions app/services/toolbox/dalle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Toolbox::Dalle < Toolbox

describe :generate_an_image, <<~S
Generate an image based on what the user asks you to generate. You will pass the user's prompt and will get back a URL to an image.
S

def self.generate_an_image(image_generation_prompt_s:)

response = client.images.generate(
parameters: {
prompt: image_generation_prompt_s,
model: "dall-e-3",
size: "1024x1792",
quality: "standard"
}
)

dalle_url = response.dig("data", 0, "url")
download(dalle_url)

{
url: dalle_url
}
end

class << self
private

def download(image_url)
filename = File.basename(URI.parse(image_url).path)
file_path = Rails.root.join('public', 'dalle_images', filename)

FileUtils.mkdir_p(File.dirname(file_path))

File.open(file_path, 'wb') do |file|
file << URI.open(image_url).read
end

file_path
end

def client
OpenAI::Client.new(
access_token: Current.user.openai_key,
)
end
end
end
2 changes: 1 addition & 1 deletion test/services/toolbox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ToolboxTest < ActiveSupport::TestCase
test "tools" do
assert_equal 4, Toolbox.tools.length
assert_equal 5, Toolbox.tools.length
assert Toolbox::OpenMeteo.tools.first[:function].values.all? { |value| value.present? }
assert Toolbox::OpenMeteo.tools.first[:function][:description].length > 100
tools = [
Expand Down

0 comments on commit 195ebc4

Please sign in to comment.