Spaces:
Paused
Paused
djmuted
commited on
Commit
•
3e7e8b4
1
Parent(s):
a6f6678
Add stats
Browse files- src/index.js +9 -1
- src/openai.js +7 -2
- src/utils.js +6 -1
src/index.js
CHANGED
@@ -3,13 +3,21 @@ require('dotenv').config();
|
|
3 |
const express = require('express');
|
4 |
const bearerToken = require('express-bearer-token');
|
5 |
const openai = require('./openai');
|
|
|
|
|
6 |
|
7 |
const app = express();
|
8 |
const port = 7860;
|
|
|
9 |
|
10 |
app.get('/', (req, res) => {
|
11 |
res.json({
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
});
|
14 |
})
|
15 |
|
|
|
3 |
const express = require('express');
|
4 |
const bearerToken = require('express-bearer-token');
|
5 |
const openai = require('./openai');
|
6 |
+
const { stats } = require('./utils');
|
7 |
+
const config = require('./config.json');
|
8 |
|
9 |
const app = express();
|
10 |
const port = 7860;
|
11 |
+
const started = new Date();
|
12 |
|
13 |
app.get('/', (req, res) => {
|
14 |
res.json({
|
15 |
+
uptime: (new Date() - started) / 1000,
|
16 |
+
slacks: config.slacks.length || 0,
|
17 |
+
prompts: stats.prompts.length || 0,
|
18 |
+
avgTime: stats.prompts.reduce((acc, curr) => acc + curr.time, 0) / stats.prompts.length || 0,
|
19 |
+
avgInputLength: stats.prompts.reduce((acc, curr) => acc + curr.inputLength, 0) / stats.prompts.length || 0,
|
20 |
+
avgOutputLength: stats.prompts.reduce((acc, curr) => acc + curr.outputLength, 0) / stats.prompts.length || 0,
|
21 |
});
|
22 |
})
|
23 |
|
src/openai.js
CHANGED
@@ -3,7 +3,7 @@ const bodyParser = require('body-parser');
|
|
3 |
const config = require('./config.json');
|
4 |
const slack = require('./slack');
|
5 |
const yup = require('yup');
|
6 |
-
const { splitJsonArray, dataToResponse, buildPrompt, wait } = require("./utils");
|
7 |
const { Queue } = require('async-await-queue');
|
8 |
|
9 |
const messageArraySchema = yup.array().of(
|
@@ -61,7 +61,8 @@ openaiRouter.post("/chat/completions", jsonParser, async (req, res) => {
|
|
61 |
});
|
62 |
}
|
63 |
|
64 |
-
const
|
|
|
65 |
let completionTokens = 0;
|
66 |
|
67 |
let lastContent = '';
|
@@ -83,9 +84,13 @@ openaiRouter.post("/chat/completions", jsonParser, async (req, res) => {
|
|
83 |
}
|
84 |
slackConfig.locked = true;
|
85 |
try {
|
|
|
86 |
await slack.sendChatReset(slackConfig);
|
87 |
await wait(500);
|
88 |
const response = await slack.waitForWebSocketResponse(slackConfig, messagesSplit, onData);
|
|
|
|
|
|
|
89 |
slackConfig.locked = false;
|
90 |
return response;
|
91 |
} catch (error) {
|
|
|
3 |
const config = require('./config.json');
|
4 |
const slack = require('./slack');
|
5 |
const yup = require('yup');
|
6 |
+
const { splitJsonArray, dataToResponse, buildPrompt, wait, stats } = require("./utils");
|
7 |
const { Queue } = require('async-await-queue');
|
8 |
|
9 |
const messageArraySchema = yup.array().of(
|
|
|
61 |
});
|
62 |
}
|
63 |
|
64 |
+
const inputPrompt = buildPrompt(messages);
|
65 |
+
const promptTokens = Math.ceil(inputPrompt.length / 4);
|
66 |
let completionTokens = 0;
|
67 |
|
68 |
let lastContent = '';
|
|
|
84 |
}
|
85 |
slackConfig.locked = true;
|
86 |
try {
|
87 |
+
const start = new Date();
|
88 |
await slack.sendChatReset(slackConfig);
|
89 |
await wait(500);
|
90 |
const response = await slack.waitForWebSocketResponse(slackConfig, messagesSplit, onData);
|
91 |
+
const end = new Date();
|
92 |
+
const time = end - start;
|
93 |
+
stats.prompts.push({ time, inputLength: inputPrompt.length, outputLength: response.length });
|
94 |
slackConfig.locked = false;
|
95 |
return response;
|
96 |
} catch (error) {
|
src/utils.js
CHANGED
@@ -171,6 +171,10 @@ const dataToResponse = (
|
|
171 |
};
|
172 |
};
|
173 |
|
|
|
|
|
|
|
|
|
174 |
module.exports = {
|
175 |
buildPrompt,
|
176 |
readBody,
|
@@ -181,5 +185,6 @@ module.exports = {
|
|
181 |
createBaseForm,
|
182 |
splitJsonArray,
|
183 |
wait,
|
184 |
-
dataToResponse
|
|
|
185 |
};
|
|
|
171 |
};
|
172 |
};
|
173 |
|
174 |
+
const stats = {
|
175 |
+
prompts: []
|
176 |
+
}
|
177 |
+
|
178 |
module.exports = {
|
179 |
buildPrompt,
|
180 |
readBody,
|
|
|
185 |
createBaseForm,
|
186 |
splitJsonArray,
|
187 |
wait,
|
188 |
+
dataToResponse,
|
189 |
+
stats,
|
190 |
};
|