From 4cf99df3fdc275e8c8b3cafa81db92d763d2839d Mon Sep 17 00:00:00 2001 From: DanPGill Date: Sun, 2 Jun 2024 15:43:58 +0100 Subject: [PATCH] Chat api key tweaks (#271) --- apps/www/src/components/Chat.tsx | 72 +++++++++++++++++++++----------- apps/www/src/pages/api/chat.ts | 2 +- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/apps/www/src/components/Chat.tsx b/apps/www/src/components/Chat.tsx index 8f4ddb62..16710eb7 100644 --- a/apps/www/src/components/Chat.tsx +++ b/apps/www/src/components/Chat.tsx @@ -65,6 +65,7 @@ export const Chat = () => { const [selectedChatGptModel, setSelectedChatGptModel] = React.useState(CHAT_GPT_MODELS[0]); const [systemMessage, setSystemMessage] = React.useState(""); + const [missingApiKey, setMissingApiKey] = React.useState(false); const [error, setError] = React.useState(); React.useEffect(() => { @@ -110,14 +111,23 @@ export const Chat = () => { }; const handleUpdateChatGptModel = (value: string) => { + setMissingApiKey(false); setSelectedChatGptModel(value); }; const onSubmit = (e: React.FormEvent) => { - setError(undefined); - storage?.setItem(CHAT_OPENAI_API_KEY, currentApiKey); - scrollToBottom(); - handleSubmit(e as unknown as React.FormEvent); + if ( + currentApiKey.length === 0 && + selectedChatGptModel != CHAT_GPT_MODELS[0] + ) { + setMissingApiKey(true); + } else { + setError(undefined); + setMissingApiKey(false); + storage?.setItem(CHAT_OPENAI_API_KEY, currentApiKey); + scrollToBottom(); + handleSubmit(e as unknown as React.FormEvent); + } }; const messagesWithoutSystem = messages.slice(1); @@ -143,27 +153,39 @@ export const Chat = () => { - { - if (!((e.ctrlKey || e.metaKey) && e.key === "v")) { - e.preventDefault(); - } - }} - onFocus={(e) => { - e.currentTarget.select(); - }} - autoComplete="off" - className="focus-within:border-white" - placeholder="Paste Your API Key" - onChange={handleUpdateApiKey} - onDragStart={(e) => e.preventDefault()} - onDragOver={(e) => e.preventDefault()} - onMouseDown={(e) => { - e.preventDefault(); - e.currentTarget.focus(); - }} - /> + {selectedChatGptModel !== CHAT_GPT_MODELS[0] && ( + <> + { + if (!((e.ctrlKey || e.metaKey) && e.key === "v")) { + e.preventDefault(); + } + }} + onFocus={(e) => { + e.currentTarget.select(); + }} + autoComplete="off" + className={cn( + "focus-within:border-white", + missingApiKey && "border-destructive", + )} + placeholder="Paste Your API Key" + onChange={handleUpdateApiKey} + onDragStart={(e) => e.preventDefault()} + onDragOver={(e) => e.preventDefault()} + onMouseDown={(e) => { + e.preventDefault(); + e.currentTarget.focus(); + }} + /> + {missingApiKey && ( + + )} + + )}
{/* Col-reverse is used to enable automatic scrolling as content populates the div */} diff --git a/apps/www/src/pages/api/chat.ts b/apps/www/src/pages/api/chat.ts index b96e563e..b7414f9b 100644 --- a/apps/www/src/pages/api/chat.ts +++ b/apps/www/src/pages/api/chat.ts @@ -14,7 +14,7 @@ const chatRequestSchema = z.object({ }); const getApiKey = (apiKey: string, model: string) => { - if (model === "gpt-3.5-turbo" && apiKey.length === 0) { + if (model === "gpt-3.5-turbo") { return process.env.OPENAI_API_KEY; } return apiKey;