-
Notifications
You must be signed in to change notification settings - Fork 462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean-up Open AI resource. #2115
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Directory.Build.props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project> | ||
|
||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" /> | ||
|
||
<!-- NOTE: This line is only required because we are using P2P references, not NuGet. It will not exist in real apps. --> | ||
<Import Project="../../../src/Aspire.Hosting/build/Aspire.Hosting.props" /> | ||
|
||
</Project> |
9 changes: 9 additions & 0 deletions
9
playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Directory.Build.targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project> | ||
|
||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" /> | ||
|
||
<!-- NOTE: These lines are only required because we are using P2P references, not NuGet. They will not exist in real apps. --> | ||
<Import Project="..\..\..\src\Aspire.Hosting\build\Aspire.Hosting.targets" /> | ||
<Import Project="..\..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" /> | ||
|
||
</Project> |
22 changes: 22 additions & 0 deletions
22
playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/OpenAIEndToEnd.AppHost.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireHost>true</IsAspireHost> | ||
<UserSecretsId>b757dbc4-6942-448f-9f26-d15de0f2e760</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="$(SharedDir)KnownResourceNames.cs" Link="KnownResourceNames.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Aspire.Dashboard\Aspire.Dashboard.csproj" /> | ||
<ProjectReference Include="..\..\..\src\Aspire.Hosting\Aspire.Hosting.csproj" IsAspireProjectResource="false" /> | ||
<ProjectReference Include="..\OpenAIEndToEnd.WebStory\OpenAIEndToEnd.WebStory.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
11 changes: 11 additions & 0 deletions
11
playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var openai = builder.AddConnectionString("openai"); | ||
|
||
builder.AddProject<Projects.OpenAIEndToEnd_WebStory>("webstory") | ||
.WithReference(openai); | ||
|
||
builder.Build().Run(); |
16 changes: 16 additions & 0 deletions
16
playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:15216", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16195" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning", | ||
"Aspire.Hosting.Dcp": "Warning" | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/App.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<base href="/" /> | ||
<link rel="stylesheet" href="app.css" /> | ||
<link rel="stylesheet" href="OpenAIEndToEnd.WebStory.styles.css" /> | ||
<HeadOutlet @rendermode="InteractiveServer" /> | ||
</head> | ||
|
||
<body> | ||
<Routes @rendermode="InteractiveServer" /> | ||
<script src="_framework/blazor.web.js"></script> | ||
</body> | ||
|
||
</html> |
9 changes: 9 additions & 0 deletions
9
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Layout/MainLayout.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@inherits LayoutComponentBase | ||
|
||
@Body | ||
|
||
<div id="blazor-error-ui"> | ||
An unhandled error has occurred. | ||
<a href="" class="reload">Reload</a> | ||
<a class="dismiss">🗙</a> | ||
</div> |
18 changes: 18 additions & 0 deletions
18
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Layout/MainLayout.razor.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#blazor-error-ui { | ||
background: lightyellow; | ||
bottom: 0; | ||
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2); | ||
display: none; | ||
left: 0; | ||
padding: 0.6rem 1.25rem 0.7rem 1.25rem; | ||
position: fixed; | ||
width: 100%; | ||
z-index: 1000; | ||
} | ||
|
||
#blazor-error-ui .dismiss { | ||
cursor: pointer; | ||
position: absolute; | ||
right: 0.75rem; | ||
top: 0.5rem; | ||
} |
36 changes: 36 additions & 0 deletions
36
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/Error.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@page "/Error" | ||
@using System.Diagnostics | ||
|
||
<PageTitle>Error</PageTitle> | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> | ||
|
||
@code{ | ||
[CascadingParameter] | ||
private HttpContext? HttpContext { get; set; } | ||
|
||
private string? RequestId { get; set; } | ||
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
protected override void OnInitialized() => | ||
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; | ||
} |
46 changes: 46 additions & 0 deletions
46
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/Home.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
@page "/" | ||
@using Azure.AI.OpenAI | ||
@inject OpenAIClient aiClient | ||
@inject ILogger<Home> logger | ||
|
||
<div class="storybox" style="margin: 25%"> | ||
@foreach (var message in chatCompletionOptions.Messages.OfType<ChatRequestAssistantMessage>()) | ||
{ | ||
<p style="font-size: 3em;">@message.Content</p> | ||
} | ||
|
||
<button @onclick="GenerateNextParagraph" autofocus>Generate</button> | ||
</div> | ||
|
||
@code { | ||
private ChatCompletionsOptions chatCompletionOptions = new ChatCompletionsOptions() | ||
{ | ||
DeploymentName = "gpt-3.5-turbo", | ||
Messages = | ||
{ | ||
new ChatRequestSystemMessage("Pick a random topic and write a sentence of a fictional story about it.") | ||
} | ||
}; | ||
|
||
private async Task GenerateNextParagraph() | ||
{ | ||
if (chatCompletionOptions.Messages.Count > 1) | ||
{ | ||
chatCompletionOptions.Messages.Add( | ||
new ChatRequestUserMessage("Write the next sentence in the story.") | ||
); | ||
} | ||
|
||
var response = await aiClient.GetChatCompletionsAsync(chatCompletionOptions); | ||
var x = new ChatRequestAssistantMessage(response.Value.Choices[0].Message); | ||
chatCompletionOptions.Messages.Add(x); | ||
|
||
this.StateHasChanged(); | ||
} | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await GenerateNextParagraph(); | ||
await base.OnInitializedAsync(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Routes.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Router AppAssembly="typeof(Program).Assembly"> | ||
<Found Context="routeData"> | ||
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" /> | ||
<FocusOnNavigate RouteData="routeData" Selector="h1" /> | ||
</Found> | ||
</Router> |
10 changes: 10 additions & 0 deletions
10
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/_Imports.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@using System.Net.Http | ||
@using System.Net.Http.Json | ||
@using Microsoft.AspNetCore.Components.Forms | ||
@using Microsoft.AspNetCore.Components.Routing | ||
@using Microsoft.AspNetCore.Components.Web | ||
@using static Microsoft.AspNetCore.Components.Web.RenderMode | ||
@using Microsoft.AspNetCore.Components.Web.Virtualization | ||
@using Microsoft.JSInterop | ||
@using OpenAIEndToEnd.WebStory | ||
@using OpenAIEndToEnd.WebStory.Components |
14 changes: 14 additions & 0 deletions
14
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/OpenAIEndToEnd.WebStory.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Components\Aspire.Azure.AI.OpenAI\Aspire.Azure.AI.OpenAI.csproj" /> | ||
<ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
34 changes: 34 additions & 0 deletions
34
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using OpenAIEndToEnd.WebStory.Components; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.AddServiceDefaults(); | ||
|
||
builder.AddAzureOpenAI("openai"); | ||
|
||
// Add services to the container. | ||
builder.Services.AddRazorComponents() | ||
.AddInteractiveServerComponents(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (!app.Environment.IsDevelopment()) | ||
{ | ||
app.UseExceptionHandler("/Error", createScopeForErrors: true); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseStaticFiles(); | ||
app.UseAntiforgery(); | ||
|
||
app.MapRazorComponents<App>() | ||
.AddInteractiveServerRenderMode(); | ||
|
||
app.Run(); |
38 changes: 38 additions & 0 deletions
38
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:54244", | ||
"sslPort": 44362 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:5292", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:7024;http://localhost:5292", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
3 changes: 3 additions & 0 deletions
3
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/wwwroot/app.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
p { | ||
font-size: 6em; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to use Parameters and ConnectionString together?
For example imagine I have:
That way people wouldn't need to know about the connection string format. They would just stick the actual key into the parameter, and the above code would do the formatting for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No that isn't possible at the moment. File an issue? There would need to be some consideration on how interpolation would work here because the "secretness" of one parameter would have to poison all the other parameters it was injected into (btw in this example you would want to use
AddParameter("OpenAIKey", secret: true)
. OnlyAddConnectionString(...)
defaults to being secret.