-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.
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.
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.
Show resolved
Hide resolved
|
||
| { | ||
gewarren marked this conversation as resolved.
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.
Show resolved
Hide resolved
|
||
| }); | ||
| // </SnippetUseImageGenerator> | ||
|
|
||
| app.Run(); | ||
| 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" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||||||
|
|
||||||
| <PropertyGroup> | ||||||
| <TargetFramework>net10.0</TargetFramework> | ||||||
| <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.1.0" /> | ||||||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.0.0-preview.1.25560.10" /> | ||||||
|
||||||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.0.0-preview.1.25560.10" /> | |
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.10.1-preview.1.25521.4" /> |
| 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,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.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.