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

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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