From ddf38083b54b456786c8c3af42c993838c5b91d0 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Sun, 17 Nov 2024 13:06:39 -0800 Subject: [PATCH 1/2] Simplified generated projects. --- AIDevGallery/ProjectGenerator/Generator.cs | 74 ++++++++++--------- .../Template/MainWindow.xaml.cs | 6 +- .../MultiModelSampleNavigationParameters.cs | 16 ---- .../Template/ProjectTemplate.csproj | 3 +- .../PublishProfiles/win-arm64.pubxml | 1 + .../Properties/PublishProfiles/win-x64.pubxml | 1 + .../Properties/PublishProfiles/win-x86.pubxml | 1 + .../Template/SampleNavigationParameters.cs | 7 -- 8 files changed, 45 insertions(+), 64 deletions(-) delete mode 100644 AIDevGallery/ProjectGenerator/Template/MultiModelSampleNavigationParameters.cs diff --git a/AIDevGallery/ProjectGenerator/Generator.cs b/AIDevGallery/ProjectGenerator/Generator.cs index d40a3f7..75ebb0b 100644 --- a/AIDevGallery/ProjectGenerator/Generator.cs +++ b/AIDevGallery/ProjectGenerator/Generator.cs @@ -222,26 +222,8 @@ private async Task GenerateAsyncInternal(Sample sample, Dictionary m.ModelPromptTemplate).ToList()); - string modelPathString; - string hardwareAcceleratorString; - string sampleNavigationParameterName; - bool isMultiModel; - if (modelInfos.Count > 1) - { - modelPathString = "[" + Environment.NewLine + string.Join($",{Environment.NewLine}", modelInfos.Values.Select(m => " " + m.ModelPathStr)) + Environment.NewLine + " ]"; - isMultiModel = true; - hardwareAcceleratorString = "[" + string.Join(", ", modelInfos.Values.Select(m => $"HardwareAccelerator.{m.HardwareAccelerator}")) + "]"; - sampleNavigationParameterName = "MultiModelSampleNavigationParameters"; - } - else - { - modelPathString = modelInfos.Values.First().ModelPathStr; - isMultiModel = false; - hardwareAcceleratorString = $"HardwareAccelerator.{modelInfos.Values.First().HardwareAccelerator}"; - sampleNavigationParameterName = "SampleNavigationParameters"; - } - var className = await AddFilesFromSampleAsync(sample, packageReferences, safeProjectName, outputPath, addLllmTypes, isMultiModel, cancellationToken); + var className = await AddFilesFromSampleAsync(sample, packageReferences, safeProjectName, outputPath, addLllmTypes, modelInfos, cancellationToken); foreach (var file in files) { @@ -253,12 +235,6 @@ private async Task GenerateAsyncInternal(Sample sample, Dictionary 1)) - { - continue; - } - var outputPathFile = Path.Join(outputPath, relativePath); // Create the directory if it doesn't exist @@ -287,10 +263,7 @@ private async Task GenerateAsyncInternal(Sample sample, Dictionary GenerateAsyncInternal(Sample sample, Dictionary promptTemplates) return modelPromptTemplateSb.ToString(); } - private async Task AddFilesFromSampleAsync(Sample sample, List<(string PackageName, string? Version)> packageReferences, string safeProjectName, string outputPath, bool addLllmTypes, bool isMultiModel, CancellationToken cancellationToken) + private async Task AddFilesFromSampleAsync( + Sample sample, + List<(string PackageName, string? Version)> packageReferences, + string safeProjectName, + string outputPath, + bool addLllmTypes, + Dictionary modelInfos, + CancellationToken cancellationToken) { var sharedCode = sample.SharedCode.ToList(); if (!sharedCode.Contains(SharedCodeEnum.LlmPromptTemplate) && @@ -557,11 +537,37 @@ private async Task AddFilesFromSampleAsync(Sample sample, List<(string P if (!string.IsNullOrEmpty(sample.CSCode)) { var cleanCsSource = CleanCsSource(sample.CSCode, safeProjectName, true); - var chatClientLoader = GetChatClientLoaderString(sample, isMultiModel); + cleanCsSource = cleanCsSource.Replace("sampleParams.NotifyCompletion();", "App.Window?.ModelLoaded();"); + + string modelPath; + if (modelInfos.Count > 1) + { + cleanCsSource = cleanCsSource.Replace("MultiModelSampleNavigationParameters", "SampleNavigationParameters"); + + int i = 0; + foreach (var modelInfo in modelInfos) + { + cleanCsSource = cleanCsSource.Replace($"sampleParams.HardwareAccelerators[{i}]", $"HardwareAccelerator.{modelInfo.Value.HardwareAccelerator}"); + cleanCsSource = cleanCsSource.Replace($"sampleParams.ModelPaths[{i}]", modelInfo.Value.ModelPathStr); + i++; + } + + modelPath = modelInfos.First().Value.ModelPathStr; + } + else + { + var modelInfo = modelInfos.Values.First(); + cleanCsSource = cleanCsSource.Replace("sampleParams.HardwareAccelerator", $"HardwareAccelerator.{modelInfo.HardwareAccelerator}"); + cleanCsSource = cleanCsSource.Replace("sampleParams.ModelPath", modelInfo.ModelPathStr); + modelPath = modelInfo.ModelPathStr; + } + + cleanCsSource = cleanCsSource.Replace("sampleParams.CancellationToken", "CancellationToken.None"); + + var chatClientLoader = GetChatClientLoaderString(sample, modelInfos.Count > 1, modelPath); if (chatClientLoader != null) { cleanCsSource = cleanCsSource.Replace("sampleParams.GetIChatClientAsync()", chatClientLoader); - cleanCsSource = cleanCsSource.Replace("sampleParams.NotifyCompletion();", "App.Window?.ModelLoaded();"); } await File.WriteAllTextAsync(Path.Join(outputPath, $"{className}.xaml.cs"), cleanCsSource, cancellationToken); diff --git a/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs b/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs index 2540a2d..f05c76d 100644 --- a/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs +++ b/AIDevGallery/ProjectGenerator/Template/MainWindow.xaml.cs @@ -11,11 +11,7 @@ public MainWindow() this.InitializeComponent(); this.RootFrame.Loaded += (sender, args) => { - var sampleLoadingCts = new CancellationTokenSource(); - - var localModelDetails = new $sampleNavigationParameterName$(sampleLoadingCts.Token); - - RootFrame.Navigate(typeof($MainSamplePage$), localModelDetails); + RootFrame.Navigate(typeof($MainSamplePage$), new SampleNavigationParameters()); }; } diff --git a/AIDevGallery/ProjectGenerator/Template/MultiModelSampleNavigationParameters.cs b/AIDevGallery/ProjectGenerator/Template/MultiModelSampleNavigationParameters.cs deleted file mode 100644 index 08748ef..0000000 --- a/AIDevGallery/ProjectGenerator/Template/MultiModelSampleNavigationParameters.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Threading; - -namespace $safeprojectname$.SharedCode -{ - internal class MultiModelSampleNavigationParameters - { - public CancellationToken CancellationToken { get; private set; } - public string[] ModelPaths => $modelPath$; - public HardwareAccelerator[] HardwareAccelerators => $modelHardwareAccelerator$; - $promptTemplate$ - public MultiModelSampleNavigationParameters(CancellationToken loadingCanceledToken) - { - CancellationToken = loadingCanceledToken; - } - } -} \ No newline at end of file diff --git a/AIDevGallery/ProjectGenerator/Template/ProjectTemplate.csproj b/AIDevGallery/ProjectGenerator/Template/ProjectTemplate.csproj index 8befad8..0ec62ba 100644 --- a/AIDevGallery/ProjectGenerator/Template/ProjectTemplate.csproj +++ b/AIDevGallery/ProjectGenerator/Template/ProjectTemplate.csproj @@ -6,8 +6,7 @@ $safeprojectname$ app.manifest x86;x64;ARM64 - win-x86;win-x64;win-arm64 - win10-x86;win10-x64;win10-arm64 + win-x86;win-x64;win-arm64 win-$(Platform).pubxml true true diff --git a/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-arm64.pubxml b/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-arm64.pubxml index 866091b..8953cce 100644 --- a/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-arm64.pubxml +++ b/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-arm64.pubxml @@ -6,6 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. FileSystem ARM64 + win-arm64 bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\ true False diff --git a/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x64.pubxml b/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x64.pubxml index c2b581f..cd99561 100644 --- a/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x64.pubxml +++ b/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x64.pubxml @@ -6,6 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. FileSystem x64 + win-x64 bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\ true False diff --git a/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x86.pubxml b/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x86.pubxml index e4de929..a70c694 100644 --- a/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x86.pubxml +++ b/AIDevGallery/ProjectGenerator/Template/Properties/PublishProfiles/win-x86.pubxml @@ -6,6 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. FileSystem x86 + win-x86 bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\ true False diff --git a/AIDevGallery/ProjectGenerator/Template/SampleNavigationParameters.cs b/AIDevGallery/ProjectGenerator/Template/SampleNavigationParameters.cs index d01b547..89f7859 100644 --- a/AIDevGallery/ProjectGenerator/Template/SampleNavigationParameters.cs +++ b/AIDevGallery/ProjectGenerator/Template/SampleNavigationParameters.cs @@ -4,13 +4,6 @@ namespace $safeprojectname$.SharedCode { internal class SampleNavigationParameters { - public CancellationToken CancellationToken { get; private set; } - public string ModelPath => $modelPath$; - public HardwareAccelerator HardwareAccelerator => $modelHardwareAccelerator$; $promptTemplate$ - public SampleNavigationParameters(CancellationToken loadingCanceledToken) - { - CancellationToken = loadingCanceledToken; - } } } \ No newline at end of file From 6ffee63b7e7b5f8b4b05946cbe05f10c09440df9 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Sun, 17 Nov 2024 14:01:21 -0800 Subject: [PATCH 2/2] Small fix. --- AIDevGallery/ProjectGenerator/Generator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AIDevGallery/ProjectGenerator/Generator.cs b/AIDevGallery/ProjectGenerator/Generator.cs index 75ebb0b..ff7e4f1 100644 --- a/AIDevGallery/ProjectGenerator/Generator.cs +++ b/AIDevGallery/ProjectGenerator/Generator.cs @@ -368,11 +368,11 @@ private string GetChatClientLoaderString(Sample sample, bool isMultiModel, strin if (isMultiModel) { - return $"GenAIModel.CreateAsync({modelPath}, sampleParams.PromptTemplates[0], sampleParams.CancellationToken)"; + return $"GenAIModel.CreateAsync({modelPath}, sampleParams.PromptTemplates[0], System.Threading.CancellationToken.None)"; } else { - return $"GenAIModel.CreateAsync({modelPath}, sampleParams.PromptTemplate, sampleParams.CancellationToken)"; + return $"GenAIModel.CreateAsync({modelPath}, sampleParams.PromptTemplate, System.Threading.CancellationToken.None)"; } }