GenerativeModel class

Class for generative model APIs.

Signature:

export declare class GenerativeModel extends AIModel 

Extends: AIModel

Constructors

Constructor Modifiers Description
(constructor)(ai, modelParams, requestOptions, chromeAdapter) Constructs a new instance of the GenerativeModel class

Properties

Property Modifiers Type Description
generationConfig GenerationConfig
requestOptions RequestOptions
safetySettings SafetySetting[]
systemInstruction Content
toolConfig ToolConfig
tools Tool[]

Methods

Method Modifiers Description
countTokens(request, singleRequestOptions) Counts the tokens in the provided request.
generateContent(request, singleRequestOptions) Makes a single non-streaming call to the model and returns an object containing a single GenerateContentResponse.
generateContentStream(request, singleRequestOptions) Makes a single streaming call to the model and returns an object containing an iterable stream that iterates over all chunks in the streaming response as well as a promise that returns the final aggregated response.
initializeDeviceModel(onDownloadProgress) Initializes on-device models.
startChat(startChatParams) Gets a new ChatSession instance which can be used for multi-turn chats.

GenerativeModel.(constructor)

Constructs a new instance of the GenerativeModel class

Signature:

constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions, chromeAdapter?: ChromeAdapter | undefined);

Parameters

Parameter Type Description
ai AI
modelParams ModelParams
requestOptions RequestOptions
chromeAdapter ChromeAdapter | undefined

GenerativeModel.generationConfig

Signature:

generationConfig: GenerationConfig;

GenerativeModel.requestOptions

Signature:

requestOptions?: RequestOptions;

GenerativeModel.safetySettings

Signature:

safetySettings: SafetySetting[];

GenerativeModel.systemInstruction

Signature:

systemInstruction?: Content;

GenerativeModel.toolConfig

Signature:

toolConfig?: ToolConfig;

GenerativeModel.tools

Signature:

tools?: Tool[];

GenerativeModel.countTokens()

Counts the tokens in the provided request.

Signature:

countTokens(request: CountTokensRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;

Parameters

Parameter Type Description
request CountTokensRequest | string | Array<string | Part>
singleRequestOptions SingleRequestOptions

Returns:

Promise<CountTokensResponse>

GenerativeModel.generateContent()

Makes a single non-streaming call to the model and returns an object containing a single GenerateContentResponse.

Signature:

generateContent(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;

Parameters

Parameter Type Description
request GenerateContentRequest | string | Array<string | Part>
singleRequestOptions SingleRequestOptions

Returns:

Promise<GenerateContentResult>

GenerativeModel.generateContentStream()

Makes a single streaming call to the model and returns an object containing an iterable stream that iterates over all chunks in the streaming response as well as a promise that returns the final aggregated response.

Signature:

generateContentStream(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;

Parameters

Parameter Type Description
request GenerateContentRequest | string | Array<string | Part>
singleRequestOptions SingleRequestOptions

Returns:

Promise<GenerateContentStreamResult>

GenerativeModel.initializeDeviceModel()

Initializes on-device models.

This may trigger a download on first use. Wait for this promise to complete before calling inference methods if you want to ensure the device models are ready before any calls. Calling inference methods before the device is ready will result in a cloud fallback if inferenceMode is set to PREFER_ON_DEVICE, and an error if set to ONLY_ON_DEVICE.

IMPORTANT: This call must be made on or after a user has interacted with the page (for example, through a button click or key press). If it is called without a user interaction, and it requires a download, this will cause an error.

See the Prompt API docs for more details on this requirement.

Signature:

initializeDeviceModel(onDownloadProgress?: (progressValue: number) => void): Promise<void>;

Parameters

Parameter Type Description
onDownloadProgress (progressValue: number) => void A callback called repeatedly as the download progresses that provides a progressValue between 0 and 1 representing how much of the download is complete. This will be ignored if monitor was populated in LanguageModelCreateOptions.

Returns:

Promise<void>

GenerativeModel.startChat()

Gets a new ChatSession instance which can be used for multi-turn chats.

Signature:

startChat(startChatParams?: StartChatParams): ChatSession;

Parameters

Parameter Type Description
startChatParams StartChatParams

Returns:

ChatSession