Open pelguetat opened 1 year ago
oops sorry I missed this.
import { NextRequest, NextResponse } from "next/server";
import { Ratelimit } from "@upstash/ratelimit"; // for deno: see above
import { Redis } from "@upstash/redis";
import { ipAddress } from "@vercel/edge";
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});
// Create a new ratelimiter, that allows 10 requests per 10 seconds
const ratelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(10, "1 h"),
analytics: true,
});
export const config = {
runtime: "edge",
};
export default async (req: NextRequest) => {
const data = await req.json();
const prompt = data.prompt;
const tone = data.tone;
const ip = ipAddress(req) || "unknown";
let response;
if (!prompt) {
return new Response("No prompt in the request", { status: 400 });
}
const { success } = await ratelimit.limit(ip);
if (!success) {
response = new NextResponse(
"During the beta phase, you may only make 10 requests per hour. We apologize for any inconvenience this may cause."
);
} else if (prompt.length > 1000) {
response = new NextResponse(
"During the beta phase, input exceeding the limit of 1000 characters will not be permitted. We apologize for any inconvenience this may cause."
);
} else {
let content = "";
if (!tone || tone === "") {
content = `Improve: ${prompt}`;
} else if (tone === "fix") {
content = `Fix the grammar: ${prompt}`;
} else {
content = `Improve writing in a ${tone} way: ${prompt}`;
}
console.log(content);
const payload = {
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: content }],
};
const res = await fetch("https://api.openai.com/v1/chat/completions", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${
process.env.OPENAI_API_KEY ??
"sk-rzThfddsdsffnj4WGI1"
}`,
},
method: "POST",
body: JSON.stringify(payload),
});
const msg = await res.json();
response = new NextResponse(msg.choices[0].message.content);
}
// Set custom header
response.headers.set("Access-Control-Allow-Credentials", "true");
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set(
"Access-Control-Allow-Methods",
"GET,OPTIONS,PATCH,DELETE,POST,PUT"
);
response.headers.set(
"Access-Control-Allow-Headers",
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
);
return response;
};
Hey, thanks for sharing this, it's amazing. I'm trying to adapt it for a similar use case but I'm having issues with Vercel when calling OpenAI. Would you mind sharing your backend code? Thanks.