Skip to content

Commit

Permalink
feat: rate limit 설정 및 안내 문구, 에러 처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
comolove committed Oct 21, 2023
1 parent 228beed commit 03eb62e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
54 changes: 54 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"@tiptap/suggestion": "^2.0.3",
"@types/node": "20.3.1",
"@types/react-dom": "18.2.6",
"@upstash/ratelimit": "^0.4.4",
"@vercel/blob": "^0.9.2",
"@vercel/kv": "^0.2.3",
"ai": "^2.2.10",
"autoprefixer": "10.4.14",
"chart.js": "^4.4.0",
Expand Down
51 changes: 26 additions & 25 deletions src/app/api/generate/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Configuration, OpenAIApi } from 'openai-edge';
import { OpenAIStream, StreamingTextResponse } from 'ai';
import { kv } from '@vercel/kv';
import { Ratelimit } from '@upstash/ratelimit';

const config = new Configuration({
apiKey: process.env.NEXT_PUBLIC_OPENAI_API_KEY,
Expand All @@ -9,33 +11,32 @@ const openai = new OpenAIApi(config);
export const runtime = 'edge';

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

// const { success, limit, reset, remaining } = await ratelimit.limit(
// `novel_ratelimit_${ip}`
// );
const { success, limit, reset, remaining } = await ratelimit.limit(
`novel_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(),
// },
// });
// }
// }
if (!success) {
return new Response('일일 자동완성 최대 사용량 50회를 초과하였습니다', {
status: 429,
headers: {
'X-RateLimit-Limit': limit.toString(),
'X-RateLimit-Remaining': remaining.toString(),
'X-RateLimit-Reset': reset.toString(),
},
});
}
}
let { prompt } = await req.json();
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
Expand Down
2 changes: 1 addition & 1 deletion src/components/create/memo/EditorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default function EditorForm() {
});
},
onError: (err) => {
console.log(err);
notifyToast(err.message, "error");
},
});

Expand Down
4 changes: 2 additions & 2 deletions src/components/editor/components/FakeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default function FakeEditor({ editor }: { editor: Editor | null }) {
ref={edirotRef}
className={`fixed flex flex-col top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-3/4 h-3/4 bg-white z-10 top-1/12 p-6 rounded-xl shadow-lg overflow-y-auto`}
>
<div className="font-medium text-xs text-soma-grey-45 self-end">
chatGpt로 생성된 답변입니다. 답변이 정확하지 않을 수 있습니다.
<div className="font-medium text-xs text-soma-grey-45 self-end mb-2">
chatGpt로 생성된 답변입니다. 답변이 정확하지 않을 수 있으며, 일일 최대 사용량은 50회로 제한됩니다.
</div>
<RingLoader className="self-center" color="#4992FF" />
<div className="text-center font-medium text-soma-grey-49 text-sm my-5">
Expand Down

0 comments on commit 03eb62e

Please sign in to comment.