diff --git a/AI Demos/AISmartPDFFormFill/SmartFill/AIHelper.cs b/AI Demos/AISmartPDFFormFill/SmartFill/AIHelper.cs
index 0551c4f..28a4923 100644
--- a/AI Demos/AISmartPDFFormFill/SmartFill/AIHelper.cs
+++ b/AI Demos/AISmartPDFFormFill/SmartFill/AIHelper.cs
@@ -1,11 +1,14 @@
-using Azure.AI.OpenAI;
+using Microsoft.Extensions.AI;
+using Azure.AI.OpenAI;
using Azure;
namespace SmartFill
{
- public class AIHelper
+ public class AIHelper
{
+ #region Fields
+
///
/// The EndPoint
///
@@ -14,35 +17,64 @@ public class AIHelper
///
/// The Deployment name
///
- internal string deploymentName = "DEPLOYMENT_NAME";
+ internal string DeploymentName = "DEPLOYMENT_NAME";
///
/// The AI key
///
- private string apiKey = "AZURE_OPENAI_API_KEY";
+ private string key = "OPENAI_AI_KEY";
+
+ ///
+ /// The IChatClient instance
+ ///
+ private IChatClient? client;
+
+ ///
+ /// The chat history
+ ///
+ private string? chatHistory;
+
+ #endregion
+
+ #region Properties
///
- /// The AzureOpenAI client
+ /// Gets or sets the chat history
///
- // C#
- internal OpenAIClient? openAIClient;
+ public string? ChatHistory
+ {
+ get { return chatHistory; }
+ set { chatHistory = value; }
+ }
///
- /// The ChatCompletion option
+ /// Gets or sets the IChatClient instance
///
- private ChatCompletionsOptions? chatCompletionsOptions;
+ public IChatClient? Client
+ {
+ get { return client; }
+ set { client = value; }
+ }
+
+ #endregion
+
+ #region Constructor
public AIHelper()
{
- chatCompletionsOptions = new ChatCompletionsOptions
- {
- DeploymentName = deploymentName,
- Temperature = (float)1.2f,
- NucleusSamplingFactor = (float)0.9,
- FrequencyPenalty = 0.8f,
- PresencePenalty = 0.8f
- };
- openAIClient = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
+ InitializeClient();
+ }
+
+ #endregion
+
+ #region Methods
+
+ ///
+ /// Initializes the Azure OpenAI client
+ ///
+ private void InitializeClient()
+ {
+ client = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key)).AsChatClient(modelId: DeploymentName);
}
///
@@ -54,24 +86,24 @@ public async Task GetChatCompletion(string userPrompt)
{
try
{
- if (apiKey != "AZURE_OPENAI_API_KEY" && deploymentName != "DEPLOYMENT_NAME" && endpoint != "https://yourendpoint.com/")
+ if (key != "OPENAI_AI_KEY" && DeploymentName != "DEPLOYMENT_NAME" && endpoint != "https://yourendpoint.com/" && Client!=null)
{
- chatCompletionsOptions.Messages.Add(new ChatRequestUserMessage(userPrompt));
- var response = await openAIClient.GetChatCompletionsAsync(chatCompletionsOptions);
- return response.Value.Choices[0].Message.Content;
+ ChatHistory = string.Empty;
+ ChatHistory = ChatHistory + userPrompt;
+ var response = await Client.CompleteAsync(ChatHistory);
+ return response.ToString();
}
else
{
- await Application.Current.MainPage.DisplayAlert("Error", "Please provide your Azure OpenAI API key, deployment name, and endpoint in the AIHelper class.", "OK");
- return string.Empty;
+ return " ";
}
}
- catch (Exception exception)
+ catch
{
- await Application.Current.MainPage.DisplayAlert("Error", exception.Message, "OK");
- return string.Empty;
+ // Return an empty string if an exception occurs
+ return " ";
}
}
-
+ #endregion
}
-}
\ No newline at end of file
+}
diff --git a/AI Demos/AISmartPDFFormFill/SmartFill/MainPage.xaml.cs b/AI Demos/AISmartPDFFormFill/SmartFill/MainPage.xaml.cs
index a8f13c6..d18f406 100644
--- a/AI Demos/AISmartPDFFormFill/SmartFill/MainPage.xaml.cs
+++ b/AI Demos/AISmartPDFFormFill/SmartFill/MainPage.xaml.cs
@@ -1,5 +1,6 @@
using Syncfusion.Maui.PdfViewer;
using Syncfusion.Pdf.Parsing;
+using System.Net;
namespace SmartFill;
@@ -31,12 +32,14 @@ private void PdfViewer_DocumentLoaded(object? sender, EventArgs? e)
#if ANDROID || IOS
MobileCopiedData.IsVisible = true;
#endif
+ if (chatService.DeploymentName != "DEPLOYMENT_NAME")
+ SubmitForm.IsEnabled = true;
}
private async void Clipboard_ClipboardContentChanged(object? sender, EventArgs e)
{
string? copiedText = await Clipboard.GetTextAsync();
- if (string.IsNullOrEmpty(copiedText) == false && chatService.deploymentName!= "DEPLOYMENT_NAME")
+ if (string.IsNullOrEmpty(copiedText) == false && chatService.DeploymentName!= "DEPLOYMENT_NAME")
{
SubmitForm.IsEnabled = true;
StartBubbleAnimation();
diff --git a/AI Demos/AISmartPDFFormFill/SmartFill/SmartFill.csproj b/AI Demos/AISmartPDFFormFill/SmartFill/SmartFill.csproj
index c22c55d..b33d418 100644
--- a/AI Demos/AISmartPDFFormFill/SmartFill/SmartFill.csproj
+++ b/AI Demos/AISmartPDFFormFill/SmartFill/SmartFill.csproj
@@ -86,7 +86,11 @@
-
+
+
+
+
+
diff --git a/AI Demos/SmartRedaction/SmartRedaction/AIService.cs b/AI Demos/SmartRedaction/SmartRedaction/AIService.cs
index bd942d8..f3c02fe 100644
--- a/AI Demos/SmartRedaction/SmartRedaction/AIService.cs
+++ b/AI Demos/SmartRedaction/SmartRedaction/AIService.cs
@@ -1,10 +1,15 @@
-using Azure;
+using Microsoft.Extensions.AI;
using Azure.AI.OpenAI;
+using Azure.Identity;
+using OpenAI;
+using Azure;
namespace SmartRedaction
{
public class AIService
{
+ #region Fields
+
///
/// The EndPoint
///
@@ -18,49 +23,88 @@ public class AIService
///
/// The AI key
///
- private string key = "AZURE_OPENAI_API_KEY";
+ private string key = "OPENAI_AI_KEY";
+
+ ///
+ /// The IChatClient instance
+ ///
+ private IChatClient client;
+
+ ///
+ /// The chat history
+ ///
+ private string chatHistory;
+
+ #endregion
+
+ #region Properties
///
- /// The AzureOpenAI client
+ /// Gets or sets the chat history
///
- private OpenAIClient? client;
+ public string ChatHistory
+ {
+ get { return chatHistory; }
+ set { chatHistory = value; }
+ }
///
- /// The ChatCompletion option
+ /// Gets or sets the IChatClient instance
///
- internal ChatCompletionsOptions? chatCompletions;
+ public IChatClient Client
+ {
+ get { return client; }
+ set { client = value; }
+ }
+
+ #endregion
+
+ #region Constructor
public AIService()
{
- chatCompletions = new ChatCompletionsOptions
- {
- DeploymentName = this.DeploymentName,
- Temperature = (float)1.2f,
- NucleusSamplingFactor = (float)0.9,
- FrequencyPenalty = 0.8f,
- PresencePenalty = 0.8f
- };
- client = new OpenAIClient(new Uri(this.endpoint), new AzureKeyCredential(this.key));
+ InitializeClient();
}
- public async Task GetAnswerFromGPT(string systemPrompt, string extractedtext)
+
+ #endregion
+
+ #region Methods
+
+ ///
+ /// Initializes the Azure OpenAI client
+ ///
+ private void InitializeClient()
+ {
+ client = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key)).AsChatClient(modelId: DeploymentName);
+ }
+
+ ///
+ /// Retrieves an answer from the GPT model using the provided system prompt and extracted text.
+ ///
+ /// The system instruction for GPT.
+ /// The input text extracted from a source.
+ /// A string containing the response from OpenAI or an empty string if an error occurs.
+ public async Task GetAnswerFromGPT(string systemPrompt, string extractedText)
{
try
{
- string message = extractedtext;
- if (this.client != null && this.chatCompletions != null && this.key != "AZURE_OPENAI_API_KEY")
+ if (Client != null && key != "OPENAI_API_KEY")
{
- chatCompletions.Messages.Clear();
- chatCompletions.Messages.Add(new ChatRequestSystemMessage(systemPrompt));
- chatCompletions.Messages.Add(new ChatRequestUserMessage(message));
- var response = await client.GetChatCompletionsAsync(chatCompletions);
- return response.Value.Choices[0].Message.Content;
+ ChatHistory = string.Empty;
+ ChatHistory += $"System: {systemPrompt}\nUser: {extractedText}";
+ var response = await Client.CompleteAsync(ChatHistory);
+ return response.ToString();
}
- return "";
+ else
+ return string.Empty;
}
catch
{
- return "";
+ // Return an empty string if an exception occurs
+ return string.Empty;
}
}
+
+ #endregion
}
}
diff --git a/AI Demos/SmartRedaction/SmartRedaction/MainPage.xaml b/AI Demos/SmartRedaction/SmartRedaction/MainPage.xaml
index 70a121c..39c30ae 100644
--- a/AI Demos/SmartRedaction/SmartRedaction/MainPage.xaml
+++ b/AI Demos/SmartRedaction/SmartRedaction/MainPage.xaml
@@ -454,7 +454,7 @@
HeightRequest="40"
HorizontalOptions="End"
Clicked="OnCancelClicked" />
-
-
-
-
-
+
+
+
+
+
+
+
diff --git a/AI Demos/Summarizer/Summarizer/AssistServices.cs b/AI Demos/Summarizer/Summarizer/AssistServices.cs
index 1582c22..7a28070 100644
--- a/AI Demos/Summarizer/Summarizer/AssistServices.cs
+++ b/AI Demos/Summarizer/Summarizer/AssistServices.cs
@@ -2,6 +2,7 @@
using System.Collections.ObjectModel;
using Azure.AI.OpenAI;
using Azure;
+using Microsoft.Extensions.AI;
namespace Summarizer
{
@@ -11,6 +12,7 @@ namespace Summarizer
///
internal class AssistServices
{
+ #region Fields
///
/// Gets or sets the extracted text from a document.
@@ -30,29 +32,53 @@ internal class AssistServices
///
/// The AI key
///
- private string key = "AZURE_OPENAI_API_KEY";
+ private string key = "OPENAI_AI_KEY";
///
- /// The AzureOpenAI client
+ /// The IChatClient instance
///
- internal OpenAIClient? client;
+ private IChatClient? client;
///
- /// The ChatCompletion option
+ /// The chat history
///
- private ChatCompletionsOptions? chatCompletions;
+ private string? chatHistory;
+
+ #endregion
+
+ #region Properties
+
+ ///
+ /// Gets or sets the chat history
+ ///
+ public string? ChatHistory
+ {
+ get { return chatHistory; }
+ set { chatHistory = value; }
+ }
+
+ ///
+ /// Gets or sets the IChatClient instance
+ ///
+ public IChatClient? Client
+ {
+ get { return client; }
+ set { client = value; }
+ }
+
+ #endregion
internal AssistServices()
{
- chatCompletions = new ChatCompletionsOptions
- {
- DeploymentName = this.DeploymentName,
- Temperature = (float)1.2f,
- NucleusSamplingFactor = (float)0.9,
- FrequencyPenalty = 0.8f,
- PresencePenalty = 0.8f
- };
- client = new OpenAIClient(new Uri(this.endpoint), new AzureKeyCredential(this.key));
+ InitializeClient();
+ }
+
+ ///
+ /// Initializes the Azure OpenAI client
+ ///
+ private void InitializeClient()
+ {
+ client = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key)).AsChatClient(modelId: DeploymentName);
}
///
@@ -62,13 +88,12 @@ internal AssistServices()
/// A predefined message requesting OpenAI connection for real-time queries.
internal async Task GetPrompt(string prompt)
{
- if (this.client != null && this.chatCompletions != null && this.key != "AZURE_OPENAI_API_KEY")
+ if (this.Client != null && key != "OPENAI_AI_KEY")
{
- chatCompletions.Messages.Clear();
- chatCompletions.Messages.Add(new ChatRequestSystemMessage("Please provide the prompt for responce" + prompt));
- chatCompletions.Messages.Add(new ChatRequestUserMessage(prompt));
- var response = await client.GetChatCompletionsAsync(chatCompletions);
- return response.Value.Choices[0].Message.Content;
+ ChatHistory = string.Empty;
+ ChatHistory += $"System: {"Please provide the prompt for responce" + prompt}\nUser: {prompt}";
+ var response = await Client.CompleteAsync(ChatHistory);
+ return response.ToString();
}
else
return "Please connect OpenAI for real time queries";
@@ -85,15 +110,14 @@ internal async Task GetSolutionToPrompt(string question)
try
{
// Use extracted text
- if (this.ExtractedText != null && this.client != null && this.chatCompletions != null && this.key != "AZURE_OPENAI_API_KEY")
+ if (this.ExtractedText != null && this.Client != null && key != "OPENAI_AI_KEY")
{
string message = ExtractedText;
- var systemPrompt = "You are a helpful assistant. Use the provided PDF document pages and pick a precise page to answer the user question,Ignore about iTextSharp related points in the details, Strictly don't bold any text all text need to plain text. Pages: " + message;
- chatCompletions.Messages.Clear();
- chatCompletions.Messages.Add(new ChatRequestSystemMessage(systemPrompt));
- chatCompletions.Messages.Add(new ChatRequestUserMessage(question));
- var response = await client.GetChatCompletionsAsync(chatCompletions);
- return response.Value.Choices[0].Message.Content;
+ var systemPrompt = "Read the PDF document contents, understand the concept, and select the precise page to answer the user's question. Ignore any points related to iTextSharp. Ensure that all text is plain and not bolded. Pages: Question: " + question;
+ ChatHistory = string.Empty;
+ ChatHistory += $"System: {systemPrompt}\nUser: {ExtractedText}";
+ var response = await Client.CompleteAsync(ChatHistory);
+ return response.ToString();
}
return "Please connect OpenAI for real time queries";
}
@@ -137,13 +161,12 @@ internal async Task GetAnswerFromGPT(string systemPrompt)
{
try
{
- if (this.ExtractedText != null && this.chatCompletions != null && this.client != null && this.key != "AZURE_OPENAI_API_KEY")
+ if (this.ExtractedText != null && this.Client != null && key != "OPENAI_AI_KEY")
{
- chatCompletions.Messages.Clear();
- chatCompletions.Messages.Add(new ChatRequestSystemMessage(systemPrompt));
- chatCompletions.Messages.Add(new ChatRequestUserMessage(ExtractedText));
- var response = await client.GetChatCompletionsAsync(chatCompletions);
- return response.Value.Choices[0].Message.Content;
+ ChatHistory = string.Empty;
+ ChatHistory += $"System: {systemPrompt}\nUser: {ExtractedText}";
+ var response = await Client.CompleteAsync(ChatHistory);
+ return response.ToString();
}
else
return "Please connect OpenAI for real time queries";
diff --git a/AI Demos/Summarizer/Summarizer/MainPage.xaml.cs b/AI Demos/Summarizer/Summarizer/MainPage.xaml.cs
index 3151f79..cc09310 100644
--- a/AI Demos/Summarizer/Summarizer/MainPage.xaml.cs
+++ b/AI Demos/Summarizer/Summarizer/MainPage.xaml.cs
@@ -53,26 +53,6 @@ internal async Task LoadPDFDataAsync()
PdfLoadedPageCollection loadedPages = loadedDocument.Pages;
await Task.Run(() =>
{
- // Extract annotations to a memory stream and convert to string
- using (MemoryStream annotationStream = new MemoryStream())
- {
- loadedDocument.ExportAnnotations(annotationStream, AnnotationDataFormat.Json);
- string annotations = ConvertToString(annotationStream);
- if (!String.IsNullOrEmpty(annotations))
- extractedText.Add("Annotations: " + annotations);
- }
-
- // Extract form fields to a memory stream and convert to string
- using (MemoryStream formStream = new MemoryStream())
- {
- if (loadedDocument.Form != null)
- {
- loadedDocument.Form.ExportData(formStream, DataFormat.Json, "form");
- string formFields = ConvertToString(formStream);
- if (!String.IsNullOrEmpty(formFields))
- extractedText.Add("Form fields: " + formFields);
- }
- }
// Extract text from existing PDF document pages
for (int i = 0; i < loadedPages.Count; i++)
{
diff --git a/AI Demos/Summarizer/Summarizer/Summarizer.csproj b/AI Demos/Summarizer/Summarizer/Summarizer.csproj
index bbfb3c0..1554372 100644
--- a/AI Demos/Summarizer/Summarizer/Summarizer.csproj
+++ b/AI Demos/Summarizer/Summarizer/Summarizer.csproj
@@ -80,7 +80,6 @@
-
@@ -91,6 +90,9 @@
+
+
+
diff --git a/AI Demos/Summarizer/Summarizer/Summarizer.csproj.user b/AI Demos/Summarizer/Summarizer/Summarizer.csproj.user
deleted file mode 100644
index 20abc95..0000000
--- a/AI Demos/Summarizer/Summarizer/Summarizer.csproj.user
+++ /dev/null
@@ -1,32 +0,0 @@
-
-
-
- False
- net8.0-android
- Android Emulator
- PhysicalDevice
-
-
-
- Designer
-
-
- Designer
-
-
- Designer
-
-
- Designer
-
-
- Designer
-
-
- Designer
-
-
- Designer
-
-
-
\ No newline at end of file