我在尝试构建一个生成图像的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();