config.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { create } from "zustand";
  2. import { persist } from "zustand/middleware";
  3. import { StoreKey } from "../constant";
  4. export enum SubmitKey {
  5. Enter = "Enter",
  6. CtrlEnter = "Ctrl + Enter",
  7. ShiftEnter = "Shift + Enter",
  8. AltEnter = "Alt + Enter",
  9. MetaEnter = "Meta + Enter",
  10. }
  11. export enum Theme {
  12. Auto = "auto",
  13. Dark = "dark",
  14. Light = "light",
  15. }
  16. export const DEFAULT_CONFIG = {
  17. submitKey: SubmitKey.CtrlEnter as SubmitKey,
  18. avatar: "1f603",
  19. fontSize: 14,
  20. theme: Theme.Auto as Theme,
  21. tightBorder: false,
  22. sendPreviewBubble: true,
  23. sidebarWidth: 300,
  24. disablePromptHint: false,
  25. dontShowMaskSplashScreen: false, // dont show splash screen when create chat
  26. modelConfig: {
  27. model: "gpt-3.5-turbo" as ModelType,
  28. temperature: 0.5,
  29. max_tokens: 2000,
  30. presence_penalty: 0,
  31. sendMemory: true,
  32. historyMessageCount: 4,
  33. compressMessageLengthThreshold: 1000,
  34. },
  35. };
  36. export type ChatConfig = typeof DEFAULT_CONFIG;
  37. export type ChatConfigStore = ChatConfig & {
  38. reset: () => void;
  39. update: (updater: (config: ChatConfig) => void) => void;
  40. };
  41. export type ModelConfig = ChatConfig["modelConfig"];
  42. const ENABLE_GPT4 = true;
  43. export const ALL_MODELS = [
  44. {
  45. name: "gpt-4",
  46. available: ENABLE_GPT4,
  47. },
  48. {
  49. name: "gpt-4-0314",
  50. available: ENABLE_GPT4,
  51. },
  52. {
  53. name: "gpt-4-32k",
  54. available: ENABLE_GPT4,
  55. },
  56. {
  57. name: "gpt-4-32k-0314",
  58. available: ENABLE_GPT4,
  59. },
  60. {
  61. name: "gpt-3.5-turbo",
  62. available: true,
  63. },
  64. {
  65. name: "gpt-3.5-turbo-0301",
  66. available: true,
  67. },
  68. {
  69. name: "qwen-v1", // 通义千问
  70. available: false,
  71. },
  72. {
  73. name: "ernie", // 文心一言
  74. available: false,
  75. },
  76. {
  77. name: "spark", // 讯飞星火
  78. available: false,
  79. },
  80. {
  81. name: "llama", // llama
  82. available: false,
  83. },
  84. {
  85. name: "chatglm", // chatglm-6b
  86. available: false,
  87. },
  88. ] as const;
  89. export type ModelType = (typeof ALL_MODELS)[number]["name"];
  90. export function limitNumber(
  91. x: number,
  92. min: number,
  93. max: number,
  94. defaultValue: number,
  95. ) {
  96. if (typeof x !== "number" || isNaN(x)) {
  97. return defaultValue;
  98. }
  99. return Math.min(max, Math.max(min, x));
  100. }
  101. export function limitModel(name: string) {
  102. return ALL_MODELS.some((m) => m.name === name && m.available)
  103. ? name
  104. : ALL_MODELS[4].name;
  105. }
  106. export const ModalConfigValidator = {
  107. model(x: string) {
  108. return limitModel(x) as ModelType;
  109. },
  110. max_tokens(x: number) {
  111. return limitNumber(x, 0, 32000, 2000);
  112. },
  113. presence_penalty(x: number) {
  114. return limitNumber(x, -2, 2, 0);
  115. },
  116. temperature(x: number) {
  117. return limitNumber(x, 0, 1, 1);
  118. },
  119. };
  120. export const useAppConfig = create<ChatConfigStore>()(
  121. persist(
  122. (set, get) => ({
  123. ...DEFAULT_CONFIG,
  124. reset() {
  125. set(() => ({ ...DEFAULT_CONFIG }));
  126. },
  127. update(updater) {
  128. const config = { ...get() };
  129. updater(config);
  130. set(() => config);
  131. },
  132. }),
  133. {
  134. name: StoreKey.Config,
  135. version: 2,
  136. migrate(persistedState, version) {
  137. if (version === 2) return persistedState as any;
  138. const state = persistedState as ChatConfig;
  139. state.modelConfig.sendMemory = true;
  140. state.modelConfig.historyMessageCount = 4;
  141. state.modelConfig.compressMessageLengthThreshold = 1000;
  142. state.dontShowMaskSplashScreen = false;
  143. return state;
  144. },
  145. },
  146. ),
  147. );