Table of Contents

Class Models

Namespace
Google.GenAI
Assembly
Google.GenAI.dll

Provides methods to interact with the Gen AI models. This class is not intended to be instantiated directly, instead it should be accessed through the Client class.

public sealed class Models
Inheritance
Models
Inherited Members
Extension Methods

Constructors

Models(ApiClient)

public Models(ApiClient apiClient)

Parameters

apiClient ApiClient

Methods

ComputeTokensAsync(string, Content, ComputeTokensConfig?, CancellationToken)

Computes the number of tokens for a single content item.

public Task<ComputeTokensResponse> ComputeTokensAsync(string model, Content contents, ComputeTokensConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for token computation.

contents Content

A Content instance to compute tokens for.

config ComputeTokensConfig

A ComputeTokensConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<ComputeTokensResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a ComputeTokensResponse instance with token information.

Exceptions

NotSupportedException

Thrown when called with a client that is not in Gemini Enterprise Agent Platform mode.

ComputeTokensAsync(string, List<Content>, ComputeTokensConfig?, CancellationToken)

Computes the number of tokens for the given content.

public Task<ComputeTokensResponse> ComputeTokensAsync(string model, List<Content> contents, ComputeTokensConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for token computation.

contents List<Content>

A List<T> to compute tokens for.

config ComputeTokensConfig

A ComputeTokensConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<ComputeTokensResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a ComputeTokensResponse instance with token information.

Exceptions

NotSupportedException

Thrown when called with a non-Gemini Enterprise Agent Platform (previously known as Vertex AI) client.

ComputeTokensAsync(string, string, ComputeTokensConfig?, CancellationToken)

Computes the number of tokens for a text string.

public Task<ComputeTokensResponse> ComputeTokensAsync(string model, string contents, ComputeTokensConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for token computation.

contents string

A string of text to compute tokens for.

config ComputeTokensConfig

A ComputeTokensConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<ComputeTokensResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a ComputeTokensResponse instance with token information.

Exceptions

NotSupportedException

Thrown when called with a with a client that is not in Gemini Enterprise Agent Platform mode.

CountTokensAsync(string, Content, CountTokensConfig?, CancellationToken)

Counts the number of tokens in the provided content.

public Task<CountTokensResponse> CountTokensAsync(string model, Content contents, CountTokensConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for token counting.

contents Content

A Content to count tokens for.

config CountTokensConfig

A CountTokensConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<CountTokensResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a CountTokensResponse instance with the total token count and other metadata.

CountTokensAsync(string, List<Content>, CountTokensConfig?, CancellationToken)

Counts the number of tokens in the provided content.

public Task<CountTokensResponse> CountTokensAsync(string model, List<Content> contents, CountTokensConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for token counting.

contents List<Content>

A List<T> to count tokens for.

config CountTokensConfig

A CountTokensConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<CountTokensResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a CountTokensResponse instance with the total token count and other metadata.

CountTokensAsync(string, string, CountTokensConfig?, CancellationToken)

Counts the number of tokens in the provided content.

public Task<CountTokensResponse> CountTokensAsync(string model, string contents, CountTokensConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for token counting.

contents string

A text string to send to count tokens for.

config CountTokensConfig

A CountTokensConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<CountTokensResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a CountTokensResponse instance with the total token count and other metadata.

DeleteAsync(string, DeleteModelConfig?, CancellationToken)

Deletes a specific model resource by its name.

public Task<DeleteModelResponse> DeleteAsync(string model, DeleteModelConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string
config DeleteModelConfig
cancellationToken CancellationToken

Returns

Task<DeleteModelResponse>

EditImageAsync(string, string, List<IReferenceImage>, EditImageConfig?, CancellationToken)

public Task<EditImageResponse> EditImageAsync(string model, string prompt, List<IReferenceImage> referenceImages, EditImageConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string
prompt string
referenceImages List<IReferenceImage>
config EditImageConfig
cancellationToken CancellationToken

Returns

Task<EditImageResponse>

EmbedContentAsync(string, Content, EmbedContentConfig?, CancellationToken)

Calculates embeddings for the given content.

public Task<EmbedContentResponse> EmbedContentAsync(string model, Content contents, EmbedContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The model to use.

contents Content

The content to embed.

config EmbedContentConfig

Optional configuration for embeddings.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<EmbedContentResponse>

A Task<TResult> that represents the asynchronous operation.

EmbedContentAsync(string, List<Content>, EmbedContentConfig?, CancellationToken)

Calculates embeddings for the given content.

public Task<EmbedContentResponse> EmbedContentAsync(string model, List<Content> contents, EmbedContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The model to use.

contents List<Content>

The content to embed.

config EmbedContentConfig

Optional configuration for embeddings.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<EmbedContentResponse>

A Task<TResult> that represents the asynchronous operation.

EmbedContentAsync(string, string, EmbedContentConfig?, CancellationToken)

Calculates embeddings for the given text string.

public Task<EmbedContentResponse> EmbedContentAsync(string model, string contents, EmbedContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The model to use.

contents string

The text string to embed.

config EmbedContentConfig

Optional configuration for embeddings.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<EmbedContentResponse>

A Task<TResult> that represents the asynchronous operation.

GenerateContentAsync(string, Content, GenerateContentConfig?, CancellationToken)

Generates content given a GenAI model and a single content item.

public Task<GenerateContentResponse> GenerateContentAsync(string model, Content contents, GenerateContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for generation.

contents Content

A Content instance to send to the generative model.

config GenerateContentConfig

A GenerateContentConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<GenerateContentResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a GenerateContentResponse instance with response contents and other metadata.

GenerateContentAsync(string, List<Content>, GenerateContentConfig?, CancellationToken)

Generates content given a GenAI model and a list of content.

public Task<GenerateContentResponse> GenerateContentAsync(string model, List<Content> contents, GenerateContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for generation.

contents List<Content>

A List<T> to send to the generative model.

config GenerateContentConfig

A GenerateContentConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<GenerateContentResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a GenerateContentResponse instance with response contents and other metadata.

GenerateContentAsync(string, string, GenerateContentConfig?, CancellationToken)

Generates content given a GenAI model and a text string.

public Task<GenerateContentResponse> GenerateContentAsync(string model, string contents, GenerateContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for generation.

contents string

A text string to send to the generative model.

config GenerateContentConfig

A GenerateContentConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<GenerateContentResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a GenerateContentResponse instance with response contents and other metadata.

GenerateContentStreamAsync(string, Content, GenerateContentConfig?, CancellationToken)

Generates content with streaming responses given a GenAI model and a single content item.

public IAsyncEnumerable<GenerateContentResponse> GenerateContentStreamAsync(string model, Content contents, GenerateContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for generation.

contents Content

A Content instance to send to the generative model.

config GenerateContentConfig

A GenerateContentConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

IAsyncEnumerable<GenerateContentResponse>

An async enumerable of GenerateContentResponse chunks.

GenerateContentStreamAsync(string, List<Content>, GenerateContentConfig?, CancellationToken)

Generates content with streaming responses given a GenAI model and a list of content.

public IAsyncEnumerable<GenerateContentResponse> GenerateContentStreamAsync(string model, List<Content> contents, GenerateContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for generation.

contents List<Content>

A List<T> to send to the generative model.

config GenerateContentConfig

A GenerateContentConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

IAsyncEnumerable<GenerateContentResponse>

An async enumerable of GenerateContentResponse chunks.

GenerateContentStreamAsync(string, string, GenerateContentConfig?, CancellationToken)

Generates content with streaming responses given a GenAI model and a text string.

public IAsyncEnumerable<GenerateContentResponse> GenerateContentStreamAsync(string model, string contents, GenerateContentConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for generation.

contents string

A text string to send to the generative model.

config GenerateContentConfig

A GenerateContentConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

IAsyncEnumerable<GenerateContentResponse>

An async enumerable of GenerateContentResponse chunks.

GenerateImagesAsync(string, string, GenerateImagesConfig?, CancellationToken)

Generates images given a GenAI model and a prompt.

public Task<GenerateImagesResponse> GenerateImagesAsync(string model, string prompt, GenerateImagesConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The name of the GenAI model to use for generation.

prompt string

A text prompt string to send to the generative model.

config GenerateImagesConfig

A GenerateImagesConfig instance that specifies the optional configurations.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<GenerateImagesResponse>

A Task<TResult> that represents the asynchronous operation. The task result contains a GenerateImagesResponse instance with response contents and other metadata.

GenerateVideosAsync(string, GenerateVideosSource, GenerateVideosConfig?, CancellationToken)

public Task<GenerateVideosOperation> GenerateVideosAsync(string model, GenerateVideosSource source, GenerateVideosConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string
source GenerateVideosSource
config GenerateVideosConfig
cancellationToken CancellationToken

Returns

Task<GenerateVideosOperation>

GetAsync(string, GetModelConfig?, CancellationToken)

Retrieves a specific model resource by its name.

public Task<Model> GetAsync(string model, GetModelConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string
config GetModelConfig
cancellationToken CancellationToken

Returns

Task<Model>

ListAsync(ListModelsConfig?, CancellationToken)

Lists models asynchronously.

public Task<Pager<Model, ListModelsConfig, ListModelsResponse>> ListAsync(ListModelsConfig? config = null, CancellationToken cancellationToken = default)

Parameters

config ListModelsConfig

A ListModelsConfig instance that specifies the optional configuration for the list request.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<Pager<Model, ListModelsConfig, ListModelsResponse>>

A Pager object that contains one page of models. When iterating over the pager, it automatically fetches the next page if there are more.

RecontextImageAsync(string, RecontextImageSource, RecontextImageConfig?, CancellationToken)

public Task<RecontextImageResponse> RecontextImageAsync(string model, RecontextImageSource source, RecontextImageConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string
source RecontextImageSource
config RecontextImageConfig
cancellationToken CancellationToken

Returns

Task<RecontextImageResponse>

SegmentImageAsync(string, SegmentImageSource, SegmentImageConfig?, CancellationToken)

public Task<SegmentImageResponse> SegmentImageAsync(string model, SegmentImageSource source, SegmentImageConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string
source SegmentImageSource
config SegmentImageConfig
cancellationToken CancellationToken

Returns

Task<SegmentImageResponse>

UpdateAsync(string, UpdateModelConfig?, CancellationToken)

Updates a specific model resource.

public Task<Model> UpdateAsync(string model, UpdateModelConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string

The model to update.

config UpdateModelConfig

An UpdateModelConfig instance that specifies parameters to update.

cancellationToken CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<Model>

A Task<TResult> that represents the asynchronous operation. The task result contains the updated Model instance.

UpscaleImageAsync(string, Image, string, UpscaleImageConfig?, CancellationToken)

public Task<UpscaleImageResponse> UpscaleImageAsync(string model, Image image, string upscaleFactor, UpscaleImageConfig? config = null, CancellationToken cancellationToken = default)

Parameters

model string
image Image
upscaleFactor string
config UpscaleImageConfig
cancellationToken CancellationToken

Returns

Task<UpscaleImageResponse>