Created
September 2, 2020 17:00
-
-
Save schnerd/7ac90b01cb8f957b5d54d9eb216fd35d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser') | |
const fetch = require('node-fetch'); | |
const app = express() | |
const port = 3008 | |
app.use(bodyParser.json()); | |
app.post('/api/generate', (req, res) => { | |
var PROMPT = req.body.subject; | |
var raw = JSON.stringify({"prompt": PROMPT, "max_tokens": 5 }); | |
var requestOptions = { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` | |
}, | |
body: raw, | |
redirect: 'follow' | |
}; | |
fetch("https://api.openai.com/v1/engines/davinci/completions", requestOptions) | |
.then(response => response.json()) | |
.then(json => { | |
res.status(200).json(json); | |
}) | |
.catch(error => { | |
console.error(error); | |
res.status(500).send('An error occurred'); | |
}) | |
}); | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment