Table of Contents

Class GenerativeAIImagenGenerator

Namespace
GenerativeAI.Microsoft
Assembly
GenerativeAI.Microsoft.dll

Implements Microsoft.Extensions.AI.IImageGenerator by creating an ImagenModel via GenAI.CreateImageModel and calling GenerateImagesAsync(GenerateImageRequest, CancellationToken).

public sealed class GenerativeAIImagenGenerator : IImageGenerator, IDisposable
Inheritance
GenerativeAIImagenGenerator
Implements
IImageGenerator
Inherited Members
Extension Methods

Constructors

GenerativeAIImagenGenerator(GenAI, string)

Creates a new instance using an existing GenAI factory and optional model name.

public GenerativeAIImagenGenerator(GenAI genai, string modelName = "models/imagen-3.0-generate-002")

Parameters

genai GenAI
modelName string

GenerativeAIImagenGenerator(string, string)

Creates a new instance using an API key and optional model name.

public GenerativeAIImagenGenerator(string apiKey, string modelName = "models/imagen-3.0-generate-002")

Parameters

apiKey string
modelName string

Properties

model

Underlying ImagenModel instance created from the provided GenAI factory.

public ImagenModel model { get; }

Property Value

ImagenModel

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GenerateAsync(ImageGenerationRequest, ImageGenerationOptions?, CancellationToken)

Sends an image generation request and returns the generated image as a Microsoft.Extensions.AI.ImageGenerationResponse.

public Task<ImageGenerationResponse> GenerateAsync(ImageGenerationRequest request, ImageGenerationOptions? options = null, CancellationToken cancellationToken = default)

Parameters

request ImageGenerationRequest

The image generation request containing the prompt and optional original images for editing.

options ImageGenerationOptions

The image generation options to configure the request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task<ImageGenerationResponse>

The images generated by the Microsoft.Extensions.AI.ImageGenerationRequest.

Exceptions

ArgumentNullException

request is null.

GetService(Type, object?)

Asks the Microsoft.Extensions.AI.IImageGenerator for an object of the specified type serviceType.

public object? GetService(Type serviceType, object? serviceKey = null)

Parameters

serviceType Type

The type of object being requested.

serviceKey object

An optional key that can be used to help identify the target service.

Returns

object

The found object, otherwise null.

Remarks

The purpose of this method is to allow for the retrieval of strongly typed services that might be provided by the Microsoft.Extensions.AI.IImageGenerator, including itself or any services it might be wrapping.

Exceptions

ArgumentNullException

serviceType is null.