From 57e602d24d48360f65a7806866bd4c0537addc20 Mon Sep 17 00:00:00 2001 From: Sang Dang Date: Thu, 7 Dec 2023 22:53:16 +0200 Subject: [PATCH] delete unused route --- app/api/generate/route.ts | 51 +-- app/api/upload/route.ts | 26 -- components/client/Uploader/index.tsx | 183 --------- package.json | 3 - pnpm-lock.yaml | 530 +-------------------------- 5 files changed, 16 insertions(+), 777 deletions(-) delete mode 100644 app/api/upload/route.ts delete mode 100644 components/client/Uploader/index.tsx diff --git a/app/api/generate/route.ts b/app/api/generate/route.ts index fa7f6d9..2efcd0d 100644 --- a/app/api/generate/route.ts +++ b/app/api/generate/route.ts @@ -1,63 +1,34 @@ -import { Configuration, OpenAIApi } from "openai-edge"; -import { OpenAIStream, StreamingTextResponse } from "ai"; -import { kv } from "@vercel/kv"; -import { Ratelimit } from "@upstash/ratelimit"; +import { Configuration, OpenAIApi } from 'openai-edge'; +import { OpenAIStream, StreamingTextResponse } from 'ai'; const config = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(config); -export const runtime = "edge"; - -export async function POST(req: Request): Promise { - 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( - `platforms_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(), - }, - }); - } - } +export const runtime = 'edge'; +export async function POST(req: Request) { let { prompt: content } = await req.json(); // remove trailing slash, // slice the content from the end to prioritize later characters - content = content.replace(/\/$/, "").slice(-5000); + content = content.replace(/\/$/, '').slice(-5000); const response = await openai.createChatCompletion({ - model: "gpt-3.5-turbo", + model: 'gpt-3.5-turbo', messages: [ { - role: "system", + role: 'system', content: - "You are an AI writing assistant that continues existing text based on context from prior text. " + - "Give more weight/priority to the later characters than the beginning ones. " + - "Limit your response to no more than 200 characters, but make sure to construct complete sentences.", + 'You are an AI writing assistant that continues existing text based on context from prior text. ' + + 'Give more weight/priority to the later characters than the beginning ones. ' + + 'Limit your response to no more than 200 characters, but make sure to construct complete sentences.', // we're disabling markdown for now until we can figure out a way to stream markdown text with proper formatting: https://github.com/steven-tey/novel/discussions/7 // "Use Markdown formatting when appropriate.", }, { - role: "user", + role: 'user', content, }, ], diff --git a/app/api/upload/route.ts b/app/api/upload/route.ts deleted file mode 100644 index 8cb647a..0000000 --- a/app/api/upload/route.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { put } from "@vercel/blob"; -import { nanoid } from "nanoid"; -import { NextResponse } from "next/server"; - -export const runtime = "edge"; - -export async function POST(req: Request) { - if (!process.env.BLOB_READ_WRITE_TOKEN) { - return new Response( - "Missing BLOB_READ_WRITE_TOKEN. Don't forget to add that to your .env file.", - { - status: 401, - }, - ); - } - - const file = req.body || ""; - const contentType = req.headers.get("content-type") || "text/plain"; - const filename = `${nanoid()}.${contentType.split("/")[1]}`; - const blob = await put(filename, file, { - contentType, - access: "public", - }); - - return NextResponse.json(blob); -} diff --git a/components/client/Uploader/index.tsx b/components/client/Uploader/index.tsx deleted file mode 100644 index c4227de..0000000 --- a/components/client/Uploader/index.tsx +++ /dev/null @@ -1,183 +0,0 @@ -'use client'; - -import { BlobResult } from '@vercel/blob'; - -import { useState, useCallback, useMemo } from '@/hooks'; -import { Spinner } from '@/components/chakra'; -import type { ChangeEvent } from '@/types'; - -export const Uploader = () => { - const [data, setData] = useState<{ - image: string | null; - }>({ - image: null, - }); - const [file, setFile] = useState(null); - - const [dragActive, setDragActive] = useState(false); - - const onChangePicture = useCallback( - (event: ChangeEvent) => { - const file = event.currentTarget.files && event.currentTarget.files[0]; - if (file) { - if (file.size / 1024 / 1024 > 50) { - // toast.error('File size too big (max 50MB)'); - } else { - setFile(file); - const reader = new FileReader(); - reader.onload = (e) => { - setData((prev) => ({ ...prev, image: e.target?.result as string })); - }; - reader.readAsDataURL(file); - } - } - }, - [setData] - ); - - const [saving, setSaving] = useState(false); - - const saveDisabled = useMemo(() => { - return !data.image || saving; - }, [data.image, saving]); - - return ( -
{ - e.preventDefault(); - setSaving(true); - fetch('/api/upload', { - method: 'POST', - headers: { 'content-type': file?.type || 'application/octet-stream' }, - body: file, - }).then(async (res) => { - if (res.status === 200) { - const { url } = (await res.json()) as BlobResult; - // toast(); - } else { - const error = await res.text(); - // toast.error(error); - } - setSaving(false); - }); - }} - > -
-
-

Upload a file

-

- Accepted formats: .png, .jpg, .gif, .mp4 -

-
-