1- using Microsoft . SemanticKernel ;
2- using Microsoft . SemanticKernel . ChatCompletion ;
1+ using Azure . AI . OpenAI ;
2+ using Azure ;
3+ using Microsoft . Extensions . AI ;
34
45namespace ChartGenerator
56{
@@ -27,26 +28,6 @@ internal class ChartAIService
2728 /// </summary>
2829 internal const string key = "API key" ;
2930
30- /// <summary>
31- /// The chat completion service
32- /// </summary>
33- private IChatCompletionService ? chatCompletions ;
34-
35- /// <summary>
36- /// The kernal
37- /// </summary>
38- private Kernel ? kernel ;
39-
40- /// <summary>
41- /// The chat histroy
42- /// </summary>
43- private ChatHistory ? chatHistory ;
44-
45- /// <summary>
46- /// The credential valid field
47- /// </summary>
48- private static bool isCredentialValid ;
49-
5031 /// <summary>
5132 /// The already credential validated field
5233 /// </summary>
@@ -66,66 +47,11 @@ public ChartAIService()
6647
6748 #region Properties
6849
69- /// <summary>
70- /// Gets or Set a value indicating whether an credentials are valid or not.
71- /// Returns <c>true</c> if the credentials are valid; otherwise, <c>false</c>.
72- /// </summary>
73- public static bool IsCredentialValid
74- {
75- get
76- {
77- return isCredentialValid ;
78- }
79- set
80- {
81- isCredentialValid = value ;
82- }
83- }
50+ internal IChatClient ? Client { get ; set ; }
8451
85- /// <summary>
86- /// Gets or sets a value indicating the chat history object
87- /// </summary>
88- public ChatHistory ? ChatHistory
89- {
90- get
91- {
92- return chatHistory ;
93- }
94- set
95- {
96- chatHistory = value ;
97- }
98- }
52+ internal string ? ChatHistory { get ; set ; }
9953
100- /// <summary>
101- /// Gets or sets a value indicating the chat completions object
102- /// </summary>
103- public IChatCompletionService ? ChatCompletions
104- {
105- get
106- {
107- return chatCompletions ;
108- }
109- set
110- {
111- chatCompletions = value ;
112- }
113- }
114-
115- /// <summary>
116- /// Gets or sets a value indicating the kernal object
117- /// </summary>
118- public Kernel ? Kernel
119- {
120- get
121- {
122- return kernel ;
123- }
124- set
125- {
126- kernel = value ;
127- }
128- }
54+ internal static bool IsCredentialValid { get ; set ; }
12955
13056 #endregion
13157
@@ -136,40 +62,31 @@ public Kernel? Kernel
13662 /// </summary>
13763 private async void ValidateCredential ( )
13864 {
139- #region Azure OpenAI
140- // Use below method for Azure Open AI
14165 this . GetAzureOpenAIKernal ( ) ;
142- #endregion
14366
14467 if ( isAlreadyValidated )
14568 {
14669 return ;
14770 }
148- bool isValidUri = Uri . TryCreate ( endpoint , UriKind . Absolute , out uriResult )
149- && ( uriResult . Scheme == Uri . UriSchemeHttp || uriResult . Scheme == Uri . UriSchemeHttps ) ;
15071
151- if ( ! isValidUri || ! endpoint . Contains ( "http" ) || string . IsNullOrEmpty ( key ) || key . Contains ( "API key" ) || string . IsNullOrEmpty ( deploymentName ) || deploymentName . Contains ( "deployment name" ) || string . IsNullOrEmpty ( imageDeploymentName ) )
152- {
153- ShowAlertAsync ( ) ;
154- return ;
155- }
15672 try
15773 {
158- if ( ChatHistory != null && chatCompletions != null )
74+ if ( Client != null )
75+ {
76+ await Client ! . CompleteAsync ( "Hello, Test Check" ) ;
77+ ChatHistory = string . Empty ;
78+ IsCredentialValid = true ;
79+ isAlreadyValidated = true ;
80+ }
81+ else
15982 {
160- // test the semantic kernal with message.
161- ChatHistory . AddSystemMessage ( "Hello, Test Check" ) ;
162- await chatCompletions . GetChatMessageContentAsync ( chatHistory : ChatHistory , kernel : kernel ) ;
83+ ShowAlertAsync ( ) ;
16384 }
16485 }
16586 catch ( Exception )
16687 {
167- // Handle any exceptions that indicate the credentials or endpoint are invalid.
168- ShowAlertAsync ( ) ;
16988 return ;
17089 }
171- IsCredentialValid = true ;
172- isAlreadyValidated = true ;
17390 }
17491
17592 #region Azure OpenAI
@@ -178,15 +95,14 @@ private async void ValidateCredential()
17895 /// </summary>
17996 private void GetAzureOpenAIKernal ( )
18097 {
181- // Create the chat history
182- chatHistory = new ChatHistory ( ) ;
183- var builder = Kernel . CreateBuilder ( ) . AddAzureOpenAIChatCompletion ( deploymentName , endpoint , key ) ;
184-
185- // Get the kernal from build
186- kernel = builder . Build ( ) ;
187-
188- //Get the chat completions from kernal
189- chatCompletions = kernel . GetRequiredService < IChatCompletionService > ( ) ;
98+ try
99+ {
100+ var client = new AzureOpenAIClient ( new Uri ( endpoint ) , new AzureKeyCredential ( key ) ) . AsChatClient ( modelId : deploymentName ) ;
101+ this . Client = client ;
102+ }
103+ catch ( Exception )
104+ {
105+ }
190106 }
191107 #endregion
192108
@@ -197,23 +113,21 @@ private void GetAzureOpenAIKernal()
197113 /// <returns>The AI response.</returns>
198114 internal async Task < string > GetAnswerFromGPT ( string userPrompt )
199115 {
200- if ( IsCredentialValid && ChatCompletions != null && ChatHistory != null )
116+ try
201117 {
202- ChatHistory . Clear ( ) ;
203-
204- // Add the user's prompt as a user message to the conversation.
205- ChatHistory . AddUserMessage ( userPrompt ) ;
206- try
118+ if ( IsCredentialValid && ChatHistory != null && Client != null )
207119 {
208- //// Send the chat completion request to the OpenAI API and await the response.
209- var response = await ChatCompletions . GetChatMessageContentAsync ( chatHistory : ChatHistory , kernel : Kernel ) ;
120+ ChatHistory = string . Empty ;
121+ // Add the system message and user message to the options
122+ ChatHistory = ChatHistory + userPrompt ;
123+ var response = await Client . CompleteAsync ( ChatHistory ) ;
210124 return response . ToString ( ) ;
211125 }
212- catch
213- {
214- // If an exception occurs (e.g., network issues, API errors), return an empty string.
215- return "" ;
216- }
126+ }
127+ catch
128+ {
129+ // If an exception occurs (e.g., network issues, API errors), return an empty string.
130+ return "" ;
217131 }
218132
219133 return "" ;
@@ -224,15 +138,14 @@ internal async Task<string> GetAnswerFromGPT(string userPrompt)
224138 /// </summary>
225139 private async void ShowAlertAsync ( )
226140 {
227- #pragma warning disable CS0618 // Type or member is obsolete
228- if ( Application . Current ? . MainPage != null && ! IsCredentialValid )
141+ var page = Application . Current ? . Windows [ 0 ] . Page ;
142+ if ( page != null && ! IsCredentialValid )
229143 {
230144 isAlreadyValidated = true ;
231- 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" ) ;
145+ 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" ) ;
232146 }
233- #pragma warning restore CS0618 // Type or member is obsolete
234147 }
235-
236- #endregion
237148 }
149+
150+ #endregion
238151}
0 commit comments