NextChat-U/app/components/model-config.tsx

276 lines
8.9 KiB
TypeScript
Raw Normal View History

2024-07-05 20:59:45 +09:00
import { ServiceProvider } from "@/app/constant";
import { ModalConfigValidator, ModelConfig } from "../store";
2023-04-23 02:27:15 +09:00
import Locale from "../locales";
import { InputRange } from "./input-range";
import { ListItem, Select } from "./ui-lib";
import { useAllModels } from "../utils/hooks";
2024-09-14 10:31:05 +09:00
import { groupBy } from "lodash-es";
2024-09-25 15:41:41 +09:00
import styles from "./model-config.module.scss";
import { getModelProvider } from "../utils/model";
2023-04-23 02:27:15 +09:00
export function ModelConfigList(props: {
modelConfig: ModelConfig;
updateConfig: (updater: (config: ModelConfig) => void) => void;
}) {
const allModels = useAllModels();
2024-09-14 10:31:05 +09:00
const groupModels = groupBy(
allModels.filter((v) => v.available),
"provider.providerName",
);
2024-07-05 20:59:45 +09:00
const value = `${props.modelConfig.model}@${props.modelConfig?.providerName}`;
const compressModelValue = `${props.modelConfig.compressModel}@${props.modelConfig?.compressProviderName}`;
2023-04-23 02:27:15 +09:00
return (
2023-04-23 02:37:47 +09:00
<>
2023-04-23 02:27:15 +09:00
<ListItem title={Locale.Settings.Model}>
<Select
2024-08-07 14:01:08 +09:00
aria-label={Locale.Settings.Model}
2024-07-05 20:59:45 +09:00
value={value}
2024-09-14 10:31:05 +09:00
align="left"
2023-04-23 02:27:15 +09:00
onChange={(e) => {
const [model, providerName] = getModelProvider(
e.currentTarget.value,
);
2024-07-05 20:59:45 +09:00
props.updateConfig((config) => {
config.model = ModalConfigValidator.model(model);
config.providerName = providerName as ServiceProvider;
});
2023-04-23 02:27:15 +09:00
}}
>
2024-09-14 10:31:05 +09:00
{Object.keys(groupModels).map((providerName, index) => (
<optgroup label={providerName} key={index}>
{groupModels[providerName]
.filter((v) => !v.name.toLowerCase().includes('tts'))
.map((v, i) => (
<option value={`${v.name}@${v.provider?.providerName}`} key={i}>
{v.displayName}
</option>
))}
2024-09-14 10:31:05 +09:00
</optgroup>
))}
</Select>
2023-04-23 02:27:15 +09:00
</ListItem>
<ListItem
title={Locale.Settings.Temperature.Title}
subTitle={Locale.Settings.Temperature.SubTitle}
>
<InputRange
2024-08-07 14:01:08 +09:00
aria={Locale.Settings.Temperature.Title}
2023-04-23 02:27:15 +09:00
value={props.modelConfig.temperature?.toFixed(1)}
min="0"
2023-04-23 22:54:18 +09:00
max="1" // lets limit it to 0-1
2023-04-23 02:27:15 +09:00
step="0.1"
onChange={(e) => {
props.updateConfig(
(config) =>
(config.temperature = ModalConfigValidator.temperature(
e.currentTarget.valueAsNumber,
)),
);
}}
></InputRange>
</ListItem>
2023-07-04 01:39:54 +09:00
<ListItem
title={Locale.Settings.TopP.Title}
subTitle={Locale.Settings.TopP.SubTitle}
>
<InputRange
2024-08-07 14:01:08 +09:00
aria={Locale.Settings.TopP.Title}
2023-07-04 01:39:54 +09:00
value={(props.modelConfig.top_p ?? 1).toFixed(1)}
min="0"
max="1"
step="0.1"
onChange={(e) => {
props.updateConfig(
(config) =>
2023-07-04 02:49:05 +09:00
(config.top_p = ModalConfigValidator.top_p(
2023-07-04 01:39:54 +09:00
e.currentTarget.valueAsNumber,
)),
);
}}
></InputRange>
</ListItem>
2023-04-23 02:27:15 +09:00
<ListItem
title={Locale.Settings.MaxTokens.Title}
subTitle={Locale.Settings.MaxTokens.SubTitle}
>
<input
2024-08-07 14:01:08 +09:00
aria-label={Locale.Settings.MaxTokens.Title}
2023-04-23 02:27:15 +09:00
type="number"
min={1024}
max={512000}
2023-04-23 02:27:15 +09:00
value={props.modelConfig.max_tokens}
onChange={(e) =>
props.updateConfig(
(config) =>
(config.max_tokens = ModalConfigValidator.max_tokens(
e.currentTarget.valueAsNumber,
)),
)
}
></input>
</ListItem>
2024-07-05 20:59:45 +09:00
{props.modelConfig?.providerName == ServiceProvider.Google ? null : (
2023-12-24 18:24:04 +09:00
<>
<ListItem
title={Locale.Settings.PresencePenalty.Title}
subTitle={Locale.Settings.PresencePenalty.SubTitle}
>
<InputRange
2024-08-07 14:01:08 +09:00
aria={Locale.Settings.PresencePenalty.Title}
2023-12-24 18:24:04 +09:00
value={props.modelConfig.presence_penalty?.toFixed(1)}
min="-2"
max="2"
step="0.1"
onChange={(e) => {
props.updateConfig(
(config) =>
(config.presence_penalty =
ModalConfigValidator.presence_penalty(
e.currentTarget.valueAsNumber,
)),
);
}}
></InputRange>
</ListItem>
2023-12-24 18:24:04 +09:00
<ListItem
title={Locale.Settings.FrequencyPenalty.Title}
subTitle={Locale.Settings.FrequencyPenalty.SubTitle}
>
<InputRange
2024-08-07 14:01:08 +09:00
aria={Locale.Settings.FrequencyPenalty.Title}
2023-12-24 18:24:04 +09:00
value={props.modelConfig.frequency_penalty?.toFixed(1)}
min="-2"
max="2"
step="0.1"
onChange={(e) => {
props.updateConfig(
(config) =>
(config.frequency_penalty =
ModalConfigValidator.frequency_penalty(
e.currentTarget.valueAsNumber,
)),
);
}}
></InputRange>
</ListItem>
2023-12-24 18:24:04 +09:00
<ListItem
title={Locale.Settings.InjectSystemPrompts.Title}
subTitle={Locale.Settings.InjectSystemPrompts.SubTitle}
>
<input
2024-08-07 14:01:08 +09:00
aria-label={Locale.Settings.InjectSystemPrompts.Title}
2023-12-24 18:24:04 +09:00
type="checkbox"
checked={props.modelConfig.enableInjectSystemPrompts}
onChange={(e) =>
props.updateConfig(
(config) =>
(config.enableInjectSystemPrompts =
e.currentTarget.checked),
)
}
></input>
</ListItem>
2023-12-24 18:24:04 +09:00
<ListItem
title={Locale.Settings.InputTemplate.Title}
subTitle={Locale.Settings.InputTemplate.SubTitle}
>
<input
2024-08-07 14:01:08 +09:00
aria-label={Locale.Settings.InputTemplate.Title}
2023-12-24 18:24:04 +09:00
type="text"
value={props.modelConfig.template}
onChange={(e) =>
props.updateConfig(
(config) => (config.template = e.currentTarget.value),
)
}
></input>
</ListItem>
</>
)}
2023-04-23 02:27:15 +09:00
<ListItem
title={Locale.Settings.HistoryCount.Title}
subTitle={Locale.Settings.HistoryCount.SubTitle}
>
<InputRange
2024-08-07 14:01:08 +09:00
aria={Locale.Settings.HistoryCount.Title}
2023-04-23 02:27:15 +09:00
title={props.modelConfig.historyMessageCount.toString()}
value={props.modelConfig.historyMessageCount}
min="0"
max="64"
2023-04-23 02:27:15 +09:00
step="1"
onChange={(e) =>
props.updateConfig(
(config) => (config.historyMessageCount = e.target.valueAsNumber),
)
}
></InputRange>
</ListItem>
<ListItem
title={Locale.Settings.CompressThreshold.Title}
subTitle={Locale.Settings.CompressThreshold.SubTitle}
>
<input
2024-08-07 14:01:08 +09:00
aria-label={Locale.Settings.CompressThreshold.Title}
2023-04-23 02:27:15 +09:00
type="number"
min={500}
max={4000}
value={props.modelConfig.compressMessageLengthThreshold}
onChange={(e) =>
props.updateConfig(
(config) =>
(config.compressMessageLengthThreshold =
e.currentTarget.valueAsNumber),
)
}
></input>
</ListItem>
<ListItem title={Locale.Memory.Title} subTitle={Locale.Memory.Send}>
<input
2024-08-07 14:01:08 +09:00
aria-label={Locale.Memory.Title}
2023-04-23 02:27:15 +09:00
type="checkbox"
checked={props.modelConfig.sendMemory}
onChange={(e) =>
props.updateConfig(
(config) => (config.sendMemory = e.currentTarget.checked),
)
}
></input>
</ListItem>
<ListItem
title={Locale.Settings.CompressModel.Title}
subTitle={Locale.Settings.CompressModel.SubTitle}
>
<Select
2024-09-25 15:41:41 +09:00
className={styles["select-compress-model"]}
aria-label={Locale.Settings.CompressModel.Title}
value={compressModelValue}
onChange={(e) => {
const [model, providerName] = getModelProvider(
e.currentTarget.value,
);
props.updateConfig((config) => {
config.compressModel = ModalConfigValidator.model(model);
config.compressProviderName = providerName as ServiceProvider;
});
}}
>
{allModels
.filter((v) => v.available)
.map((v, i) => (
<option value={`${v.name}@${v.provider?.providerName}`} key={i}>
{v.displayName}({v.provider?.providerName})
</option>
))}
</Select>
</ListItem>
2023-04-23 02:37:47 +09:00
</>
2023-04-23 02:27:15 +09:00
);
}