2023-04-21 16:12:07 +00:00
|
|
|
import { create } from "zustand";
|
|
|
|
import { persist } from "zustand/middleware";
|
2023-06-15 14:34:50 +00:00
|
|
|
import { getClientConfig } from "../config/client";
|
2023-04-26 18:00:22 +00:00
|
|
|
import { StoreKey } from "../constant";
|
2023-04-21 16:12:07 +00:00
|
|
|
|
|
|
|
export enum SubmitKey {
|
|
|
|
Enter = "Enter",
|
|
|
|
CtrlEnter = "Ctrl + Enter",
|
|
|
|
ShiftEnter = "Shift + Enter",
|
|
|
|
AltEnter = "Alt + Enter",
|
|
|
|
MetaEnter = "Meta + Enter",
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum Theme {
|
|
|
|
Auto = "auto",
|
|
|
|
Dark = "dark",
|
|
|
|
Light = "light",
|
|
|
|
}
|
|
|
|
|
2023-04-22 17:37:47 +00:00
|
|
|
export const DEFAULT_CONFIG = {
|
2023-04-21 16:12:07 +00:00
|
|
|
submitKey: SubmitKey.CtrlEnter as SubmitKey,
|
|
|
|
avatar: "1f603",
|
|
|
|
fontSize: 14,
|
|
|
|
theme: Theme.Auto as Theme,
|
2023-06-15 14:34:50 +00:00
|
|
|
tightBorder: !!getClientConfig()?.isApp,
|
2023-04-21 16:12:07 +00:00
|
|
|
sendPreviewBubble: true,
|
|
|
|
sidebarWidth: 300,
|
|
|
|
|
|
|
|
disablePromptHint: false,
|
|
|
|
|
2023-04-27 16:34:37 +00:00
|
|
|
dontShowMaskSplashScreen: false, // dont show splash screen when create chat
|
|
|
|
|
2023-04-21 16:12:07 +00:00
|
|
|
modelConfig: {
|
|
|
|
model: "gpt-3.5-turbo" as ModelType,
|
2023-05-08 16:39:00 +00:00
|
|
|
temperature: 0.5,
|
2023-04-21 16:12:07 +00:00
|
|
|
max_tokens: 2000,
|
|
|
|
presence_penalty: 0,
|
2023-04-22 17:27:15 +00:00
|
|
|
sendMemory: true,
|
2023-04-21 17:13:23 +00:00
|
|
|
historyMessageCount: 4,
|
|
|
|
compressMessageLengthThreshold: 1000,
|
2023-04-21 16:12:07 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ChatConfig = typeof DEFAULT_CONFIG;
|
|
|
|
|
|
|
|
export type ChatConfigStore = ChatConfig & {
|
|
|
|
reset: () => void;
|
|
|
|
update: (updater: (config: ChatConfig) => void) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ModelConfig = ChatConfig["modelConfig"];
|
|
|
|
|
|
|
|
const ENABLE_GPT4 = true;
|
|
|
|
|
|
|
|
export const ALL_MODELS = [
|
|
|
|
{
|
|
|
|
name: "gpt-4",
|
|
|
|
available: ENABLE_GPT4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gpt-4-0314",
|
|
|
|
available: ENABLE_GPT4,
|
|
|
|
},
|
2023-06-13 17:51:37 +00:00
|
|
|
{
|
|
|
|
name: "gpt-4-0613",
|
|
|
|
available: ENABLE_GPT4,
|
|
|
|
},
|
2023-04-21 16:12:07 +00:00
|
|
|
{
|
|
|
|
name: "gpt-4-32k",
|
|
|
|
available: ENABLE_GPT4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gpt-4-32k-0314",
|
|
|
|
available: ENABLE_GPT4,
|
|
|
|
},
|
2023-06-13 17:51:37 +00:00
|
|
|
{
|
|
|
|
name: "gpt-4-32k-0613",
|
|
|
|
available: ENABLE_GPT4,
|
|
|
|
},
|
2023-04-21 16:12:07 +00:00
|
|
|
{
|
|
|
|
name: "gpt-3.5-turbo",
|
|
|
|
available: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gpt-3.5-turbo-0301",
|
|
|
|
available: true,
|
|
|
|
},
|
2023-06-13 17:51:37 +00:00
|
|
|
{
|
|
|
|
name: "gpt-3.5-turbo-0613",
|
|
|
|
available: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gpt-3.5-turbo-16k",
|
|
|
|
available: true,
|
|
|
|
},
|
2023-06-14 15:22:59 +00:00
|
|
|
{
|
|
|
|
name: "gpt-3.5-turbo-16k-0613",
|
|
|
|
available: true,
|
|
|
|
},
|
2023-05-09 15:20:03 +00:00
|
|
|
{
|
|
|
|
name: "qwen-v1", // 通义千问
|
|
|
|
available: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ernie", // 文心一言
|
|
|
|
available: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "spark", // 讯飞星火
|
|
|
|
available: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "llama", // llama
|
|
|
|
available: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "chatglm", // chatglm-6b
|
|
|
|
available: false,
|
|
|
|
},
|
2023-04-21 16:12:07 +00:00
|
|
|
] as const;
|
|
|
|
|
2023-05-20 15:49:10 +00:00
|
|
|
export type ModelType = (typeof ALL_MODELS)[number]["name"];
|
2023-04-21 16:12:07 +00:00
|
|
|
|
|
|
|
export function limitNumber(
|
|
|
|
x: number,
|
|
|
|
min: number,
|
|
|
|
max: number,
|
2023-05-20 15:49:10 +00:00
|
|
|
defaultValue: number,
|
2023-04-21 16:12:07 +00:00
|
|
|
) {
|
|
|
|
if (typeof x !== "number" || isNaN(x)) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.min(max, Math.max(min, x));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function limitModel(name: string) {
|
|
|
|
return ALL_MODELS.some((m) => m.name === name && m.available)
|
|
|
|
? name
|
2023-06-13 17:51:37 +00:00
|
|
|
: "gpt-3.5-turbo";
|
2023-04-21 16:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const ModalConfigValidator = {
|
|
|
|
model(x: string) {
|
|
|
|
return limitModel(x) as ModelType;
|
|
|
|
},
|
|
|
|
max_tokens(x: number) {
|
|
|
|
return limitNumber(x, 0, 32000, 2000);
|
|
|
|
},
|
|
|
|
presence_penalty(x: number) {
|
|
|
|
return limitNumber(x, -2, 2, 0);
|
|
|
|
},
|
|
|
|
temperature(x: number) {
|
2023-04-23 13:54:18 +00:00
|
|
|
return limitNumber(x, 0, 1, 1);
|
2023-04-21 16:12:07 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useAppConfig = create<ChatConfigStore>()(
|
|
|
|
persist(
|
|
|
|
(set, get) => ({
|
|
|
|
...DEFAULT_CONFIG,
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
set(() => ({ ...DEFAULT_CONFIG }));
|
|
|
|
},
|
|
|
|
|
|
|
|
update(updater) {
|
|
|
|
const config = { ...get() };
|
|
|
|
updater(config);
|
|
|
|
set(() => config);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
{
|
2023-04-26 18:00:22 +00:00
|
|
|
name: StoreKey.Config,
|
|
|
|
version: 2,
|
|
|
|
migrate(persistedState, version) {
|
|
|
|
if (version === 2) return persistedState as any;
|
|
|
|
|
|
|
|
const state = persistedState as ChatConfig;
|
|
|
|
state.modelConfig.sendMemory = true;
|
|
|
|
state.modelConfig.historyMessageCount = 4;
|
|
|
|
state.modelConfig.compressMessageLengthThreshold = 1000;
|
2023-04-27 16:34:37 +00:00
|
|
|
state.dontShowMaskSplashScreen = false;
|
2023-04-26 18:00:22 +00:00
|
|
|
|
|
|
|
return state;
|
|
|
|
},
|
2023-05-20 15:49:10 +00:00
|
|
|
},
|
|
|
|
),
|
2023-04-21 16:12:07 +00:00
|
|
|
);
|