AWS Lambda中Node.js调用GPT-3.5 API报错求助
短信调用GPT-3.5时OpenAI API调用报错问题解决
问题说明
尝试实现短信调用GPT-3.5功能时,遇到两类API调用错误:
- 使用最新调用方式
openai.chat.completions.create时,报错:
Cannot read properties of undefined (reading 'completions')
- 使用旧版调用方式
openai.createCompletion时,报错:
TypeError: openai.createChatCompletion is not a function
已尝试更新openai包、切换新旧调用方式、更换API密钥、执行npm update,在AWS Lambda环境下问题仍未解决。
原代码如下:
const openai = require('openai'); const accountSid = process.env.TWILIO_ACCOUNT_SID; const authToken = process.env.TWILIO_AUTH_TOKEN; const client = require('twilio')(accountSid, authToken); openai.apiKey = process.env.OPENAI_AUTH, exports.handler = async (event, context) => { try { const buff = Buffer.from(event.body, "base64"); const formEncodedParams = buff.toString("utf-8"); const urlSearchParams = new URLSearchParams(formEncodedParams); const body = urlSearchParams.get("Body"); const from = urlSearchParams.get("From"); const completion = await openai.chat.completions.create({ engine: "gpt-3.5-turbo", // GPT-3.5 Turbo messages: [ { role: "system", content: body }, ], max_tokens: 100, // You can adjust this as needed }); await sendMessageBack(completion.data.choices[0].message.content, from); return { statusCode: 200, body: JSON.stringify('Message sent successfully'), }; } catch (error) { console.error(error); return { statusCode: 500, body: JSON.stringify('Error sending message'), }; } }; async function sendMessageBack(msg, to) { try { await client.messages.create({ body: msg, to: to, from: process.env.TWILIO_PHONE_NUM, }); console.log('Message sent:', msg); } catch (e) { console.error('Error sending message:', e); } }
问题根源
OpenAI SDK v4+版本做了重大调整:
- 不再支持通过
require('openai')直接调用顶层模块属性,必须实例化客户端对象。 - 原API密钥配置方式
openai.apiKey = ...失效,需在实例化时传入。 - API调用参数中
engine字段已更名为model,返回结果结构也做了简化(不再嵌套data字段)。
解决步骤
- 安装最新版OpenAI SDK:
npm install openai@latest
- 使用ES模块导入并正确实例化OpenAI客户端。
- 修正API调用参数及结果读取路径。
修正后的代码
import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: process.env.OPENAI_AUTH, }); import twilio from 'twilio'; const accountSid = process.env.TWILIO_ACCOUNT_SID; const authToken = process.env.TWILIO_AUTH_TOKEN; const client = twilio(accountSid, authToken); export const handler = async (event, context) => { try { const buff = Buffer.from(event.body, "base64"); const formEncodedParams = buff.toString("utf-8"); const urlSearchParams = new URLSearchParams(formEncodedParams); const msgBody = urlSearchParams.get("Body"); const from = urlSearchParams.get("From"); const chatCompletion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{ role: "system", content: msgBody }], max_tokens: 100, }); await sendMessageBack(chatCompletion.choices[0].message.content, from); return { statusCode: 200, body: JSON.stringify('Message sent successfully'), }; } catch (error) { console.error(error); return { statusCode: 500, body: JSON.stringify('Error sending message'), }; } }; async function sendMessageBack(msg, to) { try { await client.messages.create({ body: msg, to: to, from: process.env.TWILIO_PHONE_NUM, }); console.log('Message sent:', msg); } catch (e) { console.error('Error sending message:', e); } }
内容的提问来源于stack exchange,提问作者Nick Fox
相关产品推荐
相关产品推荐

