Table of Contents

Class BaseModel

Namespace
GenerativeAI
Assembly
GenerativeAI.dll

The BaseModel class is an abstract representation of the model interactions in the GenerativeAI library. It serves as a foundational implementation that enables derived classes to interact with the GenerativeAI platform. This class inherits from BaseClient. It provides protected methods for use by derived classes, allowing them to handle content generation, token management, and content embedding functionalities. BaseModel is designed for flexible extension, enabling the development of specific AI model implementations.

public abstract class BaseModel : BaseClient
Inheritance
BaseModel
Derived
Inherited Members
Extension Methods

Constructors

BaseModel(IPlatformAdapter, HttpClient?, ILogger?)

Abstract base class for models that interact with generative AI functionalities, providing common implementation details for generating content, embedding content, and token counting operations.

protected BaseModel(IPlatformAdapter platform, HttpClient? httpClient, ILogger? logger = null)

Parameters

platform IPlatformAdapter
httpClient HttpClient
logger ILogger

Remarks

This class extends BaseClient and serves as a foundational class for specific models like embedding or generative models. It provides core functionality for handling requests to the generative AI services, including streaming and non-streaming content generation, token counting, and content embedding.

Methods

BatchEmbedContentAsync(string, BatchEmbedContentRequest, CancellationToken)

Embeds a batch of content using the specified Generative AI model.

protected virtual Task<BatchEmbedContentsResponse> BatchEmbedContentAsync(string model, BatchEmbedContentRequest request, CancellationToken cancellationToken = default)

Parameters

model string

The name of the Generative AI Model to use for embedding the content. Format: "models/{model}".

request BatchEmbedContentRequest

The batch BatchEmbedContentRequest containing the content and optional settings for embedding.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<BatchEmbedContentsResponse>

The BatchEmbedContentsResponse containing the content embeddings.

See Also

CheckBlockedResponse(GenerateContentResponse?, string)

Checks the response from the generative AI model to determine if the response is blocked or invalid and throws an exception if so.

protected static void CheckBlockedResponse(GenerateContentResponse? response, string url)

Parameters

response GenerateContentResponse

The GenerateContentResponse received from the generative AI model, which may contain content candidates.

url string

The URL of the request made to the generative AI model, used for error reporting.

Exceptions

GenerativeAIException

Thrown if the response is blocked or invalid with details of the error.

CheckBlockedResponse(GenerateContentResponse?, Uri)

Checks if the response contains blocked content and handles accordingly.

protected void CheckBlockedResponse(GenerateContentResponse? response, Uri url)

Parameters

response GenerateContentResponse

The response to check for blocked content.

url Uri

The URL of the request that generated the response.

CountTokensAsync(string, CountTokensRequest, CancellationToken)

Asynchronously counts the number of tokens in a given input using the specified model and request data.

protected virtual Task<CountTokensResponse> CountTokensAsync(string model, CountTokensRequest request, CancellationToken cancellationToken = default)

Parameters

model string

The name of the Generative AI model to use for token counting, in the format models/{model}.

request CountTokensRequest

The CountTokensRequest containing the input data for token counting.

cancellationToken CancellationToken

An optional token to observe while waiting for the task to complete.

Returns

Task<CountTokensResponse>

A CountTokensResponse containing details about the token count.

EmbedContentAsync(string, EmbedContentRequest, CancellationToken)

Embeds content using the specified Generative AI Model.

protected virtual Task<EmbedContentResponse> EmbedContentAsync(string model, EmbedContentRequest request, CancellationToken cancellationToken = default)

Parameters

model string

The name of the Generative AI Model to use for embedding the content. Format: "models/{model}".

request EmbedContentRequest

The EmbedContentRequest containing the content and optional settings for embedding.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<EmbedContentResponse>

The EmbedContentResponse containing the generated text embedding vector.

See Also

GenerateAnswerAsync(string, GenerateAnswerRequest, CancellationToken)

Generates a grounded answer from the model given an input GenerateAnswerRequest.

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

Parameters

model string

The name of the Model to use for generating the grounded response. Format: models/{model}.

request GenerateAnswerRequest

The GenerateAnswerRequest containing the input data.

cancellationToken CancellationToken

Returns

Task<GenerateAnswerResponse>

The GenerateAnswerResponse containing the model's answer.

See Also

GenerateContentAsync(string, GenerateContentRequest, CancellationToken)

Generates a model response given an input GenerateContentRequest.

protected virtual Task<GenerateContentResponse> GenerateContentAsync(string model, GenerateContentRequest request, CancellationToken cancellationToken = default)

Parameters

model string

The name of the Generative AI Model to use for generating the completion. Format: models/{model}.

request GenerateContentRequest

The GenerateContentRequest containing the content of the current conversation with the model.

cancellationToken CancellationToken

An optional token to observe while waiting for the task to complete.

Returns

Task<GenerateContentResponse>

The GenerateContentResponse containing the model's response.

See Also

GenerateContentStreamAsync(string, GenerateContentRequest, CancellationToken)

T Streams model responses chunk by chunk given an input GenerateContentRequest.

protected virtual IAsyncEnumerable<GenerateContentResponse> GenerateContentStreamAsync(string model, GenerateContentRequest request, CancellationToken cancellationToken = default)

Parameters

model string

The name of the Generative AI Model to use for generating the completion. Format: "models/{model}".

request GenerateContentRequest

The GenerateContentRequest containing content of the current conversation with the model.

cancellationToken CancellationToken

Token for cancelling the streaming process.

Returns

IAsyncEnumerable<GenerateContentResponse>

An async stream of GenerateContentResponse items.

See Also