The requested module 'openai' does not provide an export named 'Configuration' error
我正在尝试使用 MERN 构建 AI 图像生成网站,但收到此错误:
The requested module 'openai' does not provide an export named 'Configuration'.
file:///C:/Users/Rashmika%20Satish/ai_website/server/routes/dalleRoutes.js:3 import {Configuration, OpenAIApi} from 'openai'; ^^^^^^^^^^^^^ SyntaxError: The requested module 'openai' does not provide an export named 'Configuration' at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21) at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
Node.js v18.15.0 [nodemon] app crashed - waiting for file changes before starting...
这是 dalleRoutes.js 页面:
import express from 'express';
import * as dotenv from 'dotenv';
import {Configuration, OpenAIApi} from 'openai';
dotenv.config();
const router = express.Router();
这是index.js页面:
import express from 'express'
import * as dotenv from 'dotenv';
import cors from 'cors';
import connectDB from './mongodb/connect.js';
import postRoutes from './routes/postRoutes.js';
import dalleRoutes from './routes/dalleRoutes.js';
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json({limit: '50mb'}));
app.use('/api/v1/post', postRoutes);
app.use('/api/v1/dalle', dalleRoutes);
app.get('/', async(req, res)=>{
res.send('Hello from CreateAI');
})
const startServer = async () =>{
try{
connectDB(process.env.MONGODB_URL);
app.listen(8080, () => console.log('Server has started on port http://localhost:8080'))
}catch(error){
console.log(error);
}
}
startServer();
这是 postRoutes.js 页面
import express from 'express';
import * as dotenv from 'dotenv';
import {v2 as cloudinary} from 'cloudinary';
import Post from '../mongodb/models/post.js';
dotenv.config();
const router = express.Router();
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY // This is also the default, can be omitted
});
我也有同样的错误。我假设您按照 JSM 教程来创建应用程序。经过大量搜索,我终于在 5 天前在 OpenAI 论坛上找到了类似的讨论,看来是版本变化——配置 api key 在 v4 中得到了简化。
如果您有兴趣,请关注此处的论坛:https://community.openai.com/t/getting-an-error-when-importing-configuration-and-openaiapi-from-openai/325012
以下是从 v3 迁移到 v4 的 Github 指南: https://github.com/openai /openai-node/discussions/217
简而言之,只需运行npm exec openai migrate
,它就会自动迁移并将代码库中的代码更改为最新版本,并且应该修复此版本问题。
import { OpenAI } from "openai";
const openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY});
创建 .env 文件并粘贴从 https://platform.openai.com/account/api-keys
如果你想生成图像,
const response = openai.images.generate();
从“openai”导入OpenAI;