|
| 1 | +const express = require('express'); |
| 2 | +const axios = require('axios'); |
| 3 | +const bodyParser = require('body-parser'); |
| 4 | + |
| 5 | +const app = express(); |
| 6 | +app.use(bodyParser.text()); |
| 7 | + |
| 8 | +app.get('/', (req, res) => { |
| 9 | + res.json({status: 'ok'}); |
| 10 | +}); |
| 11 | + |
| 12 | +app.post('/prompt', async (req, res) => { |
| 13 | + const promptText = req.body; |
| 14 | + |
| 15 | + const messages = [ |
| 16 | + // {role: 'system', content: 'You are an experienced software engineer.'}, |
| 17 | + {role: 'user', content: promptText}, |
| 18 | + ]; |
| 19 | + |
| 20 | + const apiKey = process.env.OPENAI_KEY; |
| 21 | + const url = 'https://api.openai.com/v1/chat/completions'; |
| 22 | + |
| 23 | + const postBody = { |
| 24 | + model: 'gpt-3.5-turbo', |
| 25 | + messages: messages, |
| 26 | + }; |
| 27 | + |
| 28 | + const headers = { |
| 29 | + 'Content-Type': 'application/json', |
| 30 | + 'Authorization': `Bearer ${apiKey}` |
| 31 | + }; |
| 32 | + |
| 33 | + try { |
| 34 | + const response = await axios.post(url, postBody, {headers: headers}); |
| 35 | + res.json({status: 'ok', response: response.data}); |
| 36 | + } catch (error) { |
| 37 | + console.error(error); |
| 38 | + res.status(500).json({status: 'error', message: error.message}); |
| 39 | + } |
| 40 | +}); |
| 41 | + |
| 42 | +app.listen(3000, () => { |
| 43 | + console.log('Server running on port 3000'); |
| 44 | +}); |
0 commit comments