Skip to content

Commit

Permalink
Added rate limit and use gpt 3.5 instead
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Jun 25, 2023
1 parent 0de80d5 commit 3768f6a
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 4 deletions.
31 changes: 30 additions & 1 deletion app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { kv } from "@vercel/kv";
import { Ratelimit } from "@upstash/ratelimit";
import { Configuration, OpenAIApi } from "openai-edge";
import { OpenAIStream, StreamingTextResponse } from "ai";
import { functions, runFunction } from "./functions";
Expand All @@ -11,6 +13,33 @@ const openai = new OpenAIApi(config);
export const runtime = "edge";

export async function POST(req: Request) {
if (
process.env.NODE_ENV !== "development" &&
process.env.KV_REST_API_URL &&
process.env.KV_REST_API_TOKEN
) {
const ip = req.headers.get("x-forwarded-for");
const ratelimit = new Ratelimit({
redis: kv,
limiter: Ratelimit.slidingWindow(50, "1 d"),
});

const { success, limit, reset, remaining } = await ratelimit.limit(
`chathn_ratelimit_${ip}`,
);

if (!success) {
return new Response("You have reached your request limit for the day.", {
status: 429,
headers: {
"X-RateLimit-Limit": limit.toString(),
"X-RateLimit-Remaining": remaining.toString(),
"X-RateLimit-Reset": reset.toString(),
},
});
}
}

const { messages } = await req.json();

// check if the conversation requires a function call to be made
Expand All @@ -30,7 +59,7 @@ export async function POST(req: Request) {
const functionResponse = await runFunction(name, JSON.parse(args));

finalResponse = await openai.createChatCompletion({
model: "gpt-4-0613",
model: "gpt-3.5-turbo-0613",
stream: true,
messages: [
...messages,
Expand Down
6 changes: 5 additions & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./globals.css";
import { Inter } from "next/font/google";
import { ReactNode } from "react";
import Toaster from "./toaster";
import { Analytics } from "@vercel/analytics/react";

const inter = Inter({ subsets: ["latin"] });
Expand All @@ -14,7 +15,10 @@ export const metadata = {
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
<body className={inter.className}>
{children}
<Toaster />
</body>
<Analytics />
</html>
);
Expand Down
11 changes: 9 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Bot, User } from "lucide-react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import Textarea from "react-textarea-autosize";
import { toast } from "sonner";

const examples = [
"Get me the top 5 stories on Hacker News in markdown table format. Use columns like title, link, score, and comments.",
Expand All @@ -21,8 +22,14 @@ export default function Chat() {
const inputRef = useRef<HTMLTextAreaElement>(null);

const { messages, input, setInput, handleSubmit, isLoading } = useChat({
onResponse: () => {
va.track("Chat initiated");
onResponse: (response) => {
if (response.status === 429) {
toast.error("You have reached your request limit for the day.");
va.track("Rate limited");
return;
} else {
va.track("Chat initiated");
}
},
onError: (error) => {
va.track("Chat errored", {
Expand Down
3 changes: 3 additions & 0 deletions app/toaster.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use client";

export { Toaster as default } from "sonner";
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"@types/node": "20.3.1",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.6",
"@upstash/ratelimit": "^0.4.3",
"@vercel/analytics": "^1.0.1",
"@vercel/kv": "^0.2.2",
"ai": "^2.1.8",
"autoprefixer": "10.4.14",
"clsx": "^1.2.1",
Expand All @@ -29,6 +31,7 @@
"react-markdown": "^8.0.7",
"react-textarea-autosize": "^8.4.1",
"remark-gfm": "^3.0.1",
"sonner": "^0.5.0",
"tailwindcss": "3.3.2",
"typescript": "5.1.3"
},
Expand Down
93 changes: 93 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3768f6a

Please sign in to comment.