Created
December 11, 2023 20:31
-
-
Save andycds/86ed7f35d302ec566a92eeac52527820 to your computer and use it in GitHub Desktop.
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
require('dotenv').config(); | |
const API_KEY = process.env.API_KEY; | |
const { OpenAI } = require('openai'); | |
const openai = new OpenAI(API_KEY); | |
const express = require('express'); | |
const cors = require('cors'); | |
const app = express(); | |
app.use(express.json()); | |
app.use(cors()); | |
app.post('/gpt', async (req, res) => { | |
const { prompt } = req.body; | |
const model = 'gpt-3.5-turbo'; | |
const role = 'user'; | |
const max_tokens = 50; | |
const completion = await openai.chat.completions.create({ | |
messages: [{ role: role, content: prompt }], | |
model: model, | |
max_tokens: max_tokens | |
}); | |
res.json({ completion: completion.choices[0].message.content }); | |
}); | |
console.log(API_KEY) | |
app.listen(4000, () => console.log('ChatGPT_Backend em execução na porta 4000')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment