chore: fix model cost

This commit is contained in:
Tianzhou Chen
2023-12-17 02:57:47 +08:00
parent 92b567104f
commit 319c12a4c6
3 changed files with 8 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "./auth/[...nextauth]";
import { getSubscriptionByEmail } from "./utils/subscription";
import { getModel } from "@/utils";
import { addUsage, getCurrentMonthUsage } from "./utils/usage";
import { getEndUser } from "./auth/end-user";
import { Quota } from "@/types";
@ -24,6 +25,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "GET") {
usage = await getCurrentMonthUsage(endUser);
} else if (req.method === "POST") {
const model = getModel((req.headers["x-openai-model"] as string) || "");
usage = await addUsage(endUser);
}

View File

@ -23,7 +23,7 @@ export const getCurrentMonthUsage = async (endUser: string): Promise<number> =>
};
// We coerce individual usage to the begining of the day to reduce the usage records.
export const addUsage = async (endUser: string): Promise<number> => {
export const addUsage = async (endUser: string, addition: number): Promise<number> => {
const now = new Date();
const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
const usage = await prisma.usage.findFirst({
@ -35,7 +35,7 @@ export const addUsage = async (endUser: string): Promise<number> => {
let newUsage = 0;
if (usage) {
newUsage = usage.count + 1;
newUsage = usage.count + addition;
await prisma.usage.update({
where: {
id: usage.id,
@ -45,12 +45,12 @@ export const addUsage = async (endUser: string): Promise<number> => {
},
});
} else {
newUsage = 1;
newUsage = addition;
await prisma.usage.create({
data: {
endUser: endUser,
createdAt: today,
count: 1,
count: newUsage,
},
});
}

View File

@ -4,6 +4,7 @@ const gpt35 = {
frequency_penalty: 0.0,
presence_penalty: 0.0,
max_token: 4000,
cost_per_call: 1,
};
const gpt4 = {
@ -12,6 +13,7 @@ const gpt4 = {
frequency_penalty: 0.0,
presence_penalty: 0.0,
max_token: 8000,
cost_per_call: 10,
};
export const models = [gpt35, gpt4];