Skip to content

Commit 89b59fd

Browse files
authored
Fix custom preset selection (LAION-AI#2511)
The old implementation will not work if the custom preset have the same config as one of the predefined presets. The predefined preset will take president over the custom making the inputs is disabled. I also fix the config cache bug.
1 parent bc37940 commit 89b59fd

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

website/src/components/Chat/ChatConfigDrawer.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from "@chakra-ui/react";
2626
import { Settings } from "lucide-react";
2727
import { useTranslation } from "next-i18next";
28-
import { ChangeEvent, memo, useCallback } from "react";
28+
import { ChangeEvent, memo, useCallback, useState } from "react";
2929
import { Controller, useFormContext, useWatch } from "react-hook-form";
3030
import { ChatConfigFormData, SamplingParameters } from "src/types/Chat";
3131

@@ -118,7 +118,10 @@ const ChatConfigForm = () => {
118118
const { control, register, reset, getValues } = useFormContext<ChatConfigFormData>();
119119
const selectedModel = useWatch({ name: "model_config_name", control: control });
120120
const presets = modelInfos.find((model) => model.name === selectedModel)!.parameter_configs;
121-
121+
const [selectedPresetName, setSelectedPresetName] = useState(
122+
() =>
123+
presets.find((preset) => areParametersEqual(preset.sampling_parameters, getValues()))?.name ?? customPresetName
124+
);
122125
const handlePresetChange = useCallback(
123126
(e: ChangeEvent<HTMLSelectElement>) => {
124127
const newPresetName = e.target.value;
@@ -127,13 +130,11 @@ const ChatConfigForm = () => {
127130
? customPresetDefaultValue
128131
: presets.find((preset) => preset.name === newPresetName)!.sampling_parameters;
129132
reset({ ...config, model_config_name: selectedModel });
133+
setSelectedPresetName(newPresetName);
130134
},
131135
[presets, reset, selectedModel]
132136
);
133137

134-
const selectedPresetName =
135-
presets.find((preset) => areParametersEqual(preset.sampling_parameters, getValues()))?.name ?? customPresetName;
136-
137138
return (
138139
<Stack gap="4">
139140
<FormControl>
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { useEffect } from "react";
22
import { useFormContext } from "react-hook-form";
3-
import { useCacheConfig } from "src/hooks/chat/useCacheConfig";
43
import { ChatConfigFormData } from "src/types/Chat";
4+
import { setConfigCache } from "src/utils/chat";
55

66
export const ChatConfigSaver = () => {
77
const { watch } = useFormContext<ChatConfigFormData>();
8-
const newValues = watch();
9-
const [oldValues, setConfig] = useCacheConfig();
8+
const config = watch();
109
useEffect(() => {
11-
// have to compare otherwise it will cause maximum update depth exceeded
12-
if (JSON.stringify(oldValues) !== JSON.stringify(newValues)) {
13-
setConfig(newValues);
14-
}
15-
}, [setConfig, newValues, oldValues]);
10+
setConfigCache(config);
11+
}, [config]);
1612

1713
return null;
1814
};

website/src/components/Chat/ChatConfigSummary.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Grid, Text } from "@chakra-ui/react";
22
import { useTranslation } from "next-i18next";
33
import React from "react";
4-
import { useCacheConfig } from "src/hooks/chat/useCacheConfig";
4+
import { useFormContext } from "react-hook-form";
5+
import { ChatConfigFormData } from "src/types/Chat";
56

67
export default function ChatConfigSummary() {
7-
const [config] = useCacheConfig();
88
const { t } = useTranslation("chat");
9-
9+
const { watch } = useFormContext<ChatConfigFormData>();
10+
const config = watch();
1011
return (
1112
<Grid gridTemplateColumns="repeat(2, max-content)" columnGap={4} fontSize="sm">
1213
<Text>{t("model")}</Text>

website/src/components/Chat/ChatSection.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Card, CardBody, Divider } from "@chakra-ui/react";
22
import dynamic from "next/dynamic";
33
import { FormProvider, useForm } from "react-hook-form";
4-
import { useCacheConfig } from "src/hooks/chat/useCacheConfig";
54
import { ChatConfigFormData } from "src/types/Chat";
5+
import { getConfigCache } from "src/utils/chat";
66

77
import { ChatConfigSaver } from "./ChatConfigSaver";
88
import { useChatContext } from "./ChatContext";
@@ -15,10 +15,12 @@ export const ChatSection = ({ chatId }: { chatId: string | null }) => {
1515
const { modelInfos } = useChatContext();
1616

1717
console.assert(modelInfos.length > 0, "No model config was found");
18-
const [config] = useCacheConfig();
1918

2019
const form = useForm<ChatConfigFormData>({
21-
defaultValues: config,
20+
defaultValues: getConfigCache() ?? {
21+
...modelInfos[0].parameter_configs[0].sampling_parameters,
22+
model_config_name: modelInfos[0].name,
23+
},
2224
});
2325

2426
return (

website/src/hooks/chat/useCacheConfig.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

website/src/utils/chat.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ChatConfigFormData } from "src/types/Chat";
2+
3+
const CHAT_CONFIG_KEY = "CHAT_CONFIG";
4+
5+
export const setConfigCache = (config: ChatConfigFormData) => {
6+
if (typeof localStorage !== "undefined") {
7+
localStorage.setItem(CHAT_CONFIG_KEY, JSON.stringify(config));
8+
}
9+
};
10+
11+
export const getConfigCache = () => {
12+
if (typeof localStorage !== "undefined") {
13+
const oldConfig = localStorage.getItem(CHAT_CONFIG_KEY);
14+
if (oldConfig) {
15+
return JSON.parse(oldConfig);
16+
}
17+
return null;
18+
}
19+
return null;
20+
};

0 commit comments

Comments
 (0)