Skip to content

Commit

Permalink
Dalle image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ceicke authored and Christoph Eicke committed Jun 27, 2024
1 parent b56f1de commit e432040
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
16 changes: 16 additions & 0 deletions app/jobs/get_next_ai_message_job.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'open-uri'

class GetNextAIMessageJob < ApplicationJob
class ResponseCancelled < StandardError; end
class WaitForPrevious < StandardError; end
Expand Down Expand Up @@ -156,6 +158,7 @@ def call_tools_before_wrapping_up
end

index = @message.index
url_of_dalle_generated_image = nil
msgs.each do |tool_message| # one message for each tool executed
@conversation.messages.create!(
assistant: @assistant,
Expand All @@ -166,6 +169,13 @@ def call_tools_before_wrapping_up
index: index += 1,
processed_at: Time.current,
)

content_hash = JSON.parse(tool_message[:content])

if content_hash.has_key?('url_of_dalle_generated_image')
url_of_dalle_generated_image = content_hash['url_of_dalle_generated_image']
end

end

assistant_reply = @conversation.messages.create!(
Expand All @@ -176,6 +186,12 @@ def call_tools_before_wrapping_up
index: index += 1
)

unless url_of_dalle_generated_image.nil?
d = Document.new
d.file.attach(io: URI.open(url_of_dalle_generated_image), filename: 'image.png')
assistant_reply.documents << d
end

GetNextAIMessageJob.perform_later(
@user.id,
assistant_reply.id,
Expand Down
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
36 changes: 36 additions & 0 deletions app/services/toolbox/dalle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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")

{
prompt_given: image_generation_prompt_s,
url_of_dalle_generated_image: dalle_url,
note_to_assistant: "The image at the URL is already being shown on screen so reply with a nice message confirming the image has been generated, maybe re-describing it, but don't include the link to it."
}
end

class << self
private

def client
OpenAI::Client.new(
access_token: Current.user.openai_key,
)
end
end
end
2 changes: 1 addition & 1 deletion lib/active_storage/service/postgresql_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def generate_url(key, expires_in:, filename:, disposition:, content_type:)
)

generated_url = url_helpers.rails_postgresql_service_url(verified_key_with_expiration,
**url_options,
only_path: true,
disposition: content_disposition,
content_type: content_type,
filename: filename,
Expand Down
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 e432040

Please sign in to comment.