在AWS Lambda上使用Node.js API for GPT3.5时出错:“无法读取未定义的属性(阅读”completions“)”

7cjasjjr  于 2023-10-24  发布在  Node.js
关注(0)|答案(1)|浏览(203)

我试图让自己的文本GPT3.5,不明白为什么我一直得到这一行的carious错误。
当我使用openai.chat.completions.create时,正如最新的文档所建议的那样,我得到以下错误:
Cannot read properties of undefined (reading 'completions')
如果我使用先前的格式通过openai.js Completion调用完成,我会得到以下错误:
TypeError: openai.createChatCompletion is not a function
我已经阅读了我所能阅读的每一个文档,并且尝试了调用完成的更老的方法,但是不能让任何东西工作。
我已经尝试过获取新的API密钥并进行npm更新,但仍然遇到同样的问题。以下是我的完整代码,我一定遗漏了一些东西:

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包,新的完成调用,旧的调用,阅读文档,但AWS Lambda一直在同一行上抛出错误。
编辑:下面的解决方案导致了下面的重写。在这里包括它,以防它对其他人有帮助:

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);
  }
}
plupiseo

plupiseo1#

你的代码抛出Cannot read properties of undefined (reading 'completions')的原因是因为openai.chat甚至没有定义,所以它试图访问一个尚未声明的变量或属性。这是有道理的,因为通常像这样的库提供一个基类来内部构造所有内容,就像OpenAI一样。所以你必须首先示例化它的原始类,就像这样:
使用openai:4.2.0

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'your api key', // defaults to process.env["OPENAI_API_KEY"]
});

而不是:

const openai = require('openai');

openai.apiKey = process.env.OPENAI_AUTH;

因为后者只是导入OpenAI包,但并没有示例化它,所以只是在那里设置apiKey作为变量。包基类知道如何在内部完成,这有助于他们更好地维护它。
另外,我建议在第18行将engine: "gpt-3.5-turbo",改为model: "gpt-3.5-turbo",因为model是指定它的新方法。

相关问题