Skip to content

Commit

Permalink
Merge pull request #50 from UmstadAI/add/ratelimitalert
Browse files Browse the repository at this point in the history
add rate limit modal #38
  • Loading branch information
berkingurcan authored Mar 27, 2024
2 parents 2e4f21b + aef4438 commit befb9f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
25 changes: 19 additions & 6 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
9 changes: 8 additions & 1 deletion components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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'
});
}
}
})
Expand Down

0 comments on commit befb9f6

Please sign in to comment.