-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (41 loc) · 1.14 KB
/
server.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
//********* EXPRESS SERVER */
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
const app = express();
app.use(bodyParser.json());
app.use(cors());
const port = 3080;
import dotenv from 'dotenv';
dotenv.config();
import { Configuration, OpenAIApi } from 'openai';
const configuration = new Configuration({
organization: process.env.OPENAI_ORG_KEY,
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
app.post('/prompt', async (req, res) => {
const { message } = req.body;
// console.log(message);
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt: `${message}`,
max_tokens: 100,
temperature: 0.5,
});
// console.log(response.data.choices[0].text);
res.json({ message: response.data.choices[0].text });
});
app.get('/', async (req, res) => {
res.send('hello world!');
});
app.get('/models', async (req, res) => {
const response = await openai.listEngines();
console.log(response);
res.json({
models: response.data,
});
});
app.listen(port, () => {
console.log(`server listening on port ${port}`);
});