From 4eb3809f4c88274b000b96ed2edaf160e1b559fc Mon Sep 17 00:00:00 2001 From: Bruno Sonnino Date: Wed, 4 Dec 2024 12:51:29 -0500 Subject: [PATCH 1/2] Fix crash when running and exiting the generated project Minor fixes in the Custom System Prompt sample Bump OnnxRuntimeGenAI version to 0.5.2 --- .../ProjectGenerator/Template/MainWindow.xaml.cs | 5 +++++ .../Language Models/CustomSystemPrompt.xaml.cs | 15 +++++++-------- Directory.Packages.props | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs b/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs index ba7e49a..54845ab 100644 --- a/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs +++ b/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs @@ -1,9 +1,13 @@ +using Microsoft.ML.OnnxRuntimeGenAI; using Microsoft.UI.Xaml; namespace $safeprojectname$ { public sealed partial class MainWindow : Window { + // OgaHandle is responsible of cleaning up the library during shutdown + private readonly OgaHandle _ogaHandle = new(); + public MainWindow() { this.InitializeComponent(); @@ -16,6 +20,7 @@ public MainWindow() internal void ModelLoaded() { ProgressRingGrid.Visibility = Visibility.Collapsed; + (($MainSamplePage$)this.RootFrame.Content).Unloaded += (s, e) => _ogaHandle.Dispose(); } } } diff --git a/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs b/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs index bc7f900..1dbb216 100644 --- a/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs +++ b/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs @@ -43,7 +43,7 @@ public CustomSystemPrompt() protected override async Task LoadModelAsync(SampleNavigationParameters sampleParams) { model = await sampleParams.GetIChatClientAsync(); - InputTextBox.MaxLength = chatOptions.MaxOutputTokens ?? 0; + InputTextBox.MaxLength = GenAIModel.DefaultMaxLength; sampleParams.NotifyCompletion(); } @@ -180,19 +180,18 @@ private void StopBtn_Click(object sender, RoutedEventArgs e) private void InputBox_Changed(object sender, TextChangedEventArgs e) { + var maxInputLength = GenAIModel.DefaultMaxLength; + var inputLength = InputTextBox.Text.Length; if (inputLength > 0) { - if (inputLength >= chatOptions.MaxOutputTokens) - { - InputTextBox.Description = $"{inputLength} of {chatOptions.MaxOutputTokens}. Max characters reached."; - } - else + InputTextBox.Description = $"{inputLength} of {maxInputLength}"; + if (inputLength >= maxInputLength) { - InputTextBox.Description = $"{inputLength} of {chatOptions.MaxOutputTokens}"; + InputTextBox.Description += ". Max characters reached."; } - GenerateButton.IsEnabled = inputLength <= chatOptions.MaxOutputTokens; + GenerateButton.IsEnabled = inputLength <= maxInputLength; } else { diff --git a/Directory.Packages.props b/Directory.Packages.props index ac5f3dd..11522d6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -18,9 +18,9 @@ - + - + From c798463228aa8e52bab998ba20f48f04ee85ed1c Mon Sep 17 00:00:00 2001 From: Bruno Sonnino Date: Wed, 4 Dec 2024 16:37:54 -0500 Subject: [PATCH 2/2] Removed changes in Custom System Prompt Fixed errors in Non GenAI generation --- AIDevGallery/ProjectGenerator/Generator.cs | 8 ++++++++ .../ProjectGenerator/Template/MainWindow.xaml.cs | 5 ----- .../Language Models/CustomSystemPrompt.xaml.cs | 15 ++++++++------- AIDevGallery/Samples/SharedCode/GenAIModel.cs | 7 +++++++ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/AIDevGallery/ProjectGenerator/Generator.cs b/AIDevGallery/ProjectGenerator/Generator.cs index f893c25..dcd0643 100644 --- a/AIDevGallery/ProjectGenerator/Generator.cs +++ b/AIDevGallery/ProjectGenerator/Generator.cs @@ -554,6 +554,11 @@ private async Task AddFilesFromSampleAsync( } } + if (sample.SharedCode.Contains(SharedCodeEnum.GenAIModel)) + { + cleanCsSource = RegexInitializeComponent().Replace(cleanCsSource, $"$1this.InitializeComponent();$1GenAIModel.InitializeGenAI();"); + } + await File.WriteAllTextAsync(Path.Join(outputPath, $"{className}.xaml.cs"), cleanCsSource, cancellationToken); } @@ -569,6 +574,9 @@ private async Task AddFilesFromSampleAsync( [GeneratedRegex(@"[\r\n][\s]*return Task.CompletedTask;")] private static partial Regex RegexReturnTaskCompletedTask(); + [GeneratedRegex(@"(\s*)this.InitializeComponent\(\);")] + private static partial Regex RegexInitializeComponent(); + private string CleanXamlSource(string xamlCode, string newNamespace, out string className) { var match = XClass().Match(xamlCode); diff --git a/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs b/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs index 54845ab..ba7e49a 100644 --- a/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs +++ b/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs @@ -1,13 +1,9 @@ -using Microsoft.ML.OnnxRuntimeGenAI; using Microsoft.UI.Xaml; namespace $safeprojectname$ { public sealed partial class MainWindow : Window { - // OgaHandle is responsible of cleaning up the library during shutdown - private readonly OgaHandle _ogaHandle = new(); - public MainWindow() { this.InitializeComponent(); @@ -20,7 +16,6 @@ public MainWindow() internal void ModelLoaded() { ProgressRingGrid.Visibility = Visibility.Collapsed; - (($MainSamplePage$)this.RootFrame.Content).Unloaded += (s, e) => _ogaHandle.Dispose(); } } } diff --git a/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs b/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs index 1dbb216..bc7f900 100644 --- a/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs +++ b/AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs @@ -43,7 +43,7 @@ public CustomSystemPrompt() protected override async Task LoadModelAsync(SampleNavigationParameters sampleParams) { model = await sampleParams.GetIChatClientAsync(); - InputTextBox.MaxLength = GenAIModel.DefaultMaxLength; + InputTextBox.MaxLength = chatOptions.MaxOutputTokens ?? 0; sampleParams.NotifyCompletion(); } @@ -180,18 +180,19 @@ private void StopBtn_Click(object sender, RoutedEventArgs e) private void InputBox_Changed(object sender, TextChangedEventArgs e) { - var maxInputLength = GenAIModel.DefaultMaxLength; - var inputLength = InputTextBox.Text.Length; if (inputLength > 0) { - InputTextBox.Description = $"{inputLength} of {maxInputLength}"; - if (inputLength >= maxInputLength) + if (inputLength >= chatOptions.MaxOutputTokens) + { + InputTextBox.Description = $"{inputLength} of {chatOptions.MaxOutputTokens}. Max characters reached."; + } + else { - InputTextBox.Description += ". Max characters reached."; + InputTextBox.Description = $"{inputLength} of {chatOptions.MaxOutputTokens}"; } - GenerateButton.IsEnabled = inputLength <= maxInputLength; + GenerateButton.IsEnabled = inputLength <= chatOptions.MaxOutputTokens; } else { diff --git a/AIDevGallery/Samples/SharedCode/GenAIModel.cs b/AIDevGallery/Samples/SharedCode/GenAIModel.cs index d5dd840..ab3d630 100644 --- a/AIDevGallery/Samples/SharedCode/GenAIModel.cs +++ b/AIDevGallery/Samples/SharedCode/GenAIModel.cs @@ -30,6 +30,7 @@ internal class GenAIModel : IChatClient, IDisposable private Tokenizer? _tokenizer; private LlmPromptTemplate? _template; private static readonly SemaphoreSlim _createSemaphore = new(1, 1); + private static OgaHandle? _ogaHandle; public static ChatOptions GetDefaultChatOptions() { @@ -84,6 +85,11 @@ private GenAIModel(string modelDir) return model; } + public static void InitializeGenAI() + { + _ogaHandle = new OgaHandle(); + } + [MemberNotNullWhen(true, nameof(_model), nameof(_tokenizer))] public bool IsReady => _model != null && _tokenizer != null; @@ -93,6 +99,7 @@ public void Dispose() { _model?.Dispose(); _tokenizer?.Dispose(); + _ogaHandle?.Dispose(); } private string GetPrompt(IEnumerable history)