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

Fix saving model between chats #2843

Merged
merged 4 commits into from
Apr 25, 2023
Merged
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
33 changes: 22 additions & 11 deletions website/src/components/Chat/ChatConfigForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
Tooltip,
} from "@chakra-ui/react";
import { useTranslation } from "next-i18next";
import { ChangeEvent, memo, useCallback, useState } from "react";
import { Controller, useFormContext, useWatch } from "react-hook-form";
import { ChatConfigFormData, SamplingParameters } from "src/types/Chat";
import { ChangeEvent, memo, useCallback, useEffect, useState } from "react";
import { Controller, useFormContext } from "react-hook-form";
import { ChatConfigFormData, ModelParameterConfig, SamplingParameters } from "src/types/Chat";

import { useChatContext } from "./ChatContext";
import { areParametersEqual } from "./WorkParameters";
Expand Down Expand Up @@ -81,30 +81,41 @@ const parameterLabel: Record<keyof SamplingParameters, string> = {
typical_p: "Typical P",
};

const findPresetName = (presets: ModelParameterConfig[], config: SamplingParameters) => {
return presets.find((preset) => areParametersEqual(preset.sampling_parameters, config))?.name ?? customPresetName;
};

export const ChatConfigForm = memo(function ChatConfigForm() {
const { t } = useTranslation("chat");
const { modelInfos } = useChatContext();

const { control, register, reset, getValues } = useFormContext<ChatConfigFormData>();
const selectedModel = useWatch({ name: "model_config_name", control: control });
const { control, getValues, register, setValue } = useFormContext<ChatConfigFormData>();
const selectedModel = getValues("model_config_name"); // have to use getValues to here to access latest value
const presets = modelInfos.find((model) => model.name === selectedModel)!.parameter_configs;
const [selectedPresetName, setSelectedPresetName] = useState(
() =>
presets.find((preset) => areParametersEqual(preset.sampling_parameters, getValues()))?.name ?? customPresetName
);
const [selectedPresetName, setSelectedPresetName] = useState(() => findPresetName(presets, getValues()));

const handlePresetChange = useCallback(
(e: ChangeEvent<HTMLSelectElement>) => {
const newPresetName = e.target.value;
const config =
newPresetName === customPresetName
? customPresetDefaultValue
: presets.find((preset) => preset.name === newPresetName)!.sampling_parameters;
reset({ ...config, model_config_name: selectedModel });

for (const [key, value] of Object.entries(config) as Array<[keyof SamplingParameters, number]>) {
setValue(key, value, { shouldDirty: true }); // force dirty so the ChatConfigSaver will update the cache
}
setSelectedPresetName(newPresetName);
},
[presets, reset, selectedModel]
[presets, setValue]
);

const config = getValues(); // have to use getValues to here to access latest value

useEffect(() => {
setSelectedPresetName(findPresetName(presets, config as SamplingParameters));
}, [config, presets]);

return (
<Stack gap="4">
<FormControl>
Expand Down
1 change: 0 additions & 1 deletion website/src/components/Chat/ChatConfigSaver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const ChatConfigSaver = () => {
const { watch, formState } = useFormContext<ChatConfigFormData>();
const config = watch();
useEffect(() => {
// only update when form is changed
if (formState.isDirty) {
setConfigCache(config);
}
Expand Down
1 change: 1 addition & 0 deletions website/src/components/Chat/ChatSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const ChatSection = ({ chatId }: { chatId: string | null }) => {
const cache = getConfigCache();
const model = modelInfos.find((model) => model.name === cache?.model_config_name);
if (model && cache) {
console.log(`resseting form`);
form.reset(cache);
}
}, [form.reset, modelInfos]);
Expand Down