-
Notifications
You must be signed in to change notification settings - Fork 237
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
Image generation with Dall-e #398
Draft
ceicke
wants to merge
10
commits into
AllYourBot:main
Choose a base branch
from
ceicke:dalle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e432040
Dalle image generation
ceicke 909e823
Merge branch 'main' into dalle
ceicke 5fdd4e4
Update toolbox.rb
ceicke 5c14b12
Fix rubocop issues
ceicke 08e8fe1
Fix PostgreSQL ActiveStorage tests
ceicke bc1af9e
Update lib/active_storage/service/postgresql_service.rb
ceicke 0564d1a
Update app/jobs/get_next_ai_message_job.rb
ceicke 0f20f81
Fixing tests
ceicke 0aea5c0
WIP: testing the image generation
ceicke 8414909
Merge branch 'main' into dalle
ceicke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
krschacht marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
ceicke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class << self | ||
private | ||
|
||
def client | ||
OpenAI::Client.new( | ||
access_token: Current.user.openai_key, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,19 @@ | |
class GetNextAIMessageJobOpenaiTest < ActiveJob::TestCase | ||
setup do | ||
@conversation = conversations(:greeting) | ||
@user = @conversation.user | ||
@conversation.messages.create! role: :user, content_text: "Still there?", assistant: @conversation.assistant | ||
@message = @conversation.latest_message_for_version(:latest) | ||
@test_client = TestClient::OpenAI.new(access_token: "abc") | ||
|
||
p "#########" | ||
@image_generation = conversations(:image_generation) | ||
p @image_generation | ||
p @image_generation.messages.create! role: :user, content_text: "Generate an image", assistant: @image_generation.assistant | ||
p @image_generation.messages.length | ||
@image_generation_message = @image_generation.latest_message_for_version(:latest) | ||
p @image_generation_message | ||
|
||
@user = @conversation.user | ||
@test_client = TestClient::OpenAI.new(access_token: 'abc') | ||
end | ||
|
||
test "populates the latest message from the assistant" do | ||
|
@@ -56,6 +65,52 @@ class GetNextAIMessageJobOpenaiTest < ActiveJob::TestCase | |
refute second_new_message.finished?, "This message SHOULD NOT be considered finished yet" | ||
end | ||
|
||
test "properly handles a tool response call from the assistant when images are included" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @krschacht I tried to implement a test for the job, but I cannot get it to work. It already fails to add 2 new messages. Maybe I setup something wrong in the fixtures or in the |
||
assert_difference "@image_generation.messages.reload.length", 2 do | ||
TestClient::OpenAI.stub :function, "generate_an_image" do | ||
TestClient::OpenAI.stub :arguments, { image_generation_prompt_s: "Kitten" } do | ||
TestClient::OpenAI.stub :api_response, TestClient::OpenAI.api_function_response do | ||
assert GetNextAIMessageJob.perform_now(@user.id, @message.id, @image_generation.assistant.id) | ||
end | ||
end | ||
end | ||
end | ||
|
||
p "###### Message:" | ||
p @image_generation_message | ||
|
||
@image_generation_message.reload | ||
assert @image_generation_message.content_text.blank? | ||
assert @image_generation_message.tool_call_id.nil? | ||
assert @image_generation_message.content_tool_calls.present?, "Assistant should have decided to call a tool" | ||
|
||
@new_messages = @image_generation.messages.where("id > ?", @message.id).order(:created_at) | ||
|
||
p "###### Image generation:" | ||
p @image_generation | ||
@new_messages.messages.each_with_index do |message, index| | ||
p "######## #{index}:" | ||
p message | ||
end | ||
|
||
# first | ||
first_new_message = @new_messages.first | ||
assert first_new_message.tool? | ||
# assert_equal "Hello, Keith!".to_json, first_new_message.content_text, "First new message should have the result of calling the tool" | ||
# assert first_new_message.tool_call_id.present? | ||
# assert first_new_message.content_tool_calls.blank? | ||
# assert_equal @message.content_tool_calls.dig(0, :id), first_new_message.tool_call_id, "ID of tool execution should have matched decision to call the tool" | ||
# assert first_new_message.finished?, "This message SHOULD HAVE been considered finished" | ||
|
||
# second | ||
# second_new_message = @new_messages.second | ||
# assert second_new_message.assistant?, "Second new message should be queued up for the assistant to reply" | ||
# assert second_new_message.content_text.nil?, "The content should be nil to indicate that it hasn't even started processing" | ||
# assert second_new_message.tool_call_id.nil? | ||
# assert second_new_message.content_tool_calls.blank? | ||
# refute second_new_message.finished?, "This message SHOULD NOT be considered finished yet" | ||
end | ||
|
||
test "returns early if the message id was invalid" do | ||
refute GetNextAIMessageJob.perform_now(@user.id, 0, @conversation.assistant.id) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should assume that
@message.content_tool_calls
can have multiple Dalle toolbox calls. This means that withinmsgs.each do |tool_message|
each of those calls will set theurl_of_dalle_generated_image
overwriting the previous.So let's collect multiple
url_of_dalle_generated_image
into an array so that in this block here we push multiple documents into the assistant_reply.