mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-09-26 01:23:18 +08:00
chore: fix model cost
This commit is contained in:
@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
|
|||||||
import { getServerSession } from "next-auth/next";
|
import { getServerSession } from "next-auth/next";
|
||||||
import { authOptions } from "./auth/[...nextauth]";
|
import { authOptions } from "./auth/[...nextauth]";
|
||||||
import { getSubscriptionByEmail } from "./utils/subscription";
|
import { getSubscriptionByEmail } from "./utils/subscription";
|
||||||
|
import { getModel } from "@/utils";
|
||||||
import { addUsage, getCurrentMonthUsage } from "./utils/usage";
|
import { addUsage, getCurrentMonthUsage } from "./utils/usage";
|
||||||
import { getEndUser } from "./auth/end-user";
|
import { getEndUser } from "./auth/end-user";
|
||||||
import { Quota } from "@/types";
|
import { Quota } from "@/types";
|
||||||
@ -24,6 +25,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
if (req.method === "GET") {
|
if (req.method === "GET") {
|
||||||
usage = await getCurrentMonthUsage(endUser);
|
usage = await getCurrentMonthUsage(endUser);
|
||||||
} else if (req.method === "POST") {
|
} else if (req.method === "POST") {
|
||||||
|
const model = getModel((req.headers["x-openai-model"] as string) || "");
|
||||||
usage = await addUsage(endUser);
|
usage = await addUsage(endUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.
|
// 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 now = new Date();
|
||||||
const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
||||||
const usage = await prisma.usage.findFirst({
|
const usage = await prisma.usage.findFirst({
|
||||||
@ -35,7 +35,7 @@ export const addUsage = async (endUser: string): Promise<number> => {
|
|||||||
|
|
||||||
let newUsage = 0;
|
let newUsage = 0;
|
||||||
if (usage) {
|
if (usage) {
|
||||||
newUsage = usage.count + 1;
|
newUsage = usage.count + addition;
|
||||||
await prisma.usage.update({
|
await prisma.usage.update({
|
||||||
where: {
|
where: {
|
||||||
id: usage.id,
|
id: usage.id,
|
||||||
@ -45,12 +45,12 @@ export const addUsage = async (endUser: string): Promise<number> => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
newUsage = 1;
|
newUsage = addition;
|
||||||
await prisma.usage.create({
|
await prisma.usage.create({
|
||||||
data: {
|
data: {
|
||||||
endUser: endUser,
|
endUser: endUser,
|
||||||
createdAt: today,
|
createdAt: today,
|
||||||
count: 1,
|
count: newUsage,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ const gpt35 = {
|
|||||||
frequency_penalty: 0.0,
|
frequency_penalty: 0.0,
|
||||||
presence_penalty: 0.0,
|
presence_penalty: 0.0,
|
||||||
max_token: 4000,
|
max_token: 4000,
|
||||||
|
cost_per_call: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
const gpt4 = {
|
const gpt4 = {
|
||||||
@ -12,6 +13,7 @@ const gpt4 = {
|
|||||||
frequency_penalty: 0.0,
|
frequency_penalty: 0.0,
|
||||||
presence_penalty: 0.0,
|
presence_penalty: 0.0,
|
||||||
max_token: 8000,
|
max_token: 8000,
|
||||||
|
cost_per_call: 10,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const models = [gpt35, gpt4];
|
export const models = [gpt35, gpt4];
|
||||||
|
Reference in New Issue
Block a user