config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { LLMModel } from "../client/api";
  2. import { isMacOS } from "../utils";
  3. import { getClientConfig } from "../config/client";
  4. import {
  5. DEFAULT_INPUT_TEMPLATE,
  6. DEFAULT_MODELS,
  7. DEFAULT_SIDEBAR_WIDTH,
  8. StoreKey,
  9. } from "../constant";
  10. import { createPersistStore } from "../utils/store";
  11. export type ModelType = (typeof DEFAULT_MODELS)[number]["name"];
  12. export enum SubmitKey {
  13. Enter = "Enter",
  14. CtrlEnter = "Ctrl + Enter",
  15. ShiftEnter = "Shift + Enter",
  16. AltEnter = "Alt + Enter",
  17. MetaEnter = "Meta + Enter",
  18. }
  19. export enum Theme {
  20. Auto = "auto",
  21. Dark = "dark",
  22. Light = "light",
  23. }
  24. export const DEFAULT_CONFIG = {
  25. lastUpdate: Date.now(), // timestamp, to merge state
  26. submitKey: isMacOS() ? SubmitKey.MetaEnter : SubmitKey.CtrlEnter,
  27. avatar: "1f603",
  28. fontSize: 14,
  29. theme: Theme.Auto as Theme,
  30. tightBorder: !!getClientConfig()?.isApp,
  31. sendPreviewBubble: true,
  32. enableAutoGenerateTitle: true,
  33. sidebarWidth: DEFAULT_SIDEBAR_WIDTH,
  34. disablePromptHint: false,
  35. dontShowMaskSplashScreen: false, // dont show splash screen when create chat
  36. hideBuiltinMasks: false, // dont add builtin masks
  37. customModels: "",
  38. models: DEFAULT_MODELS as any as LLMModel[],
  39. modelConfig: {
  40. model: "gpt-3.5-turbo" as ModelType,
  41. temperature: 0.5,
  42. top_p: 1,
  43. max_tokens: 2000,
  44. presence_penalty: 0,
  45. frequency_penalty: 0,
  46. sendMemory: true,
  47. historyMessageCount: 2,
  48. compressMessageLengthThreshold: 1000,
  49. enableInjectSystemPrompts: true,
  50. template: DEFAULT_INPUT_TEMPLATE,
  51. },
  52. };
  53. export type ChatConfig = typeof DEFAULT_CONFIG;
  54. export type ModelConfig = ChatConfig["modelConfig"];
  55. export function limitNumber(
  56. x: number,
  57. min: number,
  58. max: number,
  59. defaultValue: number,
  60. ) {
  61. if (isNaN(x)) {
  62. return defaultValue;
  63. }
  64. return Math.min(max, Math.max(min, x));
  65. }
  66. export const ModalConfigValidator = {
  67. model(x: string) {
  68. return x as ModelType;
  69. },
  70. max_tokens(x: number) {
  71. return limitNumber(x, 0, 512000, 1024);
  72. },
  73. presence_penalty(x: number) {
  74. return limitNumber(x, -2, 2, 0);
  75. },
  76. frequency_penalty(x: number) {
  77. return limitNumber(x, -2, 2, 0);
  78. },
  79. temperature(x: number) {
  80. return limitNumber(x, 0, 1, 1);
  81. },
  82. top_p(x: number) {
  83. return limitNumber(x, 0, 1, 1);
  84. },
  85. };
  86. export const useAppConfig = createPersistStore(
  87. { ...DEFAULT_CONFIG },
  88. (set, get) => ({
  89. reset() {
  90. set(() => ({ ...DEFAULT_CONFIG }));
  91. },
  92. mergeModels(newModels: LLMModel[]) {
  93. if (!newModels || newModels.length === 0) {
  94. return;
  95. }
  96. const oldModels = get().models;
  97. const modelMap: Record<string, LLMModel> = {};
  98. for (const model of oldModels) {
  99. model.available = false;
  100. modelMap[model.name] = model;
  101. }
  102. for (const model of newModels) {
  103. model.available = true;
  104. modelMap[model.name] = model;
  105. }
  106. set(() => ({
  107. models: Object.values(modelMap),
  108. }));
  109. },
  110. allModels() {},
  111. }),
  112. {
  113. name: StoreKey.Config,
  114. version: 3.8,
  115. migrate(persistedState, version) {
  116. const state = persistedState as ChatConfig;
  117. if (version < 3.4) {
  118. state.modelConfig.sendMemory = true;
  119. state.modelConfig.historyMessageCount = 4;
  120. state.modelConfig.compressMessageLengthThreshold = 1000;
  121. state.modelConfig.frequency_penalty = 0;
  122. state.modelConfig.top_p = 1;
  123. state.modelConfig.template = DEFAULT_INPUT_TEMPLATE;
  124. state.dontShowMaskSplashScreen = false;
  125. state.hideBuiltinMasks = false;
  126. }
  127. if (version < 3.5) {
  128. state.customModels = "claude,claude-100k";
  129. }
  130. if (version < 3.6) {
  131. state.modelConfig.enableInjectSystemPrompts = true;
  132. }
  133. if (version < 3.7) {
  134. state.enableAutoGenerateTitle = true;
  135. }
  136. if (version < 3.8) {
  137. state.lastUpdate = Date.now();
  138. }
  139. return state as any;
  140. },
  141. },
  142. );