1
- using Microsoft . SemanticKernel ;
2
- using Microsoft . SemanticKernel . ChatCompletion ;
1
+ using Azure . AI . OpenAI ;
2
+ using Azure ;
3
+ using Microsoft . Extensions . AI ;
3
4
4
5
namespace ChartGenerator
5
6
{
@@ -27,26 +28,6 @@ internal class ChartAIService
27
28
/// </summary>
28
29
internal const string key = "API key" ;
29
30
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
-
50
31
/// <summary>
51
32
/// The already credential validated field
52
33
/// </summary>
@@ -66,66 +47,11 @@ public ChartAIService()
66
47
67
48
#region Properties
68
49
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 ; }
84
51
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 ; }
99
53
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 ; }
129
55
130
56
#endregion
131
57
@@ -136,40 +62,31 @@ public Kernel? Kernel
136
62
/// </summary>
137
63
private async void ValidateCredential ( )
138
64
{
139
- #region Azure OpenAI
140
- // Use below method for Azure Open AI
141
65
this . GetAzureOpenAIKernal ( ) ;
142
- #endregion
143
66
144
67
if ( isAlreadyValidated )
145
68
{
146
69
return ;
147
70
}
148
- bool isValidUri = Uri . TryCreate ( endpoint , UriKind . Absolute , out uriResult )
149
- && ( uriResult . Scheme == Uri . UriSchemeHttp || uriResult . Scheme == Uri . UriSchemeHttps ) ;
150
71
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
- }
156
72
try
157
73
{
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
159
82
{
160
- // test the semantic kernal with message.
161
- ChatHistory . AddSystemMessage ( "Hello, Test Check" ) ;
162
- await chatCompletions . GetChatMessageContentAsync ( chatHistory : ChatHistory , kernel : kernel ) ;
83
+ ShowAlertAsync ( ) ;
163
84
}
164
85
}
165
86
catch ( Exception )
166
87
{
167
- // Handle any exceptions that indicate the credentials or endpoint are invalid.
168
- ShowAlertAsync ( ) ;
169
88
return ;
170
89
}
171
- IsCredentialValid = true ;
172
- isAlreadyValidated = true ;
173
90
}
174
91
175
92
#region Azure OpenAI
@@ -178,15 +95,14 @@ private async void ValidateCredential()
178
95
/// </summary>
179
96
private void GetAzureOpenAIKernal ( )
180
97
{
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
+ }
190
106
}
191
107
#endregion
192
108
@@ -197,23 +113,21 @@ private void GetAzureOpenAIKernal()
197
113
/// <returns>The AI response.</returns>
198
114
internal async Task < string > GetAnswerFromGPT ( string userPrompt )
199
115
{
200
- if ( IsCredentialValid && ChatCompletions != null && ChatHistory != null )
116
+ try
201
117
{
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 )
207
119
{
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 ) ;
210
124
return response . ToString ( ) ;
211
125
}
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 "" ;
217
131
}
218
132
219
133
return "" ;
@@ -224,15 +138,14 @@ internal async Task<string> GetAnswerFromGPT(string userPrompt)
224
138
/// </summary>
225
139
private async void ShowAlertAsync ( )
226
140
{
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 )
229
143
{
230
144
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" ) ;
232
146
}
233
- #pragma warning restore CS0618 // Type or member is obsolete
234
147
}
235
-
236
- #endregion
237
148
}
149
+
150
+ #endregion
238
151
}
0 commit comments