title | titleSuffix | description | manager | ms.service | ms.topic | author | ms.author | ms.date |
---|---|---|---|---|---|---|---|---|
Quickstart: Use Azure OpenAI Service with the JavaScript SDK to generate images |
Azure OpenAI |
Walkthrough on how to get started with Azure OpenAI and make your first image generation call with the JavaScript SDK. |
nitinme |
azure-ai-openai |
include |
PatrickFarley |
pafarley |
08/24/2023 |
Use this guide to get started generating images with the Azure OpenAI SDK for JavaScript.
Library source code | Package (npm) | Samples
- An Azure subscription - Create one for free
- Access granted to the Azure OpenAI service in the desired Azure subscription. Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at https://aka.ms/oai/access.
- LTS versions of Node.js
- An Azure OpenAI resource created in the East US region. For more information, see Create a resource and deploy a model with Azure OpenAI.
Note
Currently, you must submit an application to access Azure OpenAI Service. To apply for access, complete this form. If you need assistance, open an issue on this repo to contact Microsoft.
[!INCLUDE get-key-endpoint]
[!INCLUDE environment-variables]
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it. Then run the npm init
command to create a node application with a package.json file.
npm init
Install the Azure OpenAI client library for JavaScript with npm:
npm install @azure/openai
Your app's package.json file will be updated with the dependencies.
Create a new file named ImageGeneration.js and open it in your preferred code editor. Copy the following code into the ImageGeneration.js file:
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
// You will need to set these environment variables or edit the following values
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] ;
const azureApiKey = process.env["AZURE_OPENAI_API_KEY"] ;
// The prompt to generate images from
const prompt = "a monkey eating a banana";
const size = "256x256";
// The number of images to generate
const n = 2;
async function main() {
console.log("== Batch Image Generation ==");
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
const deploymentName = "dall-e";
const results = await client.getImages(deploymentName, prompt, { n, size });
for (const image of results.data) {
console.log(`Image generation result URL: ${image.url}`);
}
//console.log(`Image generation result URL: ${results.result.status}`);
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Run the script with the following command:
node _ImageGeneration.js
The URL of the generated image is printed to the console.
== Batch Image Generation ==
Image generation result URL: https://dalleproduse.blob.core.windows.net/private/images/5e7536a9-a0b5-4260-8769-2d54106f2913/generated_00.png?se=2023-08-29T19%3A12%3A57Z&sig=655GkWajOZ9ALjFykZF%2FBMZRPQALRhf4UPDImWCQoGI%3D&ske=2023-09-02T18%3A53%3A23Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-08-26T18%3A53%3A23Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02
Image generation result URL: https://dalleproduse.blob.core.windows.net/private/images/5e7536a9-a0b5-4260-8769-2d54106f2913/generated_01.png?se=2023-08-29T19%3A12%3A57Z&sig=B24ymPLSZ3HfG23uojOD9VlRFGxjvgcNmvFo4yPUbEc%3D&ske=2023-09-02T18%3A53%3A23Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-08-26T18%3A53%3A23Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02
Note
The image generation APIs come with a content moderation filter. If the service recognizes your prompt as harmful content, it won't return a generated image. For more information, see the content filter article.
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource. Before deleting the resource, you must first delete any deployed models.
- Explore the image generation APIs in more depth with the DALL-E how-to guide.
- For more examples check out the Azure OpenAI Samples GitHub repository.