chore: feature gate on collecting usage data

This commit is contained in:
Tianzhou Chen
2023-05-26 22:08:09 +08:00
parent a2ea7ce191
commit 1a7c9b70d0
4 changed files with 35 additions and 31 deletions

View File

@ -14,7 +14,7 @@ import {
useUserStore, useUserStore,
} from "@/store"; } from "@/store";
import { Conversation, CreatorRole, Message } from "@/types"; import { Conversation, CreatorRole, Message } from "@/types";
import { countTextTokens, generateUUID } from "@/utils"; import { countTextTokens, generateUUID, hasFeature } from "@/utils";
import getEventEmitter from "@/utils/event-emitter"; import getEventEmitter from "@/utils/event-emitter";
import Header from "./Header"; import Header from "./Header";
import EmptyView from "../EmptyView"; import EmptyView from "../EmptyView";
@ -290,36 +290,38 @@ const ConversationView = () => {
// Emit usage update event so quota widget can update. // Emit usage update event so quota widget can update.
getEventEmitter().emit("usage.update"); getEventEmitter().emit("usage.update");
// Collect system prompt if (hasFeature("collect")) {
// We only collect the db prompt for the system prompt. We do not collect the intermediate // Collect system prompt
// exchange to save space since those can be derived from the previous record. // We only collect the db prompt for the system prompt. We do not collect the intermediate
usageMessageList.push({ // exchange to save space since those can be derived from the previous record.
id: generateUUID(), usageMessageList.push({
createdAt: Date.now(), id: generateUUID(),
creatorRole: CreatorRole.System, createdAt: Date.now(),
content: dbPrompt, creatorRole: CreatorRole.System,
} as Message); content: dbPrompt,
} as Message);
// Collect user message // Collect user message
usageMessageList.push(userMessage); usageMessageList.push(userMessage);
// Collect assistant response // Collect assistant response
usageMessageList.push(assistantMessage); usageMessageList.push(assistantMessage);
axios axios
.post<string[]>( .post<string[]>(
"/api/collect", "/api/collect",
{ {
conversation: currentConversation, conversation: currentConversation,
messages: usageMessageList, messages: usageMessageList,
}, },
{ {
headers: requestHeaders, headers: requestHeaders,
} }
) )
.catch(() => { .catch(() => {
// do nth // do nth
}); });
}
}; };
return ( return (

View File

@ -144,7 +144,7 @@ const handler = async (req: NextRequest) => {
}, },
}); });
if (useServerKey) { if (useServerKey && hasFeature("quota")) {
// Increment usage count // Increment usage count
await fetch(usageUrl, { await fetch(usageUrl, {
method: "POST", method: "POST",

View File

@ -1,7 +1,7 @@
import { PrismaClient } from "@prisma/client"; import { PrismaClient } from "@prisma/client";
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
import { Conversation, Message } from "@/types"; import { Conversation, Message } from "@/types";
import { getModel, gpt35 } from "@/utils"; import { getModel } from "@/utils";
import { getEndUser } from "./auth/end-user"; import { getEndUser } from "./auth/end-user";
const prisma = new PrismaClient(); const prisma = new PrismaClient();

View File

@ -1,4 +1,4 @@
type FeatureType = "debug" | "account" | "payment" | "quota"; type FeatureType = "debug" | "account" | "payment" | "quota" | "collect";
const matrix: { [key: string]: { [feature: string]: boolean } } = { const matrix: { [key: string]: { [feature: string]: boolean } } = {
development: { development: {
@ -6,12 +6,14 @@ const matrix: { [key: string]: { [feature: string]: boolean } } = {
account: true, account: true,
payment: true, payment: true,
quota: true, quota: true,
collect: true,
}, },
production: { production: {
debug: false, debug: false,
account: true, account: true,
payment: true, payment: true,
quota: true, quota: true,
collect: true,
}, },
}; };