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/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) diff --git a/Directory.Packages.props b/Directory.Packages.props index 83b28e3..7cf8312 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -18,9 +18,9 @@ - + - +