Skip to content
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 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BicepSample.AppHost", "play
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BicepSample.ApiService", "playground\bicep\BicepSample.ApiService\BicepSample.ApiService.csproj", "{54FE1759-A648-4805-94FD-08F0F4B147CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAIEndToEnd.AppHost", "playground\OpenAIEndToEnd\OpenAIEndToEnd.AppHost\OpenAIEndToEnd.AppHost.csproj", "{13A785BC-A884-44CF-A8BA-BCF676294ED8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OpenAIEndToEnd", "OpenAIEndToEnd", "{80F5BEF8-A3D0-4F30-8C15-92001DF93539}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAIEndToEnd.WebStory", "playground\OpenAIEndToEnd\OpenAIEndToEnd.WebStory\OpenAIEndToEnd.WebStory.csproj", "{621BE1D9-D9D5-4D5E-8404-3F5CC492B555}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -647,6 +653,14 @@ Global
{54FE1759-A648-4805-94FD-08F0F4B147CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54FE1759-A648-4805-94FD-08F0F4B147CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54FE1759-A648-4805-94FD-08F0F4B147CF}.Release|Any CPU.Build.0 = Release|Any CPU
{13A785BC-A884-44CF-A8BA-BCF676294ED8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13A785BC-A884-44CF-A8BA-BCF676294ED8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13A785BC-A884-44CF-A8BA-BCF676294ED8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13A785BC-A884-44CF-A8BA-BCF676294ED8}.Release|Any CPU.Build.0 = Release|Any CPU
{621BE1D9-D9D5-4D5E-8404-3F5CC492B555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{621BE1D9-D9D5-4D5E-8404-3F5CC492B555}.Debug|Any CPU.Build.0 = Debug|Any CPU
{621BE1D9-D9D5-4D5E-8404-3F5CC492B555}.Release|Any CPU.ActiveCfg = Release|Any CPU
{621BE1D9-D9D5-4D5E-8404-3F5CC492B555}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -763,6 +777,9 @@ Global
{4E1F1835-7006-412D-83A1-740709A54A26} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
{7E67B5F3-ADAC-48E2-83CE-1E1BFF1F8505} = {4E1F1835-7006-412D-83A1-740709A54A26}
{54FE1759-A648-4805-94FD-08F0F4B147CF} = {4E1F1835-7006-412D-83A1-740709A54A26}
{13A785BC-A884-44CF-A8BA-BCF676294ED8} = {80F5BEF8-A3D0-4F30-8C15-92001DF93539}
{80F5BEF8-A3D0-4F30-8C15-92001DF93539} = {4E1F1835-7006-412D-83A1-740709A54A26}
{621BE1D9-D9D5-4D5E-8404-3F5CC492B555} = {80F5BEF8-A3D0-4F30-8C15-92001DF93539}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}
Expand Down
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>
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>
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 playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Program.cs
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");
Copy link
Member

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:

var openaiKey = builder.AddParameter("OpenAIKey");

var openai = builder.AddConnectionString($"Key={openaiKey.Value}");

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.

Copy link
Member Author

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). Only AddConnectionString(...) defaults to being secret.


builder.AddProject<Projects.OpenAIEndToEnd_WebStory>("webstory")
.WithReference(openai);

builder.Build().Run();
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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
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"
}
}
}
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>
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>
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;
}
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;
}
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();
}
}
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>
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
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 playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Program.cs
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();
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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
font-size: 6em;
}
Loading