Skip to content
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

Simplified OpenAI Provisioning #47174

Merged
merged 5 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public void AddResource(Azure.Provisioning.Primitives.NamedProvisionableConstruc
public partial class FeatureCollection
{
public FeatureCollection() { }
public void Add(Azure.Provisioning.CloudMachine.CloudMachineFeature item) { }
public bool TryFind<T>(out T? found) where T : Azure.Provisioning.CloudMachine.CloudMachineFeature { throw null; }
public System.Collections.Generic.IEnumerable<T> FindAll<T>() where T : Azure.Provisioning.CloudMachine.CloudMachineFeature { throw null; }
}
}
namespace Azure.CloudMachine.KeyVault
Expand All @@ -53,7 +52,7 @@ public partial class KeyVaultFeature : Azure.Provisioning.CloudMachine.CloudMach
{
public KeyVaultFeature(Azure.Provisioning.KeyVault.KeyVaultSku? sku = null) { }
public Azure.Provisioning.KeyVault.KeyVaultSku Sku { get { throw null; } set { } }
public override void AddTo(Azure.CloudMachine.CloudMachineInfrastructure infrastructure) { }
protected override Azure.Provisioning.Primitives.ProvisionableResource EmitCore(Azure.CloudMachine.CloudMachineInfrastructure infrastructure) { throw null; }
}
}
namespace Azure.CloudMachine.OpenAI
Expand All @@ -66,10 +65,10 @@ public enum AIModelKind
public partial class OpenAIModel : Azure.Provisioning.CloudMachine.CloudMachineFeature
{
public OpenAIModel(string model, string modelVersion, Azure.CloudMachine.OpenAI.AIModelKind kind = Azure.CloudMachine.OpenAI.AIModelKind.Chat) { }
public Azure.Provisioning.CognitiveServices.CognitiveServicesAccount? CognitiveServices { get { throw null; } set { } }
public string Model { get { throw null; } }
public string ModelVersion { get { throw null; } }
public override void AddTo(Azure.CloudMachine.CloudMachineInfrastructure cm) { }
protected override Azure.Provisioning.Primitives.ProvisionableResource EmitCore(Azure.CloudMachine.CloudMachineInfrastructure cm) { throw null; }
}
}
namespace Azure.Provisioning.CloudMachine
Expand All @@ -78,7 +77,12 @@ public abstract partial class CloudMachineFeature
{
protected CloudMachineFeature() { }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public abstract void AddTo(Azure.CloudMachine.CloudMachineInfrastructure cm);
public Azure.Provisioning.Primitives.ProvisionableResource Emited { get { throw null; } protected set { } }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public virtual void AddTo(Azure.CloudMachine.CloudMachineInfrastructure cm) { }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public void Emit(Azure.CloudMachine.CloudMachineInfrastructure cm) { }
protected abstract Azure.Provisioning.Primitives.ProvisionableResource EmitCore(Azure.CloudMachine.CloudMachineInfrastructure cm);
}
}
namespace System.ClientModel.TypeSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Azure.Provisioning.CloudMachine;
using Azure.Provisioning.Expressions;
using Azure.Provisioning.KeyVault;
using Azure.Provisioning.Primitives;

namespace Azure.CloudMachine.KeyVault;

Expand All @@ -20,7 +21,7 @@ public KeyVaultFeature(KeyVaultSku? sku = default)
}
Sku = sku;
}
public override void AddTo(CloudMachineInfrastructure infrastructure)
protected override ProvisionableResource EmitCore(CloudMachineInfrastructure infrastructure)
{
// Add a KeyVault to the CloudMachine infrastructure.
KeyVaultService keyVaultResource = new("cm_kv")
Expand Down Expand Up @@ -57,5 +58,7 @@ public override void AddTo(CloudMachineInfrastructure infrastructure)
kvMiRoleAssignment.RoleDefinitionId = BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", KeyVaultBuiltInRole.KeyVaultAdministrator.ToString());
kvMiRoleAssignment.PrincipalId = infrastructure.Identity.PrincipalId;
infrastructure.AddResource(kvMiRoleAssignment);

return keyVaultResource;
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using Azure.Provisioning.Authorization;
using Azure.Provisioning.CloudMachine;
using Azure.Provisioning.CognitiveServices;
using Azure.Provisioning.Primitives;

namespace Azure.CloudMachine.OpenAI;

internal class OpenAIFeature : CloudMachineFeature
{
private List<OpenAIModel> _models = new List<OpenAIModel>();

public OpenAIFeature()
{
}
{}

public override void AddTo(CloudMachineInfrastructure cloudMachine)
protected override ProvisionableResource EmitCore(CloudMachineInfrastructure cloudMachine)
{
CognitiveServicesAccount cognitiveServices = CreateAccount(cloudMachine);
CognitiveServicesAccount cognitiveServices = CreateOpenAIAccount(cloudMachine);
cloudMachine.AddResource(cognitiveServices);

cloudMachine.AddResource(cognitiveServices.CreateRoleAssignment(
CognitiveServicesBuiltInRole.CognitiveServicesOpenAIContributor,
RoleManagementPrincipalType.User,
cloudMachine.PrincipalIdParameter)
);

Emited = cognitiveServices;

OpenAIModel? previous = null;
foreach (OpenAIModel model in _models)
{
model.Emit(cloudMachine);
if (previous != null)
{
model.Emited.DependsOn.Add(previous.Emited);
}
previous = model;
}

return cognitiveServices;
}

internal void AddModel(OpenAIModel model)
{
if (model.Account!= null)
{
throw new InvalidOperationException("Model already added to an account");
}
model.Account = this;
_models.Add(model);
}

internal CognitiveServicesAccount CreateAccount(CloudMachineInfrastructure cm)
internal CognitiveServicesAccount CreateOpenAIAccount(CloudMachineInfrastructure cm)
{
return new("openai")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using Azure.Provisioning.CloudMachine;
using Azure.Provisioning.CognitiveServices;
using Azure.Provisioning.Primitives;

namespace Azure.CloudMachine.OpenAI;

public enum AIModelKind
{
Chat,
Embedding,
}
public class OpenAIModel : CloudMachineFeature
{
public OpenAIModel(string model, string modelVersion, AIModelKind kind = AIModelKind.Chat) {
Expand All @@ -22,10 +19,28 @@ public OpenAIModel(string model, string modelVersion, AIModelKind kind = AIModel

public string Model { get; }
public string ModelVersion { get; }
private AIModelKind Kind { get; } // has to be $"{cloudMachine.Id}_embedding" or {cm.Id}
public CognitiveServicesAccount? CognitiveServices { get; set; } = default;
private AIModelKind Kind { get; }

internal OpenAIFeature Account { get; set; } = default!;

private OpenAIFeature GetOrCreateOpenAI(CloudMachineInfrastructure cm)
{
foreach (OpenAIFeature feature in cm.Features.FindAll<OpenAIFeature>())
{
return feature;
}
var openAI = new OpenAIFeature();
cm.AddFeature(openAI);
return openAI;
}

public override void AddTo(CloudMachineInfrastructure cm)
{
OpenAIFeature openAI = GetOrCreateOpenAI(cm);
KrzysztofCwalina marked this conversation as resolved.
Show resolved Hide resolved
openAI.AddModel(this);
}

protected override ProvisionableResource EmitCore(CloudMachineInfrastructure cm)
{
string name = Kind switch
{
Expand All @@ -34,15 +49,13 @@ public override void AddTo(CloudMachineInfrastructure cm)
_ => throw new NotImplementedException()
};

var parent = CognitiveServices;
if (parent == default)
Debug.Assert(Account != null);
var emitted = Account!.Emited;
if (emitted == null)
{
if (!cm.Features.TryFind(out OpenAIFeature? openAI)) {
openAI = new OpenAIFeature();
cm.AddFeature(openAI);
}
parent = openAI!.CreateAccount(cm);
Account.Emit(cm);
}
CognitiveServicesAccount parent = (CognitiveServicesAccount)Account!.Emited;

CognitiveServicesAccountDeployment deployment = new($"openai_{name}", "2024-06-01-preview") {
Parent = parent,
Expand All @@ -64,6 +77,15 @@ public override void AddTo(CloudMachineInfrastructure cm)
Name = "Standard"
}
};

cm.AddResource(deployment);

return deployment;
}
}

public enum AIModelKind
{
Chat,
Embedding,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@

using System.ComponentModel;
using Azure.CloudMachine;
using Azure.Provisioning.Primitives;

namespace Azure.Provisioning.CloudMachine;

public abstract class CloudMachineFeature
{
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract void AddTo(CloudMachineInfrastructure cm);
public virtual void AddTo(CloudMachineInfrastructure cm) => cm.Features.Add(this);

[EditorBrowsable(EditorBrowsableState.Never)]
public void Emit(CloudMachineInfrastructure cm)
{
if (Emited != null) return;
ProvisionableResource provisionable = EmitCore(cm);
Emited = provisionable;
}

protected abstract ProvisionableResource EmitCore(CloudMachineInfrastructure cm);

[EditorBrowsable(EditorBrowsableState.Never)]
public ProvisionableResource Emited { get; protected set; } = default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void AddResource(NamedProvisionableConstruct resource)
}
public void AddFeature(CloudMachineFeature feature)
{
Features.Add(feature);
feature.AddTo(this);
}

public void AddEndpoints<T>()
Expand All @@ -227,7 +227,7 @@ public void AddEndpoints<T>()

public ProvisioningPlan Build(ProvisioningBuildOptions? context = null)
{
Features.AddTo(this);
Features.Emit(this);

// Always add a default location parameter.
// azd assumes there will be a location parameter for every module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Azure.Provisioning.CloudMachine;

namespace Azure.CloudMachine;
Expand All @@ -11,21 +12,18 @@ public class FeatureCollection
private CloudMachineFeature[] _items = new CloudMachineFeature[4];
private int _count;

public bool TryFind<T>(out T? found) where T: CloudMachineFeature
public IEnumerable<T> FindAll<T>() where T: CloudMachineFeature
{
for (int i = 0; i < _count; i++)
{
if (_items[i] is T item)
{
found = item;
return true;
yield return item;
}
}
found = default;
return false;
}

public void Add(CloudMachineFeature item)
internal void Add(CloudMachineFeature item)
{
if (_count == _items.Length)
{
Expand All @@ -41,14 +39,14 @@ void Resize()
}
}

internal void AddTo(CloudMachineInfrastructure infrastructure)
internal void Emit(CloudMachineInfrastructure infrastructure)
{
int index = 0;
while (true)
{
if (index >= _count) break;
CloudMachineFeature feature = _items[index++];
feature.AddTo(infrastructure);
feature.Emit(infrastructure);
}
}
}
Loading