Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
165 changes: 39 additions & 126 deletions ChartGeneratorAISample/ChartGenerator/AIService/ChartsAIService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Azure.AI.OpenAI;
using Azure;
using Microsoft.Extensions.AI;

namespace ChartGenerator
{
Expand Down Expand Up @@ -27,26 +28,6 @@ internal class ChartAIService
/// </summary>
internal const string key = "API key";

/// <summary>
/// The chat completion service
/// </summary>
private IChatCompletionService? chatCompletions;

/// <summary>
/// The kernal
/// </summary>
private Kernel? kernel;

/// <summary>
/// The chat histroy
/// </summary>
private ChatHistory? chatHistory;

/// <summary>
/// The credential valid field
/// </summary>
private static bool isCredentialValid;

/// <summary>
/// The already credential validated field
/// </summary>
Expand All @@ -66,66 +47,11 @@ public ChartAIService()

#region Properties

/// <summary>
/// Gets or Set a value indicating whether an credentials are valid or not.
/// Returns <c>true</c> if the credentials are valid; otherwise, <c>false</c>.
/// </summary>
public static bool IsCredentialValid
{
get
{
return isCredentialValid;
}
set
{
isCredentialValid = value;
}
}
internal IChatClient? Client { get; set; }

/// <summary>
/// Gets or sets a value indicating the chat history object
/// </summary>
public ChatHistory? ChatHistory
{
get
{
return chatHistory;
}
set
{
chatHistory = value;
}
}
internal string? ChatHistory { get; set; }

/// <summary>
/// Gets or sets a value indicating the chat completions object
/// </summary>
public IChatCompletionService? ChatCompletions
{
get
{
return chatCompletions;
}
set
{
chatCompletions = value;
}
}

/// <summary>
/// Gets or sets a value indicating the kernal object
/// </summary>
public Kernel? Kernel
{
get
{
return kernel;
}
set
{
kernel = value;
}
}
internal static bool IsCredentialValid { get; set; }

#endregion

Expand All @@ -136,40 +62,31 @@ public Kernel? Kernel
/// </summary>
private async void ValidateCredential()
{
#region Azure OpenAI
// Use below method for Azure Open AI
this.GetAzureOpenAIKernal();
#endregion

if (isAlreadyValidated)
{
return;
}
bool isValidUri = Uri.TryCreate(endpoint, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

if (!isValidUri || !endpoint.Contains("http") || string.IsNullOrEmpty(key) || key.Contains("API key") || string.IsNullOrEmpty(deploymentName) || deploymentName.Contains("deployment name") || string.IsNullOrEmpty(imageDeploymentName))
{
ShowAlertAsync();
return;
}
try
{
if (ChatHistory != null && chatCompletions != null)
if (Client != null)
{
await Client!.CompleteAsync("Hello, Test Check");
ChatHistory = string.Empty;
IsCredentialValid = true;
isAlreadyValidated = true;
}
else
{
// test the semantic kernal with message.
ChatHistory.AddSystemMessage("Hello, Test Check");
await chatCompletions.GetChatMessageContentAsync(chatHistory: ChatHistory, kernel: kernel);
ShowAlertAsync();
}
}
catch (Exception)
{
// Handle any exceptions that indicate the credentials or endpoint are invalid.
ShowAlertAsync();
return;
}
IsCredentialValid = true;
isAlreadyValidated = true;
}

#region Azure OpenAI
Expand All @@ -178,15 +95,14 @@ private async void ValidateCredential()
/// </summary>
private void GetAzureOpenAIKernal()
{
// Create the chat history
chatHistory = new ChatHistory();
var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(deploymentName, endpoint, key);

// Get the kernal from build
kernel = builder.Build();

//Get the chat completions from kernal
chatCompletions = kernel.GetRequiredService<IChatCompletionService>();
try
{
var client = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key)).AsChatClient(modelId: deploymentName);
this.Client = client;
}
catch (Exception)
{
}
}
#endregion

Expand All @@ -197,23 +113,21 @@ private void GetAzureOpenAIKernal()
/// <returns>The AI response.</returns>
internal async Task<string> GetAnswerFromGPT(string userPrompt)
{
if (IsCredentialValid && ChatCompletions != null && ChatHistory != null)
try
{
ChatHistory.Clear();

// Add the user's prompt as a user message to the conversation.
ChatHistory.AddUserMessage(userPrompt);
try
if (IsCredentialValid && ChatHistory != null && Client != null)
{
//// Send the chat completion request to the OpenAI API and await the response.
var response = await ChatCompletions.GetChatMessageContentAsync(chatHistory: ChatHistory, kernel: Kernel);
ChatHistory = string.Empty;
// Add the system message and user message to the options
ChatHistory = ChatHistory + userPrompt;
var response = await Client.CompleteAsync(ChatHistory);
return response.ToString();
}
catch
{
// If an exception occurs (e.g., network issues, API errors), return an empty string.
return "";
}
}
catch
{
// If an exception occurs (e.g., network issues, API errors), return an empty string.
return "";
}

return "";
Expand All @@ -224,15 +138,14 @@ internal async Task<string> GetAnswerFromGPT(string userPrompt)
/// </summary>
private async void ShowAlertAsync()
{
#pragma warning disable CS0618 // Type or member is obsolete
if (Application.Current?.MainPage != null && !IsCredentialValid)
var page = Application.Current?.Windows[0].Page;
if (page != null && !IsCredentialValid)
{
isAlreadyValidated = true;
await Application.Current.MainPage.DisplayAlert("Alert", "The Azure API key or endpoint is missing or incorrect. Please verify your credentials. You can also continue with the offline data.", "OK");
await page.DisplayAlert("Alert", "The Azure API key or endpoint is missing or incorrect. Please verify your credentials. You can also continue with the offline data.", "OK");
}
#pragma warning restore CS0618 // Type or member is obsolete
}

#endregion
}

#endregion
}
6 changes: 4 additions & 2 deletions ChartGeneratorAISample/ChartGenerator/ChartGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Azure.Identity" Version="1.13.2" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.1.0-preview.1.25064.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.1" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.33.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SampleBrowser.Maui.Base" Version="*" />
<PackageReference Include="Syncfusion.Maui.AIAssistView" Version="*" />
Expand Down