Reputation: 5611
I try to use openApi's api as OCR in node with gpt-4o model from a local image .
const api_key = "mykey"
import OpenAI from 'openai';
import fs from "fs"
const openai = new OpenAI({
apiKey: api_key,
});
function convertToBase64(filePath) {
const bitmap = fs.readFileSync(filePath);
return Buffer.from(bitmap).toString('base64');
}
(async () => {
const chatCompletion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
content: [
{
role: 'user',
content: 'Return me the text of this image.'
},
{
role: 'system',
content: 'image_url',
image_url: `data:image/png;base64${convertToBase64('./image12.png')}`
}
]
}
]
});
console.log(chatCompletion.choices[0].message.content);
})();
I have the error message
(node:23388) [DEP0040] DeprecationWarning: The
punycode
module is deprecated. Please use a userland alternative instead. (Usenode --trace-deprecation ...
to show where the warning was created) url-state-machine.js:2 Uncaught BadRequestError Error: 400 Invalid chat format. Expected 'role' fields in all messages. at generate (path\node_modules\openai\error.mjs:41:20) at makeStatusError (path\node_modules\openai\core.mjs:268:25) at makeRequest (path\node_modules\openai\core.mjs:311:30) at processTicksAndRejections (internal/process/task_queues:95:5) error.mjs:41 Process exited with code 1
Do you know how to do it?
Upvotes: 1
Views: 627
Reputation: 3
The role
property should be located on the same level as the content
:
messages: [
{
role: 'user',
content: [
{
content: 'Return me the text of this image.'
},
{
content: 'image_url',
image_url: `data:image/png;base64${convertToBase64('./image12.png')}`
}
]
}
]
Please note that your code also has other issues, as @ITgoldman pointed out.
Upvotes: 0