Constructor
new ChatSession(request)
Parameters:
Name | Type | Description |
---|---|---|
request |
StartChatSessionRequest |
Methods
fetchToken()
Gets access token from GoogleAuth. Throws GoogleAuthError when fails.
Returns:
Type | Description |
---|---|
Promise of token. |
(async) sendMessage(request)
Makes an async call to send chat message.
The response is returned in GenerateContentResult.response.
Parameters:
Name | Type | Description |
---|---|---|
request |
send message request. |
Returns:
Type | Description |
---|---|
Promise of GenerateContentResult. |
Example
```
const chat = generativeModel.startChat();
const result1 = await chat.sendMessage("How can I learn more about Node.js?");
console.log('Response: ', JSON.stringify(result1.response));
const result2 = await chat.sendMessage("What about python?");
console.log('Response: ', JSON.stringify(result2.response));
```
(async) sendMessageStream(request)
Makes an async call to stream send message.
The response is streamed chunk by chunk in StreamGenerateContentResult.stream. The aggregated response is avaliable in StreamGenerateContentResult.response after all chunks are returned.
Parameters:
Name | Type | Description |
---|---|---|
request |
send message request. |
Returns:
Type | Description |
---|---|
Promise of StreamGenerateContentResult. |
Example
```
const chat = generativeModel.startChat();
const chatInput = "How can I learn more about Node.js?";
const result = await chat.sendMessageStream(chatInput);
for await (const item of result.stream) {
console.log(item.candidates[0].content.parts[0].text);
}
const response = await result.response;
console.log('aggregated response: ', JSON.stringify(result.response));
```