|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import aiofiles |
| 7 | +from agent_framework import DataContent |
| 8 | +from agent_framework.azure import AzureAIClient |
| 9 | +from azure.ai.projects.models import ImageGenTool |
| 10 | +from azure.identity.aio import AzureCliCredential |
| 11 | + |
| 12 | +""" |
| 13 | +Azure AI Agent With Image Generation |
| 14 | +
|
| 15 | +This sample demonstrates basic usage of AzureAIClient to create an agent |
| 16 | +that can generate images based on user requirements. |
| 17 | +
|
| 18 | +Pre-requisites: |
| 19 | +- Make sure to set up the AZURE_AI_PROJECT_ENDPOINT and AZURE_AI_MODEL_DEPLOYMENT_NAME |
| 20 | + environment variables before running this sample. |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +async def main() -> None: |
| 25 | + # Since no Agent ID is provided, the agent will be automatically created. |
| 26 | + # For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred |
| 27 | + # authentication option. |
| 28 | + async with ( |
| 29 | + AzureCliCredential() as credential, |
| 30 | + AzureAIClient(async_credential=credential).create_agent( |
| 31 | + name="ImageGenAgent", |
| 32 | + instructions="Generate images based on user requirements.", |
| 33 | + tools=[ImageGenTool(quality="low", size="1024x1024")], |
| 34 | + ) as agent, |
| 35 | + ): |
| 36 | + query = "Generate an image of Microsoft logo." |
| 37 | + print(f"User: {query}") |
| 38 | + result = await agent.run( |
| 39 | + query, |
| 40 | + # These additional options are required for image generation |
| 41 | + additional_chat_options={ |
| 42 | + "extra_headers": {"x-ms-oai-image-generation-deployment": "gpt-image-1"}, |
| 43 | + }, |
| 44 | + ) |
| 45 | + print(f"Agent: {result}\n") |
| 46 | + |
| 47 | + # Save the image to a file |
| 48 | + print("Downloading generated image...") |
| 49 | + image_data = [ |
| 50 | + content |
| 51 | + for content in result.messages[0].contents |
| 52 | + if isinstance(content, DataContent) and content.media_type == "image/png" |
| 53 | + ] |
| 54 | + if image_data and image_data[0]: |
| 55 | + # Save to the same directory as this script |
| 56 | + filename = "microsoft.png" |
| 57 | + current_dir = Path(__file__).parent.resolve() |
| 58 | + file_path = current_dir / filename |
| 59 | + async with aiofiles.open(file_path, "wb") as f: |
| 60 | + await f.write(image_data[0].get_data_bytes()) |
| 61 | + |
| 62 | + print(f"Image downloaded and saved to: {file_path}") |
| 63 | + else: |
| 64 | + print("No image data found in the agent response.") |
| 65 | + |
| 66 | + """ |
| 67 | + Sample output: |
| 68 | + User: Generate an image of Microsoft logo. |
| 69 | + Agent: Here is the Microsoft logo image featuring its iconic four quadrants. |
| 70 | +
|
| 71 | + Downloading generated image... |
| 72 | + Image downloaded and saved to: .../microsoft.png |
| 73 | + """ |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + asyncio.run(main()) |
0 commit comments