0b0fc94a by RSA

fix conflict

2 parents e5ab7e31 ec12f544
...@@ -266,3 +266,30 @@ ul.suggested-options { ...@@ -266,3 +266,30 @@ ul.suggested-options {
266 padding: 10px 10px 0 10px; 266 padding: 10px 10px 0 10px;
267 } 267 }
268 } 268 }
269
270 .message .error_msg {
271 background: rgb(18 0 255 / 20%);
272 padding: 10px;
273 border-radius: 5px;
274 display: flex;
275 }
276
277 .message .error_msg span {
278 background: #0023ff;
279 width: 25px;
280 height: 25px;
281 display: inline-block;
282 text-align: center;
283 padding-top: 1px;
284 border-radius: 50px;
285 font-family: auto;
286 font-weight: 600;
287 vertical-align: top;
288 }
289
290 .message .error_msg .msg {
291 display: inline-block;
292 margin: 0 10px;
293 font-style: italic;
294 width: 80%;
295 }
......
...@@ -46,7 +46,7 @@ function App() { ...@@ -46,7 +46,7 @@ function App() {
46 const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); 46 const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi');
47 const inputMatches = chatInput.match(userInputRegex); 47 const inputMatches = chatInput.match(userInputRegex);
48 48
49 const userPunctuation = ['\.', '?', '!', ':', ';', ',']; 49 const userPunctuation = ['.', '?', '!', ':', ';', ','];
50 const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`); 50 const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`);
51 const punctuationMatches = chatInput.match(userPunctuationRegex); 51 const punctuationMatches = chatInput.match(userPunctuationRegex);
52 52
......
...@@ -3,6 +3,7 @@ const express = require('express') ...@@ -3,6 +3,7 @@ const express = require('express')
3 const bodyParser = require('body-parser') 3 const bodyParser = require('body-parser')
4 const cors = require('cors') 4 const cors = require('cors')
5 require('dotenv').config() 5 require('dotenv').config()
6 const rateLimit = require('express-rate-limit')
6 7
7 // Open AI Configuration 8 // Open AI Configuration
8 // console.log(process.env.OPENAI_API_ORG) 9 // console.log(process.env.OPENAI_API_ORG)
...@@ -12,6 +13,12 @@ const configuration = new Configuration({ ...@@ -12,6 +13,12 @@ const configuration = new Configuration({
12 }); 13 });
13 const openai = new OpenAIApi(configuration); 14 const openai = new OpenAIApi(configuration);
14 15
16 const rateLimiter = rateLimit({
17 windowMs: 1000 * 60 * 1, // 1 minute (refreshTime)
18 max: 3000, // limit each IP to x requests per windowMs (refreshTime)
19 message: 'Sorry, too many requests. Please try again in a bit!',
20 });
21
15 // Express Configuration 22 // Express Configuration
16 const app = express() 23 const app = express()
17 const port = 3080 24 const port = 3080
...@@ -19,7 +26,7 @@ const port = 3080 ...@@ -19,7 +26,7 @@ const port = 3080
19 app.use(bodyParser.json()) 26 app.use(bodyParser.json())
20 app.use(cors()) 27 app.use(cors())
21 app.use(require('morgan')('dev')) 28 app.use(require('morgan')('dev'))
22 29 app.use(rateLimiter)
23 30
24 // Routing 31 // Routing
25 32
...@@ -34,19 +41,44 @@ app.post('/api', async (req, res) => { ...@@ -34,19 +41,44 @@ app.post('/api', async (req, res) => {
34 greetingPrompt = 'Hello, how can I help you today?' 41 greetingPrompt = 'Hello, how can I help you today?'
35 } 42 }
36 43
37 const prompt = `${greetingPrompt}\n${message}`; 44 let query_prompt = `${greetingPrompt}\n${message}`;
38 45
46 str_length = req.body.message.split(' ').length;
47
48 if (str_length>=800){
49 arr_body = req.body.message.split("\n");
50 if (arr_body.length>=4){
51 var i = arr_body.length-2
52 while (i--) {
53 arr_body.splice(i, 1);
54 }
55 query_prompt = arr_body.join("\n")
56 }
57 }
58
59 try {
39 const response = await openai.createCompletion({ 60 const response = await openai.createCompletion({
40 model: `${currentModel}`,// "text-davinci-003", 61 model: `${currentModel}`,// "text-davinci-003",
41 prompt, 62 prompt: query_prompt,
42 max_tokens: 2500, 63 max_tokens: 3000,
43 temperature, 64 temperature,
44 }); 65 });
45 66
46
47 res.json({ 67 res.json({
48 message: response.data.choices[0].text, 68 message: response.data.choices[0].text,
49 }) 69 })
70
71 } catch (e) {
72 // let error_msg = e.response.data.error.message ? e.response.data.error.message : '';
73 // if (error_msg.indexOf('maximum context length')>=0){
74 // console.log(error_msg);
75 // }else{
76 // console.log(e.response);
77 // }
78 console.log(e.response);
79 } finally {
80 // console.log('We do cleanup here');
81 }
50 }); 82 });
51 83
52 // Get Models Route 84 // Get Models Route
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
13 "cors": "^2.8.5", 13 "cors": "^2.8.5",
14 "dotenv": "^16.0.3", 14 "dotenv": "^16.0.3",
15 "express": "^4.18.2", 15 "express": "^4.18.2",
16 "express-rate-limit": "^6.7.0",
16 "morgan": "^1.10.0", 17 "morgan": "^1.10.0",
17 "openai": "^3.1.0" 18 "openai": "^3.1.0"
18 } 19 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!