Skip to content

Commit 3f77ddb

Browse files
Simplified OpenAI Provisioning (Azure#47174)
* simplified commands and added model list command * simplified * disabled live test * PR feedback * fixed spelling
1 parent 4e11733 commit 3f77ddb

20 files changed

+663
-194
lines changed

sdk/cloudmachine/Azure.CloudMachine/api/Azure.CloudMachine.netstandard2.0.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public static partial class AzureOpenAIExtensions
7979
{
8080
public static void Add(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages, OpenAI.Chat.ChatCompletion completion) { }
8181
public static void Add(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages, System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.VectorbaseEntry> entries) { }
82+
public static string AsText(this OpenAI.Chat.ChatCompletion completion) { throw null; }
83+
public static string AsText(this OpenAI.Chat.ChatMessageContent completion) { throw null; }
8284
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
8385
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingsClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
8486
public static void Trim(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages) { }

sdk/cloudmachine/Azure.CloudMachine/src/CloudMachineWorkspace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public override ClientConnectionOptions GetConnectionOptions(Type clientType, st
8888
case "Azure.AI.OpenAI.AzureOpenAIClient":
8989
return new ClientConnectionOptions(new($"https://{Id}.openai.azure.com"), Credential);
9090
case "OpenAI.Chat.ChatClient":
91-
return new ClientConnectionOptions(Id);
91+
return new ClientConnectionOptions($"{Id}_chat");
9292
case "OpenAI.Embeddings.EmbeddingClient":
93-
return new ClientConnectionOptions($"{Id}-embedding");
93+
return new ClientConnectionOptions($"{Id}_embedding");
9494
default:
9595
throw new Exception($"unknown client {clientId}");
9696
}

sdk/cloudmachine/Azure.CloudMachine/src/extensions/AzureOpenAIExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System.ClientModel;
55
using System.Collections.Generic;
6+
using System.Runtime.CompilerServices;
7+
using System.Text;
68
using Azure.AI.OpenAI;
79
using Azure.Core;
810
using OpenAI.Chat;
@@ -47,6 +49,37 @@ public static EmbeddingClient GetOpenAIEmbeddingsClient(this ClientWorkspace wor
4749
return embeddingsClient;
4850
}
4951

52+
/// <summary>
53+
/// returns full text of all parts.
54+
/// </summary>
55+
/// <param name="completion"></param>
56+
/// <returns></returns>
57+
public static string AsText(this ChatCompletion completion)
58+
=> completion.Content.AsText();
59+
60+
/// <summary>
61+
/// returns full text of all parts.
62+
/// </summary>
63+
/// <param name="completion"></param>
64+
/// <returns></returns>
65+
public static string AsText(this ChatMessageContent completion)
66+
{
67+
StringBuilder sb = new();
68+
foreach (ChatMessageContentPart part in completion)
69+
{
70+
switch (part.Kind)
71+
{
72+
case ChatMessageContentPartKind.Text:
73+
sb.AppendLine(part.Text);
74+
break;
75+
default:
76+
sb.AppendLine($"<{part.Kind}>");
77+
break;
78+
}
79+
}
80+
return sb.ToString();
81+
}
82+
5083
private static AzureOpenAIClient CreateAzureOpenAIClient(this ClientWorkspace workspace)
5184
{
5285
ClientConnectionOptions connection = workspace.GetConnectionOptions(typeof(AzureOpenAIClient));

sdk/cloudmachine/Azure.CloudMachine/tests/CloudMachineTests.cs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,21 @@
1515
using Microsoft.Extensions.Primitives;
1616
using System.Linq;
1717
using System.IO;
18+
using Azure.Provisioning;
1819

1920
namespace Azure.CloudMachine.Tests;
2021

2122
public class CloudMachineTests
2223
{
2324
[Test]
24-
[TestCase([new string[] { "-bicep" }])]
25-
[TestCase([new string[] { "" }])]
26-
public void Provisioning(string[] args)
25+
public void Configuration()
2726
{
28-
if (CloudMachineInfrastructure.Configure(args, (cm) =>
27+
CloudMachineCommands.Execute(["-bicep"], (infrastructure) =>
2928
{
30-
cm.AddFeature(new KeyVaultFeature());
31-
cm.AddFeature(new OpenAIFeature() // TODO: rework it such that models can be added as features
32-
{
33-
Chat = new AIModel("gpt-35-turbo", "0125"),
34-
Embeddings = new AIModel("text-embedding-ada-002", "2")
35-
});
36-
}))
37-
return;
29+
infrastructure.AddFeature(new KeyVaultFeature());
30+
infrastructure.AddFeature(new OpenAIModel("gpt-35-turbo", "0125"));
31+
infrastructure.AddFeature(new OpenAIModel("text-embedding-ada-002", "2", AIModelKind.Embedding));
32+
}, exitProcessIfHandled: false);
3833

3934
CloudMachineWorkspace cm = new();
4035
Console.WriteLine(cm.Id);
@@ -47,12 +42,9 @@ public void Provisioning(string[] args)
4742
[TestCase([new string[] { "" }])]
4843
public void Storage(string[] args)
4944
{
50-
ManualResetEventSlim eventSlim = new(false);
51-
if (CloudMachineInfrastructure.Configure(args, (cm) =>
52-
{
53-
}))
54-
return;
45+
if (CloudMachineCommands.Execute(args, exitProcessIfHandled: false)) return;
5546

47+
ManualResetEventSlim eventSlim = new(false);
5648
CloudMachineClient cm = new();
5749

5850
cm.Storage.WhenUploaded((StorageFile file) =>
@@ -78,24 +70,15 @@ public void Storage(string[] args)
7870
[TestCase([new string[] { "" }])]
7971
public void OpenAI(string[] args)
8072
{
81-
if (CloudMachineInfrastructure.Configure(args, (cm) =>
73+
if (CloudMachineCommands.Execute(args, (infrastructure) =>
8274
{
83-
cm.AddFeature(new OpenAIFeature()
84-
{
85-
Chat = new AIModel("gpt-35-turbo", "0125")
86-
});
87-
}))
88-
return;
75+
infrastructure.AddFeature(new OpenAIModel("gpt-35-turbo", "0125"));
76+
}, exitProcessIfHandled: false)) return;
8977

9078
CloudMachineWorkspace cm = new();
9179
ChatClient chat = cm.GetOpenAIChatClient();
9280
ChatCompletion completion = chat.CompleteChat("Is Azure programming easy?");
93-
94-
ChatMessageContent content = completion.Content;
95-
foreach (ChatMessageContentPart part in content)
96-
{
97-
Console.WriteLine(part.Text);
98-
}
81+
Console.WriteLine(completion.AsText());
9982
}
10083

10184
[Ignore("no recordings yet")]
@@ -104,11 +87,10 @@ public void OpenAI(string[] args)
10487
[TestCase([new string[] { "" }])]
10588
public void KeyVault(string[] args)
10689
{
107-
if (CloudMachineInfrastructure.Configure(args, (cm) =>
90+
if (CloudMachineCommands.Execute(args, (cm) =>
10891
{
10992
cm.AddFeature(new KeyVaultFeature());
110-
}))
111-
return;
93+
}, exitProcessIfHandled: false)) return;
11294

11395
CloudMachineWorkspace cm = new();
11496
SecretClient secrets = cm.GetKeyVaultSecretsClient();
@@ -121,8 +103,7 @@ public void KeyVault(string[] args)
121103
[TestCase([new string[] { "" }])]
122104
public void Messaging(string[] args)
123105
{
124-
if (CloudMachineInfrastructure.Configure(args))
125-
return;
106+
CloudMachineCommands.Execute(args);
126107

127108
CloudMachineClient cm = new();
128109
cm.Messaging.WhenMessageReceived(message =>
@@ -143,8 +124,7 @@ public void Messaging(string[] args)
143124
[TestCase([new string[] { "" }])]
144125
public void Demo(string[] args)
145126
{
146-
if (CloudMachineInfrastructure.Configure(args))
147-
return;
127+
if (CloudMachineCommands.Execute(args, exitProcessIfHandled: false)) return;
148128

149129
CloudMachineClient cm = new();
150130

sdk/provisioning/Azure.Provisioning.CloudMachine/api/Azure.Provisioning.CloudMachine.netstandard2.0.cs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
1+
namespace Azure
2+
{
3+
public partial class RestCallFailedException : System.Exception
4+
{
5+
public RestCallFailedException(string message, System.ClientModel.Primitives.PipelineResponse response) { }
6+
}
7+
public partial class RestClient
8+
{
9+
public RestClient() { }
10+
public RestClient(System.ClientModel.Primitives.PipelinePolicy auth) { }
11+
public static Azure.RestClient Shared { get { throw null; } }
12+
public System.ClientModel.Primitives.PipelineMessage Create(string method, System.Uri uri) { throw null; }
13+
public System.ClientModel.Primitives.PipelineResponse Get(string uri, System.ClientModel.Primitives.RequestOptions? options = null) { throw null; }
14+
public System.ClientModel.Primitives.PipelineResponse Patch(string uri, System.ClientModel.BinaryContent content, System.ClientModel.Primitives.RequestOptions? options = null) { throw null; }
15+
public System.ClientModel.Primitives.PipelineResponse Post(string uri, System.ClientModel.BinaryContent content, System.ClientModel.Primitives.RequestOptions? options = null) { throw null; }
16+
public System.ClientModel.Primitives.PipelineResponse Put(string uri, System.ClientModel.BinaryContent content, System.ClientModel.Primitives.RequestOptions? options = null) { throw null; }
17+
public System.ClientModel.Primitives.PipelineResponse Send(System.ClientModel.Primitives.PipelineMessage message, System.ClientModel.Primitives.RequestOptions? options = null) { throw null; }
18+
}
19+
public partial class RestClientOptions : System.ClientModel.Primitives.ClientPipelineOptions
20+
{
21+
public RestClientOptions() { }
22+
}
23+
}
124
namespace Azure.CloudMachine
225
{
26+
public partial class CloudMachineCommands
27+
{
28+
public CloudMachineCommands() { }
29+
public static bool Execute(string[] args, System.Action<Azure.CloudMachine.CloudMachineInfrastructure>? configure = null, bool exitProcessIfHandled = true) { throw null; }
30+
}
331
public partial class CloudMachineInfrastructure
432
{
533
public CloudMachineInfrastructure(string cmId) { }
34+
public Azure.CloudMachine.FeatureCollection Features { get { throw null; } }
635
public string Id { get { throw null; } }
736
public Azure.Provisioning.Roles.UserAssignedIdentity Identity { get { throw null; } }
837
public Azure.Provisioning.ProvisioningParameter PrincipalIdParameter { get { throw null; } }
938
public void AddEndpoints<T>() { }
10-
public void AddFeature(Azure.Provisioning.CloudMachine.CloudMachineFeature resource) { }
39+
public void AddFeature(Azure.Provisioning.CloudMachine.CloudMachineFeature feature) { }
1140
public void AddResource(Azure.Provisioning.Primitives.NamedProvisionableConstruct resource) { }
1241
public Azure.Provisioning.ProvisioningPlan Build(Azure.Provisioning.ProvisioningBuildOptions? context = null) { throw null; }
13-
public static bool Configure(string[] args, System.Action<Azure.CloudMachine.CloudMachineInfrastructure>? configure = null) { throw null; }
42+
}
43+
public partial class FeatureCollection
44+
{
45+
public FeatureCollection() { }
46+
public System.Collections.Generic.IEnumerable<T> FindAll<T>() where T : Azure.Provisioning.CloudMachine.CloudMachineFeature { throw null; }
1447
}
1548
}
1649
namespace Azure.CloudMachine.KeyVault
@@ -19,23 +52,23 @@ public partial class KeyVaultFeature : Azure.Provisioning.CloudMachine.CloudMach
1952
{
2053
public KeyVaultFeature(Azure.Provisioning.KeyVault.KeyVaultSku? sku = null) { }
2154
public Azure.Provisioning.KeyVault.KeyVaultSku Sku { get { throw null; } set { } }
22-
public override void AddTo(Azure.CloudMachine.CloudMachineInfrastructure infrastructure) { }
55+
protected override Azure.Provisioning.Primitives.ProvisionableResource EmitCore(Azure.CloudMachine.CloudMachineInfrastructure infrastructure) { throw null; }
2356
}
2457
}
2558
namespace Azure.CloudMachine.OpenAI
2659
{
27-
public partial class AIModel
60+
public enum AIModelKind
2861
{
29-
public AIModel(string model, string modelVersion) { }
30-
public string Model { get { throw null; } }
31-
public string ModelVersion { get { throw null; } }
62+
Chat = 0,
63+
Embedding = 1,
3264
}
33-
public partial class OpenAIFeature : Azure.Provisioning.CloudMachine.CloudMachineFeature
65+
public partial class OpenAIModel : Azure.Provisioning.CloudMachine.CloudMachineFeature
3466
{
35-
public OpenAIFeature() { }
36-
public Azure.CloudMachine.OpenAI.AIModel? Chat { get { throw null; } set { } }
37-
public Azure.CloudMachine.OpenAI.AIModel? Embeddings { get { throw null; } set { } }
38-
public override void AddTo(Azure.CloudMachine.CloudMachineInfrastructure cloudMachine) { }
67+
public OpenAIModel(string model, string modelVersion, Azure.CloudMachine.OpenAI.AIModelKind kind = Azure.CloudMachine.OpenAI.AIModelKind.Chat) { }
68+
public string Model { get { throw null; } }
69+
public string ModelVersion { get { throw null; } }
70+
public override void AddTo(Azure.CloudMachine.CloudMachineInfrastructure cm) { }
71+
protected override Azure.Provisioning.Primitives.ProvisionableResource EmitCore(Azure.CloudMachine.CloudMachineInfrastructure cm) { throw null; }
3972
}
4073
}
4174
namespace Azure.Provisioning.CloudMachine
@@ -44,7 +77,12 @@ public abstract partial class CloudMachineFeature
4477
{
4578
protected CloudMachineFeature() { }
4679
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
47-
public abstract void AddTo(Azure.CloudMachine.CloudMachineInfrastructure cm);
80+
public Azure.Provisioning.Primitives.ProvisionableResource Emitted { get { throw null; } protected set { } }
81+
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
82+
public virtual void AddTo(Azure.CloudMachine.CloudMachineInfrastructure cm) { }
83+
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
84+
public void Emit(Azure.CloudMachine.CloudMachineInfrastructure cm) { }
85+
protected abstract Azure.Provisioning.Primitives.ProvisionableResource EmitCore(Azure.CloudMachine.CloudMachineInfrastructure cm);
4886
}
4987
}
5088
namespace System.ClientModel.TypeSpec

sdk/provisioning/Azure.Provisioning.CloudMachine/src/Azure.Provisioning.CloudMachine.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
<LangVersion>12</LangVersion>
88

99
<!-- Disable warning CS1591: Missing XML comment for publicly visible type or member -->
10-
<NoWarn>CS1591</NoWarn>
10+
<NoWarn>CS1591;AZC0007</NoWarn>
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Azure.Identity" />
1415
<PackageReference Include="Azure.Provisioning" />
1516
<PackageReference Include="Azure.Provisioning.KeyVault" />
1617
<PackageReference Include="Azure.Provisioning.Storage" />

sdk/provisioning/Azure.Provisioning.CloudMachine/src/AzureSdkExtensions/AIModel.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

sdk/provisioning/Azure.Provisioning.CloudMachine/src/AzureSdkExtensions/KeyVaultFeature.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Azure.Provisioning.CloudMachine;
66
using Azure.Provisioning.Expressions;
77
using Azure.Provisioning.KeyVault;
8+
using Azure.Provisioning.Primitives;
89

910
namespace Azure.CloudMachine.KeyVault;
1011

@@ -20,7 +21,7 @@ public KeyVaultFeature(KeyVaultSku? sku = default)
2021
}
2122
Sku = sku;
2223
}
23-
public override void AddTo(CloudMachineInfrastructure infrastructure)
24+
protected override ProvisionableResource EmitCore(CloudMachineInfrastructure infrastructure)
2425
{
2526
// Add a KeyVault to the CloudMachine infrastructure.
2627
KeyVaultService keyVaultResource = new("cm_kv")
@@ -57,5 +58,7 @@ public override void AddTo(CloudMachineInfrastructure infrastructure)
5758
kvMiRoleAssignment.RoleDefinitionId = BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", KeyVaultBuiltInRole.KeyVaultAdministrator.ToString());
5859
kvMiRoleAssignment.PrincipalId = infrastructure.Identity.PrincipalId;
5960
infrastructure.AddResource(kvMiRoleAssignment);
61+
62+
return keyVaultResource;
6063
}
6164
}

0 commit comments

Comments
 (0)