-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Added the ability to provision AOAI as an optional component #46570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
adaa9a8
fixed cminfra
KrzysztofCwalina 480ee47
openai provisioning works
KrzysztofCwalina 66cf125
open ai client added
KrzysztofCwalina d7c1cf6
progress
KrzysztofCwalina d849600
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
KrzysztofCwalina 55d2365
changed ai to key auth
KrzysztofCwalina 03187da
moved CM to WorkspaceClient abstraction
KrzysztofCwalina 9532953
refactored built-in methods and extension methods
KrzysztofCwalina 1bb89ea
openai works
KrzysztofCwalina 78b8da9
updated api file
KrzysztofCwalina 12c90f4
disabled live tests
KrzysztofCwalina 7d9bf1a
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
KrzysztofCwalina 9efebab
updated version
KrzysztofCwalina ec6bf10
small tweaks
KrzysztofCwalina 0fd2d7a
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
KrzysztofCwalina 596e591
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
KrzysztofCwalina 5d4d362
updated api file
KrzysztofCwalina 29b0754
PR feedback
KrzysztofCwalina b758c5f
removed stj override
KrzysztofCwalina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
sdk/provisioning/Azure.Provisioning.CloudMachine/src/AzureSdkExtensions/KeyVaultFeature.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using Azure.Core; | ||
| using Azure.Provisioning.Authorization; | ||
| using Azure.Provisioning.Expressions; | ||
| using Azure.Provisioning.KeyVault; | ||
| using Azure.Security.KeyVault.Secrets; | ||
|
|
||
| namespace Azure.Provisioning.CloudMachine.KeyVault; | ||
|
|
||
| public class KeyVaultFeature : CloudMachineFeature | ||
| { | ||
| public KeyVaultSku Sku { get; set; } = new KeyVaultSku { Name = KeyVaultSkuName.Standard, Family = KeyVaultSkuFamily.A, }; | ||
|
|
||
| public override void AddTo(CloudMachineInfrastructure infrastructure) | ||
| { | ||
| // Add a KeyVault to the CloudMachine infrastructure. | ||
| KeyVaultService keyVaultResource = new("cm_kv") | ||
| { | ||
| Name = infrastructure.Id, | ||
| Properties = | ||
| new KeyVaultProperties | ||
| { | ||
| Sku = this.Sku, | ||
| TenantId = BicepFunction.GetSubscription().TenantId, | ||
| EnabledForDeployment = true, | ||
| AccessPolicies = [ | ||
| new KeyVaultAccessPolicy() { | ||
| ObjectId = infrastructure.PrincipalIdParameter, | ||
| Permissions = new IdentityAccessPermissions() { | ||
| Secrets = [IdentityAccessSecretPermission.Get, IdentityAccessSecretPermission.Set] | ||
| }, | ||
| TenantId = infrastructure.Identity.TenantId | ||
| } | ||
| ] | ||
| }, | ||
| }; | ||
|
|
||
| infrastructure.AddResource(keyVaultResource); | ||
|
|
||
| RoleAssignment ra = keyVaultResource.CreateRoleAssignment(KeyVaultBuiltInRole.KeyVaultAdministrator, RoleManagementPrincipalType.User, infrastructure.PrincipalIdParameter); | ||
| infrastructure.AddResource(ra); | ||
|
|
||
| // necessary until ResourceName is settable via AssignRole. | ||
| RoleAssignment kvMiRoleAssignment = new RoleAssignment(keyVaultResource.IdentifierName + "_" + infrastructure.Identity.IdentifierName + "_" + KeyVaultBuiltInRole.GetBuiltInRoleName(KeyVaultBuiltInRole.KeyVaultAdministrator)); | ||
| kvMiRoleAssignment.Name = BicepFunction.CreateGuid(keyVaultResource.Id, infrastructure.Identity.Id, BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", KeyVaultBuiltInRole.KeyVaultAdministrator.ToString())); | ||
| kvMiRoleAssignment.Scope = new IdentifierExpression(keyVaultResource.IdentifierName); | ||
| kvMiRoleAssignment.PrincipalType = RoleManagementPrincipalType.ServicePrincipal; | ||
| kvMiRoleAssignment.RoleDefinitionId = BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", KeyVaultBuiltInRole.KeyVaultAdministrator.ToString()); | ||
| kvMiRoleAssignment.PrincipalId = infrastructure.Identity.PrincipalId; | ||
KrzysztofCwalina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| infrastructure.AddResource(kvMiRoleAssignment); | ||
| } | ||
| } | ||
|
|
||
| public static class KeyVaultExtensions | ||
| { | ||
| public static SecretClient GetKeyVaultSecretsClient(this WorkspaceClient workspace) | ||
| { | ||
| ClientConfiguration? connectionMaybe = workspace.GetConfiguration(typeof(SecretClient).FullName); | ||
| if (connectionMaybe == null) throw new Exception("Connection not found"); | ||
KrzysztofCwalina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ClientConfiguration connection = connectionMaybe.Value; | ||
| if (connection.CredentailType == CredentialType.EntraId) | ||
| return new(new Uri(connection.Endpoint), workspace.Credential); | ||
| throw new Exception("ApiKey not supported"); | ||
KrzysztofCwalina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
105 changes: 105 additions & 0 deletions
105
sdk/provisioning/Azure.Provisioning.CloudMachine/src/AzureSdkExtensions/OpenAIFeature.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.ClientModel; | ||
| using System.ClientModel.Primitives; | ||
| using System.Diagnostics.Contracts; | ||
| using Azure.AI.OpenAI; | ||
| using Azure.CloudMachine; | ||
| using Azure.Core; | ||
| using Azure.Provisioning.Authorization; | ||
| using Azure.Provisioning.CognitiveServices; | ||
| using OpenAI.Chat; | ||
|
|
||
| namespace Azure.Provisioning.CloudMachine.OpenAI; | ||
|
|
||
| public class OpenAIFeature : CloudMachineFeature | ||
| { | ||
| public string Model { get; } | ||
| public string ModelVersion { get; } | ||
|
|
||
| public OpenAIFeature(string model, string modelVersion) { Model = model; ModelVersion = modelVersion; } | ||
|
|
||
| public override void AddTo(CloudMachineInfrastructure infrastructure) | ||
| { | ||
| CognitiveServicesAccount cognitiveServices = new("openai", "2023-05-01") | ||
KrzysztofCwalina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| Name = infrastructure.Id, | ||
| Kind = "OpenAI", | ||
| Sku = new CognitiveServicesSku { Name = "S0" }, | ||
| Properties = new CognitiveServicesAccountProperties() | ||
| { | ||
| PublicNetworkAccess = ServiceAccountPublicNetworkAccess.Enabled, | ||
| CustomSubDomainName = infrastructure.Id | ||
| }, | ||
| }; | ||
|
|
||
| infrastructure.AddResource(cognitiveServices.CreateRoleAssignment( | ||
| CognitiveServicesBuiltInRole.CognitiveServicesOpenAIContributor, | ||
| RoleManagementPrincipalType.User, | ||
| infrastructure.PrincipalIdParameter) | ||
| ); | ||
|
|
||
| // TODO: if we every support more than one deployment, they need to be chained using DependsOn. | ||
| // The reason is that deployments need to be deployed/created serially. | ||
| CognitiveServicesAccountDeployment deployment = new("openai_deployment", "2023-05-01") | ||
| { | ||
| Parent = cognitiveServices, | ||
| Name = infrastructure.Id, | ||
| Properties = new CognitiveServicesAccountDeploymentProperties() | ||
| { | ||
| Model = new CognitiveServicesAccountDeploymentModel() { | ||
| Name = this.Model, | ||
| Format = "OpenAI", | ||
| Version = this.ModelVersion | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| infrastructure.AddResource(cognitiveServices); | ||
| infrastructure.AddResource(deployment); | ||
| } | ||
| } | ||
|
|
||
| public static class OpenAIFeatureExtensions | ||
| { | ||
| public static ChatClient GetOpenAIChatClient(this WorkspaceClient workspace) | ||
| { | ||
| string chatClientId = typeof(ChatClient).FullName; | ||
|
|
||
| ChatClient client = workspace.Subclients.Get(chatClientId, () => | ||
| { | ||
| string azureOpenAIClientId = typeof(AzureOpenAIClient).FullName; | ||
|
|
||
| AzureOpenAIClient aoia = workspace.Subclients.Get(azureOpenAIClientId, () => | ||
| { | ||
| ClientConfiguration? connectionMaybe = workspace.GetConfiguration(typeof(AzureOpenAIClient).FullName); | ||
| if (connectionMaybe == null) throw new Exception("Connection not found"); | ||
|
|
||
| ClientConfiguration connection = connectionMaybe.Value; | ||
| Uri endpoint = new(connection.Endpoint); | ||
| var clientOptions = new AzureOpenAIClientOptions(); | ||
| if (connection.CredentailType == CredentialType.EntraId) | ||
| { | ||
| AzureOpenAIClient aoai = new(endpoint, workspace.Credential, clientOptions); | ||
| return aoai; | ||
| } | ||
| else | ||
| { | ||
| AzureOpenAIClient aoai = new(endpoint, new ApiKeyCredential(connection.ApiKey!), clientOptions); | ||
| return aoai; | ||
| } | ||
| }); | ||
|
|
||
| string azureOpenAIChatClientId = typeof(ChatClient).FullName; | ||
| ClientConfiguration? connectionMaybe = workspace.GetConfiguration(azureOpenAIChatClientId); | ||
| if (connectionMaybe == null) throw new Exception("Connection not found"); | ||
| var connection = connectionMaybe.Value; | ||
| ChatClient chat = aoia.GetChatClient(connection.Endpoint); | ||
| return chat; | ||
| }); | ||
|
|
||
| return client; | ||
| } | ||
| } | ||
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...g.CloudMachine/src/CloudMachineFeature.cs → ...hine/src/CDKLevel3/CloudMachineFeature.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.ComponentModel; | ||
|
|
||
| namespace Azure.Provisioning.CloudMachine; | ||
|
|
||
| public abstract class CloudMachineFeature | ||
| { | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public abstract void AddTo(CloudMachineInfrastructure cm); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.