diff --git a/MAUIDataForm/MAUIDataForm/App.xaml.cs b/MAUIDataForm/MAUIDataForm/App.xaml.cs index 3c0bd90..3d38b1c 100644 --- a/MAUIDataForm/MAUIDataForm/App.xaml.cs +++ b/MAUIDataForm/MAUIDataForm/App.xaml.cs @@ -5,7 +5,6 @@ public partial class App : Application public App() { InitializeComponent(); - - MainPage = new AppShell(); + MainPage = new MainPage(); } } \ No newline at end of file diff --git a/MAUIDataForm/MAUIDataForm/AppShell.xaml b/MAUIDataForm/MAUIDataForm/AppShell.xaml deleted file mode 100644 index bf98af2..0000000 --- a/MAUIDataForm/MAUIDataForm/AppShell.xaml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/MAUIDataForm/MAUIDataForm/AppShell.xaml.cs b/MAUIDataForm/MAUIDataForm/AppShell.xaml.cs deleted file mode 100644 index 77dc323..0000000 --- a/MAUIDataForm/MAUIDataForm/AppShell.xaml.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MAUIDataForm; - -public partial class AppShell : Shell -{ - public AppShell() - { - InitializeComponent(); - } -} \ No newline at end of file diff --git a/MAUIDataForm/MAUIDataForm/MAUIDataForm.csproj b/MAUIDataForm/MAUIDataForm/MAUIDataForm.csproj index ee63b6a..b03e6aa 100644 --- a/MAUIDataForm/MAUIDataForm/MAUIDataForm.csproj +++ b/MAUIDataForm/MAUIDataForm/MAUIDataForm.csproj @@ -63,7 +63,7 @@ - + diff --git a/MAUIDataForm/MAUIDataForm/MainPage.xaml b/MAUIDataForm/MAUIDataForm/MainPage.xaml index f1f6ffd..7025ce4 100644 --- a/MAUIDataForm/MAUIDataForm/MainPage.xaml +++ b/MAUIDataForm/MAUIDataForm/MainPage.xaml @@ -17,7 +17,7 @@ MaximumHeightRequest="550" VerticalOptions="Start" HorizontalOptions="Center" - WidthRequest="550" + WidthRequest="{OnPlatform MacCatalyst=550, WinUI=450, iOS=400, Android=350}" Padding="10"> diff --git a/MAUIDataForm/MAUIDataForm/MainPage.xaml.cs b/MAUIDataForm/MAUIDataForm/MainPage.xaml.cs index 0389c53..e708659 100644 --- a/MAUIDataForm/MAUIDataForm/MainPage.xaml.cs +++ b/MAUIDataForm/MAUIDataForm/MainPage.xaml.cs @@ -10,7 +10,7 @@ public MainPage() } string clipboardText; - private AzureOpenAIService azureAIService = new AzureOpenAIService(); + SemanticKernelService semanticKernelService = new SemanticKernelService(); private async void OnSmartPasteButtonClicked(object sender, EventArgs e) { @@ -37,7 +37,7 @@ private async void OnSmartPasteButtonClicked(object sender, EventArgs e) $"\n3. Final output must be Json format" + $"\n4. No need any explanation or comments in the output" + $"\n Please provide the valid JSON object without any additional formatting characters like backticks or newlines"; - string finalResponse = await this.azureAIService.GetResponseFromGPT(prompt); + string finalResponse = await this.semanticKernelService.GetResponseFromGPT(prompt); this.ProcessSmartPasteData(finalResponse); } diff --git a/MAUIDataForm/MAUIDataForm/Service/AzureOpenAIService.cs b/MAUIDataForm/MAUIDataForm/Service/AzureOpenAIService.cs deleted file mode 100644 index a7f0e36..0000000 --- a/MAUIDataForm/MAUIDataForm/Service/AzureOpenAIService.cs +++ /dev/null @@ -1,58 +0,0 @@ -namespace MAUIDataForm -{ - using Azure; - using Azure.AI.OpenAI; - - internal class AzureOpenAIService - { - const string endpoint = "https://{YOUR_END_POINT}.openai.azure.com"; - const string deploymentName = "GPT35Turbo"; - string key = "API key"; - - OpenAIClient? client; - ChatCompletionsOptions? chatCompletions; - - internal AzureOpenAIService() - { - - } - - internal async Task GetResponseFromGPT(string userPrompt) - { - this.chatCompletions = new ChatCompletionsOptions - { - DeploymentName = deploymentName, - Temperature = (float)0.5, - MaxTokens = 800, - NucleusSamplingFactor = (float)0.95, - FrequencyPenalty = 0, - PresencePenalty = 0, - }; - - this.client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key)); - if (this.client != null) - { - // Add the user's prompt as a user message to the conversation. - this.chatCompletions?.Messages.Add(new ChatRequestSystemMessage("You are a predictive analytics assistant.")); - - // Add the user's prompt as a user message to the conversation. - this.chatCompletions?.Messages.Add(new ChatRequestUserMessage(userPrompt)); - try - { - // Send the chat completion request to the OpenAI API and await the response. - var response = await this.client.GetChatCompletionsAsync(this.chatCompletions); - - // Return the content of the first choice in the response, which contains the AI's answer. - return response.Value.Choices[0].Message.Content; - } - catch - { - // If an exception occurs (e.g., network issues, API errors), return an empty string. - return ""; - } - } - - return ""; - } - } -} \ No newline at end of file diff --git a/MAUIDataForm/MAUIDataForm/Service/SemanticKernelService.cs b/MAUIDataForm/MAUIDataForm/Service/SemanticKernelService.cs new file mode 100644 index 0000000..84358fa --- /dev/null +++ b/MAUIDataForm/MAUIDataForm/Service/SemanticKernelService.cs @@ -0,0 +1,54 @@ +namespace MAUIDataForm +{ + using Microsoft.SemanticKernel; + using Microsoft.SemanticKernel.ChatCompletion; + using Microsoft.SemanticKernel.Connectors.OpenAI; + + internal class SemanticKernelService + { + const string endpoint = "https://{YOUR_END_POINT}.openai.azure.com"; + const string deploymentName = "GPT35Turbo"; + string key = "API key"; + + IChatCompletionService chatCompletionService; + Kernel kernel; + + internal SemanticKernelService() + { + + } + + internal async Task GetResponseFromGPT(string userPrompt) + { + var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(deploymentName, endpoint, key); + this.kernel = builder.Build(); + if (this.kernel != null) + { + var chatHistory = new ChatHistory(); + chatHistory.Clear(); + + // Add the user's prompt as a user message to the conversation. + chatHistory.AddSystemMessage("You are a predictive analytics assistant."); + + // Add the user's prompt as a user message to the conversation. + chatHistory.AddUserMessage(userPrompt); + + // Get the chat completions from kernal. + chatCompletionService = kernel.GetRequiredService(); + OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new OpenAIPromptExecutionSettings(); + openAIPromptExecutionSettings.ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions; + try + { + var response = await chatCompletionService.GetChatMessageContentAsync(chatHistory, executionSettings: openAIPromptExecutionSettings, kernel: kernel); + return response.ToString(); + } + catch + { + return ""; + } + } + + return ""; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 6f98a32..a638932 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ -# Integrate-openai-powered-smart-paste-into-DataForm-for-Seamless-Data-Entry +# Integrate OpenAI-Powered Smart Paste into DataForm for Seamless Data Entry + This repository contains sample code demonstrating how to integrate OpenAI-powered Smart Paste into a Syncfusion MAUI DataForm. The AI-driven feature streamlines data entry by processing and formatting pasted content, enhancing efficiency and accuracy. + +![image](https://github.com/user-attachments/assets/987ccf81-b954-4c99-8479-b80828c2acf1) \ No newline at end of file