-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add hosting integration section to text-to-image quickstart #49896
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a9cdfb
Initial plan
Copilot 38c8190
Add hosting integration section to text-to-image article with code ex…
Copilot 8b5da8e
Update docs/ai/quickstarts/text-to-image.md
gewarren 3b9828b
Apply suggestions from code review
gewarren 1e6f6e8
human edits 2
gewarren b8f1c23
respond to feedback
gewarren 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
44 changes: 44 additions & 0 deletions
44
docs/ai/quickstarts/snippets/text-to-image/hosting/Program.cs
This file contains hidden or 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,44 @@ | ||
| // <SnippetSetup> | ||
| using Aspire.Azure.AI.OpenAI; | ||
| using Microsoft.Extensions.AI; | ||
| using OpenAI; | ||
|
|
||
| WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Add the Azure OpenAI client using hosting integration. | ||
| AspireAzureOpenAIClientBuilder openai = builder.AddAzureOpenAIClient("openai"); | ||
| // </SnippetSetup> | ||
|
|
||
| // <SnippetAddImageGenerator> | ||
| // Register the image generator with dependency injection. | ||
| ImageGeneratorBuilder imageBuilder = builder.Services.AddImageGenerator(services => | ||
| { | ||
| OpenAIClient openAiClient = services.GetRequiredService<OpenAIClient>(); | ||
| OpenAI.Images.ImageClient imageClient = openAiClient.GetImageClient("gpt-image-1"); | ||
| #pragma warning disable MEAI001 // Type is for evaluation purposes only. | ||
| return imageClient.AsIImageGenerator(); | ||
| #pragma warning restore MEAI001 | ||
| }); | ||
| // </SnippetAddImageGenerator> | ||
|
|
||
| // <SnippetConfigureOptions> | ||
| imageBuilder.ConfigureOptions(options => | ||
| { | ||
| options.MediaType = "image/png"; | ||
| }).UseLogging(); | ||
| // </SnippetConfigureOptions> | ||
|
|
||
| WebApplication app = builder.Build(); | ||
|
|
||
| // <SnippetUseImageGenerator> | ||
| // Use the image generator in an endpoint. | ||
| app.MapPost("/generate-image", async (IImageGenerator generator, string prompt) => | ||
| { | ||
| ImageGenerationResponse response = await generator.GenerateImagesAsync(prompt); | ||
| DataContent dataContent = response.Contents.OfType<DataContent>().First(); | ||
|
|
||
| return Results.File(dataContent.Data.ToArray(), "image/png"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed on my first review, but this might slightly better: return Results.File(dataContent.Data.ToArray(), dataContent.MediaType);
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll submit a PR. |
||
| }); | ||
| // </SnippetUseImageGenerator> | ||
|
|
||
| app.Run(); | ||
23 changes: 23 additions & 0 deletions
23
docs/ai/quickstarts/snippets/text-to-image/hosting/Properties/launchSettings.json
This file contains hidden or 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,23 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5219", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7210;http://localhost:5219", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
docs/ai/quickstarts/snippets/text-to-image/hosting/TextToImageHosting.csproj
This file contains hidden or 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,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <NoWarn>$(NoWarn);MEAI001</NoWarn> | ||
| <UserSecretsId>a9e545e9-e2b5-4e1b-81ce-217ca2d281c6</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Azure.AI.OpenAI" Version="13.0.0-preview.1.25560.3" /> | ||
| <PackageReference Include="Azure.AI.OpenAI" Version="2.5.0-beta.1" /> | ||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.0.0-preview.1.25560.10" /> | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
8 changes: 8 additions & 0 deletions
8
docs/ai/quickstarts/snippets/text-to-image/hosting/appsettings.Development.json
This file contains hidden or 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" | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
docs/ai/quickstarts/snippets/text-to-image/hosting/appsettings.json
This file contains hidden or 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,12 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "ConnectionStrings": { | ||
| "openai": "Endpoint=https://your-endpoint.com/;Key=your-api-key" | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.