Skip to content

Commit d92d13f

Browse files
authored
displays finsihReason
displays if response.candidates[0].finishReason is not 'STOP' chat session ends if RECITATION or OTHER error received look at: google-gemini/deprecated-generative-ai-js#124
1 parent 7434165 commit d92d13f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ <h1>Gemini 1.5 Pro Chat</h1>
6161

6262
socket.on('error', (error) => {
6363
responseArea.innerHTML = error;
64+
sendButton.disabled = false; // Enable button after response is received
65+
isSending = false; // Reset flag after response is received
66+
6467
});
6568

6669
</script>

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
1010
const fs = require('fs'); // Import the file system module
1111
const apiVersion = 'v1beta';
1212
const generationConfig = {
13-
temperature: 0.9,
13+
temperature: 0.0,
1414
};
1515
const safetySettings = [
1616
{
@@ -49,7 +49,7 @@ io.on('connection', async (socket) => {
4949
// Read system text from file asynchronously
5050
const systemText = await readSystemTextFromFile("systemi.txt");
5151
// let systemText = "";
52-
const systemInstruction = { role: "system", parts: [{ text: systemText }] };
52+
const systemInstruction = { role: "system", parts: [{ text: systemText }] };
5353

5454
const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro-latest", systemInstruction, generationConfig, safetySettings }, { apiVersion });
5555
const chat = model.startChat({});
@@ -60,9 +60,14 @@ io.on('connection', async (socket) => {
6060
const result = await chat.sendMessage(prompt);
6161
const response = await result.response;
6262
console.log('Response received');
63-
// console.log(util.inspect(response.candidates, {showHidden: false, depth: null, colors: true}))
64-
const output = response.text();
63+
console.log(util.inspect(response.candidates, {showHidden: false, depth: null, colors: true}))
64+
if (response.candidates[0].finishReason === 'STOP') {
65+
const output = response.text();
6566
socket.emit('response', output);
67+
}
68+
else {
69+
socket.emit('error', 'An error occurred: ' + response.candidates[0].finishReason );
70+
}
6671
} catch (err) {
6772
console.error(err);
6873
socket.emit('error', 'An error occurred');
@@ -103,4 +108,4 @@ function printChatNicely(chatHistory) {
103108

104109
server.listen(3000, () => {
105110
console.log('Server listening on port 3000');
106-
});
111+
});

0 commit comments

Comments
 (0)