Hello all, I wanted to know if anyone could help me with the Vision API integration in my telegram bot code? I keep getting
"Bot is running…
Error getting image description: {
error: {
message: "Invalid chat format. Expected 'content' field in all messages to be either str or list.",
type: 'invalid_request_error',
param: null,
code: null
}
}
"
I added money on openai account for the api use and it’s the correct API key as its working for the text generation part but not the vision API…
This is the Replit link to my bot code if anyone is able to help edit the code to process the images ): Thank you so much if so!
Relevant code:
const axios = require("axios");
async function processImageInput(chatId, buffer, mimeType, OPENAI_API_KEY) {
try {
// Check image size
const MAX_IMAGE_SIZE_BYTES = 20 * 1024 * 1024; // 20MB in bytes
if (buffer.byteLength > MAX_IMAGE_SIZE_BYTES) {
bot.sendMessage(chatId, "The image is too large to process. Please upload a smaller image.");
return;
}
// Convert the image buffer to base64
const base64Image = Buffer.from(buffer).toString('base64');
// Call OpenAI Vision API for image processing
const imageDescription = await getImageDescription(base64Image, mimeType, chatId, OPENAI_API_KEY);
// Update state and notify the user
updateState(chatId, 'garmentSketchDetails', imageDescription);
bot.sendMessage(chatId, "Sketch processed. Please provide any additional details or send 'done' if you are finished.");
updateState(chatId, "stage", "awaiting_additional_info");
} catch (error) {
console.error('Error processing the image:', error);
bot.sendMessage(chatId, "Error converting the image. Please try again.");
}
}
async function getImageDescription(base64Image, mimeType, chatId, OPENAI_API_KEY) {
try {
const payload = {
model: "gpt-4-vision-preview",
messages: [
{
role: "system",
content: "Analyze the garment sketch and provide a description."
},
{
role: "user",
content: `data:${mimeType};base64,${base64Image}`
}
],
max_tokens: 3500
};
const headers = {
Authorization: `Bearer ${OPENAI_API_KEY}`,
"Content-Type": "application/json"
};
const response = await axios.post("https://api.openai.com/v1/chat/completions", payload, { headers });
if (response.data.choices && response.data.choices.length > 0) {
const content = response.data.choices[0].message.content;
console.log("Response received:", content);
return content;
} else {
throw new Error('No response from OpenAI API');
}
} catch (error) {
console.error('Error getting image description:', error.response ? error.response.data : error.message);
let errorMessage = "Sorry, I couldn't analyze the image. Please try again later.";
if (error.message.includes('Unsupported image type')) {
errorMessage = error.message;
}
throw new Error(errorMessage);
}
}