forked from steven-tey/chathn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.tsx
248 lines (242 loc) · 8.36 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"use client";
import { useRef } from "react";
import { useChat } from "ai/react";
import va from "@vercel/analytics";
import clsx from "clsx";
import { VercelIcon, GithubIcon, LoadingCircle, SendIcon } from "./icons";
import { Bot, User } from "lucide-react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import Textarea from "react-textarea-autosize";
import { toast } from "sonner";
const examples = [
"Get me the top 5 stories on Hacker News in markdown table format. Use columns like title, link, score, and comments.",
"Summarize the comments in the top hacker news story.",
"What is the top story on Hacker News right now?",
];
export default function Chat() {
const formRef = useRef<HTMLFormElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
const { messages, input, setInput, handleSubmit, isLoading } = useChat({
onResponse: (response) => {
if (response.status === 429) {
toast.error("You have reached your request limit for the day.");
va.track("Rate limited");
return;
} else {
va.track("Chat initiated");
}
},
onError: (error) => {
va.track("Chat errored", {
input,
error: error.message,
});
},
});
const disabled = isLoading || input.length === 0;
return (
<main className="flex flex-col items-center justify-between pb-40">
<div className="absolute top-5 hidden w-full justify-between px-5 sm:flex">
<a
href="/deploy"
target="_blank"
className="rounded-lg p-2 transition-colors duration-200 hover:bg-stone-100 sm:bottom-auto"
>
<VercelIcon />
</a>
<a
href="/github"
target="_blank"
className="rounded-lg p-2 transition-colors duration-200 hover:bg-stone-100 sm:bottom-auto"
>
<GithubIcon />
</a>
</div>
{messages.length > 0 ? (
messages.map((message, i) => (
<div
key={i}
className={clsx(
"flex w-full items-center justify-center border-b border-gray-200 py-8",
message.role === "user" ? "bg-white" : "bg-gray-100",
)}
>
<div className="flex w-full max-w-screen-md items-start space-x-4 px-5 sm:px-0">
<div
className={clsx(
"p-1.5 text-white",
message.role === "assistant" ? "bg-green-500" : "bg-black",
)}
>
{message.role === "user" ? (
<User width={20} />
) : (
<Bot width={20} />
)}
</div>
<ReactMarkdown
className="prose mt-1 w-full break-words prose-p:leading-relaxed"
remarkPlugins={[remarkGfm]}
components={{
// open links in new tab
a: (props) => (
<a {...props} target="_blank" rel="noopener noreferrer" />
),
}}
>
{message.content}
</ReactMarkdown>
</div>
</div>
))
) : (
<div className="border-gray-200sm:mx-0 mx-5 mt-20 max-w-screen-md rounded-md border sm:w-full">
<div className="flex flex-col space-y-4 p-7 sm:p-10">
<h1 className="text-lg font-semibold text-black">
Welcome to ChatHN!
</h1>
<p className="text-gray-500">
This is an{" "}
<a
href="https://github.com/steven-tey/chathn"
target="_blank"
rel="noopener noreferrer"
className="font-medium underline underline-offset-4 transition-colors hover:text-black"
>
open-source
</a>{" "}
AI chatbot that uses{" "}
<a
href="https://platform.openai.com/docs/guides/gpt/function-calling"
target="_blank"
rel="noopener noreferrer"
className="font-medium underline underline-offset-4 transition-colors hover:text-black"
>
OpenAI Functions
</a>{" "}
and{" "}
<a
href="https://sdk.vercel.ai/docs"
target="_blank"
rel="noopener noreferrer"
className="font-medium underline underline-offset-4 transition-colors hover:text-black"
>
Vercel AI SDK
</a>{" "}
to interact with the{" "}
<a
href="https://github.com/HackerNews/API"
target="_blank"
rel="noopener noreferrer"
className="font-medium underline underline-offset-4 transition-colors hover:text-black"
>
Hacker News API
</a>{" "}
with natural language.
</p>
</div>
<div className="flex flex-col space-y-4 border-t border-gray-200 bg-gray-50 p-7 sm:p-10">
{examples.map((example, i) => (
<button
key={i}
className="rounded-md border border-gray-200 bg-white px-5 py-3 text-left text-sm text-gray-500 transition-all duration-75 hover:border-black hover:text-gray-700 active:bg-gray-50"
onClick={() => {
setInput(example);
inputRef.current?.focus();
}}
>
{example}
</button>
))}
</div>
</div>
)}
<div className="fixed bottom-0 flex w-full flex-col items-center space-y-3 bg-gradient-to-b from-transparent via-gray-100 to-gray-100 p-5 pb-3 sm:px-0">
<form
ref={formRef}
onSubmit={handleSubmit}
className="relative w-full max-w-screen-md rounded-xl border border-gray-200 bg-white px-4 pb-2 pt-3 shadow-lg sm:pb-3 sm:pt-4"
>
<Textarea
ref={inputRef}
tabIndex={0}
required
rows={1}
autoFocus
placeholder="Send a message"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
formRef.current?.requestSubmit();
e.preventDefault();
}
}}
spellCheck={false}
className="w-full pr-10 focus:outline-none"
/>
<button
className={clsx(
"absolute inset-y-0 right-3 my-auto flex h-8 w-8 items-center justify-center rounded-md transition-all",
disabled
? "cursor-not-allowed bg-white"
: "bg-green-500 hover:bg-green-600",
)}
disabled={disabled}
>
{isLoading ? (
<LoadingCircle />
) : (
<SendIcon
className={clsx(
"h-4 w-4",
input.length === 0 ? "text-gray-300" : "text-white",
)}
/>
)}
</button>
</form>
<p className="text-center text-xs text-gray-400">
Built with{" "}
<a
href="https://platform.openai.com/docs/guides/gpt/function-calling"
target="_blank"
rel="noopener noreferrer"
className="transition-colors hover:text-black"
>
OpenAI Functions
</a>{" "}
and{" "}
<a
href="https://sdk.vercel.ai/docs"
target="_blank"
rel="noopener noreferrer"
className="transition-colors hover:text-black"
>
Vercel AI SDK
</a>
.{" "}
<a
href="https://github.com/steven-tey/chathn"
target="_blank"
rel="noopener noreferrer"
className="transition-colors hover:text-black"
>
View the repo
</a>{" "}
or{" "}
<a
href="https://vercel.com/templates/next.js/chathn"
target="_blank"
rel="noopener noreferrer"
className="transition-colors hover:text-black"
>
deploy your own
</a>
.
</p>
</div>
</main>
);
}