Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rickbarraza committed Apr 19, 2023
0 parents commit ec8aede
Show file tree
Hide file tree
Showing 6 changed files with 749 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
43 changes: 43 additions & 0 deletions index.js
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');
});
Loading

0 comments on commit ec8aede

Please sign in to comment.