Skip to content

Commit 1b7061b

Browse files
Initial commit
Created from https://vercel.com/new
0 parents  commit 1b7061b

File tree

102 files changed

+10658
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+10658
-0
lines changed

.env.example

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# You must first activate a Billing Account here: https://platform.openai.com/account/billing/overview
2+
# Then get your OpenAI API Key here: https://platform.openai.com/account/api-keys
3+
OPENAI_API_KEY=XXXXXXXX
4+
5+
# Generate a random secret: https://generate-secret.vercel.app/32 or `openssl rand -base64 32`
6+
AUTH_SECRET=XXXXXXXX
7+
8+
# Instructions to create kv database here: https://vercel.com/docs/storage/vercel-kv/quickstart and
9+
KV_URL=XXXXXXXX
10+
KV_REST_API_URL=XXXXXXXX
11+
KV_REST_API_TOKEN=XXXXXXXX
12+
KV_REST_API_READ_ONLY_TOKEN=XXXXXXXX

.gitignore

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
node_modules
5+
.pnp
6+
.pnp.js
7+
8+
# testing
9+
coverage
10+
11+
# next.js
12+
.next/
13+
out/
14+
build
15+
16+
# misc
17+
.DS_Store
18+
*.pem
19+
20+
# debug
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
.pnpm-debug.log*
25+
26+
# local env files
27+
.env.local
28+
.env.development.local
29+
.env.test.local
30+
.env.production.local
31+
32+
# turbo
33+
.turbo
34+
35+
.env
36+
.vercel
37+
.vscode
38+
.env*.local

LICENSE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2024 Vercel, Inc.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<a href="https://chat.vercel.ai/">
2+
<img alt="Next.js 14 and App Router-ready AI chatbot." src="https://chat.vercel.ai/opengraph-image.png">
3+
<h1 align="center">Next.js AI Chatbot</h1>
4+
</a>
5+
6+
<p align="center">
7+
An open-source AI chatbot app template built with Next.js, the Vercel AI SDK, OpenAI, and Vercel KV.
8+
</p>
9+
10+
<p align="center">
11+
<a href="#features"><strong>Features</strong></a> ·
12+
<a href="#model-providers"><strong>Model Providers</strong></a> ·
13+
<a href="#deploy-your-own"><strong>Deploy Your Own</strong></a> ·
14+
<a href="#running-locally"><strong>Running locally</strong></a> ·
15+
<a href="#authors"><strong>Authors</strong></a>
16+
</p>
17+
<br/>
18+
19+
## Features
20+
21+
- [Next.js](https://nextjs.org) App Router
22+
- React Server Components (RSCs), Suspense, and Server Actions
23+
- [Vercel AI SDK](https://sdk.vercel.ai/docs) for streaming chat UI
24+
- Support for OpenAI (default), Anthropic, Cohere, Hugging Face, or custom AI chat models and/or LangChain
25+
- [shadcn/ui](https://ui.shadcn.com)
26+
- Styling with [Tailwind CSS](https://tailwindcss.com)
27+
- [Radix UI](https://radix-ui.com) for headless component primitives
28+
- Icons from [Phosphor Icons](https://phosphoricons.com)
29+
- Chat History, rate limiting, and session storage with [Vercel KV](https://vercel.com/storage/kv)
30+
- [NextAuth.js](https://github.com/nextauthjs/next-auth) for authentication
31+
32+
## Model Providers
33+
34+
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), [Cohere](https://cohere.com/), [Hugging Face](https://huggingface.co), or using [LangChain](https://js.langchain.com) with just a few lines of code.
35+
36+
## Deploy Your Own
37+
38+
You can deploy your own version of the Next.js AI Chatbot to Vercel with one click:
39+
40+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?demo-title=Next.js+Chat&demo-description=A+full-featured%2C+hackable+Next.js+AI+chatbot+built+by+Vercel+Labs&demo-url=https%3A%2F%2Fchat.vercel.ai%2F&demo-image=%2F%2Fimages.ctfassets.net%2Fe5382hct74si%2F4aVPvWuTmBvzM5cEdRdqeW%2F4234f9baf160f68ffb385a43c3527645%2FCleanShot_2023-06-16_at_17.09.21.png&project-name=Next.js+Chat&repository-name=nextjs-chat&repository-url=https%3A%2F%2Fgithub.com%2Fvercel-labs%2Fai-chatbot&from=templates&skippable-integrations=1&env=OPENAI_API_KEY%2CAUTH_SECRET&envDescription=How+to+get+these+env+vars&envLink=https%3A%2F%2Fgithub.com%2Fvercel-labs%2Fai-chatbot%2Fblob%2Fmain%2F.env.example&teamCreateStatus=hidden&stores=[{"type":"kv"}])
41+
42+
## Creating a KV Database Instance
43+
44+
Follow the steps outlined in the [quick start guide](https://vercel.com/docs/storage/vercel-kv/quickstart#create-a-kv-database) provided by Vercel. This guide will assist you in creating and configuring your KV database instance on Vercel, enabling your application to interact with it.
45+
46+
Remember to update your environment variables (`KV_URL`, `KV_REST_API_URL`, `KV_REST_API_TOKEN`, `KV_REST_API_READ_ONLY_TOKEN`) in the `.env` file with the appropriate credentials provided during the KV database setup.
47+
48+
## Running locally
49+
50+
You will need to use the environment variables [defined in `.env.example`](.env.example) to run Next.js AI Chatbot. It's recommended you use [Vercel Environment Variables](https://vercel.com/docs/projects/environment-variables) for this, but a `.env` file is all that is necessary.
51+
52+
> Note: You should not commit your `.env` file or it will expose secrets that will allow others to control access to your various OpenAI and authentication provider accounts.
53+
54+
1. Install Vercel CLI: `npm i -g vercel`
55+
2. Link local instance with Vercel and GitHub accounts (creates `.vercel` directory): `vercel link`
56+
3. Download your environment variables: `vercel env pull`
57+
58+
```bash
59+
pnpm install
60+
pnpm dev
61+
```
62+
63+
Your app template should now be running on [localhost:3000](http://localhost:3000/).
64+
65+
## Authors
66+
67+
This library is created by [Vercel](https://vercel.com) and [Next.js](https://nextjs.org) team members, with contributions from:
68+
69+
- Jared Palmer ([@jaredpalmer](https://twitter.com/jaredpalmer)) - [Vercel](https://vercel.com)
70+
- Shu Ding ([@shuding\_](https://twitter.com/shuding_)) - [Vercel](https://vercel.com)
71+
- shadcn ([@shadcn](https://twitter.com/shadcn)) - [Vercel](https://vercel.com)

app/(chat)/chat/[id]/page.tsx

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { type Metadata } from 'next'
2+
import { notFound, redirect } from 'next/navigation'
3+
4+
import { auth } from '@/auth'
5+
import { getChat, getMissingKeys } from '@/app/actions'
6+
import { Chat } from '@/components/chat'
7+
import { AI } from '@/lib/chat/actions'
8+
import { Session } from '@/lib/types'
9+
10+
export interface ChatPageProps {
11+
params: {
12+
id: string
13+
}
14+
}
15+
16+
export async function generateMetadata({
17+
params
18+
}: ChatPageProps): Promise<Metadata> {
19+
const session = await auth()
20+
21+
if (!session?.user) {
22+
return {}
23+
}
24+
25+
const chat = await getChat(params.id, session.user.id)
26+
27+
if (!chat || 'error' in chat) {
28+
redirect('/')
29+
} else {
30+
return {
31+
title: chat?.title.toString().slice(0, 50) ?? 'Chat'
32+
}
33+
}
34+
}
35+
36+
export default async function ChatPage({ params }: ChatPageProps) {
37+
const session = (await auth()) as Session
38+
const missingKeys = await getMissingKeys()
39+
40+
if (!session?.user) {
41+
redirect(`/login?next=/chat/${params.id}`)
42+
}
43+
44+
const userId = session.user.id as string
45+
const chat = await getChat(params.id, userId)
46+
47+
if (!chat || 'error' in chat) {
48+
redirect('/')
49+
} else {
50+
if (chat?.userId !== session?.user?.id) {
51+
notFound()
52+
}
53+
54+
return (
55+
<AI initialAIState={{ chatId: chat.id, messages: chat.messages }}>
56+
<Chat
57+
id={chat.id}
58+
session={session}
59+
initialMessages={chat.messages}
60+
missingKeys={missingKeys}
61+
/>
62+
</AI>
63+
)
64+
}
65+
}

app/(chat)/layout.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { SidebarDesktop } from '@/components/sidebar-desktop'
2+
3+
interface ChatLayoutProps {
4+
children: React.ReactNode
5+
}
6+
7+
export default async function ChatLayout({ children }: ChatLayoutProps) {
8+
return (
9+
<div className="relative flex h-[calc(100vh_-_theme(spacing.16))] overflow-hidden">
10+
<SidebarDesktop />
11+
{children}
12+
</div>
13+
)
14+
}

app/(chat)/page.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { nanoid } from '@/lib/utils'
2+
import { Chat } from '@/components/chat'
3+
import { AI } from '@/lib/chat/actions'
4+
import { auth } from '@/auth'
5+
import { Session } from '@/lib/types'
6+
import { getMissingKeys } from '@/app/actions'
7+
8+
export const metadata = {
9+
title: 'Next.js AI Chatbot'
10+
}
11+
12+
export default async function IndexPage() {
13+
const id = nanoid()
14+
const session = (await auth()) as Session
15+
const missingKeys = await getMissingKeys()
16+
17+
return (
18+
<AI initialAIState={{ chatId: id, messages: [] }}>
19+
<Chat id={id} session={session} missingKeys={missingKeys} />
20+
</AI>
21+
)
22+
}

0 commit comments

Comments
 (0)