Table of Contents

Interface IGenerativeModel

Namespace
GenerativeAI
Assembly
GenerativeAI.dll

Defines the contract for a generative AI model. This interface provides methods for generating answers and content, streaming content, and counting tokens within inputs or outputs. It also includes properties for configuring behavior related to grounding, external tools, and advanced options such as JSON mode or function calling.

public interface IGenerativeModel
Extension Methods

Properties

FunctionCallingBehaviour

Defines the behavior for function calling within the generative model.

FunctionCallingBehaviour FunctionCallingBehaviour { get; set; }

Property Value

FunctionCallingBehaviour

UseCodeExecutionTool

Indicates whether the code execution tool is enabled.

bool UseCodeExecutionTool { get; set; }

Property Value

bool

UseGoogleSearch

Specifies whether the Google Search integration is enabled.

bool UseGoogleSearch { get; set; }

Property Value

bool

UseGrounding

Determines whether grounding is enabled.

bool UseGrounding { get; set; }

Property Value

bool

UseJsonMode

Determines whether JSON mode is enabled. Incompatible with grounding, Google Search, and code execution tools.

bool UseJsonMode { get; set; }

Property Value

bool

Methods

CountTokensAsync(CountTokensRequest, CancellationToken)

Counts tokens asynchronously based on the provided request.

Task<CountTokensResponse> CountTokensAsync(CountTokensRequest request, CancellationToken cancellationToken = default)

Parameters

request CountTokensRequest

An instance of CountTokensRequest containing the content or parameters for token counting.

cancellationToken CancellationToken

A cancellation token for monitoring cancellation requests.

Returns

Task<CountTokensResponse>

Returns a CountTokensResponse containing details of the token count.

CountTokensAsync(GenerateContentRequest, CancellationToken)

Counts the number of tokens in the specified content asynchronously.

Task<CountTokensResponse> CountTokensAsync(GenerateContentRequest generateContentRequest, CancellationToken cancellationToken = default)

Parameters

generateContentRequest GenerateContentRequest

An instance of GenerateContentRequest containing the details of the content for which tokens are to be counted.

cancellationToken CancellationToken

A cancellation token that can be used to observe while waiting for the asynchronous operation to complete.

Returns

Task<CountTokensResponse>

A CountTokensResponse containing the result of the token count operation.

CountTokensAsync(IEnumerable<Content>, CancellationToken)

Asynchronously counts the tokens in the given contents.

Task<CountTokensResponse> CountTokensAsync(IEnumerable<Content> contents, CancellationToken cancellationToken = default)

Parameters

contents IEnumerable<Content>

A collection of Content objects representing the input for which tokens need to be counted.

cancellationToken CancellationToken

A cancellation token to observe while waiting for the task to complete.

Returns

Task<CountTokensResponse>

A task that represents the asynchronous operation. The task result contains the CountTokensResponse with the resulting token count.

CountTokensAsync(IEnumerable<Part>, CancellationToken)

Asynchronously counts the tokens in the provided collection of parts.

Task<CountTokensResponse> CountTokensAsync(IEnumerable<Part> parts, CancellationToken cancellationToken = default)

Parameters

parts IEnumerable<Part>

A collection of Part objects representing the input data for token counting.

cancellationToken CancellationToken

A cancellation token that can be used to cancel the token counting operation.

Returns

Task<CountTokensResponse>

A task representing the asynchronous operation, containing a CountTokensResponse with the token counting results.

GenerateAnswerAsync(GenerateAnswerRequest, CancellationToken)

Generates an answer asynchronously based on the provided request.

Task<GenerateAnswerResponse> GenerateAnswerAsync(GenerateAnswerRequest request, CancellationToken cancellationToken = default)

Parameters

request GenerateAnswerRequest

The request containing the input details for generating an answer.

cancellationToken CancellationToken

A cancellation token that can be used by other objects or threads to receive notice of cancellation.

Returns

Task<GenerateAnswerResponse>

Returns a GenerateAnswerResponse containing the generated answer and additional context.

See Also

GenerateContentAsync(GenerateContentRequest, CancellationToken)

Generates content asynchronously based on the given input parameters.

Task<GenerateContentResponse> GenerateContentAsync(GenerateContentRequest request, CancellationToken cancellationToken = default)

Parameters

request GenerateContentRequest

An instance of GenerateContentRequest containing the input configuration and settings for content generation.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<GenerateContentResponse>

A task that represents the asynchronous operation, containing the GenerateContentResponse with the generated content.

See Also

GenerateContentAsync(IEnumerable<Part>, CancellationToken)

Generate content based on a sequence of Part objects.

Task<GenerateContentResponse> GenerateContentAsync(IEnumerable<Part> parts, CancellationToken cancellationToken = default)

Parameters

parts IEnumerable<Part>

The sequence of Part objects to be used for generating content.

cancellationToken CancellationToken

A token for cancelling the operation, if needed.

Returns

Task<GenerateContentResponse>

Returns an instance of GenerateContentResponse containing the generated content.

See Also

GenerateContentAsync(string, string, string, CancellationToken)

Generates content asynchronously based on the provided text prompt and an inline file path.

Task<GenerateContentResponse> GenerateContentAsync(string prompt, string fileUri, string mimeType, CancellationToken cancellationToken = default)

Parameters

prompt string

The input text prompt used for generating content.

fileUri string

The URI to an file that should be included in the content generation request.

mimeType string

The MIME type of the file specified in fileUri.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<GenerateContentResponse>

A task that represents the asynchronous operation, containing the GenerateContentResponse or null if content generation fails.

See Also

GenerateContentAsync(string, CancellationToken)

Generates content asynchronously based on the specified text input prompt.

Task<GenerateContentResponse> GenerateContentAsync(string prompt, CancellationToken cancellationToken = default)

Parameters

prompt string

The text input used to generate the content.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<GenerateContentResponse>

A task representing the asynchronous operation, containing the generated content response.

See Also

GenerateContentAsync<T>(GenerateContentRequest, CancellationToken)

Generates content asynchronously using JSON mode based on the given input parameters and converts the resulting JSON into a C# object of type T.

Task<GenerateContentResponse> GenerateContentAsync<T>(GenerateContentRequest request, CancellationToken cancellationToken = default) where T : class

Parameters

request GenerateContentRequest

An instance of GenerateContentRequest containing the input configuration and settings for content generation.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<GenerateContentResponse>

A task that represents the asynchronous operation, containing the generated content response of type T.

Type Parameters

T

The type of C# object to send as JSON schema input in request.

Remarks

Some of the complex data types are not supported such as Dictionary. So make sure to avoid these.

GenerateEnumAsync<T>(GenerateContentRequest, CancellationToken)

Generates an enumerated value asynchronously using Enum mode based on the given input parameters.

Task<T?> GenerateEnumAsync<T>(GenerateContentRequest request, CancellationToken cancellationToken = default) where T : Enum

Parameters

request GenerateContentRequest

An instance of GenerateContentRequest containing the input configuration and settings for enum generation.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<T>

A task that represents the asynchronous operation, containing the generated enumeration value of type T.

Type Parameters

T

The enumeration type to convert the resulting JSON into.

GenerateEnumAsync<T>(IEnumerable<Part>, CancellationToken)

Asynchronously generates an enumeration of the specified type T based on the provided parts input.

Task<T?> GenerateEnumAsync<T>(IEnumerable<Part> parts, CancellationToken cancellationToken = default) where T : Enum

Parameters

parts IEnumerable<Part>

A collection of Part objects representing the input components for generating the enumeration.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<T>

A task representing the asynchronous operation, containing the generated enumeration of type T.

Type Parameters

T

The enumeration type to convert the resulting data into. Must be an enumeration.

Remarks

Ensure the type T is an enumeration; non-enumeration types are not supported.

GenerateEnumAsync<T>(string, CancellationToken)

Generates an enumeration asynchronously using Enum mode based on the provided prompt and converts the resulting Enum data into a C# enumeration of type T.

Task<T?> GenerateEnumAsync<T>(string prompt, CancellationToken cancellationToken = default) where T : Enum

Parameters

prompt string

The textual input used to generate an enumeration of type T.

cancellationToken CancellationToken

Token to monitor for cancellation requests during the asynchronous operation.

Returns

Task<T>

A task representing the asynchronous operation, containing the generated enumeration of type T.

Type Parameters

T

The type of C# enumeration to convert the resulting JSON into. Must be an Enum.

Remarks

Some of the complex data types are not supported, such as Dictionary. Ensure that all input and output types conform to the supported JSON conversion structure.

GenerateObjectAsync<T>(GenerateContentRequest, CancellationToken)

Generates content asynchronously using JSON mode based on the given input parameters and converts the resulting JSON into a C# object of type T.

Task<T?> GenerateObjectAsync<T>(GenerateContentRequest request, CancellationToken cancellationToken = default) where T : class

Parameters

request GenerateContentRequest

An instance of GenerateContentRequest containing the input configuration and settings for content generation.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<T>

A task that represents the asynchronous operation, containing the generated object of type T.

Type Parameters

T

The type of C# object to convert the resulting JSON into.

Remarks

Some of the complex data types are not supported such as Dictionary. So make sure to avoid these.

GenerateObjectAsync<T>(IEnumerable<Part>, CancellationToken)

Generates content asynchronously using JSON mode based on the specified content generation request parts and converts the resulting JSON into a C# object of type T.

Task<T?> GenerateObjectAsync<T>(IEnumerable<Part> parts, CancellationToken cancellationToken = default) where T : class

Parameters

parts IEnumerable<Part>

An enumerable containing the input parts used for generating the content.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<T>

A task that represents the asynchronous operation, containing the generated object of type T.

Type Parameters

T

The type of C# object to convert the resulting JSON into.

Remarks

Some of the complex data types are not supported such as Dictionary. So make sure to avoid these.

GenerateObjectAsync<T>(string, CancellationToken)

Generates content asynchronously using JSON mode based on the specified text input prompt and converts the resulting JSON into a C# object of type T.

Task<T?> GenerateObjectAsync<T>(string prompt, CancellationToken cancellationToken = default) where T : class

Parameters

prompt string

The text input used to generate the content.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<T>

A task representing the asynchronous operation, containing the generated object of type T.

Type Parameters

T

The type of C# object to convert the resulting JSON into.

Remarks

Some of the complex data types are not supported such as Dictionary. So make sure to avoid these.

StreamContentAsync(GenerateContentRequest, CancellationToken)

Streams content generation results asynchronously based on a given request and optional cancellation token.

IAsyncEnumerable<GenerateContentResponse> StreamContentAsync(GenerateContentRequest request, CancellationToken cancellationToken = default)

Parameters

request GenerateContentRequest

The content generation request containing input configurations and options.

cancellationToken CancellationToken

An optional token to monitor for cancellation requests.

Returns

IAsyncEnumerable<GenerateContentResponse>

An asynchronous enumerable of GenerateContentResponse objects containing the streamed generation results.

See Also

StreamContentAsync(IEnumerable<Content>, CancellationToken)

Streams generated content as an asynchronous enumerable based on the provided request.

IAsyncEnumerable<GenerateContentResponse> StreamContentAsync(IEnumerable<Content> contents, CancellationToken cancellationToken = default)

Parameters

contents IEnumerable<Content>

The contents containing the parameters for content generation.

cancellationToken CancellationToken

Optional token to cancel the streaming operation.

Returns

IAsyncEnumerable<GenerateContentResponse>

An asynchronous enumerable of GenerateContentResponse objects.

StreamContentAsync(IEnumerable<Part>, CancellationToken)

Streams content generation based on a GenerateContentRequest asynchronously.

IAsyncEnumerable<GenerateContentResponse> StreamContentAsync(IEnumerable<Part> parts, CancellationToken cancellationToken = default)

Parameters

parts IEnumerable<Part>

The Parts containing input parameters for the content generation process.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

IAsyncEnumerable<GenerateContentResponse>

An asynchronous stream of GenerateContentResponse objects representing the generated content.

StreamContentAsync(string, string, string, CancellationToken)

Streams content generation asynchronously based on the given input prompt, file URI, and MIME type.

IAsyncEnumerable<GenerateContentResponse> StreamContentAsync(string prompt, string fileUri, string mimeType, CancellationToken cancellationToken = default)

Parameters

prompt string

The input string used as a prompt for generating and streaming content.

fileUri string

The URI of the file to be used for content generation.

mimeType string

The MIME type associated with the provided file URI.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

IAsyncEnumerable<GenerateContentResponse>

An asynchronous enumerable of GenerateContentResponse instances representing the streamed content responses.

See Also

StreamContentAsync(string, CancellationToken)

Streams content generation based on a string message.

IAsyncEnumerable<GenerateContentResponse> StreamContentAsync(string prompt, CancellationToken cancellationToken = default)

Parameters

prompt string

The input prompt to be processed and streamed.

cancellationToken CancellationToken

A token to observe for cancellation requests.

Returns

IAsyncEnumerable<GenerateContentResponse>

An asynchronous enumerable of generated content responses.

See Also