djmuted commited on
Commit
fd1c59b
1 Parent(s): cbf3e84

Queue requests

Browse files
Files changed (4) hide show
  1. package.json +1 -0
  2. src/openai.js +5 -1
  3. src/utils.js +2 -0
  4. yarn.lock +5 -0
package.json CHANGED
@@ -8,6 +8,7 @@
8
  "main": "index.js",
9
  "license": "MIT",
10
  "dependencies": {
 
11
  "body-parser": "^1.20.2",
12
  "dotenv": "^16.0.3",
13
  "express": "^4.18.2",
 
8
  "main": "index.js",
9
  "license": "MIT",
10
  "dependencies": {
11
+ "async-await-queue": "^2.1.4",
12
  "body-parser": "^1.20.2",
13
  "dotenv": "^16.0.3",
14
  "express": "^4.18.2",
src/openai.js CHANGED
@@ -4,6 +4,7 @@ const config = require('./config');
4
  const slack = require('./slack');
5
  const yup = require('yup');
6
  const { splitJsonArray } = require("./utils");
 
7
 
8
  const messageArraySchema = yup.array().of(
9
  yup.object().shape({
@@ -28,6 +29,9 @@ openaiRouter.get("/models", (req, res) => {
28
  ]);
29
  });
30
 
 
 
 
31
  openaiRouter.post("/chat/completions", jsonParser, async (req, res) => {
32
  try {
33
  if (req.token !== config.API_KEY) {
@@ -46,7 +50,7 @@ openaiRouter.post("/chat/completions", jsonParser, async (req, res) => {
46
 
47
  const messagesSplit = splitJsonArray(messages, 12000);
48
 
49
- const result = await slack.waitForWebSocketResponse(messagesSplit);
50
 
51
  res.json({
52
  id, created,
 
4
  const slack = require('./slack');
5
  const yup = require('yup');
6
  const { splitJsonArray } = require("./utils");
7
+ const { Queue } = require('async-await-queue');
8
 
9
  const messageArraySchema = yup.array().of(
10
  yup.object().shape({
 
29
  ]);
30
  });
31
 
32
+ const parallelQueries = 1;
33
+ const myq = new Queue(parallelQueries, 100);
34
+
35
  openaiRouter.post("/chat/completions", jsonParser, async (req, res) => {
36
  try {
37
  if (req.token !== config.API_KEY) {
 
50
 
51
  const messagesSplit = splitJsonArray(messages, 12000);
52
 
53
+ const result = await myq.run(() => slack.waitForWebSocketResponse(messagesSplit));
54
 
55
  res.json({
56
  id, created,
src/utils.js CHANGED
@@ -12,6 +12,8 @@ const wait = (duration) => {
12
 
13
  function buildPrompt(messages) {
14
  prompt = "\n\n" + preparePrompt(messages);
 
 
15
  const escapedPrompt = prompt.replace(/\r?\n|\r/g, '\\n').replace(/"/g, '\\"');
16
  return escapedPrompt;
17
  };
 
12
 
13
  function buildPrompt(messages) {
14
  prompt = "\n\n" + preparePrompt(messages);
15
+ return prompt;
16
+ //do not escape for now
17
  const escapedPrompt = prompt.replace(/\r?\n|\r/g, '\\n').replace(/"/g, '\\"');
18
  return escapedPrompt;
19
  };
yarn.lock CHANGED
@@ -15,6 +15,11 @@ array-flatten@1.1.1:
15
  resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
16
  integrity sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==
17
 
 
 
 
 
 
18
  asynckit@^0.4.0:
19
  version "0.4.0"
20
  resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
 
15
  resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
16
  integrity sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==
17
 
18
+ async-await-queue@^2.1.4:
19
+ version "2.1.4"
20
+ resolved "https://registry.yarnpkg.com/async-await-queue/-/async-await-queue-2.1.4.tgz#0c44a405cd31369b561f6ac663b74b4b4ffb99ad"
21
+ integrity sha512-3DpDtxkKO0O/FPlWbk/CrbexjuSxWm1CH1bXlVNVyMBIkKHhT5D85gzHmGJokG3ibNGWQ7pHBmStxUW/z/0LYQ==
22
+
23
  asynckit@^0.4.0:
24
  version "0.4.0"
25
  resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"