This commit is contained in:
sijinhui 2024-04-03 00:08:35 +08:00
parent ec48c7ff19
commit 66c5852dbf
2 changed files with 39 additions and 18 deletions

View File

@ -5,6 +5,8 @@ import { collectModelTable } from "../utils/model";
import { makeAzurePath } from "../azure"; import { makeAzurePath } from "../azure";
import { getIP } from "@/app/api/auth"; import { getIP } from "@/app/api/auth";
import { getSessionName } from "@/lib/auth"; import { getSessionName } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { getTokenLength } from "@/lib/utils";
const serverConfig = getServerSideConfig(); const serverConfig = getServerSideConfig();
@ -169,15 +171,42 @@ export async function requestLog(
userName: name, userName: name,
userID: session?.user?.id, userID: session?.user?.id,
}; };
saveLogs(logData);
await fetch(`${baseUrl}/api/logs/openai`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(logData),
});
} catch (e) { } catch (e) {
console.log("[LOG]", e, "=========="); console.log("[LOG]", e, "==========");
} }
} }
export async function saveLogs(logData: {
ip?: string;
path?: string;
logEntry?: string;
model?: string;
userName?: string;
userID?: string;
logToken?: number;
}) {
try {
if (logData?.logEntry) {
const regex_message = /(?<="content":")(.*?)(?="}[,\]])/g;
const matchAllMessage = logData.logEntry.match(regex_message);
// console.log(matchAllMessage, "=====");
if (matchAllMessage && matchAllMessage.length > 0) {
logData.logToken =
getTokenLength(matchAllMessage.join(" ")) +
matchAllMessage.length * 3;
}
delete logData?.logEntry;
}
if (logData?.model == "midjourney") {
logData.logToken = 1000;
}
} catch (e) {
console.log("[LOG]", "logToken", e);
logData.logToken = 0;
}
const result = await prisma.logEntry.create({
data: logData,
});
// console.log("result", result)
}

View File

@ -9,14 +9,6 @@ async function handle(
) { ) {
try { try {
const request_data = await req.json(); const request_data = await req.json();
// console.log("log", request_data);
// if (request_data?.userName) {
// await insertUser({
// name: request_data?.userName,
// email: request_data?.userName,
// });
// }
// console.log("===========4", request_data);
try { try {
if (request_data?.logEntry) { if (request_data?.logEntry) {
const regex_message = /(?<="content":")(.*?)(?="}[,\]])/g; const regex_message = /(?<="content":")(.*?)(?="}[,\]])/g;
@ -47,12 +39,12 @@ async function handle(
} catch (e) { } catch (e) {
console.log("[LOG]", e); console.log("[LOG]", e);
return NextResponse.json({ status: 0 }); return NextResponse.json({ status: e?.toString() }, { status: 400 });
} }
return NextResponse.json({ status: 1 }); return NextResponse.json({ status: 1 });
} }
export const GET = handle; // export const GET = handle;
export const POST = handle; export const POST = handle;
// export const runtime = "edge"; // export const runtime = "edge";