adds prompt counter

This commit is contained in:
nai-degen
2023-04-08 11:13:55 -05:00
committed by nai-degen
parent c74c527f46
commit 11308b2baa
3 changed files with 15 additions and 1 deletions
+1
View File
@@ -24,6 +24,7 @@ function getInfoPageHtml(host: string) {
active: keylist.filter((k) => !k.isDisabled).length,
trial: keylist.filter((k) => k.isTrial).length,
gpt4: keylist.filter((k) => k.isGpt4).length,
proompts: keylist.reduce((acc, k) => acc + k.promptCount, 0),
},
};
+11 -1
View File
@@ -26,6 +26,8 @@ export type Key = KeySchema & {
systemHardLimit?: number;
/** The current usage of this key. */
usage?: number;
/** The number of prompts that have been sent with this key. */
promptCount: number;
/** The time at which this key was last used. */
lastUsed: number;
/** Key hash for displaying usage in the dashboard. */
@@ -57,6 +59,7 @@ function init() {
systemHardLimit: 0,
usage: 0,
lastUsed: 0,
promptCount: 0,
hash: crypto
.createHash("sha256")
.update(key.key)
@@ -107,6 +110,7 @@ function get(model: string) {
const trialKeys = availableKeys.filter((key) => key.isTrial);
if (trialKeys.length > 0) {
logger.info({ key: trialKeys[0].hash }, "Using trial key");
trialKeys[0].lastUsed = Date.now();
return trialKeys[0];
}
@@ -117,4 +121,10 @@ function get(model: string) {
return { ...oldestKey };
}
export const keys = { init, list, get, anyAvailable, disable };
function incrementPrompt(keyHash?: string) {
if (!keyHash) return;
const key = keyPool.find((k) => k.hash === keyHash)!;
key.promptCount++;
}
export const keys = { init, list, get, anyAvailable, disable, incrementPrompt };
+3
View File
@@ -59,6 +59,9 @@ export const handleResponse = (
res.status(statusCode).json(errorPayload);
});
} else {
// Increment key's usage count
keys.incrementPrompt(req.key?.hash);
Object.keys(proxyRes.headers).forEach((key) => {
res.setHeader(key, proxyRes.headers[key] as string);
});