GenerativeModelPreview

GenerativeModelPreview

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.

Constructor

new GenerativeModelPreview(getGenerativeModelParams)

Parameters:
Name Type Description
getGenerativeModelParams

GetGenerativeModelParams

Methods

(async) countTokens(request)

Makes an async request to count tokens.

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

Parameters:
Name Type Description
request

A CountTokensRequest object with the request contents.

Returns:
Type Description

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);
```

fetchToken()

Gets access token from GoogleAuth. Throws GoogleAuthError when fails.

Returns:
Type Description

Promise of token string.

(async) generateContent(request)

Makes an async call to generate content.

The response will be returned in GenerateContentResult.response.

Parameters:
Name Type Description
request

A GenerateContentRequest object with the request contents.

Returns:
Type Description

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));
```

(async) generateContentStream(request)

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.

Parameters:
Name Type Description
request

GenerateContentRequest

Returns:
Type Description

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));
```

startChat(request)

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.

Parameters:
Name Type Description
request

StartChatParams

Returns:
Type Description

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));
```