-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathpage.js
More file actions
355 lines (317 loc) · 9.82 KB
/
Copy pathpage.js
File metadata and controls
355 lines (317 loc) · 9.82 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"use client";
import { useEffect, useReducer, useRef, useState } from "react";
import ChatForm from "./components/ChatForm";
import Message from "./components/Message";
import SlideOver from "./components/SlideOver";
import EmptyState from "./components/EmptyState";
import { Cog6ToothIcon, CodeBracketIcon } from "@heroicons/react/20/solid";
import { useCompletion } from "ai/react";
import { Toaster, toast } from "react-hot-toast";
import { countTokens } from "./src/tokenizer.js";
const MODELS = [
{
id: "meta/llama-2-7b-chat",
name: "Llama 2 7B",
shortened: "7B",
},
{
id: "meta/llama-2-13b-chat",
name: "Llama 2 13B",
shortened: "13B",
},
{
id: "meta/llama-2-70b-chat",
name: "Llama 2 70B",
shortened: "70B",
},
{
id: "yorickvp/llava-13b",
name: "Llava 13B",
shortened: "Llava",
},
{
id: "nateraw/salmonn",
name: "Salmonn",
shortened: "Salmonn",
},
];
function CTA({ shortenedModelName }) {
if (shortenedModelName == "Llava") {
return (
<a
href="https://replicate.com/blog/run-llama-2-with-an-api?utm_source=project&utm_campaign=llama2ai"
target="_blank"
className="underline"
>
Run and fine-tune Llava in the cloud.
</a>
);
} else if (shortenedModelName == "Salmonn") {
return (
<a
href="https://replicate.com/blog/run-llama-2-with-an-api?utm_source=project&utm_campaign=llama2ai"
target="_blank"
className="underline"
>
Run and fine-tune Salmonn in the cloud.
</a>
);
} else {
return (
<a
href="https://replicate.com/blog/run-llama-2-with-an-api?utm_source=project&utm_campaign=llama2ai"
target="_blank"
className="underline"
>
Run and fine-tune Llama 2 in the cloud.
</a>
);
}
}
const metricsReducer = (state, action) => {
switch (action.type) {
case 'START':
return { startedAt: new Date() };
case 'FIRST_MESSAGE':
return { ...state, firstMessageAt: new Date() };
case 'COMPLETE':
return { ...state, completedAt: new Date() };
default:
throw new Error(`Unsupported action type: ${action.type}`);
}
};
export default function HomePage() {
const MAX_TOKENS = 4096;
const bottomRef = useRef(null);
const [messages, setMessages] = useState([]);
const [open, setOpen] = useState(false);
const [error, setError] = useState(null);
// Llama params
const [model, setModel] = useState(MODELS[2]); // default to 70B
const [systemPrompt, setSystemPrompt] = useState(
"You are a helpful assistant."
);
const [temp, setTemp] = useState(0.75);
const [topP, setTopP] = useState(0.9);
const [maxTokens, setMaxTokens] = useState(800);
// Llava params
const [image, setImage] = useState(null);
// Salmonn params
const [audio, setAudio] = useState(null);
const [metrics, dispatch] = useReducer(metricsReducer, {
startedAt: null,
firstMessageAt: null,
completedAt: null,
});
const { complete, completion, setInput, input } = useCompletion({
api: "/api",
body: {
model: model.id,
systemPrompt: systemPrompt,
temperature: parseFloat(temp),
topP: parseFloat(topP),
maxTokens: parseInt(maxTokens),
image: image,
audio: audio,
},
onError: (error) => {
setError(error);
},
onResponse: (response) => {
setError(null);
dispatch({ type: 'FIRST_MESSAGE' });
},
onFinish: () => {
dispatch({ type: 'COMPLETE' });
}
});
const handleFileUpload = (file) => {
if (file) {
console.log(file);
// determine if file is image or audio
if (
["audio/mpeg", "audio/wav", "audio/ogg"].includes(
file.originalFile.mime
)
) {
setAudio(file.fileUrl);
setModel(MODELS[4]);
toast.success(
"You uploaded an audio file, so you're now speaking with Salmonn."
);
} else if (["image/jpeg", "image/png"].includes(file.originalFile.mime)) {
setImage(file.fileUrl);
setModel(MODELS[3]);
toast.success(
"You uploaded an image, so you're now speaking with Llava."
);
} else {
toast.error(
`Sorry, we don't support that file type (${file.originalFile.mime}) yet. Feel free to push a PR to add support for it!`
);
}
}
};
const setAndSubmitPrompt = (newPrompt) => {
handleSubmit(newPrompt);
};
const handleSettingsSubmit = async (event) => {
event.preventDefault();
setOpen(false);
setSystemPrompt(event.target.systemPrompt.value);
};
const handleSubmit = async (userMessage) => {
const SNIP = "<!-- snip -->";
const messageHistory = [...messages];
if (completion.length > 0) {
messageHistory.push({
text: completion,
isUser: false,
});
}
messageHistory.push({
text: userMessage,
isUser: true,
});
const generatePrompt = (messages) => {
return messages
.map((message) =>
message.isUser ? `[INST] ${message.text} [/INST]` : `${message.text}`
)
.join("\n");
};
// Generate initial prompt and calculate tokens
let prompt = `${generatePrompt(messageHistory)}\n`;
// Check if we exceed max tokens and truncate the message history if so.
while (countTokens(prompt) > MAX_TOKENS) {
if (messageHistory.length < 3) {
setError(
"Your message is too long. Please try again with a shorter message."
);
return;
}
// Remove the third message from history, keeping the original exchange.
messageHistory.splice(1, 2);
// Recreate the prompt
prompt = `${SNIP}\n${generatePrompt(messageHistory)}\n`;
}
setMessages(messageHistory);
dispatch({ type: 'START' });
complete(prompt);
};
useEffect(() => {
if (!localStorage.getItem("toastShown")) {
toast.success(
"We just updated our 7B model — it's super fast. Try it out!"
);
localStorage.setItem("toastShown", "true");
}
}, []);
useEffect(() => {
if (messages?.length > 0 || completion?.length > 0) {
bottomRef.current.scrollIntoView({ behavior: "smooth" });
}
}, [messages, completion]);
return (
<>
<div className="bg-slate-100 border-b-2 text-center p-3">
Powered by Replicate. <CTA shortenedModelName={model.shortened} />
</div>
<nav className="grid grid-cols-2 pt-3 pl-6 pr-3 sm:grid-cols-3 sm:pl-0">
<div className="hidden sm:inline-block"></div>
<div className="font-semibold text-gray-500 sm:text-center">
{model.shortened == "Llava"
? "🌋"
: model.shortened == "Salmonn"
? "🐟"
: "🦙"}{" "}
<span className="hidden sm:inline-block">Chat with</span>{" "}
<button
className="py-2 font-semibold text-gray-500 hover:underline"
onClick={() => setOpen(true)}
>
{model.shortened == "Llava" || model.shortened == "Salmonn"
? model.shortened
: "Llama 2 " + model.shortened}
</button>
</div>
<div className="flex justify-end">
<a
className="inline-flex items-center px-3 py-2 mr-3 text-sm font-semibold text-gray-700 bg-white rounded-md shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
href="https://github.com/replicate/chat"
>
<CodeBracketIcon
className="w-5 h-5 text-gray-500 sm:mr-2 group-hover:text-gray-900"
aria-hidden="true"
/>{" "}
<span className="hidden sm:inline">Clone on GitHub</span>
</a>
<button
type="button"
className="inline-flex items-center px-3 py-2 text-sm font-semibold text-gray-900 bg-white rounded-md shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
onClick={() => setOpen(true)}
>
<Cog6ToothIcon
className="w-5 h-5 text-gray-500 sm:mr-2 group-hover:text-gray-900"
aria-hidden="true"
/>{" "}
<span className="hidden sm:inline">Settings</span>
</button>
</div>
</nav>
<Toaster position="top-left" reverseOrder={false} />
<main className="max-w-2xl pb-5 mx-auto mt-4 sm:px-4">
<div className="text-center"></div>
{messages.length == 0 && !image && !audio && (
<EmptyState setPrompt={setAndSubmitPrompt} setOpen={setOpen} />
)}
<SlideOver
open={open}
setOpen={setOpen}
systemPrompt={systemPrompt}
setSystemPrompt={setSystemPrompt}
handleSubmit={handleSettingsSubmit}
temp={temp}
setTemp={setTemp}
maxTokens={maxTokens}
setMaxTokens={setMaxTokens}
topP={topP}
setTopP={setTopP}
models={MODELS}
size={model}
setSize={setModel}
/>
{image && (
<div>
<img src={image} className="mt-6 sm:rounded-xl" />
</div>
)}
{audio && (
<div>
<audio controls src={audio} className="mt-6 sm:rounded-xl" />
</div>
)}
<ChatForm
prompt={input}
setPrompt={setInput}
onSubmit={handleSubmit}
handleFileUpload={handleFileUpload}
completion={completion}
metrics={metrics}
/>
{error && <div>{error}</div>}
<article className="pb-36">
{messages.map((message, index) => (
<Message
key={`message-${index}`}
message={message.text}
isUser={message.isUser}
/>
))}
<Message message={completion} isUser={false} />
<div ref={bottomRef} />
</article>
</main>
</>
);
}