-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
211 lines (183 loc) · 8.43 KB
/
MainWindow.xaml.cs
File metadata and controls
211 lines (183 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Azure Open AI Chat Client (Using Semantic Kernel)
using System;
using System.Windows;
using System.Globalization;
namespace AzureOpenAIChat
{
public partial class MainWindow : Window
{
// Azure OpenAI Chat Client (Using Semantic Kernel)
SKHelper? sk;
public MainWindow()
{
InitializeComponent();
// Init/Load From Registry
if (string.IsNullOrEmpty(txtAPIEndPoint.Text = RegistryHelper.ReadAppInfo("APIENDPOINT")))
txtAPIEndPoint.Text = "Your Azure OpenAI Endpoint";
if (string.IsNullOrEmpty(txtTenantId.Text = RegistryHelper.ReadAppInfo("TENANTID")))
txtTenantId.Text = "Your Entra Tenant ID";
if (string.IsNullOrEmpty(txtDeployment.Text = RegistryHelper.ReadAppInfo("DEPLOYMENT")))
txtDeployment.Text = "Your Azure OpenAI Model Deployment Name";
if (string.IsNullOrEmpty(txtClientId.Text = RegistryHelper.ReadAppInfo("CLIENTID")))
txtClientId.Text = "Your App Registration Client ID";
if (string.IsNullOrEmpty(txtClientSecret.Text = RegistryHelper.ReadAppInfo("CLIENTSECRET")))
txtClientSecret.Text = "Your App Registration Client Secret";
if (string.IsNullOrEmpty(txtTemperature.Text = RegistryHelper.ReadAppInfo("TEMPERATURE")))
txtTemperature.Text = "0.5";
if (string.IsNullOrEmpty(txtMaxTokens.Text = RegistryHelper.ReadAppInfo("MAXTOKENS")))
txtMaxTokens.Text = "2048";
var left = RegistryHelper.ReadAppInfo("WINDOWLEFT");
var top = RegistryHelper.ReadAppInfo("WINDOWTOP");
if (!string.IsNullOrEmpty(left) && !string.IsNullOrEmpty(top))
{
this.Left = double.Parse(left);
this.Top = double.Parse(top);
}
else
{
// Center Window on Screen
Size windowSize = new Size(this.Width, this.Height);
Rect screenSize = SystemParameters.WorkArea;
this.Left = (screenSize.Width / 2) - (windowSize.Width / 2);
this.Top = (screenSize.Height / 2) - (windowSize.Height / 2);
}
// Set focus to promt field
txtPrompt.Focus();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Save to Registry
RegistryHelper.WriteAppInfo("APIENDPOINT", txtAPIEndPoint.Text);
RegistryHelper.WriteAppInfo("TENANTID", txtTenantId.Text);
RegistryHelper.WriteAppInfo("DEPLOYMENT", txtDeployment.Text);
RegistryHelper.WriteAppInfo("CLIENTID", txtClientId.Text);
RegistryHelper.WriteAppInfo("CLIENTSECRET", txtClientSecret.Text);
RegistryHelper.WriteAppInfo("TEMPERATURE", txtTemperature.Text);
RegistryHelper.WriteAppInfo("MAXTOKENS", txtMaxTokens.Text);
RegistryHelper.WriteAppInfo("WINDOWLEFT", this.Left.ToString());
RegistryHelper.WriteAppInfo("WINDOWTOP", this.Top.ToString());
}
private async void btnSend_Click(object sender, RoutedEventArgs e)
{
if (sk == null)
{
// Get the parameters from the screen
string apiEndpoint = txtAPIEndPoint.Text;
string tenantId = txtTenantId.Text;
string deployment = txtDeployment.Text;
string clientId = txtClientId.Text;
string clientSecret = txtClientSecret.Text;
if (string.IsNullOrWhiteSpace(apiEndpoint) ||
string.IsNullOrWhiteSpace(tenantId) ||
string.IsNullOrWhiteSpace(deployment) ||
string.IsNullOrWhiteSpace(clientId) ||
string.IsNullOrWhiteSpace(clientSecret))
{
MessageBox.Show("Please fill in all required settings.", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (!double.TryParse(txtTemperature.Text, NumberStyles.Float, CultureInfo.InvariantCulture, out double temperature))
{
MessageBox.Show("Temperature must be a valid number (example: 0.5).", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (!int.TryParse(txtMaxTokens.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out int maxtokens))
{
MessageBox.Show("Max Tokens must be a valid integer.", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
try
{
sk = new SKHelper(deployment, apiEndpoint, tenantId, clientId, clientSecret, maxtokens, temperature);
// make Setup readonly
txtAPIEndPoint.IsReadOnly = true;
txtTenantId.IsReadOnly = true;
txtDeployment.IsReadOnly = true;
txtClientId.IsReadOnly = true;
txtClientSecret.IsReadOnly = true;
txtTemperature.IsReadOnly = true;
txtMaxTokens.IsReadOnly = true;
// Programmatically hide the top area
topRow.Height = new GridLength(0);
}
catch (Exception ex)
{
lblCompletion.Content = "Authentication/Initialization failed. Exception below";
txtCompletion.Text = GetFullExceptionMessage(ex);
MarkdownViewer.Markdown = string.Empty;
return;
}
}
// Trim Prompt
string myprompt = txtPrompt.Text.Trim();
// If prompt is empty reset and return
if (myprompt == string.Empty)
{
// Reset Completion
txtCompletion.Text = string.Empty;
MarkdownViewer.Markdown = string.Empty;
lblCompletion.Content = "Completion context cleared";
btnClearCtx.IsEnabled = false;
btnCopyCtx.IsEnabled = false;
if (sk != null)
sk.InitContext();
return;
}
// Wait message
lblCompletion.Content = "Request is processing...";
txtCompletion.Text = string.Empty;
MarkdownViewer.Markdown = string.Empty;
btnSend.IsEnabled = false;
try
{
var completionText = await sk.Chat(myprompt);
txtCompletion.Text = completionText;
MarkdownViewer.Markdown = completionText;
btnClearCtx.IsEnabled = true;
btnCopyCtx.IsEnabled = true;
lblCompletion.Content = "Completion";
}
catch (Exception ex)
{
lblCompletion.Content = "The request was unsuccessful. Exception below";
txtCompletion.Text = GetFullExceptionMessage(ex);
MarkdownViewer.Markdown = string.Empty;
}
finally
{
btnSend.IsEnabled = true;
}
}
// Clear Context
private void btnClearCtx_Click(object sender, RoutedEventArgs e)
{
btnClearCtx.IsEnabled = false;
btnCopyCtx.IsEnabled = false;
if (sk != null)
sk.InitContext();
lblCompletion.Content = "Completion context cleared";
}
// Copy Context
private void btnCopyCtx_Click(object sender, RoutedEventArgs e)
{
string? context = sk?.GetFullContext();
if (string.IsNullOrWhiteSpace(context))
{
lblCompletion.Content = "No context to copy";
return;
}
Clipboard.SetText(context);
lblCompletion.Content = "Completion context copied to clipboard";
}
// Helper method to get full exception details including inner exceptions
private static string GetFullExceptionMessage(Exception ex)
{
var message = ex.Message;
if (ex.InnerException != null)
{
message += "\n\nInner Exception: " + GetFullExceptionMessage(ex.InnerException);
}
return message;
}
}
}