Događaj
Izgradite AI aplikacije i agente
17. mar 21 - 21. mar 10
Pridružite se seriji sastanaka kako biste izgradili skalabilna AI rešenja zasnovana na stvarnim slučajevima korišćenja sa kolegama programerima i stručnjacima.
Registrujte se odmahOvaj pregledač više nije podržan.
Nadogradite na Microsoft Edge biste iskoristili najnovije funkcije, bezbednosne ispravke i tehničku podršku.
Napomena
The image generation API creates an image from a text prompt. It doesn't edit or create variations from existing images.
Use this guide to get started generating images with Azure OpenAI in your browser with Azure AI Foundry.
Browse to Azure AI Foundry and sign in with the credentials associated with your Azure OpenAI resource. During or after the sign-in workflow, select the appropriate directory, Azure subscription, and Azure OpenAI resource.
From the Azure AI Foundry landing page, create or select a new project. Navigate to the Models + endpoints page on the left nav. Select Deploy model and then choose one of the DALL-E models from the list. Complete the deployment process.
On the model's page, select Open in playground.
Start exploring Azure OpenAI capabilities with a no-code approach through the Images playground. Enter your image prompt into the text box and select Generate. When the AI-generated image is ready, it appears on the page.
Napomena
The image generation APIs come with a content moderation filter. If Azure OpenAI recognizes your prompt as harmful content, it doesn't return a generated image. For more information, see Content filtering.
In the Images playground, you can also view Python and cURL code samples, which are prefilled according to your settings. Select View code near the top of the page. You can use this code to write an application that completes the same task.
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this guide to get started calling the Azure OpenAI Service image generation REST APIs by using Python.
os
, requests
, json
.dalle3
model with your Azure resource. For more information, see Create a resource and deploy a model with Azure OpenAI.To successfully call the Azure OpenAI APIs, you need the following information about your Azure OpenAI resource:
Variable | Name | Value |
---|---|---|
Endpoint | api_base |
The endpoint value is located under Keys and Endpoint for your resource in the Azure portal. You can also find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
Key | api_key |
The key value is also located under Keys and Endpoint for your resource in the Azure portal. Azure generates two keys for your resource. You can use either value. |
Go to your resource in the Azure portal. On the navigation pane, select Keys and Endpoint under Resource Management. Copy the Endpoint value and an access key value. You can use either the KEY 1 or KEY 2 value. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create and assign persistent environment variables for your key and endpoint.
Važno
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Create a new Python file named quickstart.py. Open the new file in your preferred editor or IDE.
Replace the contents of quickstart.py with the following code. Change the value of prompt
to your preferred text.
You also need to replace <dalle3>
in the URL with the deployment name you chose when you deployed the DALL-E 3 model. Entering the model name will result in an error unless you chose a deployment name that is identical to the underlying model name. If you encounter an error, double check to make sure that you don't have a doubling of the /
at the separation between your endpoint and /openai/deployments
.
import requests
import time
import os
api_base = os.environ['AZURE_OPENAI_ENDPOINT'] # Enter your endpoint here
api_key = os.environ['AZURE_OPENAI_API_KEY'] # Enter your API key here
api_version = '2024-02-01'
url = f"{api_base}/openai/deployments/<dalle3>/images/generations?api-version={api_version}"
headers= { "api-key": api_key, "Content-Type": "application/json" }
body = {
# Enter your prompt text here
"prompt": "A multi-colored umbrella on the beach, disposable camera",
"size": "1024x1024", # supported values are “1792x1024”, “1024x1024” and “1024x1792”
"n": 1, #The number of images to generate. Only n=1 is supported for DALL-E 3.
"quality": "hd", # Options are “hd” and “standard”; defaults to standard
"style": "vivid" # Options are “natural” and “vivid”; defaults to “vivid”
}
submission = requests.post(url, headers=headers, json=body)
image_url = submission.json()['data'][0]['url']
print(image_url)
The script makes a synchronous image generation API call.
Važno
Remember to remove the key from your code when you're done, and never post your key publicly. For production, use a secure way of storing and accessing your credentials. For more information, see Azure Key Vault.
Run the application with the python
command:
python quickstart.py
Wait a few moments to get the response.
The output from a successful image generation API call looks like the following example. The url
field contains a URL where you can download the generated image. The URL stays active for 24 hours.
{
"created": 1698116662,
"data": [
{
"url": "<URL_to_generated_image>",
"revised_prompt": "<prompt_that_was_used>"
}
]
}
The image generation APIs come with a content moderation filter. If the service recognizes your prompt as harmful content, it doesn't generate an image. For more information, see Content filtering. For examples of error responses, see the DALL-E how-to guide.
The system returns an operation status of Failed
and the error.code
value in the message is set to contentFilter
. Here's an example:
{
"created": 1698435368,
"error":
{
"code": "contentFilter",
"message": "Your task failed as a result of our safety system."
}
}
It's also possible that the generated image itself is filtered. In this case, the error message is set to Generated image was filtered as a result of our safety system.
. Here's an example:
{
"created": 1698435368,
"error":
{
"code": "contentFilter",
"message": "Generated image was filtered as a result of our safety system."
}
}
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this guide to get started generating images with the Azure OpenAI SDK for Python.
Library source code | Package | Samples
dalle3
model with your Azure resource. For more information, see Create a resource and deploy a model with Azure OpenAI.To successfully call the Azure OpenAI APIs, you need the following information about your Azure OpenAI resource:
Variable | Name | Value |
---|---|---|
Endpoint | api_base |
The endpoint value is located under Keys and Endpoint for your resource in the Azure portal. You can also find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
Key | api_key |
The key value is also located under Keys and Endpoint for your resource in the Azure portal. Azure generates two keys for your resource. You can use either value. |
Go to your resource in the Azure portal. On the navigation pane, select Keys and Endpoint under Resource Management. Copy the Endpoint value and an access key value. You can use either the KEY 1 or KEY 2 value. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create and assign persistent environment variables for your key and endpoint.
Važno
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Open a command prompt and browse to your project folder. Install the OpenAI Python SDK by using the following command:
pip install openai
Install the following libraries as well:
pip install requests
pip install pillow
Create a new python file, quickstart.py. Open it in your preferred editor or IDE.
Replace the contents of quickstart.py with the following code.
from openai import AzureOpenAI
import os
import requests
from PIL import Image
import json
client = AzureOpenAI(
api_version="2024-02-01",
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT']
)
result = client.images.generate(
model="dalle3", # the name of your DALL-E 3 deployment
prompt="a close-up of a bear walking throughthe forest",
n=1
)
json_response = json.loads(result.model_dump_json())
# Set the directory for the stored image
image_dir = os.path.join(os.curdir, 'images')
# If the directory doesn't exist, create it
if not os.path.isdir(image_dir):
os.mkdir(image_dir)
# Initialize the image path (note the filetype should be png)
image_path = os.path.join(image_dir, 'generated_image.png')
# Retrieve the generated image
image_url = json_response["data"][0]["url"] # extract image URL from response
generated_image = requests.get(image_url).content # download the image
with open(image_path, "wb") as image_file:
image_file.write(generated_image)
# Display the image in the default image viewer
image = Image.open(image_path)
image.show()
prompt
to your preferred text.model
to the name of your deployed DALL-E 3 model.Važno
Remember to remove the key from your code when you're done, and never post your key publicly. For production, use a secure way of storing and accessing your credentials. For more information, see Azure Key Vault.
Run the application with the python
command:
python quickstart.py
Wait a few moments to get the response.
Azure OpenAI stores the output image in the generated_image.png file in your specified directory. The script also displays the image in your default image viewer.
The image generation APIs come with a content moderation filter. If the service recognizes your prompt as harmful content, it doesn't generate an image. For more information, see Content filtering.
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this guide to get started generating images with the Azure OpenAI SDK for C#.
Library source code | Package (NuGet) | Samples
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
API-KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create and assign persistent environment variables for your key and endpoint.
Važno
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
In a console window (such as cmd, PowerShell, or Bash), use the dotnet new
command to create a new console app with the name azure-openai-quickstart
. This command creates a simple "Hello World" project with a single C# source file: Program.cs.
dotnet new console -n azure-openai-quickstart
Change your directory to the newly created app folder. You can build the application with:
dotnet build
The build output should contain no warnings or errors.
...
Build succeeded.
0 Warning(s)
0 Error(s)
...
Install the client library with:
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.6
From the project directory, open the program.cs file and replace the contents with the following code:
using Azure;
using Azure.AI.OpenAI;
using OpenAI.Images;
using static System.Environment;
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
AzureOpenAIClient azureClient = new(
new Uri(endpoint),
new AzureKeyCredential(key));
// This must match the custom deployment name you chose for your model
ImageClient chatClient = azureClient.GetImageClient("dalle-3");
var imageGeneration = await chatClient.GenerateImageAsync(
"a happy monkey sitting in a tree, in watercolor",
new ImageGenerationOptions()
{
Size = GeneratedImageSize.W1024xH1024
}
);
Console.WriteLine(imageGeneration.Value.ImageUri);
Build and run the application from your application directory with these commands:
dotnet build
dotnet run
The URL of the generated image is printed to the console.
<SAS URL>
Napomena
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.
Use this guide to get started generating images with the Azure OpenAI SDK for Java.
Library source code | Artifact (Maven) | Samples
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
API-KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create and assign persistent environment variables for your key and endpoint.
Važno
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Create a new Gradle project.
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
mkdir myapp && cd myapp
Run the gradle init
command from your working directory. This command will create essential build files for Gradle, including build.gradle.kts, which is used at runtime to create and configure your application.
gradle init --type basic
When prompted to choose a DSL, select Kotlin.
This quickstart uses the Gradle dependency manager. You can find the client library and information for other dependency managers on the Maven Central Repository.
Locate build.gradle.kts and open it with your preferred IDE or text editor. Then copy in the following build configuration. This configuration defines the project as a Java application whose entry point is the class OpenAIQuickstart. It imports the Azure AI Vision library.
plugins {
java
application
}
application {
mainClass.set("OpenAIQuickstart")
}
repositories {
mavenCentral()
}
dependencies {
implementation(group = "com.azure", name = "azure-ai-openai", version = "1.0.0-beta.3")
implementation("org.slf4j:slf4j-simple:1.7.9")
}
Create a Java file.
From your working directory, run the following command to create a project source folder:
mkdir -p src/main/java
Navigate to the new folder and create a file called OpenAIQuickstart.java.
Open OpenAIQuickstart.java in your preferred editor or IDE and paste in the following code.
import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.ImageGenerationOptions;
import com.azure.ai.openai.models.ImageLocation;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.models.ResponseError;
import java.util.concurrent.TimeUnit;
/**
* Sample demonstrates how to get the images for a given prompt.
*/
public class OpenAIQuickstart {
/**
* Runs the sample algorithm and demonstrates how to get the images for a given prompt.
*
* @param args Unused. Arguments to the program.
*/
public static void main(String[] args) throws InterruptedException {
// Get key and endpoint from environment variables:
String azureOpenaiKey = System.getenv("AZURE_OPENAI_API_KEY");
String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");
OpenAIAsyncClient client = new OpenAIClientBuilder()
.endpoint(endpoint)
.credential(new AzureKeyCredential(azureOpenaiKey))
.buildAsyncClient();
ImageGenerationOptions imageGenerationOptions = new ImageGenerationOptions(
"A drawing of the Seattle skyline in the style of Van Gogh");
client.getImages(imageGenerationOptions).subscribe(
images -> {
for (ImageLocation imageLocation : images.getData()) {
ResponseError error = imageLocation.getError();
if (error != null) {
System.out.printf("Image generation operation failed. Error code: %s, error message: %s.%n",
error.getCode(), error.getMessage());
} else {
System.out.printf(
"Image location URL that provides temporary access to download the generated image is %s.%n",
imageLocation.getUrl());
}
}
},
error -> System.err.println("There was an error getting images." + error),
() -> System.out.println("Completed getImages."));
// The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep
// the thread so the program does not end before the send operation is complete. Using .block() instead of
// .subscribe() will turn this into a synchronous call.
TimeUnit.SECONDS.sleep(10);
}
}
Navigate back to the project root folder, and build the app with:
gradle build
Then, run it with the gradle run
command:
gradle run
The URL of the generated image is printed to the console.
Image location URL that provides temporary access to download the generated image is <SAS URL>.
Completed getImages.
Napomena
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.
Use this guide to get started generating images with the Azure OpenAI SDK for JavaScript.
Reference documentation | Source code | Package (npm) | Samples
For the recommended keyless authentication with Microsoft Entra ID, you need to:
Cognitive Services User
role to your user account. You can assign roles in the Azure portal under Access control (IAM) > Add role assignment.Create a new folder image-quickstart
to contain the application and open Visual Studio Code in that folder with the following command:
mkdir image-quickstart && cd image-quickstart
Create the package.json
with the following command:
npm init -y
Install the OpenAI client library for JavaScript with:
npm install openai
For the recommended passwordless authentication:
npm install @azure/identity
You need to retrieve the following information to authenticate your application with your Azure OpenAI resource:
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
This value can be found in the Keys and Endpoint section when examining your resource from the Azure portal. |
AZURE_OPENAI_DEPLOYMENT_NAME |
This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Model Deployments in the Azure portal. |
OPENAI_API_VERSION |
Learn more about API Versions. You can change the version in code or use an environment variable. |
Learn more about keyless authentication and setting environment variables.
Oprez
To use the recommended keyless authentication with the SDK, make sure that the AZURE_OPENAI_API_KEY
environment variable isn't set.
Create the index.js
file with the following code:
const { AzureOpenAI } = require("openai");
const {
DefaultAzureCredential,
getBearerTokenProvider
} = require("@azure/identity");
// You will need to set these environment variables or edit the following values
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
// Required Azure OpenAI deployment name and API version
const apiVersion = process.env.OPENAI_API_VERSION || "2024-07-01";
const deploymentName = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "dall-e-3";
// The prompt to generate images from
const prompt = "a monkey eating a banana";
const numberOfImagesToGenerate = 1;
// keyless authentication
const credential = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
function getClient(): AzureOpenAI {
return new AzureOpenAI({
endpoint,
azureADTokenProvider,
apiVersion,
deployment: deploymentName,
});
}
async function main() {
console.log("== Image Generation ==");
const client = getClient();
const results = await client.images.generate({
prompt,
size: "1024x1024",
n: numberOfImagesToGenerate,
model: "",
style: "vivid", // or "natural"
});
for (const image of results.data) {
console.log(`Image generation result URL: ${image.url}`);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Sign in to Azure with the following command:
az login
Run the JavaScript file.
node index.js
The URL of the generated image is printed to the console.
== Batch Image Generation ==
Image generation result URL: <SAS URL>
Image generation result URL: <SAS URL>
Napomena
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.
Use this guide to get started generating images with the Azure OpenAI SDK for JavaScript.
Reference documentation | Source code | Package (npm) | Samples
For the recommended keyless authentication with Microsoft Entra ID, you need to:
Cognitive Services User
role to your user account. You can assign roles in the Azure portal under Access control (IAM) > Add role assignment.Create a new folder image-quickstart
to contain the application and open Visual Studio Code in that folder with the following command:
mkdir image-quickstart && cd image-quickstart
Create the package.json
with the following command:
npm init -y
Update the package.json
to ECMAScript with the following command:
npm pkg set type=module
Install the OpenAI client library for JavaScript with:
npm install openai
For the recommended passwordless authentication:
npm install @azure/identity
You need to retrieve the following information to authenticate your application with your Azure OpenAI resource:
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
This value can be found in the Keys and Endpoint section when examining your resource from the Azure portal. |
AZURE_OPENAI_DEPLOYMENT_NAME |
This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Model Deployments in the Azure portal. |
OPENAI_API_VERSION |
Learn more about API Versions. You can change the version in code or use an environment variable. |
Learn more about keyless authentication and setting environment variables.
Oprez
To use the recommended keyless authentication with the SDK, make sure that the AZURE_OPENAI_API_KEY
environment variable isn't set.
Create the index.ts
file with the following code:
import { AzureOpenAI } from "openai";
import {
DefaultAzureCredential,
getBearerTokenProvider
} from "@azure/identity";
// You will need to set these environment variables or edit the following values
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
// Required Azure OpenAI deployment name and API version
const apiVersion = process.env.OPENAI_API_VERSION || "2024-07-01";
const deploymentName = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "dall-e-3";
// keyless authentication
const credential = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
function getClient(): AzureOpenAI {
return new AzureOpenAI({
endpoint,
azureADTokenProvider,
apiVersion,
deployment: deploymentName,
});
}
async function main() {
console.log("== Image Generation ==");
const client = getClient();
const results = await client.images.generate({
prompt,
size: "1024x1024",
n: numberOfImagesToGenerate,
model: "",
style: "vivid", // or "natural"
});
for (const image of results.data) {
console.log(`Image generation result URL: ${image.url}`);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Create the tsconfig.json
file to transpile the TypeScript code and copy the following code for ECMAScript.
{
"compilerOptions": {
"module": "NodeNext",
"target": "ES2022", // Supports top-level await
"moduleResolution": "NodeNext",
"skipLibCheck": true, // Avoid type errors from node_modules
"strict": true // Enable strict type-checking options
},
"include": ["*.ts"]
}
Transpile from TypeScript to JavaScript.
tsc
Sign in to Azure with the following command:
az login
Run the code with the following command:
node index.js
The URL of the generated image is printed to the console.
== Batch Image Generation ==
Image generation result URL: <SAS URL>
Image generation result URL: <SAS URL>
Napomena
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.
Use this guide to get started generating images with the Azure OpenAI SDK for Go.
Library source code | Package | Samples
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
API-KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create and assign persistent environment variables for your key and endpoint.
Važno
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Open the command prompt and navigate to your project folder. Create a new file sample.go.
Install the OpenAI Go SDK using the following command:
go get github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai@latest
Or, if you use dep
, within your repo run:
dep ensure -add github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai
Open sample.go in your preferred code editor.
Add the following code to your script:
package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)
func main() {
azureOpenAIKey := os.Getenv("AZURE_OPENAI_API_KEY")
// Ex: "https://<your-azure-openai-host>.openai.azure.com"
azureOpenAIEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
if azureOpenAIKey == "" || azureOpenAIEndpoint == "" {
fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
return
}
keyCredential := azcore.NewKeyCredential(azureOpenAIKey)
client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)
if err != nil {
// handle error
}
resp, err := client.GetImageGenerations(context.TODO(), azopenai.ImageGenerationOptions{
Prompt: to.Ptr("a painting of a cat in the style of Dali"),
ResponseFormat: to.Ptr(azopenai.ImageGenerationResponseFormatURL),
}, nil)
if err != nil {
// handle error
}
for _, generatedImage := range resp.Data {
// the underlying type for the generatedImage is dictated by the value of
// ImageGenerationOptions.ResponseFormat. In this example we used `azopenai.ImageGenerationResponseFormatURL`,
// so the underlying type will be ImageLocation.
resp, err := http.Head(*generatedImage.URL)
if err != nil {
// handle error
}
fmt.Fprintf(os.Stderr, "Image generated, HEAD request on URL returned %d\nImage URL: %s\n", resp.StatusCode, *generatedImage.URL)
}
}
Run the script using the go run
command:
go run sample.go
The URL of the generated image is printed to the console.
Image generated, HEAD request on URL returned 200
Image URL: <SAS URL>
Napomena
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 or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this guide to get started calling the Azure OpenAI Service image generation APIs with PowerShell.
Napomena
The image generation API creates an image from a text prompt. It doesn't edit or create variations of existing images.
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
API-KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create and assign persistent environment variables for your key and endpoint.
Važno
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Create a new PowerShell file named quickstart.ps1. Open the new file in your preferred editor or IDE.
Replace the contents of quickstart.ps1 with the following code. Enter your endpoint URL and key in the appropriate fields. Change the value of prompt
to your preferred text.
# Azure OpenAI metadata variables
$openai = @{
api_key = $Env:AZURE_OPENAI_API_KEY
api_base = $Env:AZURE_OPENAI_ENDPOINT # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
api_version = '2023-06-01-preview' # this may change in the future
}
# Text to describe image
$prompt = 'A painting of a dog'
# Header for authentication
$headers = [ordered]@{
'api-key' = $openai.api_key
}
# Adjust these values to fine-tune completions
$body = [ordered]@{
prompt = $prompt
size = '1024x1024'
n = 1
} | ConvertTo-Json
# Call the API to generate the image and retrieve the response
$url = "$($openai.api_base)/openai/images/generations:submit?api-version=$($openai.api_version)"
$submission = Invoke-RestMethod -Uri $url -Headers $headers -Body $body -Method Post -ContentType 'application/json' -ResponseHeadersVariable submissionHeaders
$operation_location = $submissionHeaders['operation-location'][0]
$status = ''
while ($status -ne 'succeeded') {
Start-Sleep -Seconds 1
$response = Invoke-RestMethod -Uri $operation_location -Headers $headers
$status = $response.status
}
# Set the directory for the stored image
$image_dir = Join-Path -Path $pwd -ChildPath 'images'
# If the directory doesn't exist, create it
if (-not(Resolve-Path $image_dir -ErrorAction Ignore)) {
New-Item -Path $image_dir -ItemType Directory
}
# Initialize the image path (note the filetype should be png)
$image_path = Join-Path -Path $image_dir -ChildPath 'generated_image.png'
# Retrieve the generated image
$image_url = $response.result.data[0].url # extract image URL from response
$generated_image = Invoke-WebRequest -Uri $image_url -OutFile $image_path # download the image
return $image_path
Važno
For production, use a secure way of storing and accessing your credentials like The PowerShell Secret Management with Azure Key Vault. For more information about credential security, see the Azure AI services security article.
Run the script using PowerShell:
./quickstart.ps1
The script loops until the generated image is ready.
PowerShell requests the image from Azure OpenAI and stores the output image in the generated_image.png file in your specified directory. For convenience, the full path for the file is returned at the end of the script.
The image generation APIs come with a content moderation filter. If the service recognizes your prompt as harmful content, it doesn't generate an image. For more information, see Content filtering.
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Događaj
Izgradite AI aplikacije i agente
17. mar 21 - 21. mar 10
Pridružite se seriji sastanaka kako biste izgradili skalabilna AI rešenja zasnovana na stvarnim slučajevima korišćenja sa kolegama programerima i stručnjacima.
Registrujte se odmahObuka
Modul
Get started with Azure OpenAI Service - Training
Azure OpenAI Service enables engineers to build enterprise-grade generative AI solutions.
Certifikacija
Microsoft Certified: Azure AI Fundamentals - Certifications
Demonstrate fundamental AI concepts related to the development of software and services of Microsoft Azure to create AI solutions.