Firebase OpenAI API 错误:”openai.createCompletion is not a function”

我在运行我的 Firebase 函数时遇到了问题。它需要每天生成一个挑战(我现在设置为每分钟运行一次以便调试),并将其保存到我的实时数据库中。代码如下所示:

const functions = require("firebase-functions");const admin = require("firebase-admin");const OpenAI = require("openai");admin.initializeApp();// Create a new instance of the OpenAI API clientconst openai = new OpenAI({  apiKey: functions.config().openai.key,});exports.generateDailyChallenge = functions.pubsub.schedule("* * * * *")    .onRun(async (context) => {      try {        const response = await openai.createCompletion({          model: "text-davinci-003",          prompt: "You are an AI model powering a social media app based " +                "around challenges. The app works in a similar way to " +                "BeReal, meaning that once a day it generates a challenge " +                "for the users. The challenge needs to be simple to " +                "complete and require just a single photo taken from both " +                "cameras, just keep this in mind, but don't mention it as " +                "the users are used to it. The challenge is supposed to be " +                "fun, but easy to complete so users don't have to spend " +                "too much time on it. The goal is to connect with your " +                "online friends in a fun and engaging way. Mainly to just " +                "see what they were up to at that specific moment. Give me " +                "just the challenge name and a brief description of the " +                "challenge as if you were challenging a user to do it",          max_tokens: 150,        });        const challengeText = response.data.choices[0].text.trim();        console.log("Generated Challenge:", challengeText);        // Save to Firestore        const db = admin.firestore();        await db.collection("challenges").add({          text: challengeText,          createdAt: admin.firestore.FieldValue.serverTimestamp(),        });      } catch (error) {        console.error("Error generating or saving challenge:", error.message);      }    });

然而,当我运行它时,我在日志中得到了这个错误:

    {  "textPayload": "Error generating or saving challenge: openai.createCompletion is not a function",  "insertId": "65b7b5f100015b11309c47bd",  "resource": {    "type": "cloud_function",    "labels": {      "project_id": "nocena-dea56",      "function_name": "generateDailyChallenge",      "region": "us-central1"    }  },  "timestamp": "2024-01-29T14:28:01.088849Z",  "labels": {    "runtime_version": "nodejs16_20240121_16_20_2_RC00",    "instance_id": "0087599d42bd678f896459629280089ca75acae30403ae873cd7bb1e45f4fe7e158e416746bfcd7cebb55af5ab4abe7bf80c2b4277d2d0d089ec3f3e043ca6018f63",    "execution_id": "lzchc47te50n"  },  "logName": "projects/nocena-dea56/logs/cloudfunctions.googleapis.com%2Fcloud-functions",  "trace": "projects/nocena-dea56/traces/8bff527e9d2bcf302a301c3630917ee8",  "receiveTimestamp": "2024-01-29T14:28:01.426853583Z"}

当我尝试调试时,有人告诉我我没有正确初始化函数。然而事实并非如此。我还尝试了多次更改初始化的方式。我使用的是这个来调用它:https://www.npmjs.com/package/openai(这是版本 4.26.0),所以我应该这样初始化它。

如果没有人能帮我,也许你可以推荐我使用其他 AI,因为在这一点上我怀疑这可能是 OpenAI API 的问题。


回答:

您使用的是 OpenAI Node.js SDK v4 或更新版本。

在 >=v4 版本中与 <v4 版本相比有多个变化,主要包括:

  1. 初始化
  2. 方法名称
  3. 消息获取

您的初始化是正确的。还剩下两个问题,再加上一个与 SDK 版本无关的额外问题。

问题 1:方法名称错误

您需要将方法名称从这个…

openai.createCompletion

…更改为这个。

openai.completions.create

问题 2:消息获取错误

将这个…

response.data.choices[0].text

…更改为这个。

response.choices[0].text

额外问题:使用已废弃的模型

此外,text-davinci-003 模型已被废弃。推荐使用 gpt-3.5-turbo-instruct 模型作为替代。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注