Skip to content

Commit c429c3c

Browse files
committed
initial commit
0 parents  commit c429c3c

11 files changed

+298
-0
lines changed

.replit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Run = "node ./bin/www"

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# GPT Answers
2+
3+
This is an example GPT-3 powered knowledge base application.
4+
5+
## Before you begin
6+
7+
You'll need two things to make this code work.
8+
9+
- An OpenAI API key
10+
- An account on replit.com
11+
12+
## Deploying the code
13+
14+
1. Click on the button below to copy this code to your replit.com account
15+
16+
[![Run on Replit.com](https://repl.it/badge/github/dabblelab/building-discord-bots-with-twilio-autopilot)](https://repl.it/github/dabblelab/building-discord-bots-with-twilio-autopilot)
17+
18+
![run on replit button goes here](./public/images/run-on-replit.svg)
19+
20+
2. Setup a secret / environment variable in replit.com for our OpenAI API key
21+
- In replit, click the padlock icon to add a new secret / environment variable
22+
- For the name use `OPENAI_API_KEY` and your OpenAI API key for the value
23+
24+
## Running the application
25+
26+
After your OpenAI API key is set as an environment variable, you can just click the `Run` button in the replit.com IDE.

answers.jsonl

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"text": "I am a day younger than I will be tomorrow"}
2+
{"text": "I like to code in Python."}
3+
{"text": "My favorite food is carrot cake."}

app.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var cookieParser = require('cookie-parser');
4+
var logger = require('morgan');
5+
6+
var indexRouter = require('./routes/index');
7+
var answerRouter = require('./routes/answer');
8+
9+
var app = express();
10+
11+
const rateLimit = require("express-rate-limit");
12+
const apiLimiter = rateLimit({
13+
windowMs: 1 * 60 * 1000,
14+
max: 6
15+
});
16+
app.use("/answer/", apiLimiter);
17+
18+
app.use(logger('dev'));
19+
app.use(express.json());
20+
app.use(express.urlencoded({ extended: false }));
21+
app.use(cookieParser());
22+
app.use(express.static(path.join(__dirname, 'public')));
23+
24+
app.use('/', indexRouter);
25+
app.use('/answer', answerRouter);
26+
27+
module.exports = app;

files-upload.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs');
2+
const axios = require('axios');
3+
const FormData = require('form-data');
4+
5+
const data = new FormData();
6+
data.append('purpose', 'answers');
7+
data.append('file', fs.createReadStream('answers.jsonl'));
8+
9+
const params = {
10+
method: 'post',
11+
url: 'https://api.openai.com/v1/files',
12+
headers: {
13+
'Authorization': 'Bearer ' + process.env.OPENAI_API_KEY,
14+
...data.getHeaders()
15+
},
16+
data: data
17+
}
18+
19+
axios(params)
20+
.then(function(response) {
21+
console.log(response.data);
22+
})
23+
.catch(function(error) {
24+
console.log(error.message);
25+
});
26+
27+

public/images/run-on-replit.svg

+49
Loading

public/index.html

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<html>
2+
3+
<head>
4+
<title>GPT Answers</title>
5+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/uikit.min.css">
6+
</head>
7+
8+
<body>
9+
10+
<div class="uk-section uk-section-large uk-height-viewport">
11+
<div class="uk-container uk-text-center uk-padding-large">
12+
<h1 class="uk-heading-medium"><strong>GPT</strong> Answers </h1>
13+
<p class="uk-text-lead">An Example Knowledge Base App Powered by GPT-3</p>
14+
</div>
15+
<div class="uk-container uk-text-center">
16+
<form class="uk-grid-collapse" uk-grid>
17+
<div class="uk-width-1-1 ">
18+
<input id="question" class="uk-input uk-width-1-3" type="text">
19+
<button type="submit" class="uk-button uk-button-default uk-width-1-5">Get Answer</button>
20+
</div>
21+
</form>
22+
</div>
23+
<div class="uk-container uk-text-center uk-padding">
24+
<div class="uk-inline">
25+
<div id="answer" class="uk-flex uk-flex-center uk-flex-middle uk-padding uk-width-expand"></div>
26+
</div>
27+
</div>
28+
</div>
29+
30+
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
31+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit.min.js"></script>
32+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit-icons.min.js"></script>
33+
<script src="/javascripts/script.js"></script>
34+
</body>
35+
36+
</html>
37+
38+

public/index_temp.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
3+
<head>
4+
<title>GPT Answers</title>
5+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/uikit.min.css">
6+
</head>
7+
8+
<body>
9+
10+
<div class="uk-section uk-section-large uk-height-viewport">
11+
<div class="uk-container uk-text-center uk-padding-large">
12+
<h1 class="uk-heading-medium"><strong>GPT</strong> Answers </h1>
13+
<p class="uk-text-lead">An Example Knowledge Base App Powered by GPT-3</p>
14+
<p>This will be a live version of the example GPT-3 application that is built in Chapter 9 and 10 of the book <a href="https://www.amazon.com/dp/1800563191">Exploring GPT-3</a></p>
15+
</div>
16+
</div>
17+
18+
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
19+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit.min.js"></script>
20+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/uikit-icons.min.js"></script>
21+
<script src="/javascripts/script.js"></script>
22+
</body>
23+
24+
</html>
25+
26+

public/javascripts/script.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const form = document.querySelector('form');
2+
const answer = document.querySelector('#answer');
3+
4+
const formEvent = form.addEventListener('submit', event => {
5+
event.preventDefault();
6+
const question = document.querySelector('#question');
7+
if (question.value) {
8+
askQuestion(question.value);
9+
} else {
10+
answer.innerHTML = "You need to enter a question to get an answer.";
11+
answer.classList.add("error");
12+
}
13+
});
14+
15+
const appendAnswer = (result) => {
16+
answer.innerHTML = `<p>${result.answer}</p>`;
17+
};
18+
19+
const askQuestion = (question) => {
20+
const params = {
21+
method: 'post',
22+
url: '/answer',
23+
headers: {
24+
'content-type': 'application/json'
25+
},
26+
data: { question }
27+
};
28+
axios(params)
29+
.then(response => {
30+
const answer = response.data;
31+
appendAnswer(answer);
32+
})
33+
.catch(error => console.error(error));
34+
};

routes/answer.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+

routes/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
4+
/* GET home page. */
5+
router.get('/', function(req, res, next) {
6+
res.render('index', { title: 'Express' });
7+
});
8+
9+
module.exports = router;

0 commit comments

Comments
 (0)