Skip to content

Commit 4f733df

Browse files
Fixed a bug and refactored some AI APIs (Azure#46908)
* bugs and refactoring * update api file
1 parent 2ed3ee8 commit 4f733df

File tree

8 files changed

+84
-71
lines changed

8 files changed

+84
-71
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
public partial class AiModel
2-
{
3-
public AiModel(string model, string modelVersion) { }
4-
public string Model { get { throw null; } }
5-
public string ModelVersion { get { throw null; } }
6-
}
71
namespace Azure.CloudMachine
82
{
93
public partial class CloudMachineClient : Azure.CloudMachine.CloudMachineWorkspace
@@ -65,6 +59,19 @@ public void DeleteBlob(string path) { }
6559
public void WhenBlobUploaded(System.Action<Azure.CloudMachine.StorageFile> function) { }
6660
}
6761
}
62+
namespace Azure.CloudMachine.OpenAI
63+
{
64+
public partial class EmbeddingKnowledgebase
65+
{
66+
internal EmbeddingKnowledgebase() { }
67+
public void Add(string fact) { }
68+
}
69+
public partial class OpenAIConversation
70+
{
71+
internal OpenAIConversation() { }
72+
public string Say(string message) { throw null; }
73+
}
74+
}
6875
namespace Azure.Core
6976
{
7077
public partial class ClientCache
@@ -136,26 +143,22 @@ public override void AddTo(Azure.Provisioning.CloudMachine.CloudMachineInfrastru
136143
}
137144
namespace Azure.Provisioning.CloudMachine.OpenAI
138145
{
146+
public partial class AIModel
147+
{
148+
public AIModel(string model, string modelVersion) { }
149+
public string Model { get { throw null; } }
150+
public string ModelVersion { get { throw null; } }
151+
}
139152
public static partial class AzureOpenAIExtensions
140153
{
141-
public static Azure.Provisioning.CloudMachine.OpenAI.EmbeddingKnowledgebase CreateEmbeddingKnowledgebase(this Azure.Core.ClientWorkspace workspace) { throw null; }
142-
public static Azure.Provisioning.CloudMachine.OpenAI.OpenAIConversation CreateOpenAIConversation(this Azure.Core.ClientWorkspace workspace) { throw null; }
143154
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
144155
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingsClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
145156
}
146-
public partial class EmbeddingKnowledgebase
147-
{
148-
internal EmbeddingKnowledgebase() { }
149-
public void Add(string fact) { }
150-
}
151-
public partial class OpenAIConversation
152-
{
153-
internal OpenAIConversation() { }
154-
public string Say(string message) { throw null; }
155-
}
156157
public partial class OpenAIFeature : Azure.Provisioning.CloudMachine.CloudMachineFeature
157158
{
158-
public OpenAIFeature(AiModel chatDeployment, AiModel? embeddingsDeployment = null) { }
159+
public OpenAIFeature() { }
160+
public Azure.Provisioning.CloudMachine.OpenAI.AIModel? Chat { get { throw null; } set { } }
161+
public Azure.Provisioning.CloudMachine.OpenAI.AIModel? Embeddings { get { throw null; } set { } }
159162
public override void AddTo(Azure.Provisioning.CloudMachine.CloudMachineInfrastructure cloudMachine) { }
160163
}
161164
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
public class AiModel
4+
namespace Azure.Provisioning.CloudMachine.OpenAI;
5+
6+
public class AIModel
57
{
6-
public AiModel(string model, string modelVersion) { Model = model; ModelVersion = modelVersion; }
8+
public AIModel(string model, string modelVersion) { Model = model; ModelVersion = modelVersion; }
79
public string Model { get; }
810
public string ModelVersion { get; }
911
}

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

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.ClientModel;
6-
using System.Linq;
76
using Azure.AI.OpenAI;
87
using Azure.Core;
98
using Azure.Provisioning.Authorization;
@@ -15,21 +14,19 @@ namespace Azure.Provisioning.CloudMachine.OpenAI;
1514

1615
public class OpenAIFeature : CloudMachineFeature
1716
{
18-
private AiModel _chatDeployment;
19-
private AiModel? _embeddingsDeployment;
20-
21-
public OpenAIFeature(AiModel chatDeployment, AiModel? embeddingsDeployment = default)
17+
public OpenAIFeature()
2218
{
23-
if (chatDeployment == null)
24-
{
25-
throw new ArgumentNullException(nameof(chatDeployment));
26-
}
27-
_chatDeployment = chatDeployment;
28-
_embeddingsDeployment = embeddingsDeployment;
2919
}
3020

21+
public AIModel? Chat { get; set; }
22+
public AIModel? Embeddings { get; set; }
23+
3124
public override void AddTo(CloudMachineInfrastructure cloudMachine)
3225
{
26+
if (Chat == default && Embeddings == default)
27+
{
28+
throw new InvalidOperationException("At least one of Chat or Embeddings must be specified.");
29+
}
3330
CognitiveServicesAccount cognitiveServices = new("openai")
3431
{
3532
Name = cloudMachine.Id,
@@ -49,23 +46,27 @@ public override void AddTo(CloudMachineInfrastructure cloudMachine)
4946
cloudMachine.PrincipalIdParameter)
5047
);
5148

52-
CognitiveServicesAccountDeployment chat = new("openai_deployment_chat", "2023-05-01")
49+
CognitiveServicesAccountDeployment? chat = default;
50+
if (Chat != default)
5351
{
54-
Parent = cognitiveServices,
55-
Name = cloudMachine.Id,
56-
Properties = new CognitiveServicesAccountDeploymentProperties()
52+
chat = new("openai_deployment_chat", "2023-05-01")
5753
{
58-
Model = new CognitiveServicesAccountDeploymentModel()
54+
Parent = cognitiveServices,
55+
Name = cloudMachine.Id,
56+
Properties = new CognitiveServicesAccountDeploymentProperties()
5957
{
60-
Name = _chatDeployment.Model,
61-
Format = "OpenAI",
62-
Version = _chatDeployment.ModelVersion
63-
}
64-
},
65-
};
66-
cloudMachine.AddResource(chat);
58+
Model = new CognitiveServicesAccountDeploymentModel()
59+
{
60+
Name = Chat.Model,
61+
Format = "OpenAI",
62+
Version = Chat.ModelVersion
63+
}
64+
},
65+
};
66+
cloudMachine.AddResource(chat);
67+
}
6768

68-
if (_embeddingsDeployment != null)
69+
if (Embeddings != null)
6970
{
7071
CognitiveServicesAccountDeployment embeddings = new("openai_deployment_embedding", "2023-05-01")
7172
{
@@ -75,16 +76,19 @@ public override void AddTo(CloudMachineInfrastructure cloudMachine)
7576
{
7677
Model = new CognitiveServicesAccountDeploymentModel()
7778
{
78-
Name = _embeddingsDeployment.Model,
79+
Name = Embeddings.Model,
7980
Format = "OpenAI",
80-
Version = _embeddingsDeployment.ModelVersion
81+
Version = Embeddings.ModelVersion
8182
}
8283
},
8384
};
8485

8586
// Ensure that additional deployments, are chained using DependsOn.
8687
// The reason is that deployments need to be deployed/created serially.
87-
embeddings.DependsOn.Add(chat);
88+
if (chat != default)
89+
{
90+
embeddings.DependsOn.Add(chat);
91+
}
8892
cloudMachine.AddResource(embeddings);
8993
}
9094
}
@@ -114,18 +118,18 @@ public static EmbeddingClient GetOpenAIEmbeddingsClient(this ClientWorkspace wor
114118
return embeddingsClient;
115119
}
116120

117-
public static EmbeddingKnowledgebase CreateEmbeddingKnowledgebase(this ClientWorkspace workspace)
118-
{
119-
EmbeddingClient embeddingsClient = workspace.GetOpenAIEmbeddingsClient();
120-
return new EmbeddingKnowledgebase(embeddingsClient);
121-
}
122-
123-
public static OpenAIConversation CreateOpenAIConversation(this ClientWorkspace workspace)
124-
{
125-
ChatClient chatClient = workspace.GetOpenAIChatClient();
126-
EmbeddingKnowledgebase knowledgebase = workspace.CreateEmbeddingKnowledgebase();
127-
return new OpenAIConversation(chatClient, [], knowledgebase);
128-
}
121+
//public static EmbeddingKnowledgebase CreateEmbeddingKnowledgebase(this ClientWorkspace workspace)
122+
//{
123+
// EmbeddingClient embeddingsClient = workspace.GetOpenAIEmbeddingsClient();
124+
// return new EmbeddingKnowledgebase(embeddingsClient);
125+
//}
126+
127+
//public static OpenAIConversation CreateOpenAIConversation(this ClientWorkspace workspace)
128+
//{
129+
// ChatClient chatClient = workspace.GetOpenAIChatClient();
130+
// EmbeddingKnowledgebase knowledgebase = workspace.CreateEmbeddingKnowledgebase();
131+
// return new OpenAIConversation(chatClient, [], knowledgebase);
132+
//}
129133

130134
private static AzureOpenAIClient CreateAzureOpenAIClient(this ClientWorkspace workspace)
131135
{

sdk/provisioning/Azure.Provisioning.CloudMachine/src/OFX/CloudMachineClient.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ public partial class CloudMachineClient : CloudMachineWorkspace
1111
public CloudMachineClient(TokenCredential? credential = default, IConfiguration? configuration = default)
1212
: base(credential, configuration)
1313
{
14+
Messaging = new MessagingServices(this);
15+
Storage = new StorageServices(this);
1416
}
1517

16-
public MessagingServices Messaging => new(this);
17-
public StorageServices Storage => new(this);
18+
public MessagingServices Messaging { get; }
19+
public StorageServices Storage { get; }
1820
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using OpenAI.Embeddings;
77

8-
namespace Azure.Provisioning.CloudMachine.OpenAI;
8+
namespace Azure.CloudMachine.OpenAI;
99

1010
/// <summary>
1111
/// Represents a knowledgebase of facts represented by embeddings that can be used to find relevant facts based on a given text.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.Linq;
77
using System.Text;
88
using OpenAI.Chat;
9-
using static Azure.Provisioning.CloudMachine.OpenAI.EmbeddingKnowledgebase;
9+
using static Azure.CloudMachine.OpenAI.EmbeddingKnowledgebase;
1010

11-
namespace Azure.Provisioning.CloudMachine.OpenAI;
11+
namespace Azure.CloudMachine.OpenAI;
1212

1313
/// <summary>
1414
/// Represents a conversation with the OpenAI chat model, incorporating a knowledgebase of embeddings data.

sdk/provisioning/Azure.Provisioning.CloudMachine/src/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
// Licensed under the MIT License.
33

44
using System.Diagnostics.CodeAnalysis;
5-
6-
[assembly: Experimental("AZPROVISION001")]

sdk/provisioning/Azure.Provisioning.CloudMachine/tests/CloudMachineTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ public void Provisioning(string[] args)
2424
if (CloudMachineInfrastructure.Configure(args, (cm) =>
2525
{
2626
cm.AddFeature(new KeyVaultFeature());
27-
cm.AddFeature(new OpenAIFeature(new AiModel("gpt-35-turbo", "0125"), new AiModel("text-embedding-ada-002", "2")));
27+
cm.AddFeature(new OpenAIFeature() // TODO: rework it such that models can be added as features
28+
{
29+
Chat = new AIModel("gpt-35-turbo", "0125"),
30+
Embeddings = new AIModel("text-embedding-ada-002", "2")
31+
});
2832
}))
2933
return;
3034

3135
CloudMachineWorkspace cm = new();
3236
Console.WriteLine(cm.Id);
3337
var embeddings = cm.GetOpenAIEmbeddingsClient();
34-
var kb = cm.CreateEmbeddingKnowledgebase();
35-
var conversation = cm.CreateOpenAIConversation();
3638
}
3739

3840
[Ignore("no recordings yet")]
@@ -74,7 +76,9 @@ public void OpenAI(string[] args)
7476
{
7577
if (CloudMachineInfrastructure.Configure(args, (cm) =>
7678
{
77-
cm.AddFeature(new OpenAIFeature(new AiModel("gpt-35-turbo", "0125")));
79+
cm.AddFeature(new OpenAIFeature() {
80+
Chat = new AIModel("gpt-35-turbo", "0125")
81+
});
7882
}))
7983
return;
8084

0 commit comments

Comments
 (0)