skip to Main Content
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)

I am getting this error in Node.js v18.14.2

This is my index.js code:

import express from 'express';
import * as dotenv from 'dotenv';
import cors from 'cors';

import dalleRoutes from './routes/dalle.routes.js';

dotenv.config();

const app = express();
app.use(cors());
app.use(express.json({ limig: "50mb" }))

app.use("/api/v1/dalle", dalleRoutes);

app.get('/', (req, res) => {
res.status(200).json({ message: "Hello from DALL.E" })
})

app.listen(8080, () => console.log('Server has started on port 8080'))

This is my dalle.route.js code:

import express from 'express';
import * as dotenv from 'dotenv';
import { Configuration, OpenAIApi} from 'openai';

dotenv.config();

const router = express.Router();

const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(config);

router.route('/').get((req, res) => {
res.status(200).json({ message: "Hello from DALL.E ROUTES" })
})

router.route('/').post(async (req, res) => {
try {
const { prompt } = req.body;

    const response = await openai.createImage({
      prompt,
      n: 1,
      size: '1024x1024',
      response_format: 'b64_json'
    });
    
    const image = response.data.data[0].b64_json;
    
    res.status(200).json({ photo: image });

} catch (error) {
console.error(error);
res.status(500).json({ message: "Something went wrong" })
}
})

export default router;

I was trying to generate image but I am not able to produce it.

2

Answers


  1. Chosen as BEST ANSWER

    Customizer.jsx:58 POST http://localhost:5173/api/v1/dalle 404 (Not Found) handleSubmit @ Customizer.jsx:58 handleClick @ AIPicker.jsx:34 Show 21 more frames after applying these code i am still getting error


  2. The method that you are using has been changed.
    Please follow official up to date OpenAI Chat Documentation and Image Generation Documentation.

    For Chat:

    import OpenAI from 'openai';
    
    const openai = new OpenAI({
         apiKey: process.env.OPENAI_API_KEY
    });
    
     const completion = await openai.chat.completions.create({
        messages: [{ role: "system", content: "You are a helpful assistant." }],
        model: "gpt-3.5-turbo",
      });
    
      console.log(completion.choices[0]);
    

    For Image Generation:

    import OpenAI from 'openai';
        
    const openai = new OpenAI({
             apiKey: process.env.OPENAI_API_KEY
        });
        
    
    const response = await openai.createImage({
      prompt: "a white siamese cat",
      n: 1,
      size: "1024x1024",
    });
    image_url = response.data.data[0].url; 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search