-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Updates to Agent image generation tool tests #44267
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
14 commits
Select commit
Hold shift + click to select a range
e7b55db
Sync image generation
dargilco f231947
Add async tests
dargilco 9276394
update .env.template
dargilco 3c08d9e
Update comment
dargilco f6dc3ca
Update test asserts
dargilco 09555e3
Add model name in Agent defintion
dargilco 91b677a
Add comments on how to run a test
dargilco a75265d
update test assets
dargilco 9b18fe0
Also fix samples
dargilco 1afc2a5
Make sure we use a non-default image generation model
dargilco 5df2752
Update test asserts
dargilco fcd327e
Fix quality gates
dargilco 77c9171
update test asserts
dargilco 0efce21
Merge branch 'main' into dargilco/add-recordings-to-agent-tool-tests
dargilco 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
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
108 changes: 108 additions & 0 deletions
108
sdk/ai/azure-ai-projects/tests/agents/tools/test_agent_image_generation_async.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,108 @@ | ||
| # pylint: disable=too-many-lines,line-too-long,useless-suppression | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| # cSpell:disable | ||
|
|
||
| import os | ||
| import base64 | ||
| import pytest | ||
| from test_base import TestBase, servicePreparer | ||
| from devtools_testutils.aio import recorded_by_proxy_async | ||
| from devtools_testutils import RecordedTransport | ||
| from azure.ai.projects.models import PromptAgentDefinition, ImageGenTool | ||
| from azure.core.exceptions import ResourceNotFoundError | ||
|
|
||
|
|
||
| class TestAgentImageGenerationAsync(TestBase): | ||
|
|
||
| @servicePreparer() | ||
| @recorded_by_proxy_async(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX) | ||
| async def test_agent_image_generation_async(self, **kwargs): | ||
|
|
||
| model = self.test_agents_params["model_deployment_name"] | ||
| image_model = self.test_agents_tools_params["image_generation_model_deployment_name"] | ||
| agent_name = "image-gen-agent" | ||
|
|
||
| async with ( | ||
| self.create_async_client(operation_group="agents", **kwargs) as project_client, | ||
| project_client.get_openai_client() as openai_client, | ||
| ): | ||
| # Check if the image model deployment exists in the project | ||
| try: | ||
| deployment = await project_client.deployments.get(image_model) | ||
| print(f"Image model deployment found: {deployment.name}") | ||
| except ResourceNotFoundError: | ||
| pytest.fail(f"Image generation model '{image_model}' not available in this project") | ||
| except Exception as e: | ||
| pytest.fail(f"Unable to verify image model deployment: {e}") | ||
|
|
||
| # Disable retries for faster failure when service returns 500 | ||
| openai_client.max_retries = 0 | ||
|
|
||
| # Create agent with image generation tool | ||
| agent = await project_client.agents.create_version( | ||
| agent_name=agent_name, | ||
| definition=PromptAgentDefinition( | ||
| model=model, | ||
| instructions="Generate images based on user prompts", | ||
| tools=[ImageGenTool(model=image_model, quality="low", size="1024x1024")], # type: ignore | ||
| ), | ||
| description="Agent for testing image generation.", | ||
| ) | ||
| print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") | ||
| self._validate_agent_version(agent, expected_name=agent_name) | ||
|
|
||
| # Request image generation | ||
| print("\nAsking agent to generate an image of a simple geometric shape...") | ||
|
|
||
| response = await openai_client.responses.create( | ||
| input="Generate an image of a blue circle on a white background.", | ||
| extra_headers={"x-ms-oai-image-generation-deployment": image_model}, # Required for image generation | ||
| extra_body={"agent": {"name": agent.name, "type": "agent_reference"}}, | ||
| ) | ||
|
|
||
| print(f"Response created (id: {response.id})") | ||
| assert response.id | ||
| assert response.output is not None | ||
| assert len(response.output) > 0 | ||
|
|
||
| # Extract image data from response | ||
| image_data = [output.result for output in response.output if output.type == "image_generation_call"] | ||
|
|
||
| # Verify image was generated | ||
| assert len(image_data) > 0, "Expected at least one image to be generated" | ||
| assert image_data[0], "Expected image data to be non-empty" | ||
|
|
||
| print(f"✓ Image data received ({len(image_data[0])} base64 characters)") | ||
|
|
||
| # Decode the base64 image | ||
| image_bytes = b"" | ||
| try: | ||
| image_bytes = base64.b64decode(image_data[0]) | ||
| assert len(image_bytes) > 0, "Decoded image should have content" | ||
| print(f"✓ Image decoded successfully ({len(image_bytes)} bytes)") | ||
| except Exception as e: | ||
| pytest.fail(f"Failed to decode base64 image data: {e}") | ||
|
|
||
| # Verify it's a PNG image (check magic bytes) | ||
| # PNG files start with: 89 50 4E 47 (‰PNG) | ||
| assert image_bytes[:4] == b"\x89PNG", "Image does not appear to be a valid PNG" | ||
| print("✓ Image is a valid PNG") | ||
|
|
||
| # Verify reasonable image size (should be > 1KB for a 1024x1024 image) | ||
| assert len(image_bytes) > 1024, f"Image seems too small ({len(image_bytes)} bytes)" | ||
| print(f"✓ Image size is reasonable ({len(image_bytes):,} bytes)") | ||
|
|
||
| print("\n✓ Agent successfully generated and returned a valid image") | ||
|
|
||
| # Save the image to a file in the .assets directory (which is .gitignored) | ||
| os.makedirs(".assets", exist_ok=True) | ||
| with open(".assets/generated_image_async.png", "wb") as f: | ||
| f.write(image_bytes) | ||
| print("✓ Image saved to .assets/generated_image_async.png") | ||
|
|
||
| # Teardown | ||
| await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) | ||
| print("Agent deleted") | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.