OpenAI API错误:”Configuration is not a constructor”

我在尝试构建一个生成图像的Discord机器人时遇到了问题。我收到了以下错误:

Configuration is not a constructor

我正在按照一个YouTube教程进行操作。我的代码如下:

const { SlashCommandBuilder, EmbedBuilder} = require(`discord.js`);const { Configuration, OpenAIApi } = require("openai");const configuration = new Configuration({  apiKey: 'My Key'});const openai = new OpenAIApi(configuration);

有谁能帮我解决这个问题吗?


回答:

问题

你的代码适用于OpenAI Node.js SDK的版本低于v4,但你正在使用v4或更高版本。

解决方案

如果你使用的是OpenAI Node.js SDK的v4或更高版本,正确的初始化方式如下:

import OpenAI from "openai";const client = new OpenAI(  {apiKey: "sk-xxxxxxxxxxxxxxxxxxxxxxxx"});

然后你可以使用,例如,Images API,如下所示:

 import OpenAI from "openai"; const client = new OpenAI(  { apiKey: "sk-xxxxxxxxxxxxxxxxxx"} );async function main() {  const image = await client.images.generate({ model: "dall-e-3", prompt: "A cute baby sea otter" });  console.log(image.data);}main();

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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