├── .gitignore ├── CODE_OF_CONDUCT.md ├── ChatAppGenAI.sln ├── ChatAppGenAI ├── App.xaml ├── App.xaml.cs ├── Assets │ ├── LockScreenLogo.scale-200.png │ ├── SplashScreen.scale-200.png │ ├── Square150x150Logo.scale-200.png │ ├── Square44x44Logo.scale-200.png │ ├── Square44x44Logo.targetsize-24_altform-unplated.png │ ├── StoreLogo.png │ └── Wide310x150Logo.scale-200.png ├── ChatAppGenAI.csproj ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── Package.appxmanifest ├── Phi3Runner.cs ├── Properties │ └── launchSettings.json ├── app.manifest └── phi3 │ └── .gitignore ├── LICENSE ├── README.md ├── SECURITY.md └── SUPPORT.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Microsoft Open Source Code of Conduct 2 | 3 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 4 | 5 | Resources: 6 | 7 | - [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) 8 | - [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) 9 | - Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns 10 | -------------------------------------------------------------------------------- /ChatAppGenAI.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.9.34728.123 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatAppGenAI", "ChatAppGenAI\ChatAppGenAI.csproj", "{77F7B9D0-C842-48B0-9904-800DA51F5274}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|ARM64 = Debug|ARM64 11 | Debug|x64 = Debug|x64 12 | Debug|x86 = Debug|x86 13 | Release|ARM64 = Release|ARM64 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|ARM64.ActiveCfg = Debug|ARM64 19 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|ARM64.Build.0 = Debug|ARM64 20 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|ARM64.Deploy.0 = Debug|ARM64 21 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|x64.ActiveCfg = Debug|x64 22 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|x64.Build.0 = Debug|x64 23 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|x64.Deploy.0 = Debug|x64 24 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|x86.ActiveCfg = Debug|x86 25 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|x86.Build.0 = Debug|x86 26 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Debug|x86.Deploy.0 = Debug|x86 27 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|ARM64.ActiveCfg = Release|ARM64 28 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|ARM64.Build.0 = Release|ARM64 29 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|ARM64.Deploy.0 = Release|ARM64 30 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|x64.ActiveCfg = Release|x64 31 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|x64.Build.0 = Release|x64 32 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|x64.Deploy.0 = Release|x64 33 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|x86.ActiveCfg = Release|x86 34 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|x86.Build.0 = Release|x86 35 | {77F7B9D0-C842-48B0-9904-800DA51F5274}.Release|x86.Deploy.0 = Release|x86 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | GlobalSection(ExtensibilityGlobals) = postSolution 41 | SolutionGuid = {CC7D9610-5CEC-40A2-81F5-F5E052198846} 42 | EndGlobalSection 43 | EndGlobal 44 | -------------------------------------------------------------------------------- /ChatAppGenAI/App.xaml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /ChatAppGenAI/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.UI.Xaml; 2 | using Microsoft.UI.Xaml.Controls; 3 | using Microsoft.UI.Xaml.Controls.Primitives; 4 | using Microsoft.UI.Xaml.Data; 5 | using Microsoft.UI.Xaml.Input; 6 | using Microsoft.UI.Xaml.Media; 7 | using Microsoft.UI.Xaml.Navigation; 8 | using Microsoft.UI.Xaml.Shapes; 9 | using System; 10 | using System.Collections.Generic; 11 | using System.IO; 12 | using System.Linq; 13 | using System.Runtime.InteropServices.WindowsRuntime; 14 | using Windows.ApplicationModel; 15 | using Windows.ApplicationModel.Activation; 16 | using Windows.Foundation; 17 | using Windows.Foundation.Collections; 18 | 19 | // To learn more about WinUI, the WinUI project structure, 20 | // and more about our project templates, see: http://aka.ms/winui-project-info. 21 | 22 | namespace ChatAppGenAI 23 | { 24 | /// 25 | /// Provides application-specific behavior to supplement the default Application class. 26 | /// 27 | public partial class App : Application 28 | { 29 | /// 30 | /// Initializes the singleton application object. This is the first line of authored code 31 | /// executed, and as such is the logical equivalent of main() or WinMain(). 32 | /// 33 | public App() 34 | { 35 | this.InitializeComponent(); 36 | } 37 | 38 | /// 39 | /// Invoked when the application is launched. 40 | /// 41 | /// Details about the launch request and process. 42 | protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) 43 | { 44 | m_window = new MainWindow(); 45 | m_window.Activate(); 46 | } 47 | 48 | private Window m_window; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /ChatAppGenAI/Assets/LockScreenLogo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Phi3-Chat-WinUI3-Sample/623edcbe9332f0b01c5a6524075790bbbb4646ec/ChatAppGenAI/Assets/LockScreenLogo.scale-200.png -------------------------------------------------------------------------------- /ChatAppGenAI/Assets/SplashScreen.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Phi3-Chat-WinUI3-Sample/623edcbe9332f0b01c5a6524075790bbbb4646ec/ChatAppGenAI/Assets/SplashScreen.scale-200.png -------------------------------------------------------------------------------- /ChatAppGenAI/Assets/Square150x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Phi3-Chat-WinUI3-Sample/623edcbe9332f0b01c5a6524075790bbbb4646ec/ChatAppGenAI/Assets/Square150x150Logo.scale-200.png -------------------------------------------------------------------------------- /ChatAppGenAI/Assets/Square44x44Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Phi3-Chat-WinUI3-Sample/623edcbe9332f0b01c5a6524075790bbbb4646ec/ChatAppGenAI/Assets/Square44x44Logo.scale-200.png -------------------------------------------------------------------------------- /ChatAppGenAI/Assets/Square44x44Logo.targetsize-24_altform-unplated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Phi3-Chat-WinUI3-Sample/623edcbe9332f0b01c5a6524075790bbbb4646ec/ChatAppGenAI/Assets/Square44x44Logo.targetsize-24_altform-unplated.png -------------------------------------------------------------------------------- /ChatAppGenAI/Assets/StoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Phi3-Chat-WinUI3-Sample/623edcbe9332f0b01c5a6524075790bbbb4646ec/ChatAppGenAI/Assets/StoreLogo.png -------------------------------------------------------------------------------- /ChatAppGenAI/Assets/Wide310x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Phi3-Chat-WinUI3-Sample/623edcbe9332f0b01c5a6524075790bbbb4646ec/ChatAppGenAI/Assets/Wide310x150Logo.scale-200.png -------------------------------------------------------------------------------- /ChatAppGenAI/ChatAppGenAI.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | WinExe 4 | net6.0-windows10.0.19041.0 5 | 10.0.17763.0 6 | ChatAppGenAI 7 | app.manifest 8 | x86;x64;ARM64 9 | win10-x86;win10-x64;win10-arm64 10 | win10-$(Platform).pubxml 11 | true 12 | true 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | PreserveNewest 36 | 37 | 38 | 39 | 44 | 45 | 46 | 47 | 48 | 53 | 54 | true 55 | 56 | 57 | -------------------------------------------------------------------------------- /ChatAppGenAI/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 28 | 29 | 30 | 31 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 51 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /ChatAppGenAI/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using CommunityToolkit.Mvvm.ComponentModel; 2 | using Microsoft.UI; 3 | using Microsoft.UI.Dispatching; 4 | using Microsoft.UI.Xaml; 5 | using Microsoft.UI.Xaml.Controls; 6 | using Microsoft.UI.Xaml.Controls.Primitives; 7 | using Microsoft.UI.Xaml.Data; 8 | using Microsoft.UI.Xaml.Input; 9 | using Microsoft.UI.Xaml.Media; 10 | using Microsoft.UI.Xaml.Navigation; 11 | using System; 12 | using System.Collections.Generic; 13 | using System.Collections.ObjectModel; 14 | using System.IO; 15 | using System.Linq; 16 | using System.Runtime.InteropServices.WindowsRuntime; 17 | using System.Threading.Tasks; 18 | using Windows.Foundation; 19 | using Windows.Foundation.Collections; 20 | using Windows.UI; 21 | 22 | // To learn more about WinUI, the WinUI project structure, 23 | // and more about our project templates, see: http://aka.ms/winui-project-info. 24 | 25 | namespace ChatAppGenAI 26 | { 27 | /// 28 | /// An empty window that can be used on its own or navigated to within a Frame. 29 | /// 30 | public sealed partial class MainWindow : Window 31 | { 32 | private VM VM; 33 | 34 | public MainWindow() 35 | { 36 | this.InitializeComponent(); 37 | VM = new VM(DispatcherQueue); 38 | } 39 | 40 | private async void TextBox_KeyUp(object sender, KeyRoutedEventArgs e) 41 | { 42 | var textBox = sender as TextBox; 43 | if (e.Key == Windows.System.VirtualKey.Enter) 44 | { 45 | if (textBox.Text.Length > 0) 46 | { 47 | VM.AddMessage(textBox.Text); 48 | textBox.Text = string.Empty; 49 | } 50 | } 51 | } 52 | public static SolidColorBrush PhiMessageTypeToColor(PhiMessageType type) 53 | { 54 | return (type == PhiMessageType.User) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 68, 228, 255)); 55 | } 56 | 57 | public static SolidColorBrush PhiMessageTypeToForeground(PhiMessageType type) 58 | { 59 | return (type == PhiMessageType.User) ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Color.FromArgb(255, 80, 80, 80)); 60 | } 61 | 62 | public static Visibility BoolToVisibleInversed(bool value) 63 | { 64 | return value ? Visibility.Collapsed : Visibility.Visible; 65 | } 66 | } 67 | public partial class VM: ObservableObject 68 | { 69 | public ObservableCollection Messages = new(); 70 | 71 | [ObservableProperty] 72 | public bool acceptsMessages; 73 | 74 | private Phi3Runner phi3 = new(); 75 | private DispatcherQueue dispatcherQueue; 76 | 77 | public VM(DispatcherQueue dispatcherQueue) 78 | { 79 | phi3.ModelLoaded += Phi3_ModelLoaded; 80 | phi3.InitializeAsync(); 81 | this.dispatcherQueue = dispatcherQueue; 82 | } 83 | 84 | private void Phi3_ModelLoaded(object sender, EventArgs e) 85 | { 86 | dispatcherQueue.TryEnqueue(() => 87 | { 88 | AcceptsMessages = true; 89 | }); 90 | } 91 | 92 | public void AddMessage(string text) 93 | { 94 | AcceptsMessages = false; 95 | Messages.Add(new Message(text, DateTime.Now, PhiMessageType.User)); 96 | 97 | Task.Run(async () => 98 | { 99 | var systemPrompt = "You are a helpfull assistant"; 100 | var history = Messages.Select(m => new PhiMessage(m.Text, m.Type)).ToList(); 101 | 102 | var responseMessage = new Message("...", DateTime.Now, PhiMessageType.Assistant); 103 | 104 | dispatcherQueue.TryEnqueue(async () => 105 | { 106 | await Task.Delay(1000); 107 | Messages.Add(responseMessage); 108 | }); 109 | 110 | bool firstPart = true; 111 | 112 | await foreach (var messagePart in phi3.InferStreaming(systemPrompt, history, text)) 113 | { 114 | var part = messagePart; 115 | dispatcherQueue.TryEnqueue(() => 116 | { 117 | if (firstPart) 118 | { 119 | responseMessage.Text = string.Empty; 120 | firstPart = false; 121 | part = messagePart.TrimStart(); 122 | } 123 | 124 | responseMessage.Text += part; 125 | }); 126 | } 127 | 128 | dispatcherQueue.TryEnqueue(() => 129 | { 130 | AcceptsMessages = true; 131 | }); 132 | }); 133 | } 134 | } 135 | 136 | public partial class Message : ObservableObject 137 | { 138 | [ObservableProperty] 139 | public string text; 140 | public DateTime MsgDateTime { get; private set; } 141 | 142 | public PhiMessageType Type { get; set; } 143 | public HorizontalAlignment MsgAlignment => Type == PhiMessageType.User ? HorizontalAlignment.Right : HorizontalAlignment.Left; 144 | public Message(string text, DateTime dateTime, PhiMessageType type) 145 | { 146 | Text = text; 147 | MsgDateTime = dateTime; 148 | Type = type; 149 | } 150 | 151 | public override string ToString() 152 | { 153 | return MsgDateTime.ToString() + " " + Text; 154 | } 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /ChatAppGenAI/Package.appxmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 9 | 10 | 14 | 15 | 16 | 17 | 18 | ChatAppGenAI 19 | nikol 20 | Assets\StoreLogo.png 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 36 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /ChatAppGenAI/Phi3Runner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.ML.OnnxRuntimeGenAI; 3 | using System.IO; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using System.Diagnostics; 8 | using System.Diagnostics.CodeAnalysis; 9 | using System.Linq; 10 | using Windows.Services.Maps; 11 | using System.Runtime.CompilerServices; 12 | using System.Threading; 13 | 14 | namespace ChatAppGenAI 15 | { 16 | public class Phi3Runner : IDisposable 17 | { 18 | private readonly string ModelDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "phi3"); 19 | 20 | private Model? model = null; 21 | private Tokenizer? tokenizer = null; 22 | public event EventHandler? ModelLoaded = null; 23 | 24 | [MemberNotNullWhen(true, nameof(model), nameof(tokenizer))] 25 | public bool IsReady => model != null && tokenizer != null; 26 | 27 | public void Dispose() 28 | { 29 | model?.Dispose(); 30 | tokenizer?.Dispose(); 31 | } 32 | 33 | public IAsyncEnumerable InferStreaming(string systemPrompt, string userPrompt, [EnumeratorCancellation] CancellationToken ct = default) 34 | { 35 | var prompt = $@"<|system|>{systemPrompt}<|end|><|user|>{userPrompt}<|end|><|assistant|>"; 36 | return InferStreaming(prompt, ct); 37 | } 38 | 39 | public IAsyncEnumerable InferStreaming(string systemPrompt, List history, string userPrompt, [EnumeratorCancellation] CancellationToken ct = default) 40 | { 41 | var prompt = $@"<|system|>{systemPrompt}<|end|>"; 42 | foreach (var message in history) 43 | { 44 | prompt += $"<|{message.Type.ToString().ToLower()}|>{message.Text}<|end|>"; 45 | } 46 | prompt += "<|assistant|>"; 47 | 48 | return InferStreaming(prompt, ct); 49 | 50 | } 51 | public async IAsyncEnumerable InferStreaming(string prompt, [EnumeratorCancellation] CancellationToken ct = default) 52 | { 53 | if (!IsReady) 54 | { 55 | throw new InvalidOperationException("Model is not ready"); 56 | } 57 | 58 | var generatorParams = new GeneratorParams(model); 59 | 60 | var sequences = tokenizer.Encode(prompt); 61 | 62 | generatorParams.SetSearchOption("max_length", 1024); 63 | generatorParams.SetInputSequences(sequences); 64 | generatorParams.TryGraphCaptureWithMaxBatchSize(1); 65 | 66 | using var tokenizerStream = tokenizer.CreateStream(); 67 | using var generator = new Generator(model, generatorParams); 68 | StringBuilder stringBuilder = new(); 69 | while (!generator.IsDone()) 70 | { 71 | string part; 72 | try 73 | { 74 | if (ct.IsCancellationRequested) 75 | { 76 | break; 77 | } 78 | 79 | generator.ComputeLogits(); 80 | generator.GenerateNextToken(); 81 | part = tokenizerStream.Decode(generator.GetSequence(0)[^1]); 82 | stringBuilder.Append(part); 83 | if (stringBuilder.ToString().Contains("<|end|>") 84 | || stringBuilder.ToString().Contains("<|user|>") 85 | || stringBuilder.ToString().Contains("<|system|>")) 86 | { 87 | break; 88 | } 89 | } 90 | catch (Exception ex) 91 | { 92 | Debug.WriteLine(ex); 93 | break; 94 | } 95 | 96 | yield return part; 97 | } 98 | } 99 | 100 | public Task InitializeAsync() 101 | { 102 | return Task.Run(() => 103 | { 104 | var sw = Stopwatch.StartNew(); 105 | model = new Model(ModelDir); 106 | tokenizer = new Tokenizer(model); 107 | sw.Stop(); 108 | Debug.WriteLine($"Model loading took {sw.ElapsedMilliseconds} ms"); 109 | ModelLoaded?.Invoke(this, EventArgs.Empty); 110 | }); 111 | } 112 | } 113 | 114 | public class PhiMessage 115 | { 116 | public string Text { get; set; } 117 | public PhiMessageType Type { get; set; } 118 | 119 | public PhiMessage(string text, PhiMessageType type) 120 | { 121 | Text = text; 122 | Type = type; 123 | } 124 | } 125 | 126 | public enum PhiMessageType 127 | { 128 | User, 129 | Assistant 130 | } 131 | } -------------------------------------------------------------------------------- /ChatAppGenAI/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "ChatAppGenAI (Package)": { 4 | "commandName": "MsixPackage" 5 | }, 6 | "ChatAppGenAI (Unpackaged)": { 7 | "commandName": "Project" 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /ChatAppGenAI/app.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PerMonitorV2 17 | 18 | 19 | -------------------------------------------------------------------------------- /ChatAppGenAI/phi3/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Sample] Phi3 in WinUI3 app 2 | 3 | ## Set Up 4 | 5 | You will need to have Visual Studio installed with the latest workloads for WinAppSDK and WinUI 3 development. You can find instructions on how to set up your environment [here.](https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/set-up-your-development-environment?tabs=cs-vs-community%2Ccpp-vs-community%2Cvs-2022-17-1-a%2Cvs-2022-17-1-b#install-visual-studio) 6 | 7 | Clone the repository and open the solution in Visual Studio. Before you can get started exploring the sample, you will need to get the Phi3 model files required for the project. 8 | 9 | ## Downloading Phi3 10 | 11 | The model can be downloaded from the following link: 12 | - https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx 13 | 14 | Huggingface models are in repositories which you can clone to get the model files. Clone the Phi3 model repository and copy the required files to the phi3 folder in the project. 15 | 16 | Phi-3-mini-4k-instruct-onnx has 3 different versions inside it's repo. We are using the DirectML versions in this project. 17 | Copy the contents of the "directml/directml-int4-awq-block-128" folder to the phi3 folder in the solution. 18 | 19 | You don't need to modify the *.csproj, as it is already including all the files in the phi3 folder to the output directory. 20 | 21 | The final folder structure should look like this: 22 | 23 | ``` 24 | ChatAppGenAI 25 | ├── phi3 26 | │ ├── added_tokens.json 27 | │ ├── genai_config.json 28 | │ ├── model.onnx 29 | │ ├── model.onnx.data 30 | │ ├── special_tokens_map.json 31 | │ ├── tokenizer.json 32 | │ ├── tokenizer.model 33 | │ ├── tokenizer_config.json 34 | ``` -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Security 4 | 5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet) and [Xamarin](https://github.com/xamarin). 6 | 7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/security.md/definition), please report it to us as described below. 8 | 9 | ## Reporting Security Issues 10 | 11 | **Please do not report security vulnerabilities through public GitHub issues.** 12 | 13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/security.md/msrc/create-report). 14 | 15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/security.md/msrc/pgp). 16 | 17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc). 18 | 19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: 20 | 21 | * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) 22 | * Full paths of source file(s) related to the manifestation of the issue 23 | * The location of the affected source code (tag/branch/commit or direct URL) 24 | * Any special configuration required to reproduce the issue 25 | * Step-by-step instructions to reproduce the issue 26 | * Proof-of-concept or exploit code (if possible) 27 | * Impact of the issue, including how an attacker might exploit the issue 28 | 29 | This information will help us triage your report more quickly. 30 | 31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/security.md/msrc/bounty) page for more details about our active programs. 32 | 33 | ## Preferred Languages 34 | 35 | We prefer all communications to be in English. 36 | 37 | ## Policy 38 | 39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/security.md/cvd). 40 | 41 | 42 | -------------------------------------------------------------------------------- /SUPPORT.md: -------------------------------------------------------------------------------- 1 | # Support 2 | 3 | ## How to file issues and get help 4 | 5 | This project uses GitHub Issues to track bugs and feature requests. Please search the existing 6 | issues before filing new issues to avoid duplicates. For new issues, file your bug or 7 | feature request as a new Issue. 8 | 9 | ## Microsoft Support Policy 10 | 11 | Support for this sample is limited to the resources listed above. 12 | --------------------------------------------------------------------------------