Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: model selection error handling #73

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/components/modals/SettingsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MIXPANEL_TOKEN } from "../../main";
import { checkModelAccess } from "../../utils/apikey";
import { getFluxNodeTypeDarkColor } from "../../utils/color";
import { DEFAULT_SETTINGS, SUPPORTED_MODELS } from "../../utils/constants";
import { DEFAULT_SETTINGS, SUPPORTED_MODELS, TOAST_CONFIG } from "../../utils/constants";
import { Settings, FluxNodeType } from "../../utils/types";
import { APIKeyInput } from "../utils/APIKeyInput";
import { LabeledSelect, LabeledSlider } from "../utils/LabeledInputs";
Expand All @@ -15,6 +16,7 @@ import {
ModalHeader,
ModalOverlay,
Checkbox,
useToast,
} from "@chakra-ui/react";
import mixpanel from "mixpanel-browser";
import { ChangeEvent, memo } from "react";
Expand All @@ -34,6 +36,8 @@ export const SettingsModal = memo(function SettingsModal({
apiKey: string | null;
setApiKey: (apiKey: string) => void;
}) {
const toast = useToast();

const reset = () => {
if (
confirm(
Expand Down Expand Up @@ -68,6 +72,29 @@ export const SettingsModal = memo(function SettingsModal({
}
};

const handleChangeModel = async (v: string) => {
if (await checkModelAccess(v)) {
Copy link
Author

@nmandal nmandal May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a loading state with the <Spinner /> when calling handleChangeModel? User experience feels less snappy when selecting new model with small delay.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm if we could do with a small diff that might be nice but its probably fine as is imo

setSettings({ ...settings, model: v });
} else {
let errorText = "";
if (v === "gpt-4") {
errorText = "You don't have access to GPT-4, sign up for the waitlist at https://openai.com/waitlist/gpt-4-api";
} else if (v === "gpt-4-32k") {
errorText = "You don't have access to GPT-4 32k.";
} else {
errorText = "Something went wrong.";
}

toast({
title: errorText,
status: "error",
...TOAST_CONFIG,
});
}

if (MIXPANEL_TOKEN) mixpanel.track("Changed model");
};

return (
<Modal isOpen={isOpen} onClose={onClose} size="2xl">
<ModalOverlay />
Expand All @@ -79,11 +106,7 @@ export const SettingsModal = memo(function SettingsModal({
label="Model"
value={settings.model}
options={SUPPORTED_MODELS}
setValue={(v: string) => {
setSettings({ ...settings, model: v });

if (MIXPANEL_TOKEN) mixpanel.track("Changed model");
}}
setValue={handleChangeModel}
/>

<APIKeyInput mt={4} width="100%" apiKey={apiKey} setApiKey={setApiKey} />
Expand Down Expand Up @@ -145,4 +168,4 @@ export const SettingsModal = memo(function SettingsModal({
</ModalContent>
</Modal>
);
});
});
26 changes: 26 additions & 0 deletions src/utils/apikey.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
import { API_KEY_LOCAL_STORAGE_KEY } from "./constants";

export function isValidAPIKey(apiKey: string | null) {
return apiKey?.length == 51 && apiKey?.startsWith("sk-");
}

export async function checkModelAccess(model: string): Promise<boolean> {
try {
const response = await fetch("https://api.openai.com/v1/models", {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY_LOCAL_STORAGE_KEY}`,
},
});

if (response.ok) {
const data = await response.json();
const models = data.data;
return models.some((m: { id: string }) => m.id === model);
} else {
console.error("Error fetching models:", response.status);
return false;
}
} catch (error) {
console.error("Error fetching models:", error);
return false;
}
}