Przeglądaj źródła

#5406 AI Agents revamp: vector store

Aleksandr Trofimov 7 miesięcy temu
rodzic
commit
dfdd49161a

+ 14 - 0
inc/classes/BxDolAIQuery.php

@@ -798,6 +798,20 @@ class BxDolAIQuery extends BxDolDb
         return $this->getRow($sQuery, ['id' => $iId]); 
         return $this->getRow($sQuery, ['id' => $iId]); 
     }
     }
 
 
+    public function insertVectorStore($aVectorStore)
+    {
+        if(empty($aVectorStore) || !is_array($aVectorStore))
+            return false;
+
+        return (int)$this->query("INSERT INTO `sys_agents_vector_store` SET " . $this->arrayToSQL($aVectorStore));
+    }
+
+    public function getVectorStoreDataNum (int $iVectorStoreId): int
+    {
+        $sQuery = "SELECT COUNT(*) FROM `sys_agents_vector_store_data` WHERE `vector_store_id` = :vector_store_id";
+        return (int)$this->getOne($sQuery, ['vector_store_id' => $iVectorStoreId]);
+    }
+
     public function addVectorStoreData (int $iVectorStoreId, string $sType, string $sName, int $iFileSize, string $sMetadata, string $sSettings, string $sContent): bool
     public function addVectorStoreData (int $iVectorStoreId, string $sType, string $sName, int $iFileSize, string $sMetadata, string $sSettings, string $sContent): bool
     {
     {
         $sQuery = "INSERT INTO `sys_agents_vector_store_data` SET  
         $sQuery = "INSERT INTO `sys_agents_vector_store_data` SET  

+ 25 - 24
install/sql/system.sql

@@ -5782,19 +5782,19 @@ INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `fie
 ('sys_studio_agents_vector_store', 'Sql', 'SELECT * FROM `sys_agents_vector_store` WHERE 1 ', 'sys_agents_vector_store', 'id', 'id', 'active', '', 20, NULL, 'start', '', '', '', 'like', '', '', 128, 1, 1, 'BxTemplStudioAgentsVectorStore', '');
 ('sys_studio_agents_vector_store', 'Sql', 'SELECT * FROM `sys_agents_vector_store` WHERE 1 ', 'sys_agents_vector_store', 'id', 'id', 'active', '', 20, NULL, 'start', '', '', '', 'like', '', '', 128, 1, 1, 'BxTemplStudioAgentsVectorStore', '');
 
 
 INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `translatable`, `chars_limit`, `params`, `hidden_on`, `order`) VALUES
 INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `translatable`, `chars_limit`, `params`, `hidden_on`, `order`) VALUES
-('sys_studio_agents_vector_store', 'checkbox', '', '2%', 0, 0, '', '', 1),
-('sys_studio_agents_vector_store', 'switcher', '_sys_active', '8%', 0, 0, '', '', 2),
-('sys_studio_agents_vector_store', 'title', '_Title', '20%', 0, 0, '', '', 3),
-('sys_studio_agents_vector_store', 'type', '_adm_form_txt_fields_type', '10%', 0, 0, '', '', 5),
-('sys_studio_agents_vector_store', 'topk', '_sys_agents_vector_store_txt_topk', '10%', 0, 0, '', '', 6),
-('sys_studio_agents_vector_store', 'actions', '', '25%', 0, 0, '', '', 9);
+('sys_studio_agents_vector_store', 'switcher', '_sys_active', '10%', 0, 0, '', '', 10),
+('sys_studio_agents_vector_store', 'title', '_Title', '20%', 0, 0, '', '', 20),
+('sys_studio_agents_vector_store', 'type', '_adm_form_txt_fields_type', '10%', 0, 0, '', '', 30),
+('sys_studio_agents_vector_store', 'topk', '_sys_agents_vector_store_txt_topk', '5%', 0, 0, '', '', 40),
+('sys_studio_agents_vector_store', 'files_num', '_sys_agents_vector_store_txt_files_num', '5%', 0, 0, '', '', 50),
+('sys_studio_agents_vector_store', 'actions', '', '25%', 0, 0, '', '', 60);
 
 
 INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `icon`, `icon_only`, `confirm`, `active`, `order`) VALUES
 INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `icon`, `icon_only`, `confirm`, `active`, `order`) VALUES
 ('sys_studio_agents_vector_store', 'single', 'add_data', '_sys_uploader_simple_attach_one_more_file', 'plus', 1, 0, 1, 10),
 ('sys_studio_agents_vector_store', 'single', 'add_data', '_sys_uploader_simple_attach_one_more_file', 'plus', 1, 0, 1, 10),
 ('sys_studio_agents_vector_store', 'single', 'files', '_adm_lmi_cpt_files', 'folder', 1, 0, 1, 20),
 ('sys_studio_agents_vector_store', 'single', 'files', '_adm_lmi_cpt_files', 'folder', 1, 0, 1, 20),
 ('sys_studio_agents_vector_store', 'single', 'edit', '_Edit', 'pencil-alt', 1, 0, 1, 30),
 ('sys_studio_agents_vector_store', 'single', 'edit', '_Edit', 'pencil-alt', 1, 0, 1, 30),
 ('sys_studio_agents_vector_store', 'single', 'delete', '_Delete', 'remove', 1, 1, 1, 40),
 ('sys_studio_agents_vector_store', 'single', 'delete', '_Delete', 'remove', 1, 1, 1, 40),
-('sys_studio_agents_vector_store', 'single', 'duplicate', '_Duplicate', 'copy', 1, 1, 1, 50);
+('sys_studio_agents_vector_store', 'single', 'duplicate', '_Duplicate', 'copy', 1, 0, 1, 50);
 
 
 -- GRID: Agents Vector Store Data
 -- GRID: Agents Vector Store Data
 INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `responsive`, `show_total_count`, `override_class_name`, `override_class_file`) VALUES
 INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `responsive`, `show_total_count`, `override_class_name`, `override_class_file`) VALUES
@@ -7083,7 +7083,7 @@ CREATE TABLE IF NOT EXISTS `sys_agents_vector_store_data` (
   `metadata` text NOT NULL DEFAULT '',
   `metadata` text NOT NULL DEFAULT '',
   `settings` varchar(255) NOT NULL DEFAULT '',
   `settings` varchar(255) NOT NULL DEFAULT '',
   `content` longtext NOT NULL DEFAULT '',
   `content` longtext NOT NULL DEFAULT '',
-  `status` enum('pending', 'processing','ready') NOT NULL DEFAULT 'pending',
+  `status` enum('pending', 'processing','ready') NOT NULL DEFAULT 'pending',  
   `added` int(11) NOT NULL DEFAULT 0,
   `added` int(11) NOT NULL DEFAULT 0,
   PRIMARY KEY (`id`)
   PRIMARY KEY (`id`)
 );
 );
@@ -7095,6 +7095,7 @@ CREATE TABLE IF NOT EXISTS `sys_agents_vector_store` (
   `topk` tinyint(4) DEFAULT 4,
   `topk` tinyint(4) DEFAULT 4,
   `params` text DEFAULT NULL,
   `params` text DEFAULT NULL,
   `params_user` text DEFAULT NULL,
   `params_user` text DEFAULT NULL,
+  `duplicate` tinyint(4) NOT NULL DEFAULT 1,
   `changed` int(11) NOT NULL DEFAULT 0,
   `changed` int(11) NOT NULL DEFAULT 0,
   `active` tinyint(4) NOT NULL DEFAULT 0,
   `active` tinyint(4) NOT NULL DEFAULT 0,
   PRIMARY KEY (`id`)
   PRIMARY KEY (`id`)
@@ -7104,16 +7105,16 @@ CREATE TABLE IF NOT EXISTS `sys_agents_vector_store` (
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
     'folder', 'vectorstore'
     'folder', 'vectorstore'
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('file', 'File', 4, CAST(@j AS CHAR), 0, 1);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('file', 'File', 4, CAST(@j AS CHAR), 0, 0, 1);
 
 
 -- 2. Pinecone
 -- 2. Pinecone
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
     'key', 'PINECONE_API_KEY',
     'key', 'PINECONE_API_KEY',
     'indexUrl', 'PINECONE_INDEX_URL'
     'indexUrl', 'PINECONE_INDEX_URL'
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('pinecone', 'Pinecone', 4, CAST(@j AS CHAR), 0, 0);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('pinecone', 'Pinecone', 4, CAST(@j AS CHAR), 0, 0, 0);
 
 
 -- 3. Elasticsearch
 -- 3. Elasticsearch
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
@@ -7123,8 +7124,8 @@ SET @j = JSON_OBJECT(
     ),
     ),
     'index', 'vectorstore'
     'index', 'vectorstore'
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('elasticsearch', 'Elasticsearch', 4, CAST(@j AS CHAR), 0, 0);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('elasticsearch', 'Elasticsearch', 4, CAST(@j AS CHAR), 0, 0, 0);
 
 
 -- 4. Opensearch
 -- 4. Opensearch
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
@@ -7133,8 +7134,8 @@ SET @j = JSON_OBJECT(
     ),
     ),
     'index', 'vectorstore'
     'index', 'vectorstore'
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('opensearch', 'Opensearch', 4, CAST(@j AS CHAR), 0, 0);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('opensearch', 'Opensearch', 4, CAST(@j AS CHAR), 0, 0, 0);
 
 
 -- 5. Typesense
 -- 5. Typesense
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
@@ -7151,24 +7152,24 @@ SET @j = JSON_OBJECT(
     'collection', 'vectorstore',
     'collection', 'vectorstore',
     'vectorDimension', 1024
     'vectorDimension', 1024
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('typesense', 'Typesense', 4, CAST(@j AS CHAR), 0, 0);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('typesense', 'Typesense', 4, CAST(@j AS CHAR), 0, 0, 0);
 
 
 -- 6. Qdrant
 -- 6. Qdrant
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
     'collectionUrl', 'http://localhost:6333/collections/neuron-ai/',
     'collectionUrl', 'http://localhost:6333/collections/neuron-ai/',
     'key', 'QDRANT_API_KEY'
     'key', 'QDRANT_API_KEY'
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('qdrant', 'Qdrant', 4, CAST(@j AS CHAR), 0, 0);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('qdrant', 'Qdrant', 4, CAST(@j AS CHAR), 0, 0, 0);
 
 
 -- 7. ChromaDB
 -- 7. ChromaDB
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
     'collection', 'vectorstore',
     'collection', 'vectorstore',
     'host', 'http://localhost:8000'
     'host', 'http://localhost:8000'
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('chromadb', 'ChromaDB', 5, CAST(@j AS CHAR), 0, 0);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('chromadb', 'ChromaDB', 5, CAST(@j AS CHAR), 0, 0, 0);
 
 
 -- 8. Meilisearch
 -- 8. Meilisearch
 SET @j = JSON_OBJECT(
 SET @j = JSON_OBJECT(
@@ -7177,8 +7178,8 @@ SET @j = JSON_OBJECT(
     'key', 'MEILISEARCH_API_KEY',
     'key', 'MEILISEARCH_API_KEY',
     'embedder', 'default'
     'embedder', 'default'
 );
 );
-INSERT INTO sys_agents_vector_store (type, title, topk, params, changed, active)
-VALUES ('meilisearch', 'Meilisearch', 5, CAST(@j AS CHAR), 0, 0);
+INSERT INTO sys_agents_vector_store (type, title, topk, params, duplicate, changed, active)
+VALUES ('meilisearch', 'Meilisearch', 5, CAST(@j AS CHAR), 0, 0, 0);
 
 
 -- --------------------------------------------------------
 -- --------------------------------------------------------
 
 

+ 45 - 10
studio/template/scripts/BxBaseStudioAgentsVectorStore.php

@@ -32,6 +32,26 @@ class BxBaseStudioAgentsVectorStore extends BxDolStudioAgentsVectorStore
         return 'oBxDolStudioPageAgents';
         return 'oBxDolStudioPageAgents';
     }
     }
     
     
+    public function performActionDuplicate()
+    {
+        $iId = $this->_getId();
+        $aVectorStore = $this->_oDb->getVectorStoreById($iId);
+        if (!empty($aVectorStore)) {
+            unset($aVectorStore['id']);
+            $aVectorStore['title'] .= ' (Copy)';
+            $aVectorStore['duplicate'] = 1;
+            $aVectorStore['changed'] = time();
+            $aVectorStore['active'] = 0;
+            $iNewId = $this->_oDb->insertVectorStore($aVectorStore);
+            if ($iNewId) {
+                $aRes = ['grid' => $this->getCode(false), 'blink' => $iNewId];
+                return echoJson($aRes);
+            }
+        }
+        $aRes = ['msg' => _t('_sys_txt_error_occured')];
+        return echoJson($aRes);
+    }
+
     public function performActionAddData()
     public function performActionAddData()
     {
     {
         $sAction = 'add_data';
         $sAction = 'add_data';
@@ -102,7 +122,7 @@ class BxBaseStudioAgentsVectorStore extends BxDolStudioAgentsVectorStore
         $oForm->initChecker();
         $oForm->initChecker();
 
 
         if($oForm->isSubmittedAndValid()) {
         if($oForm->isSubmittedAndValid()) {
-            if($oForm->update($iId) === false)
+            if($oForm->update($iId, ['changed' => time()]) === false)
                 return echoJson(['msg' => _t('_sys_txt_error_occured')]);
                 return echoJson(['msg' => _t('_sys_txt_error_occured')]);
 
 
             return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
             return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
@@ -137,15 +157,6 @@ class BxBaseStudioAgentsVectorStore extends BxDolStudioAgentsVectorStore
         return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
         return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
     }
     }
 
 
-    protected function _delete ($mixedId)
-    {
-        $mixedResult = parent::_delete($mixedId);
-        if($mixedResult)
-            $this->_oDb->deleteAutomatorHelpers(['helper_id' => (int)$mixedId]);
-
-        return $mixedResult;
-    }
-
     protected function _getForm($sAction = '', $aVectorStore = [])
     protected function _getForm($sAction = '', $aVectorStore = [])
     {
     {
         $sJsObject = $this->getPageJsObject();
         $sJsObject = $this->getPageJsObject();
@@ -308,6 +319,30 @@ class BxBaseStudioAgentsVectorStore extends BxDolStudioAgentsVectorStore
         $html = new \Html2Text\Html2Text($s);
         $html = new \Html2Text\Html2Text($s);
         return $html->getText();
         return $html->getText();
     }
     }
+
+    protected function _getCellFilesNum($mixedValue, $sKey, $aField, $aRow)
+    {
+        $mixedValue = $this->_oDb->getVectorStoreDataNum($aRow['id']);
+        return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
+    }
+    
+    protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
+    {
+        if ($sType == 'single' && $aRow['duplicate'] == 0)
+            return '';
+        return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
+    }
+
+    protected function _delete ($mixedId)
+    {
+        $aModel = $this->_oDb->getVectorStoreById($mixedId);
+        if (empty($aModel) || $aModel['duplicate'] == 0)
+            return false;
+
+        // TODO: delete actual vector store data if needed
+
+        return parent::_delete($mixedId);
+    }
 }
 }
 
 
 /** @} */
 /** @} */