generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ec8aede
Showing
6 changed files
with
749 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const app = express(); | ||
const { Configuration, OpenAIApi } = require("openai"); | ||
|
||
const configuration = new Configuration({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
const openai = new OpenAIApi(configuration); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(express.static('public')); | ||
|
||
const messages = []; | ||
|
||
function addMessage(role, content) { | ||
messages.push({ role: role, content: content }); | ||
} | ||
|
||
async function sendToOpenAI() { | ||
const completion = await openai.createChatCompletion({ | ||
model: "gpt-3.5-turbo", | ||
messages: messages, | ||
}).catch((err) => { | ||
console.log("error received: " + err); | ||
}); | ||
reply = completion.data.choices[0].message.content; | ||
return reply; | ||
} | ||
|
||
app.post('/api/gpt', async (req, res) => { | ||
console.log("Received: " + req.body.prompt); | ||
addMessage('user', req.body.prompt); | ||
reply = await sendToOpenAI(); | ||
console.log("Reply: " + reply); | ||
addMessage("assistant", reply); | ||
res.send({ message: reply }); | ||
}); | ||
|
||
|
||
app.listen(3000, () => { | ||
console.log('Server listening on port 3000'); | ||
}); |
Oops, something went wrong.