-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Enhancement/gpt4o image gen support #4859
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
139 changes: 139 additions & 0 deletions
139
backend/tests/integration/tests/tools/test_image_generation_tool.py
This file contains hidden or 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,139 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from onyx.tools.tool_implementations.images.image_generation_tool import ( | ||
IMAGE_GENERATION_RESPONSE_ID, | ||
) | ||
from onyx.tools.tool_implementations.images.image_generation_tool import ImageFormat | ||
from onyx.tools.tool_implementations.images.image_generation_tool import ( | ||
ImageGenerationResponse, | ||
) | ||
from onyx.tools.tool_implementations.images.image_generation_tool import ( | ||
ImageGenerationTool, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def dalle3_tool() -> ImageGenerationTool: | ||
api_key = os.getenv("OPENAI_API_KEY") | ||
if not api_key: | ||
pytest.skip("OPENAI_API_KEY environment variable not set") | ||
|
||
return ImageGenerationTool( | ||
api_key=api_key, | ||
api_base=None, | ||
api_version=None, | ||
model="dall-e-3", | ||
num_imgs=1, | ||
output_format=ImageFormat.URL, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def gpt_image_tool() -> ImageGenerationTool: | ||
api_key = os.getenv("OPENAI_API_KEY") | ||
if not api_key: | ||
pytest.skip("OPENAI_API_KEY environment variable not set") | ||
|
||
return ImageGenerationTool( | ||
api_key=api_key, | ||
api_base=None, | ||
api_version=None, | ||
model="gpt-image-1", | ||
output_format=ImageFormat.BASE64, | ||
num_imgs=1, | ||
) | ||
|
||
|
||
def test_dalle3_generates_image_url_successfully( | ||
dalle3_tool: ImageGenerationTool, | ||
) -> None: | ||
# Run the tool with a simple prompt | ||
results = list(dalle3_tool.run(prompt="A simple red circle")) | ||
|
||
# Verify we get a response | ||
assert len(results) == 1 | ||
tool_response = results[0] | ||
|
||
# Check response structure | ||
assert tool_response.id == IMAGE_GENERATION_RESPONSE_ID | ||
assert isinstance(tool_response.response, list) | ||
assert len(tool_response.response) == 1 | ||
|
||
# Check ImageGenerationResponse content | ||
image_response = tool_response.response[0] | ||
assert isinstance(image_response, ImageGenerationResponse) | ||
assert image_response.revised_prompt is not None | ||
assert len(image_response.revised_prompt) > 0 | ||
assert image_response.url is not None | ||
assert image_response.url.startswith("https://") | ||
assert image_response.image_data is None | ||
|
||
|
||
def test_dalle3_with_base64_format() -> None: | ||
api_key = os.getenv("OPENAI_API_KEY") | ||
if not api_key: | ||
pytest.skip("OPENAI_API_KEY environment variable not set") | ||
Comment on lines
+75
to
+77
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. style: test_dalle3_with_base64_format doesn't use the fixture pattern established by other tests, leading to code duplication |
||
|
||
# Create tool with base64 format | ||
tool = ImageGenerationTool( | ||
api_key=api_key, | ||
api_base=None, | ||
api_version=None, | ||
model="dall-e-3", | ||
output_format=ImageFormat.BASE64, | ||
num_imgs=1, | ||
) | ||
|
||
# Run the tool | ||
results = list(tool.run(prompt="A simple blue square", shape="square")) | ||
|
||
# Verify response | ||
assert len(results) == 1 | ||
image_response = results[0].response[0] | ||
assert image_response.url is None | ||
assert image_response.image_data is not None | ||
assert len(image_response.image_data) > 0 | ||
|
||
|
||
def test_gpt_image_1_generates_base64_successfully( | ||
gpt_image_tool: ImageGenerationTool, | ||
) -> None: | ||
# Run the tool with a simple prompt | ||
results = list(gpt_image_tool.run(prompt="A simple red circle")) | ||
|
||
# Verify we get a response | ||
assert len(results) == 1 | ||
tool_response = results[0] | ||
|
||
# Check response structure | ||
assert tool_response.id == IMAGE_GENERATION_RESPONSE_ID | ||
assert isinstance(tool_response.response, list) | ||
assert len(tool_response.response) == 1 | ||
|
||
# Check ImageGenerationResponse content | ||
image_response = tool_response.response[0] | ||
assert isinstance(image_response, ImageGenerationResponse) | ||
assert image_response.revised_prompt is not None | ||
assert len(image_response.revised_prompt) > 0 | ||
assert image_response.url is None | ||
assert image_response.image_data is not None | ||
assert len(image_response.image_data) > 0 | ||
|
||
|
||
def test_gpt_image_1_with_url_format_fails() -> None: | ||
api_key = os.getenv("OPENAI_API_KEY") | ||
if not api_key: | ||
pytest.skip("OPENAI_API_KEY environment variable not set") | ||
|
||
# This should fail during tool creation since gpt-image-1 doesn't support URL format | ||
with pytest.raises(ValueError, match="gpt-image-1 does not support URL format"): | ||
ImageGenerationTool( | ||
api_key=api_key, | ||
api_base=None, | ||
api_version=None, | ||
model="gpt-image-1", | ||
output_format=ImageFormat.URL, | ||
num_imgs=1, | ||
) |
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.
style: Consider adding a constant for 'gpt-image-1' to avoid magic strings, similar to how AZURE_DALLE_DEPLOYMENT_NAME is used