Skip to content

Commit

Permalink
remove rate limit since apikey is required
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimondev committed May 1, 2024
1 parent 5db976a commit 5f28f42
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions apps/web/app/api/code-generation/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,44 @@ import { NextResponse } from "next/server";
import { cookies } from "next/headers";
import OpenAI from "openai";
import { OpenAIStream, StreamingTextResponse } from "ai";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { TOTAL_GENERATIONS } from "@/constants";
// import { Ratelimit } from "@upstash/ratelimit";
// import { Redis } from "@upstash/redis";
// import { TOTAL_GENERATIONS } from "@/constants";
import { PROMPT } from "@/prompt";

const openai = new OpenAI();

export const runtime = "edge";

const ratelimit =
process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN
? new Ratelimit({
redis: new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
}),
limiter: Ratelimit.slidingWindow(TOTAL_GENERATIONS, "1440 m"), // 1 per day
analytics: true,
})
: false;
// const ratelimit =
// process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN
// ? new Ratelimit({
// redis: new Redis({
// url: process.env.UPSTASH_REDIS_REST_URL,
// token: process.env.UPSTASH_REDIS_REST_TOKEN,
// }),
// limiter: Ratelimit.slidingWindow(TOTAL_GENERATIONS, "1440 m"), // 1 per day
// analytics: true,
// })
// : false;

export async function POST(req: Request) {
if (!process.env.OPENAI_API_KEY || process.env.OPENAI_API_KEY === "") {
const customApiKey = cookies().get("api-key")?.value;

if (process.env.NODE_ENV === "production" && !customApiKey) {
return NextResponse.json(
{
data: undefined,
error: "Missing API KEY – make sure to set it.",
},
{ status: 400 }
);
}

if (
process.env.NODE_ENV === "development" &&
(!process.env.OPENAI_API_KEY || process.env.OPENAI_API_KEY === "")
) {
return NextResponse.json(
{
data: undefined,
Expand All @@ -35,34 +50,31 @@ export async function POST(req: Request) {
);
}

const customApiKey = cookies().get("api-key")?.value;
const hasCustomApiKey = customApiKey && customApiKey.trim().length > 0;
if (customApiKey) {
// Set user's api key
openai.apiKey = customApiKey as string;
}

if (process.env.NODE_ENV === "production" && !customApiKey) {
if (ratelimit) {
const ip = req.headers.get("x-real-ip") ?? "local";
// const hasCustomApiKey = customApiKey && customApiKey.trim().length > 0;

const { success, limit, reset, remaining } = await ratelimit.limit(ip);
if (!success) {
return NextResponse.json(
{ message: "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(),
},
}
);
}
}
}
// if (ratelimit) {
// const ip = req.headers.get("x-real-ip") ?? "local";

if (hasCustomApiKey) {
// Set user's api key
openai.apiKey = customApiKey;
}
// const { success, limit, reset, remaining } = await ratelimit.limit(ip);
// if (!success) {
// return NextResponse.json(
// { message: "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 { prompt: base64 } = await req.json();

Expand Down

0 comments on commit 5f28f42

Please sign in to comment.