I am trying to use the Gemini Pro Vision AI API in my Node.js application to generate text based on a prompt. However, I am receiving an "Invalid JSON response: null" error when I send a request to the /speech endpoint.
I have tried the following:
Verified that my API key is correct. Tested the endpoint using Postman and received a successful response. Checked the server-side code for errors in JSON serialization. I am not sure what is causing the error and would appreciate any help in debugging this issue.
async function sendPrompt(file, prompt, language) {
try {
const datasend = { file, prompt, language };
const jsonData = JSON.stringify(datasend);
const response = await fetch("/speech", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: jsonData
});
if (response.ok) {
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
const data = await response.json();
const text = data.text;
// const image = data.file;
// Speak the text using the p5.js library
speech.speak(text);
// Redirect naar de pagina met de image URL
// window.location.href = `/generate-prompt?imageUrl=${encodeURIComponent(
// JSON.stringify(image) + "?imageUrl=" ${encodeURIComponent(JSON.stringify(text))}
// )}`;
console.log("Upload response:", data);
} else {
console.warn("Unexpected response format. Not JSON.");
}
} else {
console.error("Failed to upload photo:", response.statusText);
}
console.log("Upload response:", data);
} catch (err) {
console.log(err);
}
}
app.post("/speech", async (req, res) => {
const prompt = req.body.prompt;
const file = req.body.file;
const language = req.body.language;
console.log(prompt, file, language);
if (prompt) {
const userId = await getUserIdFromSession(req.session);
// Call the new function
console.log("userId", userId);
const options = {
method: "POST",
url: "https://gemini-pro-vision-ai1.p.rapidapi.com/",
headers: {
"content-type": "application/json",
"X-RapidAPI-Key": "71956f59b5msh089630402391f02p108b9bjsna7adb689e8c4",
"X-RapidAPI-Host": "gemini-pro-vision-ai1.p.rapidapi.com"
},
data: {
contents: [
{
parts: [
{
inline_data: {
mime_type: "image/png",
data: "https://www.takeaway.com/be/foodwiki/uploads/sites/2/2017/10/doner-kebab-3-1080x960.jpg"
}
},
{
text:
"gedraag je als een hulp assistent voor een blind persoon" +
prompt +
"in de volgende taal" +
language
}
]
}
]
}
};
try {
const response = await axios.request(options);
const jsonData = response.data;
if (jsonData && jsonData.candidates && jsonData.candidates.length > 0) {
const text = jsonData.candidates[0].content.parts[0].text;
console.log(text); // Log de volledige respons
await savePrompt(prompt, file, language, userId, text);
res.json({ text, file });
} else {
console.error("Invalid JSON response:", jsonData);
// Mogelijk moet hier een fallback-logica worden toegevoegd
}
} catch (error) {
console.error(error);
}
}
});
Verified that my API key is correct. Tested the endpoint using Postman and received a successful response. Checked the server-side code for errors in JSON serialization. I am not sure what is causing the error and would appreciate any help in debugging this issue.