Skip to content

Commit

Permalink
feat: unlimited messages for contributors (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoy authored Oct 13, 2024
2 parents 9054b6a + 290f567 commit 0c93cfc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ <h4>RecipeSage Privacy Policy</h4>
understanding that we may be unable to provide you with some of your desired
services.
<br /><br />
Use of the cooking assistant relies on the APIs provided by OpenAI and
therefore any interactions that use the cooking assistant or related
functionality is also by proxy subject to their terms of service.
<br /><br />
The contributor-only feature that specifies "unlimited" messages is subject
to protections against abuse. Currently, we consider more than 1440 messages
per day as abuse. (there are 1440 minutes in a day, allowing for an
effective rate of 1 message per minute).
<br /><br />
Your continued use of our website will be regarded as acceptance of our
practices around privacy and personal information. If you have any questions
about how we handle user data and personal information, feel free to contact
Expand Down Expand Up @@ -206,6 +215,6 @@ <h4>RecipeSage Refund Policy</h4>
</p>
<br /><br />

These terms last updated and effective as of the 20th of January 2024.
These terms last updated and effective as of the 13th of October 2024.
<br /><br />
</ion-content>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ <h2>{{ 'pages.assistant.welcome.1' | translate }}</h2>
<p>
{{ 'pages.assistant.welcome.2' | translate }}<br />
{{ 'pages.assistant.welcome.3' | translate }}<br />
{{ 'pages.assistant.welcome.4' | translate }}<br />
<a
href="https://docs.recipesage.com/docs/tutorials/assistant/"
target="_blank"
Expand Down
7 changes: 4 additions & 3 deletions packages/frontend/src/assets/i18n/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
"pages.contribute.bonus.title": "As a thank you to those who make RecipeSage possible, your contributions will unlock some bonus features for your account. You will be able to:",
"pages.contribute.bonus.feature.1": "Upload images in high-resolution",
"pages.contribute.bonus.feature.2": "Upload multiple images for each recipe",
"pages.contribute.bonus.feature.3": "Increased assistant message limit",
"pages.contribute.bonus.feature.3": "Unlimited cooking assistant messages",
"pages.contribute.bonus.enabled": "Enabled",
"pages.contribute.bonus.reason": "Bonus features features are accessible only to contributors because these particular features are costly for me to host.",
"pages.contribute.cta.1": "I originally created RecipeSage for my own family to host recipes we enjoy sharing. Since then, RecipeSage has grown into a fully-fledged open source project.",
Expand Down Expand Up @@ -467,7 +467,8 @@
"pages.assistant.title": "Cooking Assistant",
"pages.assistant.welcome.1": "Welcome to the RecipeSage cooking assistant!",
"pages.assistant.welcome.2": "The cooking assistant is an AI model that can respond to your cooking questions and help create recipes.",
"pages.assistant.welcome.3": "The cooking assistant has a message limit of 5 messages/day because of the high costs to run the AI. This limit is increased to 75 messages/day for contributors.",
"pages.assistant.welcome.3": "The cooking assistant has a message limit of 5 messages/day because of the high costs to run the AI. This limit is removed for contributors.",
"pages.assistant.welcome.4": "This limit only applies to messages sent by you, and not to the AI's responses.",
"pages.assistant.welcome.documentation": "Please read the assistant documentation",
"pages.assistant.messageLimit": "Maximum messages reached (sorry, it's costly!). Message count resets at 0:00GMT.",

Expand Down Expand Up @@ -599,7 +600,7 @@
"pages.account.bonus.description": "Contributors unlock certain features that are costly to host:",
"pages.account.bonus.highResImages": "High Resolution Images",
"pages.account.bonus.multipleImages": "Multiple Images Per Recipe",
"pages.account.bonus.assistantMoreMessages": "Increased Assistant Message Limit",
"pages.account.bonus.assistantMoreMessages": "Unlimited Cooking Assistant Messages",
"pages.account.bonus.enabled": "Enabled",
"pages.account.bonus.notEnabled": "Not Enabled",
"pages.account.bonus.subscriptionExpired": "Expired {{date}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ export const sendAssistantMessage = publicProcedure
});
}

const isOverMessageLimit = await assistant.checkMessageLimit(
session.userId,
);
if (isOverMessageLimit) {
const { isOverLimit, useLowQualityModel } =
await assistant.checkMessageLimit(session.userId);
if (isOverLimit) {
throw new TRPCError({
message: "Over daily message limit",
code: "TOO_MANY_REQUESTS",
});
}

await assistant.sendChat(input.content, session.userId);
await assistant.sendChat(input.content, session.userId, useLowQualityModel);

return "ok";
});
29 changes: 21 additions & 8 deletions packages/util/server/src/ml/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const showdown = new Converter({
openLinksInNewWindow: true,
});

const FREE_MESSAGE_CAP = 5;
const CONTRIB_MESSAGE_CAP = 50;
const ABUSE_MESSAGE_CAP = 1440;

export class Assistant {
private openAiHelper: OpenAIHelper;
/**
Expand Down Expand Up @@ -126,8 +130,6 @@ export class Assistant {
}

async checkMessageLimit(userId: string) {
if (process.env.NODE_ENV === "development") return false;

const moreMessages = await userHasCapability(
userId,
Capabilities.AssistantMoreMessages,
Expand All @@ -148,14 +150,23 @@ export class Assistant {
},
});

const messageLimit = moreMessages ? 75 : 5;
const isOverLimit =
(!moreMessages && todayMessageCount >= FREE_MESSAGE_CAP) ||
todayMessageCount >= ABUSE_MESSAGE_CAP;
const useLowQualityModel =
moreMessages && todayMessageCount >= CONTRIB_MESSAGE_CAP;

const isOverLimit = todayMessageCount >= messageLimit;

return isOverLimit;
return {
isOverLimit,
useLowQualityModel,
};
}

async sendChat(content: string, userId: string): Promise<void> {
async sendChat(
content: string,
userId: string,
useLowQualityModel: boolean,
): Promise<void> {
const assistantUser = await prisma.user.findUniqueOrThrow({
where: {
email: "[email protected]",
Expand All @@ -172,7 +183,9 @@ export class Assistant {
const recipes: Prisma.RecipeUncheckedCreateInput[] = [];

const response = await this.openAiHelper.getChatResponseWithTools(
SupportedGPTModel.GPT4O,
useLowQualityModel
? SupportedGPTModel.GPT4OMini
: SupportedGPTModel.GPT4O,
context,
[initBuildRecipe(assistantUser.id, recipes)],
);
Expand Down

0 comments on commit 0c93cfc

Please sign in to comment.