chat.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. import { trimTopic } from "../utils";
  2. import Locale, { getLang } from "../locales";
  3. import { showToast } from "../components/ui-lib";
  4. import { ModelConfig, ModelType, useAppConfig } from "./config";
  5. import { createEmptyMask, Mask } from "./mask";
  6. import {
  7. DEFAULT_INPUT_TEMPLATE,
  8. DEFAULT_SYSTEM_TEMPLATE,
  9. KnowledgeCutOffDate,
  10. StoreKey,
  11. SUMMARIZE_MODEL,
  12. } from "../constant";
  13. import { api, RequestMessage } from "../client/api";
  14. import { ChatControllerPool } from "../client/controller";
  15. import { prettyObject } from "../utils/format";
  16. import { estimateTokenLength } from "../utils/token";
  17. import { nanoid } from "nanoid";
  18. import { createPersistStore } from "../utils/store";
  19. export type ChatMessage = RequestMessage & {
  20. date: string;
  21. streaming?: boolean;
  22. isError?: boolean;
  23. id: string;
  24. model?: ModelType;
  25. };
  26. export function createMessage(override: Partial<ChatMessage>): ChatMessage {
  27. return {
  28. id: nanoid(),
  29. date: new Date().toLocaleString(),
  30. role: "user",
  31. content: "",
  32. ...override,
  33. };
  34. }
  35. export interface ChatStat {
  36. tokenCount: number;
  37. wordCount: number;
  38. charCount: number;
  39. }
  40. export interface ChatSession {
  41. id: string;
  42. topic: string;
  43. memoryPrompt: string;
  44. messages: ChatMessage[];
  45. stat: ChatStat;
  46. lastUpdate: number;
  47. lastSummarizeIndex: number;
  48. clearContextIndex?: number;
  49. mask: Mask;
  50. }
  51. export const DEFAULT_TOPIC = Locale.Store.DefaultTopic;
  52. export const BOT_HELLO: ChatMessage = createMessage({
  53. role: "assistant",
  54. content: Locale.Store.BotHello,
  55. });
  56. function createEmptySession(): ChatSession {
  57. return {
  58. id: nanoid(),
  59. topic: DEFAULT_TOPIC,
  60. memoryPrompt: "",
  61. messages: [],
  62. stat: {
  63. tokenCount: 0,
  64. wordCount: 0,
  65. charCount: 0,
  66. },
  67. lastUpdate: Date.now(),
  68. lastSummarizeIndex: 0,
  69. mask: createEmptyMask(),
  70. };
  71. }
  72. function getSummarizeModel(currentModel: string) {
  73. // if it is using gpt-* models, force to use 3.5 to summarize
  74. return currentModel.startsWith("gpt") ? SUMMARIZE_MODEL : currentModel;
  75. }
  76. function countMessages(msgs: ChatMessage[]) {
  77. return msgs.reduce((pre, cur) => pre + estimateTokenLength(cur.content), 0);
  78. }
  79. function fillTemplateWith(input: string, modelConfig: ModelConfig) {
  80. let cutoff =
  81. KnowledgeCutOffDate[modelConfig.model] ?? KnowledgeCutOffDate.default;
  82. const vars = {
  83. cutoff,
  84. model: modelConfig.model,
  85. time: new Date().toLocaleString(),
  86. lang: getLang(),
  87. input: input,
  88. };
  89. let output = modelConfig.template ?? DEFAULT_INPUT_TEMPLATE;
  90. // must contains {{input}}
  91. const inputVar = "{{input}}";
  92. if (!output.includes(inputVar)) {
  93. output += "\n" + inputVar;
  94. }
  95. Object.entries(vars).forEach(([name, value]) => {
  96. output = output.replaceAll(`{{${name}}}`, value);
  97. });
  98. return output;
  99. }
  100. const DEFAULT_CHAT_STATE = {
  101. sessions: [createEmptySession()],
  102. currentSessionIndex: 0,
  103. };
  104. export const useChatStore = createPersistStore(
  105. DEFAULT_CHAT_STATE,
  106. (set, _get) => {
  107. function get() {
  108. return {
  109. ..._get(),
  110. ...methods,
  111. };
  112. }
  113. const methods = {
  114. clearSessions() {
  115. set(() => ({
  116. sessions: [createEmptySession()],
  117. currentSessionIndex: 0,
  118. }));
  119. },
  120. selectSession(index: number) {
  121. set({
  122. currentSessionIndex: index,
  123. });
  124. },
  125. moveSession(from: number, to: number) {
  126. set((state) => {
  127. const { sessions, currentSessionIndex: oldIndex } = state;
  128. // move the session
  129. const newSessions = [...sessions];
  130. const session = newSessions[from];
  131. newSessions.splice(from, 1);
  132. newSessions.splice(to, 0, session);
  133. // modify current session id
  134. let newIndex = oldIndex === from ? to : oldIndex;
  135. if (oldIndex > from && oldIndex <= to) {
  136. newIndex -= 1;
  137. } else if (oldIndex < from && oldIndex >= to) {
  138. newIndex += 1;
  139. }
  140. return {
  141. currentSessionIndex: newIndex,
  142. sessions: newSessions,
  143. };
  144. });
  145. },
  146. newSession(mask?: Mask) {
  147. const session = createEmptySession();
  148. if (mask) {
  149. const config = useAppConfig.getState();
  150. const globalModelConfig = config.modelConfig;
  151. session.mask = {
  152. ...mask,
  153. modelConfig: {
  154. ...globalModelConfig,
  155. ...mask.modelConfig,
  156. },
  157. };
  158. session.topic = mask.name;
  159. }
  160. set((state) => ({
  161. currentSessionIndex: 0,
  162. sessions: [session].concat(state.sessions),
  163. }));
  164. },
  165. nextSession(delta: number) {
  166. const n = get().sessions.length;
  167. const limit = (x: number) => (x + n) % n;
  168. const i = get().currentSessionIndex;
  169. get().selectSession(limit(i + delta));
  170. },
  171. deleteSession(index: number) {
  172. const deletingLastSession = get().sessions.length === 1;
  173. const deletedSession = get().sessions.at(index);
  174. if (!deletedSession) return;
  175. const sessions = get().sessions.slice();
  176. sessions.splice(index, 1);
  177. const currentIndex = get().currentSessionIndex;
  178. let nextIndex = Math.min(
  179. currentIndex - Number(index < currentIndex),
  180. sessions.length - 1,
  181. );
  182. if (deletingLastSession) {
  183. nextIndex = 0;
  184. sessions.push(createEmptySession());
  185. }
  186. // for undo delete action
  187. const restoreState = {
  188. currentSessionIndex: get().currentSessionIndex,
  189. sessions: get().sessions.slice(),
  190. };
  191. set(() => ({
  192. currentSessionIndex: nextIndex,
  193. sessions,
  194. }));
  195. showToast(
  196. Locale.Home.DeleteToast,
  197. {
  198. text: Locale.Home.Revert,
  199. onClick() {
  200. set(() => restoreState);
  201. },
  202. },
  203. 5000,
  204. );
  205. },
  206. currentSession() {
  207. let index = get().currentSessionIndex;
  208. const sessions = get().sessions;
  209. if (index < 0 || index >= sessions.length) {
  210. index = Math.min(sessions.length - 1, Math.max(0, index));
  211. set(() => ({ currentSessionIndex: index }));
  212. }
  213. return sessions[index];
  214. },
  215. onNewMessage(message: ChatMessage) {
  216. get().updateCurrentSession((session) => {
  217. session.messages = session.messages.concat();
  218. session.lastUpdate = Date.now();
  219. });
  220. get().updateStat(message);
  221. get().summarizeSession();
  222. },
  223. async onUserInput(content: string) {
  224. const session = get().currentSession();
  225. const modelConfig = session.mask.modelConfig;
  226. const userContent = fillTemplateWith(content, modelConfig);
  227. console.log("[User Input] after template: ", userContent);
  228. const userMessage: ChatMessage = createMessage({
  229. role: "user",
  230. content: userContent,
  231. });
  232. const botMessage: ChatMessage = createMessage({
  233. role: "assistant",
  234. streaming: true,
  235. model: modelConfig.model,
  236. });
  237. // get recent messages
  238. const recentMessages = get().getMessagesWithMemory();
  239. const sendMessages = recentMessages.concat(userMessage);
  240. const messageIndex = get().currentSession().messages.length + 1;
  241. // save user's and bot's message
  242. get().updateCurrentSession((session) => {
  243. const savedUserMessage = {
  244. ...userMessage,
  245. content,
  246. };
  247. session.messages = session.messages.concat([
  248. savedUserMessage,
  249. botMessage,
  250. ]);
  251. });
  252. // make request
  253. api.llm.chat({
  254. messages: sendMessages,
  255. config: { ...modelConfig, stream: true },
  256. onUpdate(message) {
  257. botMessage.streaming = true;
  258. if (message) {
  259. botMessage.content = message;
  260. }
  261. get().updateCurrentSession((session) => {
  262. session.messages = session.messages.concat();
  263. });
  264. },
  265. onFinish(message) {
  266. botMessage.streaming = false;
  267. if (message) {
  268. botMessage.content = message;
  269. get().onNewMessage(botMessage);
  270. }
  271. ChatControllerPool.remove(session.id, botMessage.id);
  272. },
  273. onError(error) {
  274. const isAborted = error.message.includes("aborted");
  275. botMessage.content +=
  276. "\n\n" +
  277. prettyObject({
  278. error: true,
  279. message: error.message,
  280. });
  281. botMessage.streaming = false;
  282. userMessage.isError = !isAborted;
  283. botMessage.isError = !isAborted;
  284. get().updateCurrentSession((session) => {
  285. session.messages = session.messages.concat();
  286. });
  287. ChatControllerPool.remove(
  288. session.id,
  289. botMessage.id ?? messageIndex,
  290. );
  291. console.error("[Chat] failed ", error);
  292. },
  293. onController(controller) {
  294. // collect controller for stop/retry
  295. ChatControllerPool.addController(
  296. session.id,
  297. botMessage.id ?? messageIndex,
  298. controller,
  299. );
  300. },
  301. });
  302. },
  303. getMemoryPrompt() {
  304. const session = get().currentSession();
  305. return {
  306. role: "system",
  307. content:
  308. session.memoryPrompt.length > 0
  309. ? Locale.Store.Prompt.History(session.memoryPrompt)
  310. : "",
  311. date: "",
  312. } as ChatMessage;
  313. },
  314. getMessagesWithMemory() {
  315. const session = get().currentSession();
  316. const modelConfig = session.mask.modelConfig;
  317. const clearContextIndex = session.clearContextIndex ?? 0;
  318. const messages = session.messages.slice();
  319. const totalMessageCount = session.messages.length;
  320. // in-context prompts
  321. const contextPrompts = session.mask.context.slice();
  322. // system prompts, to get close to OpenAI Web ChatGPT
  323. const shouldInjectSystemPrompts = modelConfig.enableInjectSystemPrompts;
  324. const systemPrompts = shouldInjectSystemPrompts
  325. ? [
  326. createMessage({
  327. role: "system",
  328. content: fillTemplateWith("", {
  329. ...modelConfig,
  330. template: DEFAULT_SYSTEM_TEMPLATE,
  331. }),
  332. }),
  333. ]
  334. : [];
  335. if (shouldInjectSystemPrompts) {
  336. console.log(
  337. "[Global System Prompt] ",
  338. systemPrompts.at(0)?.content ?? "empty",
  339. );
  340. }
  341. // long term memory
  342. const shouldSendLongTermMemory =
  343. modelConfig.sendMemory &&
  344. session.memoryPrompt &&
  345. session.memoryPrompt.length > 0 &&
  346. session.lastSummarizeIndex > clearContextIndex;
  347. const longTermMemoryPrompts = shouldSendLongTermMemory
  348. ? [get().getMemoryPrompt()]
  349. : [];
  350. const longTermMemoryStartIndex = session.lastSummarizeIndex;
  351. // short term memory
  352. const shortTermMemoryStartIndex = Math.max(
  353. 0,
  354. totalMessageCount - modelConfig.historyMessageCount,
  355. );
  356. // lets concat send messages, including 4 parts:
  357. // 0. system prompt: to get close to OpenAI Web ChatGPT
  358. // 1. long term memory: summarized memory messages
  359. // 2. pre-defined in-context prompts
  360. // 3. short term memory: latest n messages
  361. // 4. newest input message
  362. const memoryStartIndex = shouldSendLongTermMemory
  363. ? Math.min(longTermMemoryStartIndex, shortTermMemoryStartIndex)
  364. : shortTermMemoryStartIndex;
  365. // and if user has cleared history messages, we should exclude the memory too.
  366. const contextStartIndex = Math.max(clearContextIndex, memoryStartIndex);
  367. const maxTokenThreshold = modelConfig.max_tokens;
  368. // get recent messages as much as possible
  369. const reversedRecentMessages = [];
  370. for (
  371. let i = totalMessageCount - 1, tokenCount = 0;
  372. i >= contextStartIndex && tokenCount < maxTokenThreshold;
  373. i -= 1
  374. ) {
  375. const msg = messages[i];
  376. if (!msg || msg.isError) continue;
  377. tokenCount += estimateTokenLength(msg.content);
  378. reversedRecentMessages.push(msg);
  379. }
  380. // concat all messages
  381. const recentMessages = [
  382. ...systemPrompts,
  383. ...longTermMemoryPrompts,
  384. ...contextPrompts,
  385. ...reversedRecentMessages.reverse(),
  386. ];
  387. return recentMessages;
  388. },
  389. updateMessage(
  390. sessionIndex: number,
  391. messageIndex: number,
  392. updater: (message?: ChatMessage) => void,
  393. ) {
  394. const sessions = get().sessions;
  395. const session = sessions.at(sessionIndex);
  396. const messages = session?.messages;
  397. updater(messages?.at(messageIndex));
  398. set(() => ({ sessions }));
  399. },
  400. resetSession() {
  401. get().updateCurrentSession((session) => {
  402. session.messages = [];
  403. session.memoryPrompt = "";
  404. });
  405. },
  406. summarizeSession() {
  407. const config = useAppConfig.getState();
  408. const session = get().currentSession();
  409. // remove error messages if any
  410. const messages = session.messages;
  411. // should summarize topic after chating more than 50 words
  412. const SUMMARIZE_MIN_LEN = 50;
  413. if (
  414. config.enableAutoGenerateTitle &&
  415. session.topic === DEFAULT_TOPIC &&
  416. countMessages(messages) >= SUMMARIZE_MIN_LEN
  417. ) {
  418. const topicMessages = messages.concat(
  419. createMessage({
  420. role: "user",
  421. content: Locale.Store.Prompt.Topic,
  422. }),
  423. );
  424. api.llm.chat({
  425. messages: topicMessages,
  426. config: {
  427. model: getSummarizeModel(session.mask.modelConfig.model),
  428. },
  429. onFinish(message) {
  430. get().updateCurrentSession(
  431. (session) =>
  432. (session.topic =
  433. message.length > 0 ? trimTopic(message) : DEFAULT_TOPIC),
  434. );
  435. },
  436. });
  437. }
  438. const modelConfig = session.mask.modelConfig;
  439. const summarizeIndex = Math.max(
  440. session.lastSummarizeIndex,
  441. session.clearContextIndex ?? 0,
  442. );
  443. let toBeSummarizedMsgs = messages
  444. .filter((msg) => !msg.isError)
  445. .slice(summarizeIndex);
  446. const historyMsgLength = countMessages(toBeSummarizedMsgs);
  447. if (historyMsgLength > modelConfig?.max_tokens ?? 4000) {
  448. const n = toBeSummarizedMsgs.length;
  449. toBeSummarizedMsgs = toBeSummarizedMsgs.slice(
  450. Math.max(0, n - modelConfig.historyMessageCount),
  451. );
  452. }
  453. // add memory prompt
  454. toBeSummarizedMsgs.unshift(get().getMemoryPrompt());
  455. const lastSummarizeIndex = session.messages.length;
  456. console.log(
  457. "[Chat History] ",
  458. toBeSummarizedMsgs,
  459. historyMsgLength,
  460. modelConfig.compressMessageLengthThreshold,
  461. );
  462. if (
  463. historyMsgLength > modelConfig.compressMessageLengthThreshold &&
  464. modelConfig.sendMemory
  465. ) {
  466. api.llm.chat({
  467. messages: toBeSummarizedMsgs.concat(
  468. createMessage({
  469. role: "system",
  470. content: Locale.Store.Prompt.Summarize,
  471. date: "",
  472. }),
  473. ),
  474. config: {
  475. ...modelConfig,
  476. stream: true,
  477. model: getSummarizeModel(session.mask.modelConfig.model),
  478. },
  479. onUpdate(message) {
  480. session.memoryPrompt = message;
  481. },
  482. onFinish(message) {
  483. console.log("[Memory] ", message);
  484. get().updateCurrentSession((session) => {
  485. session.lastSummarizeIndex = lastSummarizeIndex;
  486. session.memoryPrompt = message; // Update the memory prompt for stored it in local storage
  487. });
  488. },
  489. onError(err) {
  490. console.error("[Summarize] ", err);
  491. },
  492. });
  493. }
  494. },
  495. updateStat(message: ChatMessage) {
  496. get().updateCurrentSession((session) => {
  497. session.stat.charCount += message.content.length;
  498. // TODO: should update chat count and word count
  499. });
  500. },
  501. updateCurrentSession(updater: (session: ChatSession) => void) {
  502. const sessions = get().sessions;
  503. const index = get().currentSessionIndex;
  504. updater(sessions[index]);
  505. set(() => ({ sessions }));
  506. },
  507. clearAllData() {
  508. localStorage.clear();
  509. location.reload();
  510. },
  511. };
  512. return methods;
  513. },
  514. {
  515. name: StoreKey.Chat,
  516. version: 3.1,
  517. migrate(persistedState, version) {
  518. const state = persistedState as any;
  519. const newState = JSON.parse(
  520. JSON.stringify(state),
  521. ) as typeof DEFAULT_CHAT_STATE;
  522. if (version < 2) {
  523. newState.sessions = [];
  524. const oldSessions = state.sessions;
  525. for (const oldSession of oldSessions) {
  526. const newSession = createEmptySession();
  527. newSession.topic = oldSession.topic;
  528. newSession.messages = [...oldSession.messages];
  529. newSession.mask.modelConfig.sendMemory = true;
  530. newSession.mask.modelConfig.historyMessageCount = 4;
  531. newSession.mask.modelConfig.compressMessageLengthThreshold = 1000;
  532. newState.sessions.push(newSession);
  533. }
  534. }
  535. if (version < 3) {
  536. // migrate id to nanoid
  537. newState.sessions.forEach((s) => {
  538. s.id = nanoid();
  539. s.messages.forEach((m) => (m.id = nanoid()));
  540. });
  541. }
  542. // Enable `enableInjectSystemPrompts` attribute for old sessions.
  543. // Resolve issue of old sessions not automatically enabling.
  544. if (version < 3.1) {
  545. newState.sessions.forEach((s) => {
  546. if (
  547. // Exclude those already set by user
  548. !s.mask.modelConfig.hasOwnProperty("enableInjectSystemPrompts")
  549. ) {
  550. // Because users may have changed this configuration,
  551. // the user's current configuration is used instead of the default
  552. const config = useAppConfig.getState();
  553. s.mask.modelConfig.enableInjectSystemPrompts =
  554. config.modelConfig.enableInjectSystemPrompts;
  555. }
  556. });
  557. }
  558. return newState as any;
  559. },
  560. },
  561. );