Skip to content

Commit

Permalink
Move rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Jul 9, 2024
1 parent 5a4da01 commit 0d18816
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
47 changes: 19 additions & 28 deletions apps/dashboard/src/actions/ai/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,11 @@ const ratelimit = new Ratelimit({
redis: RedisClient,
});

export async function selectModel() {
return openai("gpt-4o");

// const settings = await getAssistantSettings();

// switch (settings.provider) {
// case "mistralai": {
// return mistral("mistral-large-latest");
// }
// default: {
// return openai("gpt-4o");
// }
// }
}

export async function submitUserMessage(
content: string
): Promise<ClientMessage> {
"use server";
const ip = headers().get("x-forwarded-for");
const user = await getUser();
const teamId = user?.data?.team_id as string;

const defaultValues = {
from: subMonths(startOfMonth(new Date()), 12).toISOString(),
to: new Date().toISOString(),
currency:
(await getBankAccountsCurrencies())?.data?.at(0)?.currency ?? "USD",
};

const model = await selectModel();

const { success } = await ratelimit.limit(ip);

const aiState = getMutableAIState<typeof AI>();
Expand All @@ -83,8 +56,26 @@ export async function submitUserMessage(
},
],
});

return {
id: nanoid(),
role: "assistant",
display: (
<BotMessage content="Not so fast, tiger. You've reached your message limit. Please wait a minute and try again." />
),
};
}

const user = await getUser();
const teamId = user?.data?.team_id as string;

const defaultValues = {
from: subMonths(startOfMonth(new Date()), 12).toISOString(),
to: new Date().toISOString(),
currency:
(await getBankAccountsCurrencies())?.data?.at(0)?.currency ?? "USD",
};

aiState.update({
...aiState.get(),
messages: [
Expand All @@ -101,7 +92,7 @@ export async function submitUserMessage(
let textNode: undefined | React.ReactNode;

const result = await streamUI({
model,
model: openai("gpt-4o"),
initial: <SpinnerMessage />,
system: `\
You are a helpful assistant in Midday who can help users ask questions about their transactions, revenue, spending find invoices and more.
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/actions/ai/chat/tools/forecast.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { MutableAIState } from "@/actions/ai/types";
import { openai } from "@ai-sdk/openai";
import { getMetrics } from "@midday/supabase/cached-queries";
import { generateText } from "ai";
import { startOfMonth } from "date-fns";
import { nanoid } from "nanoid";
import { z } from "zod";
import { selectModel } from "..";
import { ForecastUI } from "./ui/forecast-ui";

type Args = {
Expand Down Expand Up @@ -50,7 +50,7 @@ export function getForecastTool({ aiState, currency, dateFrom, dateTo }: Args) {
});

const { text } = await generateText({
model: await selectModel(),
model: openai("gpt-4o"),
system:
"You are a financial forecaster and analyst. Your task is to provide simple, clear, and concise content. Return only the result with a short description only with text. Make sure to mention that this is an indication of the forecast and should be verified.",
prompt: `forecast next month ${type} based on the last 12 months ${type}:\n${prev}`,
Expand Down

0 comments on commit 0d18816

Please sign in to comment.