Skip to content

Commit

Permalink
displays finsihReason
Browse files Browse the repository at this point in the history
displays if  response.candidates[0].finishReason is not 'STOP'
chat session ends if RECITATION or OTHER error received
look at: google-gemini/generative-ai-js#124
  • Loading branch information
zakcali authored May 6, 2024
1 parent 7434165 commit d92d13f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ <h1>Gemini 1.5 Pro Chat</h1>

socket.on('error', (error) => {
responseArea.innerHTML = error;
sendButton.disabled = false; // Enable button after response is received
isSending = false; // Reset flag after response is received

});

</script>
Expand Down
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const fs = require('fs'); // Import the file system module
const apiVersion = 'v1beta';
const generationConfig = {
temperature: 0.9,
temperature: 0.0,
};
const safetySettings = [
{
Expand Down Expand Up @@ -49,7 +49,7 @@ io.on('connection', async (socket) => {
// Read system text from file asynchronously
const systemText = await readSystemTextFromFile("systemi.txt");
// let systemText = "";
const systemInstruction = { role: "system", parts: [{ text: systemText }] };
const systemInstruction = { role: "system", parts: [{ text: systemText }] };

const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro-latest", systemInstruction, generationConfig, safetySettings }, { apiVersion });
const chat = model.startChat({});
Expand All @@ -60,9 +60,14 @@ io.on('connection', async (socket) => {
const result = await chat.sendMessage(prompt);
const response = await result.response;
console.log('Response received');
// console.log(util.inspect(response.candidates, {showHidden: false, depth: null, colors: true}))
const output = response.text();
console.log(util.inspect(response.candidates, {showHidden: false, depth: null, colors: true}))
if (response.candidates[0].finishReason === 'STOP') {
const output = response.text();
socket.emit('response', output);
}
else {
socket.emit('error', 'An error occurred: ' + response.candidates[0].finishReason );
}
} catch (err) {
console.error(err);
socket.emit('error', 'An error occurred');
Expand Down Expand Up @@ -103,4 +108,4 @@ function printChatNicely(chatHistory) {

server.listen(3000, () => {
console.log('Server listening on port 3000');
});
});

0 comments on commit d92d13f

Please sign in to comment.