Skip to content

Commit c336956

Browse files
committed
using Entra ID for AOAI
1 parent ed2801e commit c336956

File tree

5 files changed

+162
-15
lines changed

5 files changed

+162
-15
lines changed

OutputCacheDallESample/GenerateImageSC.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Redis.OM.Vectorizers;
55
using Microsoft.AspNetCore.DataProtection.KeyManagement;
66
using OpenAI.Images;
7+
using Azure.Identity;
78

89
namespace OutputCacheDallESample
910
{
@@ -29,7 +30,8 @@ await context.Response.WriteAsync("<!DOCTYPE html><html><body> " +
2930
}
3031
else
3132
{
32-
AzureOpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
33+
// AzureOpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
34+
AzureOpenAIClient client = new(new Uri(endpoint), new DefaultAzureCredential());
3335

3436
ImageClient imageClient = client.GetImageClient("dall-e-3");
3537
GeneratedImage generatedImage = await imageClient.GenerateImageAsync(_prompt, new ImageGenerationOptions() {

OutputCacheDallESample/GenerateImageSDK.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Azure.AI.OpenAI;
2+
using Azure.Identity;
23
using Azure;
34
using Microsoft.AspNetCore.DataProtection.KeyManagement;
45
using OpenAI.Images;
@@ -12,7 +13,9 @@ public static async Task GenerateImageSDKAsync(HttpContext context, string _prom
1213
string endpoint = _config["AZURE_OPENAI_ENDPOINT"];
1314
string key = _config["apiKey"];
1415

15-
AzureOpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
16+
// AzureOpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
17+
18+
AzureOpenAIClient client = new(new Uri(endpoint), new DefaultAzureCredential());
1619

1720
ImageClient imageClient = client.GetImageClient("dall-e-3");
1821
GeneratedImage generatedImage = await imageClient.GenerateImageAsync(_prompt, new ImageGenerationOptions()

OutputCacheDallESample/OutputCacheDallESample.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
13-
<PackageReference Include="Microsoft.AspNetCore.OutputCaching.StackExchangeRedis" Version="9.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0" />
13+
<PackageReference Include="Azure.Identity" Version="1.13.2" />
14+
<PackageReference Include="Microsoft.AspNetCore.OutputCaching.StackExchangeRedis" Version="9.0.1" />
15+
<PackageReference Include="Microsoft.Azure.StackExchangeRedis" Version="3.2.1" />
16+
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.1" />
1517
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
1618
<PackageReference Include="Redis.OM" Version="0.7.6" />
1719
<PackageReference Include="Redis.OM.Vectorizers" Version="0.7.6" />

OutputCacheDallESample/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.OutputCaching;
22
using OutputCacheDallESample;
3+
using Azure.AI.OpenAI;
34
using Redis.OM;
45
using Redis.OM.Vectorizers;
56

infra/app/OutputCacheDallESample.bicep

Lines changed: 150 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,100 @@ module fetchLatestImage '../modules/fetch-container-image.bicep' = {
7171
}
7272
}
7373

74-
resource app 'Microsoft.App/containerApps@2023-04-01-preview' = {
74+
resource keyVault 'Microsoft.KeyVault/vaults@2021-06-01-preview' = {
75+
name: '${substring(name, 0, 15)}-keyvault'
76+
location: location
77+
properties: {
78+
sku: {
79+
family: 'A'
80+
name: 'standard'
81+
}
82+
tenantId: subscription().tenantId
83+
accessPolicies: [
84+
{
85+
tenantId: subscription().tenantId
86+
objectId: app.identity.principalId
87+
permissions: {
88+
secrets: [
89+
'get'
90+
]
91+
}
92+
}
93+
]
94+
}
95+
}
96+
97+
resource applicationinsights__connectionstring 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
98+
parent: keyVault
99+
name: 'applicationinsights--connectionstring'
100+
properties: {
101+
value: applicationInsights.properties.ConnectionString
102+
}
103+
}
104+
105+
resource apiKey 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
106+
parent: keyVault
107+
name: 'apiKey'
108+
properties:{
109+
value: cognitiveAccount.listKeys().key1
110+
}
111+
}
112+
113+
resource azure__openai__endpoint 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
114+
parent: keyVault
115+
name: 'azure--openai--endpoint'
116+
properties:{
117+
value: cognitiveAccount.properties.endpoint
118+
}
119+
}
120+
121+
resource aoai__resourcename 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
122+
parent: keyVault
123+
name: 'aoai--resourcename'
124+
properties:{
125+
value: cognitiveAccount.name
126+
}
127+
}
128+
129+
resource aoai__embedding__deploymentname 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
130+
parent: keyVault
131+
name: 'aoai--embedding--deploymentname'
132+
properties: {
133+
value: '${name}-textembedding'
134+
}
135+
}
136+
137+
resource api__url 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
138+
parent: keyVault
139+
name: 'api--url'
140+
properties:{
141+
value: '${cognitiveAccount.properties.endpoint}openai/deployments/Dalle3/images/generations?api-version=2024-02-01'
142+
}
143+
}
144+
145+
resource redis__cache__connection 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
146+
parent: keyVault
147+
name: 'redis--cache--connection'
148+
properties: {
149+
value: '${redisCache.properties.hostName}:10000,password=${redisdatabase.listKeys().primaryKey},ssl=True,abortConnect=False'
150+
}
151+
}
152+
153+
resource semantic__cache__azure__provider 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
154+
parent: keyVault
155+
name: 'semantic--cache--azure--provider'
156+
properties: {
157+
value: 'rediss://:${redisdatabase.listKeys().primaryKey}@${redisCache.properties.hostName}:10000'
158+
}
159+
}
160+
161+
resource app 'Microsoft.App/containerApps@2024-03-01' = {
75162
name: name
76163
location: location
77164
tags: union(tags, {'azd-service-name': 'OutputCacheDallESample' })
78165
dependsOn: [ acrPullRole]
79166
identity: {
80-
type: 'UserAssigned'
167+
type: 'UserAssigned, SystemAssigned'
81168
userAssignedIdentities: { '${identity.id}': {} }
82169
}
83170
properties: {
@@ -95,6 +182,38 @@ resource app 'Microsoft.App/containerApps@2023-04-01-preview' = {
95182
}
96183
]
97184
secrets: [
185+
{
186+
name: 'applicationinsights--connectionstring'
187+
value: applicationInsights.properties.ConnectionString
188+
}
189+
{
190+
name: 'api--key'
191+
value: cognitiveAccount.listKeys().key1
192+
}
193+
{
194+
name: 'azure--openai--endpoint'
195+
value: cognitiveAccount.properties.endpoint
196+
}
197+
{
198+
name: 'aoai--resourcename'
199+
value: cognitiveAccount.name
200+
}
201+
{
202+
name: 'aoai--embedding--deploymentname'
203+
value: '${name}-textembedding'
204+
}
205+
{
206+
name: 'api--url'
207+
value: '${cognitiveAccount.properties.endpoint}openai/deployments/Dalle3/images/generations?api-version=2024-02-01'
208+
}
209+
{
210+
name: 'redis--cache--connection'
211+
value: '${redisCache.properties.hostName}:10000,password=${redisdatabase.listKeys().primaryKey},ssl=True,abortConnect=False'
212+
}
213+
{
214+
name: 'semantic--cache--azure--provider'
215+
value: 'rediss://:${redisdatabase.listKeys().primaryKey}@${redisCache.properties.hostName}:10000'
216+
}
98217
]
99218
}
100219
template: {
@@ -105,39 +224,39 @@ resource app 'Microsoft.App/containerApps@2023-04-01-preview' = {
105224
env: [
106225
{
107226
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
108-
value: applicationInsights.properties.ConnectionString
227+
secretRef: 'applicationinsights--connectionstring'
109228
}
110229
{
111230
name: 'PORT'
112231
value: '8080'
113232
}
114233
{
115234
name: 'apiKey'
116-
value: cognitiveAccount.listKeys().key1
235+
secretRef: 'api--key'
117236
}
118237
{
119238
name: 'AZURE_OPENAI_ENDPOINT'
120-
value: cognitiveAccount.properties.endpoint
239+
secretRef: 'azure--openai--endpoint'
121240
}
122241
{
123242
name: 'AOAIResourceName'
124-
value: cognitiveAccount.name
243+
secretRef: 'aoai--resourcename'
125244
}
126245
{
127246
name: 'AOAIEmbeddingDeploymentName'
128-
value: '${name}-textembedding'
247+
secretRef: 'aoai--embedding--deploymentname'
129248
}
130249
{
131250
name: 'apiUrl'
132-
value: '${cognitiveAccount.properties.endpoint}openai/deployments/Dalle3/images/generations?api-version=2024-02-01'
251+
secretRef: 'api--url'
133252
}
134253
{
135254
name: 'RedisCacheConnection'
136-
value: '${redisCache.properties.hostName}:10000,password=${redisdatabase.listKeys().primaryKey},ssl=True,abortConnect=False'
255+
secretRef: 'redis--cache--connection'
137256
}
138257
{
139258
name:'SemanticCacheAzureProvider'
140-
value: 'rediss://:${redisdatabase.listKeys().primaryKey}@${redisCache.properties.hostName}:10000'
259+
secretRef: 'semantic--cache--azure--provider'
141260
}
142261
]
143262
resources: {
@@ -188,13 +307,33 @@ resource model 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [
188307
}
189308
}]
190309

310+
resource openai_CognitiveServicesOpenAIUser 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
311+
name: guid(cognitiveAccount.id, identity.id, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd'))
312+
properties: {
313+
principalId: identity.properties.principalId
314+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd')
315+
principalType: 'ServicePrincipal'
316+
}
317+
scope: cognitiveAccount
318+
}
319+
320+
resource openai_CognitiveServicesOpenAIContributor 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
321+
name: guid(cognitiveAccount.id, app.id, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'a001fd3d-188f-4b5d-821b-7da978bf7442'))
322+
properties: {
323+
principalId: app.identity.principalId
324+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'a001fd3d-188f-4b5d-821b-7da978bf7442')
325+
principalType: 'ServicePrincipal'
326+
}
327+
scope: cognitiveAccount
328+
}
329+
191330
//azure cache for redis resource
192331
resource redisCache 'Microsoft.Cache/redisEnterprise@2024-02-01' = {
193332
location:location
194333
name: '${name}-rediscache'
195334
sku:{
196335
capacity:2
197-
name: 'Enterprise_E10'
336+
name: 'Enterprise_E1'
198337
}
199338
}
200339
resource redisdatabase 'Microsoft.Cache/redisEnterprise/databases@2024-02-01' = {

0 commit comments

Comments
 (0)