const express = require('express');
const { OpenAIApi } = require('openai');
const openaiApiKey = '■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■yZFgCb0c';
const openai = new OpenAIApi({ api_key: openaiApiKey });
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3002;
app.use(express.json());
app.use(cors({ origin: '*' }));
app.post('/api/modify-code', async (req, res) => {
const { command } = req.body;
// Create a series of messages for the chat-based API call
const messages = [
{ role: "user", content: "You are assisting in generating React code. Your response should be the React code equivalent to the given command." },
{ role: "user", content: `create a ${command}` }
];
try {
const openaiResponse = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: messages
});
const generatedCode = openaiResponse.data.choices[0].text;
res.json({ code: generatedCode });
} catch (error) {
console.error(error); // Log the entire error object
console.error('Error processing command with OpenAI:', error.message);
res.json({ code: '' });
it seems you are using v4.0 of OpenAI Node.JS library.
You need to just replace await openai.createChatCompletion
with await openai.chat.completions.create
Reference:
2 Likes
It’s pretty good at pointing you in the direct direction.
1 Like