quota/tokenCount: allow arbitrary multipliers for tokens
I didn't notice that OpenAI changed pricing again
This commit is contained in:
parent
552143e345
commit
80f4f18eab
1 changed files with 12 additions and 28 deletions
|
@ -12,16 +12,19 @@ import { Usage } from "@prisma/client";
|
|||
export default class tokenCount implements IQuota {
|
||||
defaultQuota: number;
|
||||
lookback: number;
|
||||
considerInputTokensAsHalf: boolean;
|
||||
requestTokenMultiplier: number;
|
||||
responseTokenMultiplier: number;
|
||||
|
||||
constructor(
|
||||
defaultQuota: number = 512 * 25,
|
||||
lookback: number = 1000 * 60 * 60 * 24,
|
||||
considerInputTokensAsHalf: boolean = true,
|
||||
requestTokenMultiplier: number = 1,
|
||||
responseTokenMultiplier: number = 1,
|
||||
) {
|
||||
this.defaultQuota = defaultQuota;
|
||||
this.lookback = lookback;
|
||||
this.considerInputTokensAsHalf = considerInputTokensAsHalf;
|
||||
this.requestTokenMultiplier = requestTokenMultiplier;
|
||||
this.responseTokenMultiplier = responseTokenMultiplier;
|
||||
}
|
||||
|
||||
private getUserQuota(id: string) {
|
||||
|
@ -55,9 +58,7 @@ export default class tokenCount implements IQuota {
|
|||
const usageResponse = usedTokens.usageResponse === null ? 0 : usedTokens.usageResponse;
|
||||
|
||||
const usedUnits = (() => {
|
||||
if (this.considerInputTokensAsHalf)
|
||||
return usageResponse + usageRequest / 2;
|
||||
return usageResponse + usageRequest;
|
||||
return usageRequest * this.requestTokenMultiplier + usageResponse * this.responseTokenMultiplier;
|
||||
})();
|
||||
|
||||
if (userQuota?.vip) return this.createUserQuotaData(Infinity, usedUnits);
|
||||
|
@ -74,30 +75,13 @@ export default class tokenCount implements IQuota {
|
|||
* @returns promise of giving out the record
|
||||
*/
|
||||
findNthUsage(user: string, requestTimestamp: number, unitCount: number) {
|
||||
if (this.considerInputTokensAsHalf)
|
||||
return database.$queryRaw<Array<Usage & {usage: number}>>`
|
||||
return database.$queryRaw<Array<Usage & {usage: number | bigint}>>`
|
||||
SELECT t1.*, (
|
||||
SELECT
|
||||
SUM(usageResponse + usageRequest/2) AS usage
|
||||
FROM \`usage\`
|
||||
WHERE
|
||||
user = ${user} AND
|
||||
timestamp >= ${requestTimestamp - this.lookback} AND
|
||||
timestamp <= t1.timestamp
|
||||
) as usage
|
||||
FROM
|
||||
\`usage\` AS t1
|
||||
WHERE
|
||||
user = ${user} AND
|
||||
timestamp >= ${requestTimestamp - this.lookback} AND
|
||||
usage >= ${unitCount}
|
||||
ORDER BY timestamp ASC
|
||||
LIMIT 1
|
||||
`;
|
||||
return database.$queryRaw<Array<Usage & {usage: bigint}>>`
|
||||
SELECT t1.*, (
|
||||
SELECT
|
||||
SUM(usageResponse + usageRequest) AS usage
|
||||
SUM(
|
||||
usageRequest * ${this.requestTokenMultiplier} +
|
||||
usageResponse * ${this.responseTokenMultiplier}
|
||||
) AS usage
|
||||
FROM \`usage\`
|
||||
WHERE
|
||||
user = ${user} AND
|
||||
|
|
Loading…
Reference in a new issue