From 98d7266291c9765cd9d50f5c02eaa87dfc57852d Mon Sep 17 00:00:00 2001
From: AntonioCiolino <antonio.ciolino@gmail.com>
Date: Wed, 28 Jun 2023 15:53:26 -0400
Subject: [PATCH] History per workspace (#126)

* Enable saving history value per workspace

* added history

* added history

* Changes re: the PR for message limits from history
---
 .../Modals/MangeWorkspace/Settings/index.jsx  | 34 +++++++++++++++++++
 server/models/workspace.js                    | 11 ++++--
 server/utils/chats/index.js                   |  4 ++-
 3 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx b/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx
index c35df7634..77d131625 100644
--- a/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx
+++ b/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx
@@ -140,6 +140,40 @@ export default function WorkspaceSettings({ workspace }) {
                 onChange={() => setHasChanges(true)}
               />
             </div>
+
+            <div>
+              <div className="flex flex-col gap-y-1 mb-4">
+                <label
+                  htmlFor="name"
+                  className="block text-sm font-medium text-gray-900 dark:text-white"
+                >
+                  Chat History
+                </label>
+                <p className="text-xs text-gray-600 dark:text-stone-400">
+                  Chat history: The number of previous chats that 
+                  will be included in the response's short-term memory.
+                  <br />
+                  Recommend 20. Anything more than 45 is likely to lead to 
+                  continuous chat failures depending on message size.
+                  <br />
+                  Recommended: 20
+                </p>
+              </div>
+              <input
+                name="openAiHistory"
+                type="number"
+                min={1}
+                max={45}
+                step={1}
+                onWheel={(e) => e.target.blur()}
+                defaultValue={workspace?.openAiHistory ?? 20}
+                className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-stone-600 dark:border-stone-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
+                placeholder="20"
+                required={true}
+                autoComplete="off"
+                onChange={() => setHasChanges(true)}
+              />
+            </div>
           </div>
 
           {error && (
diff --git a/server/models/workspace.js b/server/models/workspace.js
index 09c3712ce..8abd7b7c6 100644
--- a/server/models/workspace.js
+++ b/server/models/workspace.js
@@ -10,6 +10,7 @@ const Workspace = {
     "slug",
     "vectorTag",
     "openAiTemp",
+    "openAiHistory",
     "lastUpdatedAt",
   ],
   colsInit: `
@@ -19,6 +20,7 @@ const Workspace = {
   vectorTag TEXT DEFAULT NULL,
   createdAt TEXT DEFAULT CURRENT_TIMESTAMP,
   openAiTemp REAL DEFAULT NULL,
+  openAiHistory INTEGER DEFAULT 20,
   lastUpdatedAt TEXT DEFAULT CURRENT_TIMESTAMP
   `,
   migrateTable: async function () {
@@ -42,6 +44,11 @@ const Workspace = {
                                  END`,
         doif: true,
       },
+      {
+        colName: "openAiHistory",
+        execCmd: `ALTER TABLE ${this.tablename} ADD COLUMN openAiHistory INTEGER DEFAULT 20`,
+        doif: false,
+      },
     ];
   },
   db: async function (tracing = true) {
@@ -106,7 +113,7 @@ const Workspace = {
     const values = Object.values(data);
     if (validKeys.length === 0 || validKeys.length !== values.length)
       return { workspace: { id }, message: "No valid fields to update!" };
-
+      
     const template = `UPDATE ${this.tablename} SET ${validKeys.map((key) => {
       return `${key}=?`;
     })} WHERE id = ?`;
@@ -120,7 +127,7 @@ const Workspace = {
         return { success: false, message: error.message };
       });
 
-    db.close();
+      db.close();
     if (!success) {
       return { workspace: null, message };
     }
diff --git a/server/utils/chats/index.js b/server/utils/chats/index.js
index 7b2cc4847..58381cb56 100644
--- a/server/utils/chats/index.js
+++ b/server/utils/chats/index.js
@@ -104,7 +104,9 @@ async function chatWithWorkspace(workspace, message, chatMode = "chat") {
       error: null,
     };
   } else {
-    const rawHistory = await WorkspaceChats.forWorkspace(workspace.id, 20);
+    var messageLimit = workspace?.openAiHistory;
+    
+    const rawHistory = await WorkspaceChats.forWorkspace(workspace.id, messageLimit);
     const chatHistory = convertToPromptHistory(rawHistory);
     const {
       response,
-- 
GitLab