Skip to content

Commit

Permalink
Fixed a bug and refactored some AI APIs (#46908)
Browse files Browse the repository at this point in the history
* bugs and refactoring

* update api file
  • Loading branch information
KrzysztofCwalina authored Oct 30, 2024
1 parent 2ed3ee8 commit 4f733df
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
public partial class AiModel
{
public AiModel(string model, string modelVersion) { }
public string Model { get { throw null; } }
public string ModelVersion { get { throw null; } }
}
namespace Azure.CloudMachine
{
public partial class CloudMachineClient : Azure.CloudMachine.CloudMachineWorkspace
Expand Down Expand Up @@ -65,6 +59,19 @@ public void DeleteBlob(string path) { }
public void WhenBlobUploaded(System.Action<Azure.CloudMachine.StorageFile> function) { }
}
}
namespace Azure.CloudMachine.OpenAI
{
public partial class EmbeddingKnowledgebase
{
internal EmbeddingKnowledgebase() { }
public void Add(string fact) { }
}
public partial class OpenAIConversation
{
internal OpenAIConversation() { }
public string Say(string message) { throw null; }
}
}
namespace Azure.Core
{
public partial class ClientCache
Expand Down Expand Up @@ -136,26 +143,22 @@ public override void AddTo(Azure.Provisioning.CloudMachine.CloudMachineInfrastru
}
namespace Azure.Provisioning.CloudMachine.OpenAI
{
public partial class AIModel
{
public AIModel(string model, string modelVersion) { }
public string Model { get { throw null; } }
public string ModelVersion { get { throw null; } }
}
public static partial class AzureOpenAIExtensions
{
public static Azure.Provisioning.CloudMachine.OpenAI.EmbeddingKnowledgebase CreateEmbeddingKnowledgebase(this Azure.Core.ClientWorkspace workspace) { throw null; }
public static Azure.Provisioning.CloudMachine.OpenAI.OpenAIConversation CreateOpenAIConversation(this Azure.Core.ClientWorkspace workspace) { throw null; }
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingsClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
}
public partial class EmbeddingKnowledgebase
{
internal EmbeddingKnowledgebase() { }
public void Add(string fact) { }
}
public partial class OpenAIConversation
{
internal OpenAIConversation() { }
public string Say(string message) { throw null; }
}
public partial class OpenAIFeature : Azure.Provisioning.CloudMachine.CloudMachineFeature
{
public OpenAIFeature(AiModel chatDeployment, AiModel? embeddingsDeployment = null) { }
public OpenAIFeature() { }
public Azure.Provisioning.CloudMachine.OpenAI.AIModel? Chat { get { throw null; } set { } }
public Azure.Provisioning.CloudMachine.OpenAI.AIModel? Embeddings { get { throw null; } set { } }
public override void AddTo(Azure.Provisioning.CloudMachine.CloudMachineInfrastructure cloudMachine) { }
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

public class AiModel
namespace Azure.Provisioning.CloudMachine.OpenAI;

public class AIModel
{
public AiModel(string model, string modelVersion) { Model = model; ModelVersion = modelVersion; }
public AIModel(string model, string modelVersion) { Model = model; ModelVersion = modelVersion; }
public string Model { get; }
public string ModelVersion { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.ClientModel;
using System.Linq;
using Azure.AI.OpenAI;
using Azure.Core;
using Azure.Provisioning.Authorization;
Expand All @@ -15,21 +14,19 @@ namespace Azure.Provisioning.CloudMachine.OpenAI;

public class OpenAIFeature : CloudMachineFeature
{
private AiModel _chatDeployment;
private AiModel? _embeddingsDeployment;

public OpenAIFeature(AiModel chatDeployment, AiModel? embeddingsDeployment = default)
public OpenAIFeature()
{
if (chatDeployment == null)
{
throw new ArgumentNullException(nameof(chatDeployment));
}
_chatDeployment = chatDeployment;
_embeddingsDeployment = embeddingsDeployment;
}

public AIModel? Chat { get; set; }
public AIModel? Embeddings { get; set; }

public override void AddTo(CloudMachineInfrastructure cloudMachine)
{
if (Chat == default && Embeddings == default)
{
throw new InvalidOperationException("At least one of Chat or Embeddings must be specified.");
}
CognitiveServicesAccount cognitiveServices = new("openai")
{
Name = cloudMachine.Id,
Expand All @@ -49,23 +46,27 @@ public override void AddTo(CloudMachineInfrastructure cloudMachine)
cloudMachine.PrincipalIdParameter)
);

CognitiveServicesAccountDeployment chat = new("openai_deployment_chat", "2023-05-01")
CognitiveServicesAccountDeployment? chat = default;
if (Chat != default)
{
Parent = cognitiveServices,
Name = cloudMachine.Id,
Properties = new CognitiveServicesAccountDeploymentProperties()
chat = new("openai_deployment_chat", "2023-05-01")
{
Model = new CognitiveServicesAccountDeploymentModel()
Parent = cognitiveServices,
Name = cloudMachine.Id,
Properties = new CognitiveServicesAccountDeploymentProperties()
{
Name = _chatDeployment.Model,
Format = "OpenAI",
Version = _chatDeployment.ModelVersion
}
},
};
cloudMachine.AddResource(chat);
Model = new CognitiveServicesAccountDeploymentModel()
{
Name = Chat.Model,
Format = "OpenAI",
Version = Chat.ModelVersion
}
},
};
cloudMachine.AddResource(chat);
}

if (_embeddingsDeployment != null)
if (Embeddings != null)
{
CognitiveServicesAccountDeployment embeddings = new("openai_deployment_embedding", "2023-05-01")
{
Expand All @@ -75,16 +76,19 @@ public override void AddTo(CloudMachineInfrastructure cloudMachine)
{
Model = new CognitiveServicesAccountDeploymentModel()
{
Name = _embeddingsDeployment.Model,
Name = Embeddings.Model,
Format = "OpenAI",
Version = _embeddingsDeployment.ModelVersion
Version = Embeddings.ModelVersion
}
},
};

// Ensure that additional deployments, are chained using DependsOn.
// The reason is that deployments need to be deployed/created serially.
embeddings.DependsOn.Add(chat);
if (chat != default)
{
embeddings.DependsOn.Add(chat);
}
cloudMachine.AddResource(embeddings);
}
}
Expand Down Expand Up @@ -114,18 +118,18 @@ public static EmbeddingClient GetOpenAIEmbeddingsClient(this ClientWorkspace wor
return embeddingsClient;
}

public static EmbeddingKnowledgebase CreateEmbeddingKnowledgebase(this ClientWorkspace workspace)
{
EmbeddingClient embeddingsClient = workspace.GetOpenAIEmbeddingsClient();
return new EmbeddingKnowledgebase(embeddingsClient);
}

public static OpenAIConversation CreateOpenAIConversation(this ClientWorkspace workspace)
{
ChatClient chatClient = workspace.GetOpenAIChatClient();
EmbeddingKnowledgebase knowledgebase = workspace.CreateEmbeddingKnowledgebase();
return new OpenAIConversation(chatClient, [], knowledgebase);
}
//public static EmbeddingKnowledgebase CreateEmbeddingKnowledgebase(this ClientWorkspace workspace)
//{
// EmbeddingClient embeddingsClient = workspace.GetOpenAIEmbeddingsClient();
// return new EmbeddingKnowledgebase(embeddingsClient);
//}

//public static OpenAIConversation CreateOpenAIConversation(this ClientWorkspace workspace)
//{
// ChatClient chatClient = workspace.GetOpenAIChatClient();
// EmbeddingKnowledgebase knowledgebase = workspace.CreateEmbeddingKnowledgebase();
// return new OpenAIConversation(chatClient, [], knowledgebase);
//}

private static AzureOpenAIClient CreateAzureOpenAIClient(this ClientWorkspace workspace)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public partial class CloudMachineClient : CloudMachineWorkspace
public CloudMachineClient(TokenCredential? credential = default, IConfiguration? configuration = default)
: base(credential, configuration)
{
Messaging = new MessagingServices(this);
Storage = new StorageServices(this);
}

public MessagingServices Messaging => new(this);
public StorageServices Storage => new(this);
public MessagingServices Messaging { get; }
public StorageServices Storage { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using OpenAI.Embeddings;

namespace Azure.Provisioning.CloudMachine.OpenAI;
namespace Azure.CloudMachine.OpenAI;

/// <summary>
/// Represents a knowledgebase of facts represented by embeddings that can be used to find relevant facts based on a given text.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using System.Linq;
using System.Text;
using OpenAI.Chat;
using static Azure.Provisioning.CloudMachine.OpenAI.EmbeddingKnowledgebase;
using static Azure.CloudMachine.OpenAI.EmbeddingKnowledgebase;

namespace Azure.Provisioning.CloudMachine.OpenAI;
namespace Azure.CloudMachine.OpenAI;

/// <summary>
/// Represents a conversation with the OpenAI chat model, incorporating a knowledgebase of embeddings data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;

[assembly: Experimental("AZPROVISION001")]
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ public void Provisioning(string[] args)
if (CloudMachineInfrastructure.Configure(args, (cm) =>
{
cm.AddFeature(new KeyVaultFeature());
cm.AddFeature(new OpenAIFeature(new AiModel("gpt-35-turbo", "0125"), new AiModel("text-embedding-ada-002", "2")));
cm.AddFeature(new OpenAIFeature() // TODO: rework it such that models can be added as features
{
Chat = new AIModel("gpt-35-turbo", "0125"),
Embeddings = new AIModel("text-embedding-ada-002", "2")
});
}))
return;

CloudMachineWorkspace cm = new();
Console.WriteLine(cm.Id);
var embeddings = cm.GetOpenAIEmbeddingsClient();
var kb = cm.CreateEmbeddingKnowledgebase();
var conversation = cm.CreateOpenAIConversation();
}

[Ignore("no recordings yet")]
Expand Down Expand Up @@ -74,7 +76,9 @@ public void OpenAI(string[] args)
{
if (CloudMachineInfrastructure.Configure(args, (cm) =>
{
cm.AddFeature(new OpenAIFeature(new AiModel("gpt-35-turbo", "0125")));
cm.AddFeature(new OpenAIFeature() {
Chat = new AIModel("gpt-35-turbo", "0125")
});
}))
return;

Expand Down

0 comments on commit 4f733df

Please sign in to comment.