Node.js中使用OpenAI GPT-4o API实现本地图片OCR报错求助
问题重现
尝试用Node.js调用OpenAI的GPT-4O实现本地图片OCR,编写的代码如下:
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); })();
运行后出现错误:
(node:23388) [DEP0040] DeprecationWarning: The
punycodemodule 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
错误原因
代码存在3个关键格式问题,不符合OpenAI多模态API的要求:
- 消息结构错误:外层消息对象缺少
role字段,且错误地将不同角色的内容嵌套在同一个content数组中。多模态消息应是单个角色(通常为user)的content数组,包含文本和图片元素。 - 图片URL格式错误:Base64前缀
data:image/png;base64后缺少逗号,正确格式应为data:image/png;base64,。 - 图片内容格式错误:图片元素需要指定
type: "image_url",并将图片URL放在image_url对象的url字段中,而非直接写image_url属性。
修正后的代码
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 () => { try { const chatCompletion = await openai.chat.completions.create({ model: "gpt-4o", messages: [ { role: "user", // 外层消息必须指定角色 content: [ { type: "text", // 文本内容需指定类型 text: "Return me the text of this image." }, { type: "image_url", // 图片内容指定类型为image_url image_url: { url: `data:image/png;base64,${convertToBase64('./image12.png')}` // 修复Base64格式,添加逗号 } } ] } ] }); console.log(chatCompletion.choices[0].message.content); } catch (error) { console.error("调用失败:", error); } })();
额外说明
- 关于
punycode模块的 deprecation warning:这是依赖库内部的问题,不影响功能,可忽略或更新OpenAI SDK版本解决。 - 确保图片路径正确,且图片格式与URL中指定的
image/png一致(如果是JPG则改为image/jpeg)。
内容的提问来源于stack exchange,提问作者Pipo

