BxBaseStudioAgentsVectorStore.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioAgentsVectorStore extends BxDolStudioAgentsVectorStore
  11. {
  12. protected $_sUrlPage;
  13. protected $_sFieldName;
  14. protected $_aDataExt = [
  15. 'txt' => 'getFileContentPlain',
  16. 'md' => 'getFileContentPlain',
  17. 'html' => 'getFileContentHtml',
  18. ];
  19. public function __construct ($aOptions, $oTemplate = false)
  20. {
  21. parent::__construct ($aOptions, $oTemplate);
  22. $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=vector_store';
  23. $this->_sFieldName = 'name';
  24. }
  25. public function getPageJsObject()
  26. {
  27. return 'oBxDolStudioPageAgents';
  28. }
  29. public function performActionDuplicate()
  30. {
  31. $iId = $this->_getId();
  32. $aVectorStore = $this->_oDb->getVectorStoreById($iId);
  33. if (!empty($aVectorStore)) {
  34. unset($aVectorStore['id']);
  35. $aVectorStore['title'] .= ' (Copy)';
  36. $aVectorStore['duplicate'] = 1;
  37. $aVectorStore['changed'] = time();
  38. $aVectorStore['active'] = 0;
  39. $iNewId = $this->_oDb->insertVectorStore($aVectorStore);
  40. if ($iNewId) {
  41. $aRes = ['grid' => $this->getCode(false), 'blink' => $iNewId];
  42. return echoJson($aRes);
  43. }
  44. }
  45. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  46. return echoJson($aRes);
  47. }
  48. public function performActionAddData()
  49. {
  50. $sAction = 'add_data';
  51. $iId = $this->_getId();
  52. $aVectorStore = $this->_oDb->getVectorStoreById($iId);
  53. $aForm = $this->_getFormAddData($sAction, $aVectorStore);
  54. $oForm = new BxTemplFormView($aForm);
  55. $oForm->initChecker();
  56. if($oForm->isSubmittedAndValid()) {
  57. $iFilesAdded = 0;
  58. foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
  59. $sFileName = $_FILES['files']['name'][$key];
  60. $sFileType = $_FILES['files']['type'][$key];
  61. $sFileSize = $_FILES['files']['size'][$key];
  62. $sFileTmpName = $_FILES['files']['tmp_name'][$key];
  63. $sMetadata = $oForm->getCleanValue('metadata');
  64. $iVectorStoreId = $oForm->getCleanValue('vector_store_id');
  65. $sSettings = json_encode([
  66. 'chunk_size' => $oForm->getCleanValue('chunk_size'),
  67. 'delimeter' => trim($oForm->getCleanValue('delimeter')),
  68. 'overlap' => $oForm->getCleanValue('overlap'),
  69. ]);
  70. $sExt = pathinfo($sFileName, PATHINFO_EXTENSION);
  71. $sMethod = isset($this->_aDataExt[$sExt]) ? $this->_aDataExt[$sExt] : 'getFileContentPlain';
  72. $sContent = $this->$sMethod($sFileTmpName);
  73. if (empty($sContent)) {
  74. continue;
  75. }
  76. $bRet = $this->_oDb->addVectorStoreData ($iVectorStoreId, 'custom', $sFileName, (int)$sFileSize, $sMetadata, $sSettings, $sContent);
  77. if ($bRet) {
  78. $iFilesAdded++;
  79. }
  80. }
  81. return echoJson(['msg' => _t('_sys_agents_vector_store_data_queued', $iFilesAdded) . ' / num:' . $iFilesAdded]);
  82. if($oForm->add($iId) === false)
  83. return echoJson(['msg' => _t('_sys_txt_error_occured')]);
  84. return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  85. }
  86. $sFormId = $oForm->getId();
  87. $sForm = $oForm->getCode(true);
  88. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_vector_store_popup_add_data'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  89. 'form_id' => $sFormId,
  90. 'form' => $sForm,
  91. 'object' => $this->_sObject,
  92. 'action' => $sAction
  93. ]));
  94. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  95. }
  96. public function performActionEdit()
  97. {
  98. $sAction = 'edit';
  99. $iId = $this->_getId();
  100. $aVectorStore = $this->_oDb->getVectorStoreById($iId);
  101. $aForm = $this->_getFormEdit($sAction, $aVectorStore);
  102. $oForm = new BxTemplFormView($aForm);
  103. $oForm->initChecker();
  104. if($oForm->isSubmittedAndValid()) {
  105. if($oForm->update($iId, ['changed' => time()]) === false)
  106. return echoJson(['msg' => _t('_sys_txt_error_occured')]);
  107. return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  108. }
  109. $sFormId = $oForm->getId();
  110. $sForm = $oForm->getCode(true);
  111. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_vector_store_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  112. 'form_id' => $sFormId,
  113. 'form' => $sForm,
  114. 'object' => $this->_sObject,
  115. 'action' => $sAction
  116. ]));
  117. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  118. }
  119. public function performActionFiles()
  120. {
  121. $iId = $this->_getId();
  122. $aVectorStore = $this->_oDb->getVectorStoreById($iId);
  123. $oGrid = BxDolGrid::getObjectInstance('sys_studio_agents_vector_store_data');
  124. $oGrid->addMarkers(['vector_store_id' => $iId]);
  125. $oGrid->setBrowseParams(['vector_store_id' => $iId]);
  126. $sGrid = $oGrid->getCode();
  127. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('popup_files_' . $iId, _t('_sys_agents_vector_store_popup_files'), $this->_oTemplate->parseHtmlByName('agents_popup_grid.html', [
  128. 'grid' => $sGrid,
  129. ]));
  130. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  131. }
  132. protected function _getForm($sAction = '', $aVectorStore = [])
  133. {
  134. $sJsObject = $this->getPageJsObject();
  135. if (empty($aVectorStore['params_user'])) {
  136. $aVectorStore['params_user'] = $aVectorStore['params'];
  137. }
  138. $data = json_decode($aVectorStore['params_user'], true);
  139. $aVectorStore['params_user'] = json_encode($data, JSON_PRETTY_PRINT);
  140. return [
  141. 'form_attrs' => [
  142. 'id' => 'bx_std_agents_helpers_' . $sAction,
  143. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_vector_store&a=' . $sAction,
  144. 'method' => 'post',
  145. ],
  146. 'params' => array (
  147. 'db' => array(
  148. 'table' => 'sys_agents_vector_store',
  149. 'key' => 'id',
  150. 'submit_name' => 'do_submit',
  151. ),
  152. ),
  153. 'inputs' => [
  154. 'topk' => [
  155. 'type' => 'text',
  156. 'name' => 'topk',
  157. 'caption' => '_sys_agents_vector_store_field_topk',
  158. 'required' => true,
  159. 'value' => !empty($aVectorStore['topk']) ? $aVectorStore['topk'] : '',
  160. 'db' => [
  161. 'pass' => 'Int'
  162. ]
  163. ],
  164. 'params_user' => [
  165. 'type' => 'textarea',
  166. 'name' => 'params_user',
  167. 'caption' => '_sys_agents_vector_store_field_params',
  168. 'required' => false,
  169. 'value' => !empty($aVectorStore['params_user']) ? $aVectorStore['params_user'] : '',
  170. 'checker' => [
  171. 'func' => 'Json',
  172. 'params' => ['allow_empty' => true],
  173. 'error' => _t('_sys_agents_json_field_err'),
  174. ],
  175. 'db' => [
  176. 'pass' => 'All'
  177. ]
  178. ],
  179. 'submit' => $this->_getFormControls(),
  180. ]
  181. ];
  182. }
  183. protected function _getFormAddData(string $sAction, array $aVectorStore)
  184. {
  185. $sJsObject = $this->getPageJsObject();
  186. $aExt = array_keys($this->_aDataExt);
  187. $sExtList = '.' . implode(', .', $aExt);
  188. return [
  189. 'form_attrs' => [
  190. 'id' => 'bx_std_agents_helpers_' . $sAction,
  191. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_vector_store&a=' . $sAction,
  192. 'method' => 'post',
  193. ],
  194. 'params' => array (
  195. 'db' => array(
  196. 'table' => 'sys_agents_vector_store_data',
  197. 'key' => 'id',
  198. 'submit_name' => 'do_submit',
  199. ),
  200. ),
  201. 'inputs' => [
  202. 'vector_store_id' => [
  203. 'type' => 'hidden',
  204. 'name' => 'vector_store_id',
  205. 'value' => $aVectorStore ? $aVectorStore['id'] : 0,
  206. ],
  207. 'chunk_size' => [
  208. 'type' => 'text',
  209. 'name' => 'chunk_size',
  210. 'caption' => '_sys_agents_vector_store_field_chunk_size',
  211. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  212. 'required' => true,
  213. 'value' => 512,
  214. 'db' => [
  215. 'pass' => 'Int'
  216. ]
  217. ],
  218. 'delimeter' => [
  219. 'type' => 'text',
  220. 'name' => 'delimeter',
  221. 'caption' => '_sys_agents_vector_store_field_delimeter',
  222. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  223. 'required' => true,
  224. 'value' => '.',
  225. 'db' => [
  226. 'pass' => 'Xss'
  227. ]
  228. ],
  229. 'overlap' => [
  230. 'type' => 'text',
  231. 'name' => 'overlap',
  232. 'caption' => '_sys_agents_vector_store_field_overlap',
  233. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  234. 'required' => true,
  235. 'value' => 2,
  236. 'db' => [
  237. 'pass' => 'Int'
  238. ]
  239. ],
  240. 'metadata' => [
  241. 'type' => 'textarea',
  242. 'name' => 'metadata',
  243. 'caption' => '_sys_agents_vector_store_field_metadata',
  244. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  245. 'required' => false,
  246. 'checker' => [
  247. 'func' => 'Json',
  248. 'params' => ['allow_empty' => true],
  249. 'error' => _t('_sys_agents_vector_store_json_field_err'),
  250. ],
  251. 'db' => [
  252. 'pass' => 'All'
  253. ]
  254. ],
  255. 'files' => [
  256. 'attrs' => [
  257. 'multiple' => true,
  258. 'accept' => $sExtList
  259. ],
  260. 'type' => 'file',
  261. 'name' => 'files[]',
  262. 'caption' => _t('_sys_agents_vector_store_field_files'),
  263. 'caption_info' => _t('_sys_agents_vector_store_field_files_info'),
  264. 'required' => true,
  265. ],
  266. 'submit' => $this->_getFormControls(),
  267. ]
  268. ];
  269. }
  270. protected function _getFormEdit($sAction, $aVectorStore = [])
  271. {
  272. $aForm = $this->_getForm($sAction, $aVectorStore);
  273. $aForm['form_attrs']['action'] .= '&id=' . $aVectorStore['id'];
  274. return $aForm;
  275. }
  276. protected function getFileContentPlain($sFilePath)
  277. {
  278. return file_get_contents($sFilePath);
  279. }
  280. protected function getFileContentHtml($sFilePath)
  281. {
  282. $s = file_get_contents($sFilePath);
  283. $html = new \Html2Text\Html2Text($s);
  284. return $html->getText();
  285. }
  286. protected function _getCellFilesNum($mixedValue, $sKey, $aField, $aRow)
  287. {
  288. $mixedValue = $this->_oDb->getVectorStoreDataNum($aRow['id']);
  289. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  290. }
  291. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  292. {
  293. if ($sType == 'single' && $aRow['duplicate'] == 0)
  294. return '';
  295. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  296. }
  297. protected function _delete ($mixedId)
  298. {
  299. $aModel = $this->_oDb->getVectorStoreById($mixedId);
  300. if (empty($aModel) || $aModel['duplicate'] == 0)
  301. return false;
  302. // TODO: delete actual vector store data if needed
  303. return parent::_delete($mixedId);
  304. }
  305. }
  306. /** @} */