Skip to content

Migrated code in server.js to work with OpenAI API v4 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions dream/server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as dotenv from 'dotenv';
dotenv.config();

import { Configuration, OpenAIApi } from 'openai';
import OpenAI from 'openai';

const configuration = new Configuration({
const openai = new OpenAI({
apiKey: process.env.OPENAI,
});

const openai = new OpenAIApi(configuration);

import express from 'express';
import cors from 'cors';

Expand All @@ -20,18 +18,28 @@ app.post('/dream', async (req, res) => {
try {
const prompt = req.body.prompt;

const aiResponse = await openai.createImage({
const aiResponse = await openai.images.generate({
prompt,
n: 1,
size: '1024x1024',
});
console.log(aiResponse.data);
const image = aiResponse.data[0].url;
console.log(image);

const image = aiResponse.data.data[0].url;
res.send({ image });
res.send({ image }); // Sends the response to the client
} catch (error) {
console.error(error)
res.status(500).send(error?.response.data.error.message || 'Something went wrong');
if (error instanceof OpenAI.APIError) {
console.error(error.status); // e.g. 401
console.error(error.message); // e.g. The authentication token you passed was invalid...
console.error(error.code); // e.g. 'invalid_api_key'
console.error(error.type); // e.g. 'invalid_request_error'
} else {
console.error(error)
res.status(500).send(error?.response.data.error.message || 'Something went wrong');
}
}
});


app.listen(8080, () => console.log('make art on http://localhost:8080/dream'));