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
platformIPlatformAdapterThe platform adapter used for the generative AI model integration.
modelstringThe name or identifier of the model to be used.
configGenerationConfigOptional configuration for text generation, including settings like temperature and max tokens.
safetySettingsICollection<SafetySetting>Optional collection of safety settings to ensure content generation adheres to desired guidelines.
systemInstructionstringOptional instruction for the model, defining the initial prompt or system behavior.
httpClientHttpClientOptional HTTP client for API communication.
loggerILoggerOptional 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
apiKeystringThe API key required for authenticating requests with the target platform.
modelParamsModelParamsModel parameters to specify generation configuration, safety settings, and behavior customization.
clientHttpClientOptional HTTP client for managing communication with the generative AI platform.
loggerILoggerOptional 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
apiKeystringThe API key used for authenticating requests to the generative AI platform.
modelstringOptional identifier or name of the model to be used. Defaults to system environment variables or a platform-specific default model if not provided.
configGenerationConfigOptional configuration for text generation to customize behavior, such as adjusting temperature or token limits.
safetySettingsICollection<SafetySetting>Optional collection of safety settings to enforce content generation rules and guidelines.
systemInstructionstringOptional instruction defining the system's behavior, providing an initial context or prompt.
httpClientHttpClientOptional HTTP client used for executing API communication with the generative platform.
loggerILoggerOptional 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
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
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
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
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
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
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
FunctionCallingBehaviour
Specifies the configuration settings for function calling behavior in the generative model.
public FunctionCallingBehaviour FunctionCallingBehaviour { get; set; }
Property Value
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
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
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
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
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
nullif 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
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
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
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
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
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
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
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
requestGenerateContentRequestThe 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
toolGoogleFunctionToolThe Google function tool to be added.
toolConfigToolConfigOptional configuration for the tool.
functionCallingBehaviourFunctionCallingBehaviourOptional 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
toolIFunctionToolThe implementation of the IFunctionTool interface to be added.
toolConfigToolConfigOptional configuration for the tool.
functionCallingBehaviourFunctionCallingBehaviourOptional 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
requestGenerateContentRequestThe 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
originalRequestGenerateContentRequestThe original content generation request containing existing contents.
responseGenerateContentResponseThe 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
originalRequestGenerateContentRequestresponseGenerateContentResponsecancellationTokenCancellationToken
Returns
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
originalRequestGenerateContentRequestresponseGenerateContentResponsecancellationTokenCancellationToken
Returns
CountTokensAsync(CountTokensRequest, CancellationToken)
Counts tokens asynchronously based on the provided request.
public Task<CountTokensResponse> CountTokensAsync(CountTokensRequest request, CancellationToken cancellationToken = default)
Parameters
requestCountTokensRequestAn instance of CountTokensRequest containing the details of contents or generation parameters for counting tokens.
cancellationTokenCancellationTokenToken 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
generateContentRequestGenerateContentRequestAn instance of GenerateContentRequest containing the input data for which the token count is calculated.
cancellationTokenCancellationTokenA 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
contentsIEnumerable<Content>A collection of Content objects for which the token count needs to be determined.
cancellationTokenCancellationTokenToken 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
partsIEnumerable<Part>A collection of Part objects representing the input data for token counting.
cancellationTokenCancellationTokenToken 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
requestGenerateAnswerRequestThe request containing the input details for generating an answer.
cancellationTokenCancellationTokenA 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
requestGenerateContentRequestAn instance of GenerateContentRequest containing the input configuration and settings for content generation.
cancellationTokenCancellationTokenToken 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
partsIEnumerable<Part>The sequence of Part objects to be used for generating content.
cancellationTokenCancellationTokenA 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
promptstringThe input text prompt used for content generation.
fileUristringThe URI of the file to be included in the content generation process.
mimeTypestringThe MIME type of the file referenced by the URI.
cancellationTokenCancellationTokenA 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
promptstringThe text input used to generate the content.
cancellationTokenCancellationTokenToken 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
requestGenerateContentRequestAn instance of GenerateContentRequest containing the input configuration and settings for content generation.
cancellationTokenCancellationTokenToken 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
TThe 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
requestGenerateContentRequestAn instance of GenerateContentRequest containing the input configuration and settings for enum generation.
cancellationTokenCancellationTokenToken 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
TThe 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
partsIEnumerable<Part>A collection of Part objects representing the input components for generating the enumeration.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task<T>
A task representing the asynchronous operation, containing the generated enumeration of type
T.
Type Parameters
TThe 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
promptstringThe textual input used to generate an enumeration of type
T.cancellationTokenCancellationTokenToken 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
TThe 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
requestGenerateContentRequestAn instance of GenerateContentRequest containing the input configuration and settings for content generation.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
- Task<T>
A task that represents the asynchronous operation, containing the generated object of type
T.
Type Parameters
TThe 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
partsIEnumerable<Part>An enumerable containing the input parts used for generating the content.
cancellationTokenCancellationTokenA 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
TThe 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
promptstringThe text input used to generate the content.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
- Task<T>
A task representing the asynchronous operation, containing the generated object of type
T.
Type Parameters
TThe 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
requestGenerateContentRequestThe 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
chatSessionBackUpDataChatSessionBackUpDataThe backup data for the chat session, containing the model, safety settings, generation configuration, and history.
toolsList<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
historyList<Content>Optional chat history to be included in the new session.
configGenerationConfigOptional configuration for text generation, including parameters such as temperature and maximum tokens.
safetySettingsICollection<SafetySetting>Optional collection of safety settings to ensure the chat session adheres to defined guidelines.
systemInstructionstringOptional 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
requestGenerateContentRequestThe content generation request containing input configurations and options.
cancellationTokenCancellationTokenAn 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
contentsIEnumerable<Content>The Contents containing the parameters for content generation.
cancellationTokenCancellationTokenOptional 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
partsIEnumerable<Part>The Parts containing input parameters for the content generation process.
cancellationTokenCancellationTokenA 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
promptstringThe input string used as a prompt for generating and streaming content.
fileUristringThe URI of the file to be used for content generation.
mimeTypestringThe MIME type associated with the provided file URI.
cancellationTokenCancellationTokenToken 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
promptstringThe input prompt to be processed and streamed.
cancellationTokenCancellationTokenA 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
fileSearchStoreNamesIEnumerable<string>Store names, either
fileSearchStores/{id}or just{id}.topKint?Optional number of chunks to retrieve.
metadataFilterstringOptional 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
fileSearchStoreNamesstring[]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
corpusIdstringThe identifier for the corpus to be associated with the Vertex Retrieval Tool.
retrievalConfigRagRetrievalConfigAn 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
requestGenerateContentRequestThe 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.