diff --git a/.env.example b/.env.example index 3c45061..643a5cb 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ -## Get your OpenAI API Key +## You must first activate a Billing Account here: https://platform.openai.com/account/billing/overview +## Then get your OpenAI API Key here: https://platform.openai.com/account/api-keys OPENAI_API_KEY=XXXXXXXX ## Generate a random secret: https://generate-secret.vercel.app/32 diff --git a/README.md b/README.md index cb32307..1105e31 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ - [Next.js](https://nextjs.org) App Router - React Server Components (RSCs), Suspense, and Server Actions - [Vercel AI SDK](https://sdk.vercel.ai/docs) for streaming chat UI -- Support for OpenAI (default), Anthropic, HuggingFace, or custom AI chat models and/or LangChain +- Support for OpenAI (default), Anthropic, Hugging Face, or custom AI chat models and/or LangChain - Edge runtime-ready - [shadcn/ui](https://ui.shadcn.com) - Styling with [Tailwind CSS](https://tailwindcss.com) @@ -32,7 +32,7 @@ ## Model Providers -This template ships with OpenAI `gpt-3.5-turbo` as the default. However, thanks to the [Vercel AI SDK](https://sdk.vercel.ai/docs), you can switch LLM providers to [Anthropic](https://anthropic.com), [HuggingFace](https://huggingface.co), or using [LangChain](https://js.langchain.com) with just a few lines of code. +This template ships with OpenAI `gpt-3.5-turbo` as the default. However, thanks to the [Vercel AI SDK](https://sdk.vercel.ai/docs), you can switch LLM providers to [Anthropic](https://anthropic.com), [Hugging Face](https://huggingface.co), or using [LangChain](https://js.langchain.com) with just a few lines of code. ## Deploy Your Own diff --git a/app/actions.ts b/app/actions.ts index 90c6973..2c8a5dd 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -74,7 +74,9 @@ export async function clearChats() { } const chats: string[] = await kv.zrange(`user:chat:${session.user.id}`, 0, -1) - + if (!chats.length) { + return redirect('/') + } const pipeline = kv.pipeline() for (const chat of chats) { diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 883210b..35e9719 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -1,2 +1 @@ export { GET, POST } from '@/auth' -export const runtime = 'edge' diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 4f63d1e..b39b26c 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -7,22 +7,26 @@ import { nanoid } from '@/lib/utils' export const runtime = 'edge' +const configuration = new Configuration({ + apiKey: process.env.OPENAI_API_KEY +}) + +const openai = new OpenAIApi(configuration) + export async function POST(req: Request) { const json = await req.json() const { messages, previewToken } = json const session = await auth() - if (process.env.VERCEL_ENV !== 'preview') { + if (process.env.VERCEL_ENV === 'preview') { if (session == null) { return new Response('Unauthorized', { status: 401 }) } } - const configuration = new Configuration({ - apiKey: previewToken || process.env.OPENAI_API_KEY - }) - - const openai = new OpenAIApi(configuration) + if (previewToken) { + configuration.apiKey = previewToken + } const res = await openai.createChatCompletion({ model: 'gpt-3.5-turbo', @@ -34,7 +38,7 @@ export async function POST(req: Request) { const stream = OpenAIStream(res, { async onCompletion(completion) { const title = json.messages[0].content.substring(0, 100) - const userId = session?.user.id + const userId = session?.user?.id if (userId) { const id = json.id ?? nanoid() const createdAt = Date.now() diff --git a/app/chat/[id]/page.tsx b/app/chat/[id]/page.tsx index d7ad6d6..25d6c30 100644 --- a/app/chat/[id]/page.tsx +++ b/app/chat/[id]/page.tsx @@ -25,7 +25,7 @@ export async function generateMetadata({ const chat = await getChat(params.id, session.user.id) return { - title: chat?.title.slice(0, 50) ?? 'Chat' + title: chat?.title.toString().slice(0, 50) ?? 'Chat' } } diff --git a/app/share/[id]/page.tsx b/app/share/[id]/page.tsx index 3ec3f3e..4ee6620 100644 --- a/app/share/[id]/page.tsx +++ b/app/share/[id]/page.tsx @@ -35,9 +35,9 @@ export default async function SharePage({ params }: SharePageProps) { return ( <>