Skip to content

Commit 4cf99df

Browse files
authored
Chat api key tweaks (#271)
1 parent 6dea1c3 commit 4cf99df

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

apps/www/src/components/Chat.tsx

+47-25
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const Chat = () => {
6565
const [selectedChatGptModel, setSelectedChatGptModel] =
6666
React.useState<string>(CHAT_GPT_MODELS[0]);
6767
const [systemMessage, setSystemMessage] = React.useState<string>("");
68+
const [missingApiKey, setMissingApiKey] = React.useState<boolean>(false);
6869
const [error, setError] = React.useState<Error>();
6970

7071
React.useEffect(() => {
@@ -110,14 +111,23 @@ export const Chat = () => {
110111
};
111112

112113
const handleUpdateChatGptModel = (value: string) => {
114+
setMissingApiKey(false);
113115
setSelectedChatGptModel(value);
114116
};
115117

116118
const onSubmit = (e: React.FormEvent) => {
117-
setError(undefined);
118-
storage?.setItem(CHAT_OPENAI_API_KEY, currentApiKey);
119-
scrollToBottom();
120-
handleSubmit(e as unknown as React.FormEvent<HTMLFormElement>);
119+
if (
120+
currentApiKey.length === 0 &&
121+
selectedChatGptModel != CHAT_GPT_MODELS[0]
122+
) {
123+
setMissingApiKey(true);
124+
} else {
125+
setError(undefined);
126+
setMissingApiKey(false);
127+
storage?.setItem(CHAT_OPENAI_API_KEY, currentApiKey);
128+
scrollToBottom();
129+
handleSubmit(e as unknown as React.FormEvent<HTMLFormElement>);
130+
}
121131
};
122132

123133
const messagesWithoutSystem = messages.slice(1);
@@ -143,27 +153,39 @@ export const Chat = () => {
143153
</SelectGroup>
144154
</SelectContent>
145155
</Select>
146-
<Input
147-
value={getMaskedKey(currentApiKey)}
148-
onKeyDown={(e) => {
149-
if (!((e.ctrlKey || e.metaKey) && e.key === "v")) {
150-
e.preventDefault();
151-
}
152-
}}
153-
onFocus={(e) => {
154-
e.currentTarget.select();
155-
}}
156-
autoComplete="off"
157-
className="focus-within:border-white"
158-
placeholder="Paste Your API Key"
159-
onChange={handleUpdateApiKey}
160-
onDragStart={(e) => e.preventDefault()}
161-
onDragOver={(e) => e.preventDefault()}
162-
onMouseDown={(e) => {
163-
e.preventDefault();
164-
e.currentTarget.focus();
165-
}}
166-
/>
156+
{selectedChatGptModel !== CHAT_GPT_MODELS[0] && (
157+
<>
158+
<Input
159+
value={getMaskedKey(currentApiKey)}
160+
onKeyDown={(e) => {
161+
if (!((e.ctrlKey || e.metaKey) && e.key === "v")) {
162+
e.preventDefault();
163+
}
164+
}}
165+
onFocus={(e) => {
166+
e.currentTarget.select();
167+
}}
168+
autoComplete="off"
169+
className={cn(
170+
"focus-within:border-white",
171+
missingApiKey && "border-destructive",
172+
)}
173+
placeholder="Paste Your API Key"
174+
onChange={handleUpdateApiKey}
175+
onDragStart={(e) => e.preventDefault()}
176+
onDragOver={(e) => e.preventDefault()}
177+
onMouseDown={(e) => {
178+
e.preventDefault();
179+
e.currentTarget.focus();
180+
}}
181+
/>
182+
{missingApiKey && (
183+
<label className="w-full text-center mt-2 text-destructive">
184+
Please paste an API key
185+
</label>
186+
)}
187+
</>
188+
)}
167189
</div>
168190
<div className="flex flex-col flex-1 overflow-y-hidden">
169191
{/* Col-reverse is used to enable automatic scrolling as content populates the div */}

apps/www/src/pages/api/chat.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const chatRequestSchema = z.object({
1414
});
1515

1616
const getApiKey = (apiKey: string, model: string) => {
17-
if (model === "gpt-3.5-turbo" && apiKey.length === 0) {
17+
if (model === "gpt-3.5-turbo") {
1818
return process.env.OPENAI_API_KEY;
1919
}
2020
return apiKey;

0 commit comments

Comments
 (0)