I’m working on NodeJs server to connect with google gemini ai api
everything is good except when i add the safety parameter as the documentation say’s here
here is my code
const express = require('express');
require('dotenv').config();
const router = express.Router();
const { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } = require('@google/generative-ai');
const GEMINI_KEY = process.env.GEMINI_AI_KEY
const GEMINI_MODEL = process.env.GEMINI_MODEL
const genAI = new GoogleGenerativeAI(GEMINI_KEY);
router.post('/text-generation', async (req, res) => {
const { prompt } = req.body; // Corrected variable name
try {
// Check if prompt is provided
if (!prompt) {
return res.status(400).json({ error: 'Enter prompt text before generating.' });
}
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
];
const model = genAI.getGenerativeModel({ model: GEMINI_MODEL, safetySettings });
const result = await model.generateContent(prompt);
const response = await result.response.text();
res.status(200).json({ success: true, code: 100, data: response });
} catch (error) {
res.status(500).json({ success: false, code: 200, data: error });
console.error(error);
// res.status(500).json({ error: 'Failed to generate text' });
}
});
module.exports = router;
now when i make a post request i get this response
{
"success": false,
"code": 200,
"data": {
"status": 400,
"statusText": "Bad Request"
}
}
i tried most of the solutions online and i tried google documentations