Skip to content

Instantly share code, notes, and snippets.

@andycds
Created December 11, 2023 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andycds/86ed7f35d302ec566a92eeac52527820 to your computer and use it in GitHub Desktop.
Save andycds/86ed7f35d302ec566a92eeac52527820 to your computer and use it in GitHub Desktop.
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