Class GenerativeModelPreview (1.9.0)

The GenerativeModelPreview class is the base class for the generative models that are in preview. NOTE: Don't instantiate this class directly. Use vertexai.preview.getGenerativeModel() instead.

Package

@google-cloud/vertexai

Constructors

(constructor)(getGenerativeModelParams)

constructor(getGenerativeModelParams: GetGenerativeModelParams);

Constructs a new instance of the GenerativeModelPreview class

Parameter
Name Description
getGenerativeModelParams GetGenerativeModelParams

GetGenerativeModelParams

Methods

countTokens(request)

countTokens(request: CountTokensRequest): Promise<CountTokensResponse>;

Makes an async request to count tokens.

The countTokens function returns the token count and the number of billable characters for a prompt.

Parameter
Name Description
request CountTokensRequest

A CountTokensRequest object with the request contents.

Returns
Type Description
Promise<CountTokensResponse>

The CountTokensResponse object with the token count.

Example

const request = {
  contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
};
const resp = await generativeModelPreview.countTokens(request);
console.log('count tokens response: ', resp);

generateContent(request)

generateContent(request: GenerateContentRequest | string): Promise<GenerateContentResult>;

Makes an async call to generate content.

The response will be returned in GenerateContentResult.response.

Parameter
Name Description
request GenerateContentRequest | string

A GenerateContentRequest object with the request contents.

Returns
Type Description
Promise<GenerateContentResult>

The GenerateContentResponse object with the response candidates.

Example

const request = {
  contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
};
const result = await generativeModelPreview.generateContent(request);
console.log('Response: ', JSON.stringify(result.response));

generateContentStream(request)

generateContentStream(request: GenerateContentRequest | string): Promise<StreamGenerateContentResult>;

Makes an async stream request to generate content.

The response is returned chunk by chunk as it's being generated in StreamGenerateContentResult.stream. After all chunks of the response are returned, the aggregated response is available in StreamGenerateContentResult.response.

Parameter
Name Description
request GenerateContentRequest | string

GenerateContentRequest

Returns
Type Description
Promise<StreamGenerateContentResult>

Promise of StreamGenerateContentResult

Example

const request = {
  contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
};
const streamingResult = await generativeModelPreview.generateContentStream(request);
for await (const item of streamingResult.stream) {
  console.log('stream chunk: ', JSON.stringify(item));
}
const aggregatedResponse = await streamingResult.response;
console.log('aggregated response: ', JSON.stringify(aggregatedResponse));

getCachedContent()

getCachedContent(): CachedContent | undefined;
Returns
Type Description
CachedContent | undefined

getModelName()

getModelName(): string;
Returns
Type Description
string

getSystemInstruction()

getSystemInstruction(): Content | undefined;
Returns
Type Description
Content | undefined

startChat(request)

startChat(request?: StartChatParams): ChatSessionPreview;

Instantiates a ChatSessionPreview.

The ChatSessionPreview class is a stateful class that holds the state of the conversation with the model and provides methods to interact with the model in chat mode. Calling this method doesn't make any calls to a remote endpoint. To make remote call, use ChatSessionPreview.sendMessage() or ChatSessionPreview.sendMessageStream().

Parameter
Name Description
request StartChatParams

StartChatParams

Returns
Type Description
ChatSessionPreview

ChatSessionPreview

Example

const chat = generativeModelPreview.startChat();
const result1 = await chat.sendMessage("How can I learn more about Node.js?");
const response1 = await result1.response;
console.log('Response: ', JSON.stringify(response1));

const result2 = await chat.sendMessageStream("What about python?");
const response2 = await result2.response;
console.log('Response: ', JSON.stringify(await response2));