-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (60 loc) · 1.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const { Configuration, OpenAIApi } = require("openai");
const express = require('express');
const dotenv = require('dotenv');
dotenv.config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const app = express();
app.use(express.static('public'));
app.use(express.json());
app.post('/completion', async (req, res) => {
// get manipulated text from openai chat
let completion = null;
const prompt = req.body.prompt;
let text, images;
try {
const model = 'text-davinci-003';
const max_tokens = 150;
const temperature = 0.9;
const top_p = 1;
const frequency_penalty = 0;
const presence_penalty = 0.6;
const stop = [" Human:", " AI:"];
completion = await openai.createCompletion({
prompt,
model,
max_tokens,
temperature,
top_p,
frequency_penalty,
presence_penalty,
stop });
text = completion.data.choices[0].text;
} catch (error) {
console.error(error);
res.status(500).send({ error: 'OpenAI createCompletion error' });
}
try {
const response = await openai.createImage({
prompt,
n: 4,
size: "512x512",
});
images = response.data.data.reduce((a,c) => [...a,c.url], []);
} catch (error) {
console.error(error);
res.status(500).send({ error: 'OpenAI createImage error' });
}
if ( text && images ) {
res.send({text, images});
} else if (text && !images) {
res.send({text});
} else if (images && !text) {
res.send({images});
}
});
app.listen(3000, () => {
console.log('Listening on port 3000');
});