-
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
Open
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/add-hosting-section
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 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 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
41 changes: 41 additions & 0 deletions
41
docs/ai/quickstarts/snippets/text-to-image/hosting/TextToImageHosting/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,41 @@ | ||
| // <SnippetSetup> | ||
| using Microsoft.Extensions.AI; | ||
| using OpenAI; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Add the Azure OpenAI client using hosting integration | ||
| var openai = builder.AddAzureOpenAIClient("openai"); | ||
| // </SnippetSetup> | ||
|
|
||
| // <SnippetAddImageGenerator> | ||
| // Register the image generator with dependency injection | ||
| builder.Services.AddImageGenerator(services => | ||
| { | ||
| var openAiClient = services.GetRequiredService<OpenAIClient>(); | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var imageClient = openAiClient.GetImageClient("gpt-image-1"); | ||
| #pragma warning disable MEAI001 // Type is for evaluation purposes only. | ||
| return imageClient.AsIImageGenerator(); | ||
| #pragma warning restore MEAI001 | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| // </SnippetAddImageGenerator> | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| // <SnippetUseImageGenerator> | ||
| // Use the image generator in an endpoint | ||
| app.MapPost("/generate-image", async (IImageGenerator generator, string prompt) => | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var options = new ImageGenerationOptions | ||
| { | ||
| MediaType = "image/png" | ||
| }; | ||
|
|
||
| var response = await generator.GenerateImagesAsync(prompt, options); | ||
| var dataContent = response.Contents.OfType<DataContent>().First(); | ||
|
|
||
| return Results.File(dataContent.Data.ToArray(), "image/png"); | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| // </SnippetUseImageGenerator> | ||
|
|
||
| app.Run(); | ||
23 changes: 23 additions & 0 deletions
23
...ckstarts/snippets/text-to-image/hosting/TextToImageHosting/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" | ||
| } | ||
| } | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...i/quickstarts/snippets/text-to-image/hosting/TextToImageHosting/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,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <NoWarn>$(NoWarn);MEAI001</NoWarn> | ||
| </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" /> | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.0.0-preview.1.25560.10" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
8 changes: 8 additions & 0 deletions
8
...uickstarts/snippets/text-to-image/hosting/TextToImageHosting/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/TextToImageHosting/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-resource-name.openai.azure.com/;Key=your-api-key" | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.