Hi Everyone,
I m using the open ai api (/chat/completions) with the request that takes up an image and a prompt below is the code:
app.post(‘/upload’, upload.single(‘image’), async (req, res) => {
const { prompt } = req.body;
const imagePath = req.file.path;
try {
// Read the image file
const image = fs.readFileSync(req.file.path);
console.log("image : ",image);
console.log("prompt : ", req.body.prompt);
// Send the image and prompt to OpenAI’s API
const response = await axios.post(
‘url’,
{
model: “gpt-4-turbo”,
messages: [
{
role: “system”,
content: “You are an assistant that analyzes image.”,
},
{
role: “user”,
content: req.body.prompt,
},
],
image: image
},
{
headers: {
‘Content-Type’: ‘multipart/form-data’,
Authorization: Bearer OPEN_AI_API_KEY
,
},
}
);
res.json(response.data);
} catch (error) {
console.error(error);
res.status(500).json({ error: ‘Failed to process request’ });
} finally {
fs.unlinkSync(imagePath); // Clean up uploaded image
}
});
But I m getting below error:
status: 400
Can you please guide me with the solution?
The 400 error indicates that something is wrong with the format or syntax of your request.
Make sure to read the documentation carefully and compare it to your code.
Thanks for your response.
The code mentioned in the link works well but I want to pass it as a image file received in the request of the api and not as a image url.
Oh I see, thank you for the clarification, I misunderstood.
Check this resource instead.
Hi,
I tried the code present in the given link but still I m facing error as below:
Error: Request failed with status code 400
OpenAI Response Error: {
error: {
message: “Invalid type for ‘messages[0].content’: expected one of a string or array of objects, but got an object instead.”,
type: ‘invalid_request_error’,
param: ‘messages[0].content’,
code: ‘invalid_type’
}
}
code is as below:
const gptResponse = await axios.post(“url”,
{
model: “gpt-4-turbo”,
messages: [
{
role: “system”,
content: {prompt},
},
{
role: “user”,
content: “data:image/jpeg;base64,”+{base64EncodedImage},
},
],
},
{
headers: {
“Content-Type”: “application/json”,
Authorization: Bearer ...
, // Replace with your OpenAI API key
},
}
);
// Function to encode an image to Base64
function encodeImageToBase64(filePath) {
// Read the image file as binary data
const imageBuffer = fs.readFileSync(filePath);
// Convert the binary data to Base64
const base64Image = imageBuffer.toString(‘base64’);
return base64Image;
}
Silly question, but did you encode the image?
I see you passing {base64EncodedImage}
but is it the result of you calling the encodeImageToBase64 function with the path to the image file as an argument?
Your format doesn’t seem to be right from what I can tell, please read the documentation carefully and compare to your format.
You are passing role and content, where in role: “system”
content is {prompt}
, but in content there has to be a type
and text
key.
Same for where you’re passing the role: “user”
Fix your format and everything should work.
Also, if you press ctrl + e in the forum when answering, pasting code
will look like this and it will be a lot easier to read!
Let me know if this helped.
1 Like