Skip to content

Commit 2990f68

Browse files
committed
aoai via entra quickstarts in powershell
1 parent ec1ab35 commit 2990f68

File tree

2 files changed

+60
-49
lines changed

2 files changed

+60
-49
lines changed

articles/ai-services/openai/includes/chatgpt-powershell.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
8585
return $response
8686
```
8787

88+
> [!IMPORTANT]
89+
> For production, use a secure way of storing and accessing your credentials like [The PowerShell Secret Management with Azure Key Vault](/powershell/utility-modules/secretmanagement/how-to/using-azure-keyvault). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
90+
8891
1. Run the script using PowerShell. In this example, we're using the `-Depth` parameter to ensure that the output isn't truncated.
8992

9093
```powershell

articles/ai-services/openai/includes/dall-e-powershell.md

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
title: "Quickstart: Generate images with Azure OpenAI Service using PowerShell"
33
titleSuffix: Azure OpenAI Service
44
description: Learn how to generate images with Azure OpenAI Service by using PowerShell and the endpoint and access keys for your Azure OpenAI resource.
5-
#services: cognitive-services
65
manager: nitinme
76
ms.service: azure-ai-openai
87
ms.topic: include
9-
ms.date: 08/29/2023
8+
ms.date: 3/21/2025
109
---
1110

1211
Use this guide to get started calling the Azure OpenAI Service image generation APIs with PowerShell.
@@ -21,70 +20,79 @@ Use this guide to get started calling the Azure OpenAI Service image generation
2120
- An Azure OpenAI resource created in a supported region (see [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability)). For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md).
2221

2322

24-
## Setup
23+
### Microsoft Entra ID prerequisites
2524

26-
[!INCLUDE [get-key-endpoint](get-key-endpoint.md)]
25+
For the recommended keyless authentication with Microsoft Entra ID, you need to:
26+
- Install the [Azure CLI](/cli/azure/install-azure-cli) used for keyless authentication with Microsoft Entra ID.
27+
- Assign the `Cognitive Services User` role to your user account. You can assign roles in the Azure portal under **Access control (IAM)** > **Add role assignment**.
2728

28-
[!INCLUDE [environment-variables](environment-variables.md)]
29+
## Retrieve resource information
2930

31+
[!INCLUDE [resource authentication](resource-authentication.md)]
3032

31-
## Generate images with DALL-E 2
33+
## Generate images
3234

33-
1. Create a new PowerShell file named _quickstart.ps1_. Open the new file in your preferred editor or IDE.
35+
1. For the **recommended** keyless authentication with Microsoft Entra ID, sign in to Azure with the following command:
36+
37+
```console
38+
az login
39+
```
40+
41+
1. Create a new PowerShell file called *quickstart.ps1*. Then open it up in your preferred editor or IDE.
3442

3543
1. 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.
3644

3745
```powershell
38-
# Azure OpenAI metadata variables
39-
$openai = @{
40-
api_key = $Env:AZURE_OPENAI_API_KEY
41-
api_base = $Env:AZURE_OPENAI_ENDPOINT # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
42-
api_version = '2023-06-01-preview' # this may change in the future
43-
}
44-
45-
# Text to describe image
46-
$prompt = 'A painting of a dog'
47-
48-
# Header for authentication
49-
$headers = [ordered]@{
50-
'api-key' = $openai.api_key
51-
}
52-
53-
# Adjust these values to fine-tune completions
54-
$body = [ordered]@{
55-
prompt = $prompt
56-
size = '1024x1024'
57-
n = 1
58-
} | ConvertTo-Json
59-
46+
# Azure OpenAI metadata variables
47+
$openai = @{
48+
api_base = $Env:AZURE_OPENAI_ENDPOINT
49+
api_version = '2023-06-01-preview' # This can change in the future.
50+
}
51+
52+
# Use the recommended keyless authentication via bearer token.
53+
$headers = [ordered]@{
54+
#'api-key' = $Env:AZURE_OPENAI_API_KEY
55+
'Authorization' = "Bearer $($Env:DEFAULT_AZURE_CREDENTIAL_TOKEN)"
56+
}
57+
58+
# Text to describe image
59+
$prompt = 'A painting of a dog'
60+
61+
# Adjust these values to fine-tune completions
62+
$body = [ordered]@{
63+
prompt = $prompt
64+
size = '1024x1024'
65+
n = 1
66+
} | ConvertTo-Json
67+
6068
# Call the API to generate the image and retrieve the response
61-
$url = "$($openai.api_base)/openai/images/generations:submit?api-version=$($openai.api_version)"
62-
63-
$submission = Invoke-RestMethod -Uri $url -Headers $headers -Body $body -Method Post -ContentType 'application/json' -ResponseHeadersVariable submissionHeaders
64-
69+
$url = "$($openai.api_base)/openai/images/generations:submit?api-version=$($openai.api_version)"
70+
71+
$submission = Invoke-RestMethod -Uri $url -Headers $headers -Body $body -Method Post -ContentType 'application/json' -ResponseHeadersVariable submissionHeaders
72+
6573
$operation_location = $submissionHeaders['operation-location'][0]
6674
$status = ''
6775
while ($status -ne 'succeeded') {
6876
Start-Sleep -Seconds 1
6977
$response = Invoke-RestMethod -Uri $operation_location -Headers $headers
7078
$status = $response.status
7179
}
72-
73-
# Set the directory for the stored image
74-
$image_dir = Join-Path -Path $pwd -ChildPath 'images'
75-
76-
# If the directory doesn't exist, create it
77-
if (-not(Resolve-Path $image_dir -ErrorAction Ignore)) {
78-
New-Item -Path $image_dir -ItemType Directory
79-
}
80-
81-
# Initialize the image path (note the filetype should be png)
82-
$image_path = Join-Path -Path $image_dir -ChildPath 'generated_image.png'
83-
84-
# Retrieve the generated image
85-
$image_url = $response.result.data[0].url # extract image URL from response
86-
$generated_image = Invoke-WebRequest -Uri $image_url -OutFile $image_path # download the image
87-
return $image_path
80+
81+
# Set the directory for the stored image
82+
$image_dir = Join-Path -Path $pwd -ChildPath 'images'
83+
84+
# If the directory doesn't exist, create it
85+
if (-not(Resolve-Path $image_dir -ErrorAction Ignore)) {
86+
New-Item -Path $image_dir -ItemType Directory
87+
}
88+
89+
# Initialize the image path (note the filetype should be png)
90+
$image_path = Join-Path -Path $image_dir -ChildPath 'generated_image.png'
91+
92+
# Retrieve the generated image
93+
$image_url = $response.result.data[0].url # extract image URL from response
94+
$generated_image = Invoke-WebRequest -Uri $image_url -OutFile $image_path # download the image
95+
return $image_path
8896
```
8997

9098
> [!IMPORTANT]

0 commit comments

Comments
 (0)