Table of Contents

Class GenerativeModel

Namespace
GenerativeAI
Assembly
GenerativeAI.dll

A Generative AI Model that extends BaseModel

public class GenerativeModel : BaseModel, IGenerativeModel
Inheritance
GenerativeModel
Implements
Derived
Inherited Members
Extension Methods

Constructors

GenerativeModel(IPlatformAdapter, string?, GenerationConfig?, ICollection<SafetySetting>?, string?, HttpClient?, ILogger?)

Represents a generative AI model, providing functionality for text generation tasks.

public GenerativeModel(IPlatformAdapter platform, string? model, GenerationConfig? config = null, ICollection<SafetySetting>? safetySettings = null, string? systemInstruction = null, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

platform IPlatformAdapter

The platform adapter used for the generative AI model integration.

model string

The name or identifier of the model to be used.

config GenerationConfig

Optional configuration for text generation, including settings like temperature and max tokens.

safetySettings ICollection<SafetySetting>

Optional collection of safety settings to ensure content generation adheres to desired guidelines.

systemInstruction string

Optional instruction for the model, defining the initial prompt or system behavior.

httpClient HttpClient

Optional HTTP client for API communication.

logger ILogger

Optional logger instance for diagnostic and logging purposes.

Remarks

Provides constructors for initializing the model with various configurations and integrates with platform-specific operations.

GenerativeModel(string, ModelParams, HttpClient?, ILogger?)

Represents a generative model designed for handling text generation tasks, offering a range of configurations and functionalities including platform-specific integration, safety settings, and optional system instructions.

public GenerativeModel(string apiKey, ModelParams modelParams, HttpClient? client = null, ILogger? logger = null)

Parameters

apiKey string

The API key required for authenticating requests with the target platform.

modelParams ModelParams

Model parameters to specify generation configuration, safety settings, and behavior customization.

client HttpClient

Optional HTTP client for managing communication with the generative AI platform.

logger ILogger

Optional logger instance to facilitate diagnostic logging and debugging.

Remarks

This constructor allows the creation of an instance with default or user-specified configurations, streamlined for integration with external generative AI services. The underlying implementation leverages adapter-based architecture for modularity and scalability.

GenerativeModel(string, string?, GenerationConfig?, ICollection<SafetySetting>?, string?, HttpClient?, ILogger?)

Represents a generative AI model, extending base functionalities and supporting text generation tasks.

public GenerativeModel(string apiKey, string? model, GenerationConfig? config = null, ICollection<SafetySetting>? safetySettings = null, string? systemInstruction = null, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

apiKey string

The API key used for authenticating requests to the generative AI platform.

model string

Optional identifier or name of the model to be used. Defaults to system environment variables or a platform-specific default model if not provided.

config GenerationConfig

Optional configuration for text generation to customize behavior, such as adjusting temperature or token limits.

safetySettings ICollection<SafetySetting>

Optional collection of safety settings to enforce content generation rules and guidelines.

systemInstruction string

Optional instruction defining the system's behavior, providing an initial context or prompt.

httpClient HttpClient

Optional HTTP client used for executing API communication with the generative platform.

logger ILogger

Optional logger instance for tracking or debugging the model operations and interactions.

Remarks

Initializes a GenerativeModel instance, leveraging the Google AI platform adapter and enabling advanced text generation capabilities with configurable parameters.

Properties

CachedContent

Gets or sets preloaded or previously generated content associated with the generative model. This property allows for the reuse of specific content, bypassing the need for regeneration, while ensuring alignment with the current model configuration and constraints.

public CachedContent? CachedContent { get; set; }

Property Value

CachedContent

CachingClient

Gets or sets the client used for managing cached content operations in the generative AI model. This property provides an interface for interacting with caching-related functionalities, such as creating, updating, retrieving, or deleting cached content. It helps manage content efficiently by leveraging the CachingClient within the model's context.

public CachingClient? CachingClient { get; set; }

Property Value

CachingClient

Config

Gets or sets the configuration for content generation. This property defines specific parameters, such as temperature and maximum output tokens, that influence the behavior and output of the generative model. It serves as a central customization point for tailoring the content creation process.

public GenerationConfig? Config { get; set; }

Property Value

GenerationConfig

DefaultCodeExecutionTool

Represents the default tool used for executing code in a generative AI context. This tool allows for dynamic code execution as part of the generative model’s operations.

public Tool DefaultCodeExecutionTool { get; set; }

Property Value

Tool

Remarks

The tool is automatically added to the available tools if no other code execution tool is specified.

DefaultGoogleSearchRetrieval

Represents the default Google Search Retrieval tool configuration used within the generative model. This tool leverages dynamic retrieval capabilities to fetch real-time data and enhance model responses.

public Tool DefaultGoogleSearchRetrieval { get; set; }

Property Value

Tool

Remarks

Configured with a dynamic retrieval mode and threshold. Primarily used when no specific retrieval tool is provided in the request. Ensures the integration of up-to-date search results.

DefaultSearchTool

Represents the default tool configured for search-related operations in the generative model. By default, it utilizes a Google Search Tool for retrieving information to assist in content generation tasks.

public Tool DefaultSearchTool { get; set; }

Property Value

Tool

Remarks

This tool is automatically added to the request tools if no other search tool is explicitly defined.

EnableAutomaticContinuation

Whether to resume automatically when the API ends a response early with CONTINUATION and a continuation token (default true, as in the official SDKs). The follow-up responses are merged into one response (non-streaming) or streamed on (streaming).

public bool EnableAutomaticContinuation { get; set; }

Property Value

bool

FunctionCallingBehaviour

Specifies the configuration settings for function calling behavior in the generative model.

public FunctionCallingBehaviour FunctionCallingBehaviour { get; set; }

Property Value

FunctionCallingBehaviour

Remarks

This property determines how the model interacts with functions, including enabling or disabling function calls, automatically calling functions, replying to functions, and handling incorrect function calls.

FunctionTools

Represents a collection of function tools that can be utilized within the generative model. Function tools enable additional functionalities by allowing external or custom tools to be integrated into the model's processing pipeline.

public List<IFunctionTool> FunctionTools { get; set; }

Property Value

List<IFunctionTool>

Remarks

The tools provided via this property are managed and invoked based on the specified configurations and behaviours within the generative model. Tools are used during content generation to enhance or extend the model's capabilities.

GenerateObjectJsonSerializerOptions

Specifies the JSON serializer options to be used when generating objects as JSON outputs. These options configure the behavior of JSON serialization and deserialization for object generation in the context of JSON mode.

public JsonSerializerOptions GenerateObjectJsonSerializerOptions { get; set; }

Property Value

JsonSerializerOptions

Remarks

For NativeAOT/Trimming GenerateObjectJsonSerializerOptions are required, you can ignore it if you have already specified a resolver in

MaxContinuations

The maximum number of follow-up requests made for one generation when EnableAutomaticContinuation is on (default 10).

public int MaxContinuations { get; set; }

Property Value

int

Model

Gets or sets the name or identifier of the model used for generative tasks. This property determines the specific model implementation to be used for content generation and other related operations.

public string Model { get; set; }

Property Value

string

RetrievalTool

Gets the retrieval tool configuration for the model, if any.

public Tool? RetrievalTool { get; protected set; }

Property Value

Tool

The retrieval tool that enables the model to access external data sources, or null if no retrieval tool is configured.

SafetySettings

Gets or sets the collection of safety settings that define constraints or limits on content generation. This property is used to enforce specified safety rules to ensure the generated content aligns with predetermined guidelines or restrictions.

public List<SafetySetting>? SafetySettings { get; set; }

Property Value

List<SafetySetting>

SystemInstruction

Gets or sets the system instruction used to guide the behavior of the generative model. This property defines the overarching context or guideline for content generation, shaping the responses produced by the model to align with specific requirements or objectives.

public string? SystemInstruction { get; set; }

Property Value

string

ToolConfig

Represents the configuration settings for a specific tool. Provides options to define the behavior and setup of tools, such as function calling configurations, to align with the requirements of the generative model.

public ToolConfig? ToolConfig { get; set; }

Property Value

ToolConfig

UseCodeExecutionTool

Indicates whether the code execution tool is enabled. The code execution tool facilitates the integration of code execution capabilities for processing specific requests and enhancing model functionalities.

public bool UseCodeExecutionTool { get; set; }

Property Value

bool

Remarks

Use the Code Execution Tool for tasks requiring direct code evaluation or execution within the generative model. This feature is incompatible with JSON mode or cached content mode.

UseGoogleSearch

Specifies whether the Google Search integration is enabled. This feature allows utilizing the Google Search Tool within the generative model for enhanced search and retrieval capabilities.

public bool UseGoogleSearch { get; set; }

Property Value

bool

Remarks

Enabling this property incorporates Google Search as a tool for the generative model, providing dynamic search support based on the requested content needs.

UseGrounding

Indicates whether grounding is enabled. Grounding integrates external tools, such as Google Search Retrieval, to support specific generative model requests for enhanced content generation.

public bool UseGrounding { get; set; }

Property Value

bool

Remarks

Use Google Search Tool for Latest Models

UseJsonMode

Determines whether JSON mode is enabled. JSON mode adjusts the content generation response to specifically produce outputs in JSON format as defined in generation configurations.

public bool UseJsonMode { get; set; }

Property Value

bool

Remarks

JSON mode is incompatible with grounding, Google Search, and code execution tools. Enabling this mode will override other response formats with "application/json".

Methods

AddCachedContent(GenerateContentRequest)

Adds cached content to the generate content request, ensuring the model and request contents align with the cached data.

protected void AddCachedContent(GenerateContentRequest request)

Parameters

request GenerateContentRequest

The generate content request to which cached content is added.

Remarks

Cached content includes predefined responses or data related to the current model. If cached content is present, it sets the cached contents and disables additional configurations related to tools or system instructions. An exception is thrown if the cached content's model does not match the generative model.

AddFunctionTool(GoogleFunctionTool, ToolConfig?, FunctionCallingBehaviour?)

Adds a Google function tool to the generative model.

public void AddFunctionTool(GoogleFunctionTool tool, ToolConfig? toolConfig = null, FunctionCallingBehaviour? functionCallingBehaviour = null)

Parameters

tool GoogleFunctionTool

The Google function tool to be added.

toolConfig ToolConfig

Optional configuration for the tool.

functionCallingBehaviour FunctionCallingBehaviour

Optional behavior configuration for function calling.

AddFunctionTool(IFunctionTool, ToolConfig?, FunctionCallingBehaviour?)

Adds a new function tool to the list of available function tools.

public void AddFunctionTool(IFunctionTool tool, ToolConfig? toolConfig = null, FunctionCallingBehaviour? functionCallingBehaviour = null)

Parameters

tool IFunctionTool

The implementation of the IFunctionTool interface to be added.

toolConfig ToolConfig

Optional configuration for the tool.

functionCallingBehaviour FunctionCallingBehaviour

Optional behavior configuration for function calling.

AdjustJsonMode(GenerateContentRequest)

Adjusts the response mime type to JSON mode if enabled in the configuration.

protected void AdjustJsonMode(GenerateContentRequest request)

Parameters

request GenerateContentRequest

The generate content request being processed, which includes configuration and settings for content generation.

BeforeRegeneration(GenerateContentRequest, GenerateContentResponse)

Process the request before regenerating with a function call response

protected virtual List<Content> BeforeRegeneration(GenerateContentRequest originalRequest, GenerateContentResponse response)

Parameters

originalRequest GenerateContentRequest

The original content generation request containing existing contents.

response GenerateContentResponse

The content generation response containing candidates from the model.

Returns

List<Content>

A list of contents combining the original request contents and updated content from the response.

CallFunctionAsync(GenerateContentRequest, GenerateContentResponse, CancellationToken)

Handles invoking a function if the response requests one and optionally feeding the response back into the model

protected virtual Task<GenerateContentResponse> CallFunctionAsync(GenerateContentRequest originalRequest, GenerateContentResponse response, CancellationToken cancellationToken)

Parameters

originalRequest GenerateContentRequest
response GenerateContentResponse
cancellationToken CancellationToken

Returns

Task<GenerateContentResponse>

CallFunctionStreamingAsync(GenerateContentRequest, GenerateContentResponse, CancellationToken)

Handles invoking a function if the response requests one and optionally feeding the response back into the model

protected virtual IAsyncEnumerable<GenerateContentResponse> CallFunctionStreamingAsync(GenerateContentRequest originalRequest, GenerateContentResponse response, CancellationToken cancellationToken)

Parameters

originalRequest GenerateContentRequest
response GenerateContentResponse
cancellationToken CancellationToken

Returns

IAsyncEnumerable<GenerateContentResponse>

CountTokensAsync(CountTokensRequest, CancellationToken)

Counts tokens asynchronously based on the provided request.

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

Parameters

request CountTokensRequest

An instance of CountTokensRequest containing the details of contents or generation parameters for counting tokens.

cancellationToken CancellationToken

Token to monitor for cancellation requests during the operation.

Returns

Task<CountTokensResponse>

A task that represents the asynchronous operation, containing a CountTokensResponse with the token count details.

CountTokensAsync(GenerateContentRequest, CancellationToken)

Counts the number of tokens in the content based on the provided GenerateContentRequest.

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

Parameters

generateContentRequest GenerateContentRequest

An instance of GenerateContentRequest containing the input data for which the token count is calculated.

cancellationToken CancellationToken

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

Returns

Task<CountTokensResponse>

A task that represents the asynchronous operation, containing a CountTokensResponse with token count details.

CountTokensAsync(IEnumerable<Content>, CancellationToken)

Asynchronously counts the tokens in the provided contents.

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

Parameters

contents IEnumerable<Content>

A collection of Content objects for which the token count needs to be determined.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<CountTokensResponse>

A task representing the asynchronous operation, containing the CountTokensResponse with the token count for the provided contents.

CountTokensAsync(IEnumerable<Part>, CancellationToken)

Asynchronously counts the tokens in the provided collection of parts.

public 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

Token to monitor for cancellation requests.

Returns

Task<CountTokensResponse>

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

DisableFunctions()

Disable Global Functions

public void DisableFunctions()

EnableFunctions()

Enable Global Functions

public void EnableFunctions()

GenerateAnswerAsync(GenerateAnswerRequest, CancellationToken)

Generates an answer asynchronously based on the provided request.

public 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.

public virtual 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.

public 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 a remote file URI.

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

Parameters

prompt string

The input text prompt used for content generation.

fileUri string

The URI of the file to be included in the content generation process.

mimeType string

The MIME type of the file referenced by the URI.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<GenerateContentResponse>

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

GenerateContentAsync(string, CancellationToken)

Generates content asynchronously based on the specified text input prompt.

public 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.

public virtual 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.

public virtual 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.

public 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.

public 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.

public virtual 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.

public 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.

public 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.

PrepareRequest(GenerateContentRequest)

Prepares the GenerateContentRequest for content generation by populating global properties, configuring JSON mode, adding tools, cached content, and validating the request.

protected virtual void PrepareRequest(GenerateContentRequest request)

Parameters

request GenerateContentRequest

The content generation request to be prepared with required settings and validations.

Remarks

This method ensures that the request is appropriately configured with default global parameters, tools, and any necessary system-level instructions before proceeding with content generation.

StartChat(ChatSessionBackUpData, List<IFunctionTool>?)

Starts a new chat session with the specified configuration, tools, and backup data.

public virtual ChatSession StartChat(ChatSessionBackUpData chatSessionBackUpData, List<IFunctionTool>? tools = null)

Parameters

chatSessionBackUpData ChatSessionBackUpData

The backup data for the chat session, containing the model, safety settings, generation configuration, and history.

tools List<IFunctionTool>

Optional collection of tools to be used during the chat session. If not specified, default tools associated with the model will be used.

Returns

ChatSession

A new instance of the ChatSession class initialized with the provided parameters and model settings.

StartChat(List<Content>?, GenerationConfig?, ICollection<SafetySetting>?, string?)

Starts a new chat session with the given parameters and configuration.

public virtual ChatSession StartChat(List<Content>? history = null, GenerationConfig? config = null, ICollection<SafetySetting>? safetySettings = null, string? systemInstruction = null)

Parameters

history List<Content>

Optional chat history to be included in the new session.

config GenerationConfig

Optional configuration for text generation, including parameters such as temperature and maximum tokens.

safetySettings ICollection<SafetySetting>

Optional collection of safety settings to ensure the chat session adheres to defined guidelines.

systemInstruction string

Optional instruction defining the system's behavior or context for the chat session.

Returns

ChatSession

A new instance of the ChatSession class representing the initiated chat session.

StreamContentAsync(GenerateContentRequest, CancellationToken)

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

public virtual 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.

public 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.

public 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.

public 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.

public 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

UseFileSearch(IEnumerable<string>, int?, string?)

Grounds every request on the given File Search stores by adding a FileSearch tool (replacing any previously configured RetrievalTool).

public void UseFileSearch(IEnumerable<string> fileSearchStoreNames, int? topK = null, string? metadataFilter = null)

Parameters

fileSearchStoreNames IEnumerable<string>

Store names, either fileSearchStores/{id} or just {id}.

topK int?

Optional number of chunks to retrieve.

metadataFilter string

Optional AIP-160 filter over document custom metadata, e.g. author = "Robert Graves".

Exceptions

NotSupportedException

The model is configured for Vertex AI, which does not support File Search stores.

ArgumentException

No store names were given.

UseFileSearch(params string[])

Grounds every request on the given File Search stores by adding a FileSearch tool.

public void UseFileSearch(params string[] fileSearchStoreNames)

Parameters

fileSearchStoreNames string[]

Store names, either fileSearchStores/{id} or just {id}.

UseVertexRetrievalTool(string, RagRetrievalConfig?)

Configures and assigns a Vertex Retrieval Tool with the specified corpus ID and retrieval configuration.

public void UseVertexRetrievalTool(string corpusId, RagRetrievalConfig? retrievalConfig = null)

Parameters

corpusId string

The identifier for the corpus to be associated with the Vertex Retrieval Tool.

retrievalConfig RagRetrievalConfig

An optional retrieval configuration for customizing the behavior of the Vertex Retrieval Tool.

Exceptions

NotSupportedException

Thrown when the platform does not support Retrieval Augmentation Generation on Vertex AI.

ValidateGenerateContentRequest(GenerateContentRequest)

Validates the provided GenerateContentRequest to ensure compatibility with the current model configuration.

protected virtual void ValidateGenerateContentRequest(GenerateContentRequest request)

Parameters

request GenerateContentRequest

The content generation request containing parameters such as generation configuration.

Remarks

This method ensures that specific features like grounding, Google Search, or code execution tools are not used concurrently with incompatible modes such as JSON mode or cached content mode. It also checks for consistency in the generation configuration, such as the specified response MIME type.