Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 9 additions & 7 deletions docs/ai/quickstarts/snippets/text-to-image/hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// <SnippetAddImageGenerator>
// Register the image generator with dependency injection.
builder.Services.AddImageGenerator(services =>
ImageGeneratorBuilder imageBuilder = builder.Services.AddImageGenerator(services =>
{
OpenAIClient openAiClient = services.GetRequiredService<OpenAIClient>();
OpenAI.Images.ImageClient imageClient = openAiClient.GetImageClient("gpt-image-1");
Expand All @@ -21,18 +21,20 @@
});
// </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) =>
{
var options = new ImageGenerationOptions
{
MediaType = "image/png"
};

ImageGenerationResponse response = await generator.GenerateImagesAsync(prompt, options);
ImageGenerationResponse response = await generator.GenerateImagesAsync(prompt);
DataContent dataContent = response.Contents.OfType<DataContent>().First();

return Results.File(dataContent.Data.ToArray(), "image/png");
Copy link
Member

Choose a reason for hiding this comment

The 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);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll submit a PR.

Expand Down
9 changes: 9 additions & 0 deletions docs/ai/quickstarts/text-to-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ The `Aspire.Azure.AI.OpenAI` package provides extension methods to register Azur

The <xref:Microsoft.Extensions.DependencyInjection.ImageGeneratorBuilderServiceCollectionExtensions.AddImageGenerator*> method registers the image generator as a singleton service that can be injected into controllers, services, or minimal API endpoints.

1. Add options and logging::

:::code language="csharp" source="snippets/text-to-image/hosting/Program.cs" id="SnippetConfigureOptions":::

The preceding code:

- Configures options by calling the <xref:Microsoft.Extensions.AI.ConfigureOptionsImageGeneratorBuilderExtensions.ConfigureOptions(Microsoft.Extensions.AI.ImageGeneratorBuilder,System.Action{Microsoft.Extensions.AI.ImageGenerationOptions})> extension method on the <xref:Microsoft.Extensions.AI.ImageGeneratorBuilder>. This method configures the <xref:Microsoft.Extensions.AI.ImageGenerationOptions> to be passed to the next generator in the pipeline.
- Adds logging to the image generator pipeline by calling the <xref:Microsoft.Extensions.AI.LoggingImageGeneratorBuilderExtensions.UseLogging(Microsoft.Extensions.AI.ImageGeneratorBuilder,Microsoft.Extensions.Logging.ILoggerFactory,System.Action{Microsoft.Extensions.AI.LoggingImageGenerator})> extension method.

### Use the image generator in endpoints

Once registered, you can inject `IImageGenerator` into your endpoints or services:
Expand Down