Add GPT-4 support (#25)
* mobile ui updates * fixes sidebar btn * return if null * mobile input blur * handle mobile enter key * new convo name * new delete mechanism * test height * revert * change padding * remove overflow * check relative * padding * done * retry * test * test * should work now * test * test * more * max h * revert * done
This commit is contained in:
parent
9a4824818e
commit
7810a3e7dc
|
@ -1,4 +1,4 @@
|
||||||
import { Message, OpenAIModel, OpenAIModelNames } from "@/types";
|
import { Conversation, Message, OpenAIModel } from "@/types";
|
||||||
import { FC, useEffect, useRef } from "react";
|
import { FC, useEffect, useRef } from "react";
|
||||||
import { ChatInput } from "./ChatInput";
|
import { ChatInput } from "./ChatInput";
|
||||||
import { ChatLoader } from "./ChatLoader";
|
import { ChatLoader } from "./ChatLoader";
|
||||||
|
@ -6,16 +6,16 @@ import { ChatMessage } from "./ChatMessage";
|
||||||
import { ModelSelect } from "./ModelSelect";
|
import { ModelSelect } from "./ModelSelect";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
model: OpenAIModel;
|
conversation: Conversation;
|
||||||
messages: Message[];
|
models: OpenAIModel[];
|
||||||
messageIsStreaming: boolean;
|
messageIsStreaming: boolean;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
lightMode: "light" | "dark";
|
lightMode: "light" | "dark";
|
||||||
onSend: (message: Message) => void;
|
onSend: (message: Message) => void;
|
||||||
onSelect: (model: OpenAIModel) => void;
|
onModelChange: (conversation: Conversation, model: OpenAIModel) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Chat: FC<Props> = ({ model, messages, messageIsStreaming, loading, lightMode, onSend, onSelect }) => {
|
export const Chat: FC<Props> = ({ conversation, models, messageIsStreaming, loading, lightMode, onSend, onModelChange }) => {
|
||||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const scrollToBottom = () => {
|
const scrollToBottom = () => {
|
||||||
|
@ -24,27 +24,28 @@ export const Chat: FC<Props> = ({ model, messages, messageIsStreaming, loading,
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
}, [messages]);
|
}, [conversation.messages]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex-1 overflow-scroll dark:bg-[#343541]">
|
<div className="flex-1 overflow-scroll dark:bg-[#343541]">
|
||||||
<div>
|
<div>
|
||||||
{messages.length === 0 ? (
|
{conversation.messages.length === 0 ? (
|
||||||
<>
|
<>
|
||||||
<div className="flex justify-center pt-8">
|
<div className="flex justify-center pt-8">
|
||||||
<ModelSelect
|
<ModelSelect
|
||||||
model={model}
|
model={conversation.model}
|
||||||
onSelect={onSelect}
|
models={models}
|
||||||
|
onModelChange={(model) => onModelChange(conversation, model)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="text-4xl text-center text-neutral-600 dark:text-neutral-200 pt-[160px] sm:pt-[280px]">Chatbot UI</div>
|
<div className="text-4xl text-center text-neutral-600 dark:text-neutral-200 pt-[160px] sm:pt-[280px]">{loading ? "Loading..." : "Chatbot UI"}</div>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<div className="flex justify-center py-2 text-neutral-500 bg-neutral-100 dark:bg-[#444654] dark:text-neutral-200 text-sm border border-b-neutral-300 dark:border-none">Model: {OpenAIModelNames[model]}</div>
|
<div className="flex justify-center py-2 text-neutral-500 bg-neutral-100 dark:bg-[#444654] dark:text-neutral-200 text-sm border border-b-neutral-300 dark:border-none">Model: {conversation.model.name}</div>
|
||||||
|
|
||||||
{messages.map((message, index) => (
|
{conversation.messages.map((message, index) => (
|
||||||
<ChatMessage
|
<ChatMessage
|
||||||
key={index}
|
key={index}
|
||||||
message={message}
|
message={message}
|
||||||
|
|
|
@ -1,27 +1,30 @@
|
||||||
import { OpenAIModel, OpenAIModelNames } from "@/types";
|
import { OpenAIModel } from "@/types";
|
||||||
import { FC } from "react";
|
import { FC } from "react";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
model: OpenAIModel;
|
model: OpenAIModel;
|
||||||
onSelect: (model: OpenAIModel) => void;
|
models: OpenAIModel[];
|
||||||
|
onModelChange: (model: OpenAIModel) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ModelSelect: FC<Props> = ({ model, onSelect }) => {
|
export const ModelSelect: FC<Props> = ({ model, models, onModelChange }) => {
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col">
|
<div className="flex flex-col">
|
||||||
<label className="text-left mb-2 dark:text-neutral-400 text-neutral-700">Model</label>
|
<label className="text-left mb-2 dark:text-neutral-400 text-neutral-700">Model</label>
|
||||||
<select
|
<select
|
||||||
className="w-[300px] p-3 dark:text-white dark:bg-[#343541] border border-neutral-500 rounded-lg appearance-none focus:shadow-outline text-neutral-900 cursor-pointer"
|
className="w-[300px] p-3 dark:text-white dark:bg-[#343541] border border-neutral-500 rounded-lg appearance-none focus:shadow-outline text-neutral-900 cursor-pointer"
|
||||||
placeholder="Select a model"
|
placeholder="Select a model"
|
||||||
value={model}
|
value={model.id}
|
||||||
onChange={(e) => onSelect(e.target.value as OpenAIModel)}
|
onChange={(e) => {
|
||||||
|
onModelChange(models.find((model) => model.id === e.target.value) as OpenAIModel);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{Object.entries(OpenAIModelNames).map(([value, name]) => (
|
{models.map((model) => (
|
||||||
<option
|
<option
|
||||||
key={value}
|
key={model.id}
|
||||||
value={value}
|
value={model.id}
|
||||||
>
|
>
|
||||||
{name}
|
{model.name}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Message, OpenAIModel } from "@/types";
|
import { Message, OpenAIModel } from "@/types";
|
||||||
import { OpenAIStream } from "@/utils";
|
import { OpenAIStream } from "@/utils/server";
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
runtime: "edge"
|
runtime: "edge"
|
||||||
|
@ -23,7 +23,7 @@ const handler = async (req: Request): Promise<Response> => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
charCount += message.content.length;
|
charCount += message.content.length;
|
||||||
messagesToSend = [message, ...messagesToSend]
|
messagesToSend = [message, ...messagesToSend];
|
||||||
}
|
}
|
||||||
|
|
||||||
const stream = await OpenAIStream(model, key, messagesToSend);
|
const stream = await OpenAIStream(model, key, messagesToSend);
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
import { OpenAIModel, OpenAIModelID, OpenAIModels } from "@/types";
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
runtime: "edge"
|
||||||
|
};
|
||||||
|
|
||||||
|
const handler = async (req: Request): Promise<Response> => {
|
||||||
|
try {
|
||||||
|
const { key } = (await req.json()) as {
|
||||||
|
key: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch("https://api.openai.com/v1/models", {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status !== 200) {
|
||||||
|
throw new Error("OpenAI API returned an error");
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = await response.json();
|
||||||
|
|
||||||
|
const models: OpenAIModel[] = json.data
|
||||||
|
.map((model: any) => {
|
||||||
|
for (const [key, value] of Object.entries(OpenAIModelID)) {
|
||||||
|
if (value === model.id) {
|
||||||
|
return {
|
||||||
|
id: model.id,
|
||||||
|
name: OpenAIModels[value].name
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(models), { status: 200 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return new Response("Error", { status: 500 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default handler;
|
|
@ -1,7 +1,8 @@
|
||||||
import { Chat } from "@/components/Chat/Chat";
|
import { Chat } from "@/components/Chat/Chat";
|
||||||
import { Navbar } from "@/components/Mobile/Navbar";
|
import { Navbar } from "@/components/Mobile/Navbar";
|
||||||
import { Sidebar } from "@/components/Sidebar/Sidebar";
|
import { Sidebar } from "@/components/Sidebar/Sidebar";
|
||||||
import { Conversation, Message, OpenAIModel } from "@/types";
|
import { Conversation, Message, OpenAIModel, OpenAIModelID, OpenAIModels } from "@/types";
|
||||||
|
import { cleanConversationHistory, cleanSelectedConversation } from "@/utils/app";
|
||||||
import { IconArrowBarLeft, IconArrowBarRight } from "@tabler/icons-react";
|
import { IconArrowBarLeft, IconArrowBarRight } from "@tabler/icons-react";
|
||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
@ -10,7 +11,7 @@ export default function Home() {
|
||||||
const [conversations, setConversations] = useState<Conversation[]>([]);
|
const [conversations, setConversations] = useState<Conversation[]>([]);
|
||||||
const [selectedConversation, setSelectedConversation] = useState<Conversation>();
|
const [selectedConversation, setSelectedConversation] = useState<Conversation>();
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const [model, setModel] = useState<OpenAIModel>(OpenAIModel.GPT_3_5);
|
const [models, setModels] = useState<OpenAIModel[]>([]);
|
||||||
const [lightMode, setLightMode] = useState<"dark" | "light">("dark");
|
const [lightMode, setLightMode] = useState<"dark" | "light">("dark");
|
||||||
const [messageIsStreaming, setMessageIsStreaming] = useState<boolean>(false);
|
const [messageIsStreaming, setMessageIsStreaming] = useState<boolean>(false);
|
||||||
const [showSidebar, setShowSidebar] = useState<boolean>(true);
|
const [showSidebar, setShowSidebar] = useState<boolean>(true);
|
||||||
|
@ -33,7 +34,7 @@ export default function Home() {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model,
|
model: updatedConversation.model,
|
||||||
messages: updatedConversation.messages,
|
messages: updatedConversation.messages,
|
||||||
key: apiKey
|
key: apiKey
|
||||||
})
|
})
|
||||||
|
@ -47,6 +48,8 @@ export default function Home() {
|
||||||
const data = response.body;
|
const data = response.body;
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
setLoading(false);
|
||||||
|
setMessageIsStreaming(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,13 +147,35 @@ export default function Home() {
|
||||||
localStorage.setItem("selectedConversation", JSON.stringify(updatedConversation));
|
localStorage.setItem("selectedConversation", JSON.stringify(updatedConversation));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleChangeModel = (conversation: Conversation, model: OpenAIModel) => {
|
||||||
|
const updatedConversation = {
|
||||||
|
...conversation,
|
||||||
|
model
|
||||||
|
};
|
||||||
|
|
||||||
|
const updatedConversations = conversations.map((c) => {
|
||||||
|
if (c.id === updatedConversation.id) {
|
||||||
|
return updatedConversation;
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
|
});
|
||||||
|
|
||||||
|
setConversations(updatedConversations);
|
||||||
|
localStorage.setItem("conversationHistory", JSON.stringify(updatedConversations));
|
||||||
|
|
||||||
|
setSelectedConversation(updatedConversation);
|
||||||
|
localStorage.setItem("selectedConversation", JSON.stringify(updatedConversation));
|
||||||
|
};
|
||||||
|
|
||||||
const handleNewConversation = () => {
|
const handleNewConversation = () => {
|
||||||
const lastConversation = conversations[conversations.length - 1];
|
const lastConversation = conversations[conversations.length - 1];
|
||||||
|
|
||||||
const newConversation: Conversation = {
|
const newConversation: Conversation = {
|
||||||
id: lastConversation ? lastConversation.id + 1 : 1,
|
id: lastConversation ? lastConversation.id + 1 : 1,
|
||||||
name: `Conversation ${lastConversation ? lastConversation.id + 1 : 1}`,
|
name: `Conversation ${lastConversation ? lastConversation.id + 1 : 1}`,
|
||||||
messages: []
|
messages: [],
|
||||||
|
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||||
};
|
};
|
||||||
|
|
||||||
const updatedConversations = [...conversations, newConversation];
|
const updatedConversations = [...conversations, newConversation];
|
||||||
|
@ -160,7 +185,6 @@ export default function Home() {
|
||||||
setSelectedConversation(newConversation);
|
setSelectedConversation(newConversation);
|
||||||
localStorage.setItem("selectedConversation", JSON.stringify(newConversation));
|
localStorage.setItem("selectedConversation", JSON.stringify(newConversation));
|
||||||
|
|
||||||
setModel(OpenAIModel.GPT_3_5);
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -181,7 +205,8 @@ export default function Home() {
|
||||||
setSelectedConversation({
|
setSelectedConversation({
|
||||||
id: 1,
|
id: 1,
|
||||||
name: "New conversation",
|
name: "New conversation",
|
||||||
messages: []
|
messages: [],
|
||||||
|
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||||
});
|
});
|
||||||
localStorage.removeItem("selectedConversation");
|
localStorage.removeItem("selectedConversation");
|
||||||
}
|
}
|
||||||
|
@ -192,6 +217,27 @@ export default function Home() {
|
||||||
localStorage.setItem("apiKey", apiKey);
|
localStorage.setItem("apiKey", apiKey);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fetchModels = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
const response = await fetch("/api/models", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
key: apiKey
|
||||||
|
})
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
setModels(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const theme = localStorage.getItem("theme");
|
const theme = localStorage.getItem("theme");
|
||||||
if (theme) {
|
if (theme) {
|
||||||
|
@ -208,21 +254,27 @@ export default function Home() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const conversationHistory = localStorage.getItem("conversationHistory");
|
const conversationHistory = localStorage.getItem("conversationHistory");
|
||||||
|
|
||||||
if (conversationHistory) {
|
if (conversationHistory) {
|
||||||
setConversations(JSON.parse(conversationHistory));
|
const parsedConversationHistory: Conversation[] = JSON.parse(conversationHistory);
|
||||||
|
const cleanedConversationHistory = cleanConversationHistory(parsedConversationHistory);
|
||||||
|
setConversations(cleanedConversationHistory);
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedConversation = localStorage.getItem("selectedConversation");
|
const selectedConversation = localStorage.getItem("selectedConversation");
|
||||||
if (selectedConversation) {
|
if (selectedConversation) {
|
||||||
setSelectedConversation(JSON.parse(selectedConversation));
|
const parsedSelectedConversation: Conversation = JSON.parse(selectedConversation);
|
||||||
|
const cleanedSelectedConversation = cleanSelectedConversation(parsedSelectedConversation);
|
||||||
|
setSelectedConversation(cleanedSelectedConversation);
|
||||||
} else {
|
} else {
|
||||||
setSelectedConversation({
|
setSelectedConversation({
|
||||||
id: 1,
|
id: 1,
|
||||||
name: "New conversation",
|
name: "New conversation",
|
||||||
messages: []
|
messages: [],
|
||||||
|
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fetchModels();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -242,7 +294,6 @@ export default function Home() {
|
||||||
href="/favicon.ico"
|
href="/favicon.ico"
|
||||||
/>
|
/>
|
||||||
</Head>
|
</Head>
|
||||||
|
|
||||||
{selectedConversation && (
|
{selectedConversation && (
|
||||||
<div className={`flex flex-col h-screen w-screen text-white ${lightMode}`}>
|
<div className={`flex flex-col h-screen w-screen text-white ${lightMode}`}>
|
||||||
<div className="sm:hidden w-full fixed top-0">
|
<div className="sm:hidden w-full fixed top-0">
|
||||||
|
@ -283,13 +334,13 @@ export default function Home() {
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Chat
|
<Chat
|
||||||
|
conversation={selectedConversation}
|
||||||
messageIsStreaming={messageIsStreaming}
|
messageIsStreaming={messageIsStreaming}
|
||||||
model={model}
|
models={models}
|
||||||
messages={selectedConversation.messages}
|
|
||||||
loading={loading}
|
loading={loading}
|
||||||
lightMode={lightMode}
|
lightMode={lightMode}
|
||||||
onSend={handleSend}
|
onSend={handleSend}
|
||||||
onSelect={setModel}
|
onModelChange={handleChangeModel}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,13 +1,22 @@
|
||||||
export enum OpenAIModel {
|
export interface OpenAIModel {
|
||||||
GPT_3_5 = "gpt-3.5-turbo",
|
id: string;
|
||||||
GPT_3_5_LEGACY = "gpt-3.5-turbo-0301"
|
name: string;
|
||||||
// GPT_4 = "gpt-4"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const OpenAIModelNames: Record<OpenAIModel, string> = {
|
export enum OpenAIModelID {
|
||||||
[OpenAIModel.GPT_3_5]: "Default (GPT-3.5)",
|
GPT_3_5 = "gpt-3.5-turbo",
|
||||||
[OpenAIModel.GPT_3_5_LEGACY]: "Legacy (GPT-3.5)"
|
GPT_4 = "gpt-4"
|
||||||
// [OpenAIModel.GPT_4]: "GPT-4"
|
}
|
||||||
|
|
||||||
|
export const OpenAIModels: Record<OpenAIModelID, OpenAIModel> = {
|
||||||
|
[OpenAIModelID.GPT_3_5]: {
|
||||||
|
id: OpenAIModelID.GPT_3_5,
|
||||||
|
name: "Default (GPT-3.5)"
|
||||||
|
},
|
||||||
|
[OpenAIModelID.GPT_4]: {
|
||||||
|
id: OpenAIModelID.GPT_4,
|
||||||
|
name: "GPT-4"
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface Message {
|
export interface Message {
|
||||||
|
@ -21,4 +30,13 @@ export interface Conversation {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
messages: Message[];
|
messages: Message[];
|
||||||
|
model: OpenAIModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
// keep track of local storage schema
|
||||||
|
export interface LocalStorage {
|
||||||
|
apiKey: string;
|
||||||
|
conversationHistory: Conversation[];
|
||||||
|
selectedConversation: Conversation;
|
||||||
|
theme: "light" | "dark";
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { Conversation, OpenAIModelID, OpenAIModels } from "@/types";
|
||||||
|
|
||||||
|
export const cleanConversationHistory = (history: Conversation[]) => {
|
||||||
|
// added model for each conversation (3/20/23)
|
||||||
|
|
||||||
|
if (history.length === 0) {
|
||||||
|
return history;
|
||||||
|
} else {
|
||||||
|
return history.map((conversation) => {
|
||||||
|
if (conversation.model) {
|
||||||
|
return conversation;
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
...conversation,
|
||||||
|
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const cleanSelectedConversation = (conversation: Conversation) => {
|
||||||
|
// added model for each conversation (3/20/23)
|
||||||
|
|
||||||
|
if (conversation.model) {
|
||||||
|
return conversation;
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
...conversation,
|
||||||
|
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
|
@ -12,7 +12,7 @@ export const OpenAIStream = async (model: OpenAIModel, key: string, messages: Me
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model,
|
model: model.id,
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: "system",
|
role: "system",
|
Loading…
Reference in New Issue