diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 8320a20..4d20ee4 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -81,14 +81,21 @@ export async function POST(req: Request) { ) if (!success) { - return new Response('You have reached your request limit for the day.', { + return new Response(JSON.stringify({ + error: "You have reached your request limit for the day.", + rateLimitReached: true, + limit, + remaining, + reset + }), { status: 429, headers: { + 'Content-Type': 'application/json', 'X-RateLimit-Limit': limit.toString(), 'X-RateLimit-Remaining': remaining.toString(), 'X-RateLimit-Reset': reset.toString() } - }) + }); } model = 'gpt-4-1106-preview' @@ -99,23 +106,29 @@ export async function POST(req: Request) { const ratelimit = new Ratelimit({ redis: kv, - limiter: Ratelimit.slidingWindow(20, '1d') + limiter: Ratelimit.slidingWindow(15, '1d') }) const { success, limit, reset, remaining } = await ratelimit.limit( `ratelimit_${ip}` ) - // TODO: Add Pop Up for Rate Limit if (!success) { - return new Response('You have reached your request limit for the day.', { + return new Response(JSON.stringify({ + error: "You have reached your request limit for the day.", + rateLimitReached: true, + limit, + remaining, + reset + }), { status: 429, headers: { + 'Content-Type': 'application/json', 'X-RateLimit-Limit': limit.toString(), 'X-RateLimit-Remaining': remaining.toString(), 'X-RateLimit-Reset': reset.toString() } - }) + }); } model = 'gpt-4-1106-preview' diff --git a/components/chat.tsx b/components/chat.tsx index 4ea2f78..79c9d8c 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -8,6 +8,7 @@ import { ChatPanel } from '@/components/chat-panel' import { EmptyScreen } from '@/components/empty-screen' import { ChatScrollAnchor } from '@/components/chat-scroll-anchor' import { toast } from 'react-hot-toast' +import Swal from 'sweetalert2' export interface ChatProps extends React.ComponentProps<'div'> { initialMessages?: Message[] @@ -33,7 +34,13 @@ export function Chat({ id, initialMessages, className }: ChatProps) { }, onResponse(response) { if (response.status === 401) { - toast.error(response.statusText) + toast.error(response.statusText); + } else if (response.status === 429) { + Swal.fire({ + icon: 'error', + title: 'Rate Limit Exceeded', + text: 'You have exceed daily rate limit. In order to continue please use your own OPEN AI API KEY from Settings(add link of settings page) or wait 1 day' + }); } } })