├── .gitignore ├── OpenAiQuickStartCSharp ├── Configuration │ └── OpenAiConfig.cs ├── Controllers │ ├── AnimalRequestModel.cs │ └── OpenAiController.cs ├── Dockerfile ├── OpenAiQuickStartCSharp.csproj ├── Pages │ ├── Index.cshtml │ └── Index.cshtml.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── appsettings.json └── wwwroot │ └── css │ └── site.css ├── README.md ├── openai-quickstart-csharp.sln └── public └── dog.png /.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 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.tlog 97 | *.vspscc 98 | *.vssscc 99 | .builds 100 | *.pidb 101 | *.svclog 102 | *.scc 103 | 104 | # Chutzpah Test files 105 | _Chutzpah* 106 | 107 | # Visual C++ cache files 108 | ipch/ 109 | *.aps 110 | *.ncb 111 | *.opendb 112 | *.opensdf 113 | *.sdf 114 | *.cachefile 115 | *.VC.db 116 | *.VC.VC.opendb 117 | 118 | # Visual Studio profiler 119 | *.psess 120 | *.vsp 121 | *.vspx 122 | *.sap 123 | 124 | # Visual Studio Trace Files 125 | *.e2e 126 | 127 | # TFS 2012 Local Workspace 128 | $tf/ 129 | 130 | # Guidance Automation Toolkit 131 | *.gpState 132 | 133 | # ReSharper is a .NET coding add-in 134 | _ReSharper*/ 135 | *.[Rr]e[Ss]harper 136 | *.DotSettings.user 137 | 138 | # TeamCity is a build add-in 139 | _TeamCity* 140 | 141 | # DotCover is a Code Coverage Tool 142 | *.dotCover 143 | 144 | # AxoCover is a Code Coverage Tool 145 | .axoCover/* 146 | !.axoCover/settings.json 147 | 148 | # Coverlet is a free, cross platform Code Coverage Tool 149 | coverage*.json 150 | coverage*.xml 151 | coverage*.info 152 | 153 | # Visual Studio code coverage results 154 | *.coverage 155 | *.coveragexml 156 | 157 | # NCrunch 158 | _NCrunch_* 159 | .*crunch*.local.xml 160 | nCrunchTemp_* 161 | 162 | # MightyMoose 163 | *.mm.* 164 | AutoTest.Net/ 165 | 166 | # Web workbench (sass) 167 | .sass-cache/ 168 | 169 | # Installshield output folder 170 | [Ee]xpress/ 171 | 172 | # DocProject is a documentation generator add-in 173 | DocProject/buildhelp/ 174 | DocProject/Help/*.HxT 175 | DocProject/Help/*.HxC 176 | DocProject/Help/*.hhc 177 | DocProject/Help/*.hhk 178 | DocProject/Help/*.hhp 179 | DocProject/Help/Html2 180 | DocProject/Help/html 181 | 182 | # Click-Once directory 183 | publish/ 184 | 185 | # Publish Web Output 186 | *.[Pp]ublish.xml 187 | *.azurePubxml 188 | # Note: Comment the next line if you want to checkin your web deploy settings, 189 | # but database connection strings (with potential passwords) will be unencrypted 190 | *.pubxml 191 | *.publishproj 192 | 193 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 194 | # checkin your Azure Web App publish settings, but sensitive information contained 195 | # in these scripts will be unencrypted 196 | PublishScripts/ 197 | 198 | # NuGet Packages 199 | *.nupkg 200 | # NuGet Symbol Packages 201 | *.snupkg 202 | # The packages folder can be ignored because of Package Restore 203 | **/[Pp]ackages/* 204 | # except build/, which is used as an MSBuild target. 205 | !**/[Pp]ackages/build/ 206 | # Uncomment if necessary however generally it will be regenerated when needed 207 | #!**/[Pp]ackages/repositories.config 208 | # NuGet v3's project.json files produces more ignorable files 209 | *.nuget.props 210 | *.nuget.targets 211 | 212 | # Microsoft Azure Build Output 213 | csx/ 214 | *.build.csdef 215 | 216 | # Microsoft Azure Emulator 217 | ecf/ 218 | rcf/ 219 | 220 | # Windows Store app package directories and files 221 | AppPackages/ 222 | BundleArtifacts/ 223 | Package.StoreAssociation.xml 224 | _pkginfo.txt 225 | *.appx 226 | *.appxbundle 227 | *.appxupload 228 | 229 | # Visual Studio cache files 230 | # files ending in .cache can be ignored 231 | *.[Cc]ache 232 | # but keep track of directories ending in .cache 233 | !?*.[Cc]ache/ 234 | 235 | # Others 236 | ClientBin/ 237 | ~$* 238 | *~ 239 | *.dbmdl 240 | *.dbproj.schemaview 241 | *.jfm 242 | *.pfx 243 | *.publishsettings 244 | orleans.codegen.cs 245 | 246 | # Including strong name files can present a security risk 247 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 248 | #*.snk 249 | 250 | # Since there are multiple workflows, uncomment next line to ignore bower_components 251 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 252 | #bower_components/ 253 | 254 | # RIA/Silverlight projects 255 | Generated_Code/ 256 | 257 | # Backup & report files from converting an old project file 258 | # to a newer Visual Studio version. Backup files are not needed, 259 | # because we have git ;-) 260 | _UpgradeReport_Files/ 261 | Backup*/ 262 | UpgradeLog*.XML 263 | UpgradeLog*.htm 264 | ServiceFabricBackup/ 265 | *.rptproj.bak 266 | 267 | # SQL Server files 268 | *.mdf 269 | *.ldf 270 | *.ndf 271 | 272 | # Business Intelligence projects 273 | *.rdl.data 274 | *.bim.layout 275 | *.bim_*.settings 276 | *.rptproj.rsuser 277 | *- [Bb]ackup.rdl 278 | *- [Bb]ackup ([0-9]).rdl 279 | *- [Bb]ackup ([0-9][0-9]).rdl 280 | 281 | # Microsoft Fakes 282 | FakesAssemblies/ 283 | 284 | # GhostDoc plugin setting file 285 | *.GhostDoc.xml 286 | 287 | # Node.js Tools for Visual Studio 288 | .ntvs_analysis.dat 289 | node_modules/ 290 | 291 | # Visual Studio 6 build log 292 | *.plg 293 | 294 | # Visual Studio 6 workspace options file 295 | *.opt 296 | 297 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 298 | *.vbw 299 | 300 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 301 | *.vbp 302 | 303 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 304 | *.dsw 305 | *.dsp 306 | 307 | # Visual Studio 6 technical files 308 | *.ncb 309 | *.aps 310 | 311 | # Visual Studio LightSwitch build output 312 | **/*.HTMLClient/GeneratedArtifacts 313 | **/*.DesktopClient/GeneratedArtifacts 314 | **/*.DesktopClient/ModelManifest.xml 315 | **/*.Server/GeneratedArtifacts 316 | **/*.Server/ModelManifest.xml 317 | _Pvt_Extensions 318 | 319 | # Paket dependency manager 320 | .paket/paket.exe 321 | paket-files/ 322 | 323 | # FAKE - F# Make 324 | .fake/ 325 | 326 | # CodeRush personal settings 327 | .cr/personal 328 | 329 | # Python Tools for Visual Studio (PTVS) 330 | __pycache__/ 331 | *.pyc 332 | 333 | # Cake - Uncomment if you are using it 334 | # tools/** 335 | # !tools/packages.config 336 | 337 | # Tabs Studio 338 | *.tss 339 | 340 | # Telerik's JustMock configuration file 341 | *.jmconfig 342 | 343 | # BizTalk build output 344 | *.btp.cs 345 | *.btm.cs 346 | *.odx.cs 347 | *.xsd.cs 348 | 349 | # OpenCover UI analysis results 350 | OpenCover/ 351 | 352 | # Azure Stream Analytics local run output 353 | ASALocalRun/ 354 | 355 | # MSBuild Binary and Structured Log 356 | *.binlog 357 | 358 | # NVidia Nsight GPU debugger configuration file 359 | *.nvuser 360 | 361 | # MFractors (Xamarin productivity tool) working folder 362 | .mfractor/ 363 | 364 | # Local History for Visual Studio 365 | .localhistory/ 366 | 367 | # Visual Studio History (VSHistory) files 368 | .vshistory/ 369 | 370 | # BeatPulse healthcheck temp database 371 | healthchecksdb 372 | 373 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 374 | MigrationBackup/ 375 | 376 | # Ionide (cross platform F# VS Code tools) working folder 377 | .ionide/ 378 | 379 | # Fody - auto-generated XML schema 380 | FodyWeavers.xsd 381 | 382 | # VS Code files for those working on multiple tools 383 | .vscode/* 384 | !.vscode/settings.json 385 | !.vscode/tasks.json 386 | !.vscode/launch.json 387 | !.vscode/extensions.json 388 | *.code-workspace 389 | 390 | # Local History for Visual Studio Code 391 | .history/ 392 | 393 | # Windows Installer files from build outputs 394 | *.cab 395 | *.msi 396 | *.msix 397 | *.msm 398 | *.msp 399 | 400 | # JetBrains Rider 401 | *.sln.iml 402 | 403 | ## 404 | ## Visual studio for Mac 405 | ## 406 | 407 | 408 | # globs 409 | Makefile.in 410 | *.userprefs 411 | *.usertasks 412 | config.make 413 | config.status 414 | aclocal.m4 415 | install-sh 416 | autom4te.cache/ 417 | *.tar.gz 418 | tarballs/ 419 | test-results/ 420 | 421 | # Mac bundle stuff 422 | *.dmg 423 | *.app 424 | 425 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 426 | # General 427 | .DS_Store 428 | .AppleDouble 429 | .LSOverride 430 | 431 | # Icon must end with two \r 432 | Icon 433 | 434 | 435 | # Thumbnails 436 | ._* 437 | 438 | # Files that might appear in the root of a volume 439 | .DocumentRevisions-V100 440 | .fseventsd 441 | .Spotlight-V100 442 | .TemporaryItems 443 | .Trashes 444 | .VolumeIcon.icns 445 | .com.apple.timemachine.donotpresent 446 | 447 | # Directories potentially created on remote AFP share 448 | .AppleDB 449 | .AppleDesktop 450 | Network Trash Folder 451 | Temporary Items 452 | .apdisk 453 | 454 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 455 | # Windows thumbnail cache files 456 | Thumbs.db 457 | ehthumbs.db 458 | ehthumbs_vista.db 459 | 460 | # Dump file 461 | *.stackdump 462 | 463 | # Folder config file 464 | [Dd]esktop.ini 465 | 466 | # Recycle Bin used on file shares 467 | $RECYCLE.BIN/ 468 | 469 | # Windows Installer files 470 | *.cab 471 | *.msi 472 | *.msix 473 | *.msm 474 | *.msp 475 | 476 | # Windows shortcuts 477 | *.lnk 478 | 479 | # Rider files 480 | .idea -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Configuration/OpenAiConfig.cs: -------------------------------------------------------------------------------- 1 | namespace OpenAiQuickStartCSharp.Configuration; 2 | 3 | public class OpenAiConfig 4 | { 5 | public required string ApiKey { get; init; } 6 | } -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Controllers/AnimalRequestModel.cs: -------------------------------------------------------------------------------- 1 | namespace OpenAiQuickStartCSharp.Controllers; 2 | 3 | public class AnimalRequestModel 4 | { 5 | public string Animal { get; set; } 6 | } -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Controllers/OpenAiController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using Microsoft.Extensions.Options; 3 | using OpenAiQuickStartCSharp.Configuration; 4 | 5 | namespace OpenAiQuickStartCSharp.Controllers; 6 | 7 | [ApiController] 8 | [Route("[controller]")] 9 | public class OpenAiController : ControllerBase 10 | { 11 | private readonly OpenAiConfig _config; 12 | 13 | public OpenAiController(IOptions config) 14 | { 15 | _config = config.Value; 16 | } 17 | 18 | [HttpPost] 19 | public async Task GenerateSuperheroNames([FromBody] AnimalRequestModel request) 20 | { 21 | var apiKey = _config.ApiKey; 22 | 23 | if (string.IsNullOrEmpty(apiKey)) 24 | { 25 | return StatusCode(500, new 26 | { 27 | error = new 28 | { 29 | message = "OpenAI API key not configured, please follow instructions in README.md" 30 | } 31 | }); 32 | } 33 | 34 | var openai = new OpenAI_API.OpenAIAPI(apiKey); 35 | 36 | var animal = request.Animal ?? ""; 37 | if (string.IsNullOrWhiteSpace(animal)) 38 | { 39 | return StatusCode(400, new 40 | { 41 | error = new 42 | { 43 | message = "Please enter a valid animal" 44 | } 45 | }); 46 | } 47 | 48 | try 49 | { 50 | var completion = await openai.Completions.CreateCompletionAsync( 51 | prompt: GeneratePrompt(animal), 52 | temperature: 1 53 | ); 54 | 55 | return Ok(completion.Completions[0].Text); 56 | } 57 | catch (Exception error) 58 | { 59 | await Console.Error.WriteLineAsync($"Error with OpenAI API request: {error.Message}"); 60 | return StatusCode(500, new 61 | { 62 | error = new 63 | { 64 | message = "An error occurred during your request." 65 | } 66 | }); 67 | } 68 | } 69 | 70 | private string GeneratePrompt(string animal) 71 | { 72 | var capitalizedAnimal = $"{char.ToUpper(animal[0])}{animal.Substring(1).ToLower()}"; 73 | return @$"Suggest three names for an animal that is a superhero. 74 | 75 | Animal: Cat 76 | Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline 77 | Animal: Dog 78 | Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot 79 | Animal: {capitalizedAnimal} 80 | Names:"; 81 | } 82 | } -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base 2 | WORKDIR /app 3 | EXPOSE 80 4 | EXPOSE 443 5 | 6 | FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build 7 | WORKDIR /src 8 | COPY ["OpenAiQuickStartCSharp/OpenAiQuickStartCSharp.csproj", "OpenAiQuickStartCSharp/"] 9 | RUN dotnet restore "OpenAiQuickStartCSharp/OpenAiQuickStartCSharp.csproj" 10 | COPY . . 11 | WORKDIR "/src/OpenAiQuickStartCSharp" 12 | RUN dotnet build "OpenAiQuickStartCSharp.csproj" -c Release -o /app/build 13 | 14 | FROM build AS publish 15 | RUN dotnet publish "OpenAiQuickStartCSharp.csproj" -c Release -o /app/publish 16 | 17 | FROM base AS final 18 | WORKDIR /app 19 | COPY --from=publish /app/publish . 20 | ENTRYPOINT ["dotnet", "OpenAiQuickStartCSharp.dll"] 21 | -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/OpenAiQuickStartCSharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | Linux 8 | OpenAiQuickStartCSharp 9 | 10 | 11 | 12 | 13 | .dockerignore 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | <_ContentIncludedByDefault Remove="Pages\Index.cshtml" /> 23 | <_ContentIncludedByDefault Remove="Pages\Shared\_Layout.cshtml" /> 24 | <_ContentIncludedByDefault Remove="Pages\Shared\_ValidationScriptsPartial.cshtml" /> 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Pages/Index.cshtml: -------------------------------------------------------------------------------- 1 | @page 2 | @model OpenAiQuickStartCSharp.Pages.IndexModel 3 | 4 | 5 | 6 | 7 | My Page Title 8 | 9 | 10 | 11 |
12 |

🐕‍🦺

13 | 14 | 15 | 16 |
17 |
18 | Suggested Names: 19 | 20 |
21 | 22 | 23 | 46 | -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Pages/Index.cshtml.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc.RazorPages; 2 | 3 | namespace OpenAiQuickStartCSharp.Pages; 4 | 5 | public class IndexModel : PageModel 6 | { 7 | public string Name { get; set; } 8 | 9 | public void OnGet() 10 | { 11 | Name = "World"; 12 | } 13 | } -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Program.cs: -------------------------------------------------------------------------------- 1 | using OpenAiQuickStartCSharp.Configuration; 2 | 3 | var builder = WebApplication.CreateBuilder(args); 4 | 5 | builder.Services.AddRazorPages(); 6 | builder.Services.AddControllers(); 7 | 8 | LoadConfiguration(builder); 9 | 10 | var app = builder.Build(); 11 | 12 | app.UseStaticFiles(); 13 | 14 | app.MapControllers(); 15 | app.UseRouting(); 16 | 17 | app.UseEndpoints(endpoints => 18 | { 19 | endpoints.MapRazorPages(); 20 | }); 21 | 22 | app.Run(); 23 | 24 | void LoadConfiguration(WebApplicationBuilder webApplicationBuilder) 25 | { 26 | const string aspNetCoreEnvironment = "ASPNETCORE_ENVIRONMENT"; 27 | string? environmentName = Environment.GetEnvironmentVariable(aspNetCoreEnvironment); 28 | 29 | webApplicationBuilder.Host.ConfigureAppConfiguration((_, config) => 30 | { 31 | config.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true); 32 | config.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true); 33 | }); 34 | webApplicationBuilder.Services.Configure( 35 | webApplicationBuilder.Configuration.GetSection("OpenAiConfig")); 36 | } -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "http": { 4 | "commandName": "Project", 5 | "dotnetRunMessages": true, 6 | "applicationUrl": "http://localhost:5078", 7 | "environmentVariables": { 8 | "ASPNETCORE_ENVIRONMENT": "Development" 9 | } 10 | }, 11 | "https": { 12 | "commandName": "Project", 13 | "dotnetRunMessages": true, 14 | "applicationUrl": "https://localhost:7015;http://localhost:5078", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*", 9 | "OpenAiConfig": { 10 | "ApiKey": "paste your key here" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /OpenAiQuickStartCSharp/wwwroot/css/site.css: -------------------------------------------------------------------------------- 1 | .input-form { 2 | display: flex; 3 | flex-direction: column; 4 | justify-content: center; 5 | align-items: center; 6 | margin-top: 100px; 7 | } 8 | 9 | .input-form label { 10 | margin-bottom: 20px; 11 | font-size: xx-large; 12 | } 13 | 14 | .input-form input { 15 | width: 200px; 16 | margin-bottom: 20px; 17 | padding: 10px; 18 | border-radius: 5px; 19 | border: 1px solid #ccc; 20 | } 21 | 22 | .input-form button { 23 | width: 220px; 24 | padding: 10px 20px; 25 | border-radius: 5px; 26 | border: none; 27 | background-color: #007bff; 28 | color: #fff; 29 | font-weight: bold; 30 | cursor: pointer; 31 | } 32 | 33 | .output-block { 34 | margin-top: 40px; 35 | display: flex; 36 | flex-direction: column; 37 | justify-content: center; 38 | align-items: center; 39 | height: 100%; 40 | } 41 | 42 | .output-block span { 43 | margin-top: 10px; 44 | font-size: 18px; 45 | text-align: center; 46 | } 47 | 48 | .output-block span:first-child { 49 | margin-right: 10px; 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenAI API Quickstart - CSharp example app 2 | 3 | This is an example pet name generator app used in the OpenAI API [quickstart tutorial](https://platform.openai.com/docs/quickstart). It uses the [.NET 7.0 framework.](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) 4 | 5 | ![Text box that says name my pet with an icon of a dog](https://github.com/Undermove/openai-quickstart-csharp/blob/main/public/dog.png?raw=true) 6 | 7 | ## Setup 8 | 9 | 1. If you don’t have .NET SDK 7.0 installed, [install it from here](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) 10 | 11 | 2. Clone this repository 12 | 13 | 3. Generate OpenAi API key [here](https://platform.openai.com/docs/quickstart/add-your-api-key) 14 | 15 | 4. Paste API key value to appsettings.json file in following section: 16 | ```yaml 17 | "OpenAiConfig": { 18 | "ApiKey": "" 19 | } 20 | ``` 21 | 22 | 5. Navigate into the project directory 23 | 24 | ```bash 25 | $ cd openai-quickstart-csharp/OpenAiQuickStartCSharp/ 26 | ``` 27 | 28 | 6. Run the app 29 | 30 | ```bash 31 | $ dotnet run 32 | ``` 33 | 34 | 7. Test your app: 35 | 36 | Open app web page http://localhost:5078/ 37 | 38 | Or use curl: 39 | ```bash 40 | $ curl --location 'http://localhost:5078/OpenAi' --header 'Content-Type: application/json' --data '{ "Animal": "Cat"}' 41 | ``` 42 | -------------------------------------------------------------------------------- /openai-quickstart-csharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAiQuickStartCSharp", "OpenAiQuickStartCSharp\OpenAiQuickStartCSharp.csproj", "{73BB5705-246A-4607-86AD-241EC150B184}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Any CPU = Debug|Any CPU 8 | Release|Any CPU = Release|Any CPU 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {73BB5705-246A-4607-86AD-241EC150B184}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 12 | {73BB5705-246A-4607-86AD-241EC150B184}.Debug|Any CPU.Build.0 = Debug|Any CPU 13 | {73BB5705-246A-4607-86AD-241EC150B184}.Release|Any CPU.ActiveCfg = Release|Any CPU 14 | {73BB5705-246A-4607-86AD-241EC150B184}.Release|Any CPU.Build.0 = Release|Any CPU 15 | EndGlobalSection 16 | EndGlobal 17 | -------------------------------------------------------------------------------- /public/dog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Undermove/openai-quickstart-csharp/a24940d46c64f8bb021fd09675edfc3236ae8a27/public/dog.png --------------------------------------------------------------------------------