|
| 1 | +const Filter = require('bad-words'); |
| 2 | +const axios = require('axios'); |
| 3 | +const express = require('express'); |
| 4 | +const router = express.Router(); |
| 5 | + |
| 6 | +const apiKey = process.env.OPENAI_API_KEY; |
| 7 | +const client = axios.create({ |
| 8 | + headers: { 'Authorization': 'Bearer ' + apiKey } |
| 9 | +}); |
| 10 | + |
| 11 | +const documents = [ |
| 12 | + "I am a day older than I was yesterday.<|endoftext|>", |
| 13 | + "I build applications that use GPT-3.<|endoftext|>", |
| 14 | + "My preferred programming is Python.<|endoftext|>" |
| 15 | +] |
| 16 | + |
| 17 | +const endpoint = 'https://api.openai.com/v1/answers'; |
| 18 | + |
| 19 | +router.post('/', (req, res) => { |
| 20 | + if (req.rateLimit.remaining == 0) { |
| 21 | + res.send({ "answer": "Ask me again in a minute." }); |
| 22 | + return; |
| 23 | + }; |
| 24 | + |
| 25 | + if (req.body.question.length > 150) { |
| 26 | + res.send({ "answer": "Sorry. That question is too long." }); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + let filter = new Filter(); |
| 31 | + if (filter.isProfane(req.body.question)) { |
| 32 | + res.send({ "answer": "That’s not a question we can answer." }); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const data = { |
| 37 | + "file": process.env.ANSWERS_FILE, |
| 38 | + "question": req.body.question, |
| 39 | + "search_model": "ada", |
| 40 | + "model": "curie", |
| 41 | + "examples_context": "My favorite programming language is Python.", |
| 42 | + "examples": [["How old are you?", "I'm a day older than I was yesterday."], ["What languages do you know?", "I speak English and write code in Python."]], |
| 43 | + "max_tokens": 15, |
| 44 | + "temperature": 0, |
| 45 | + "return_prompt": false, |
| 46 | + "expand": ["completion"], |
| 47 | + "stop": ["\n", "<|endoftext|>"], |
| 48 | + } |
| 49 | + client.post(endpoint, data) |
| 50 | + .then(result => { |
| 51 | + res.send({ "answer": result.data.answers[0] }) |
| 52 | + }).catch(result => { |
| 53 | + res.send({ "answer": "Sorry, I don’t have an answer." }) |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +module.exports = router; |
| 58 | + |
0 commit comments