BxBaseStudioAgentsVectorStore.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 performActionAddData()
  30. {
  31. $sAction = 'add_data';
  32. $iId = $this->_getId();
  33. $aVectorStore = $this->_oDb->getVectorStoreById($iId);
  34. $aForm = $this->_getFormAddData($sAction, $aVectorStore);
  35. $oForm = new BxTemplFormView($aForm);
  36. $oForm->initChecker();
  37. if($oForm->isSubmittedAndValid()) {
  38. $iFilesAdded = 0;
  39. foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
  40. $sFileName = $_FILES['files']['name'][$key];
  41. $sFileType = $_FILES['files']['type'][$key];
  42. $sFileSize = $_FILES['files']['size'][$key];
  43. $sFileTmpName = $_FILES['files']['tmp_name'][$key];
  44. $sMetadata = $oForm->getCleanValue('metadata');
  45. $iVectorStoreId = $oForm->getCleanValue('vector_store_id');
  46. $sSettings = json_encode([
  47. 'chunk_size' => $oForm->getCleanValue('chunk_size'),
  48. 'delimeter' => trim($oForm->getCleanValue('delimeter')),
  49. 'overlap' => $oForm->getCleanValue('overlap'),
  50. ]);
  51. $sExt = pathinfo($sFileName, PATHINFO_EXTENSION);
  52. $sMethod = isset($this->_aDataExt[$sExt]) ? $this->_aDataExt[$sExt] : 'getFileContentPlain';
  53. $sContent = $this->$sMethod($sFileTmpName);
  54. if (empty($sContent)) {
  55. continue;
  56. }
  57. $bRet = $this->_oDb->addVectorStoreData ($iVectorStoreId, 'custom', $sFileName, (int)$sFileSize, $sMetadata, $sSettings, $sContent);
  58. if ($bRet) {
  59. $iFilesAdded++;
  60. }
  61. }
  62. return echoJson(['msg' => _t('_sys_agents_vector_store_data_queued', $iFilesAdded) . ' / num:' . $iFilesAdded]);
  63. if($oForm->add($iId) === false)
  64. return echoJson(['msg' => _t('_sys_txt_error_occured')]);
  65. return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  66. }
  67. $sFormId = $oForm->getId();
  68. $sForm = $oForm->getCode(true);
  69. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_vector_store_popup_add_data'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  70. 'form_id' => $sFormId,
  71. 'form' => $sForm,
  72. 'object' => $this->_sObject,
  73. 'action' => $sAction
  74. ]));
  75. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  76. }
  77. public function performActionEdit()
  78. {
  79. $sAction = 'edit';
  80. $iId = $this->_getId();
  81. $aVectorStore = $this->_oDb->getVectorStoreById($iId);
  82. $aForm = $this->_getFormEdit($sAction, $aVectorStore);
  83. $oForm = new BxTemplFormView($aForm);
  84. $oForm->initChecker();
  85. if($oForm->isSubmittedAndValid()) {
  86. if($oForm->update($iId) === false)
  87. return echoJson(['msg' => _t('_sys_txt_error_occured')]);
  88. return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  89. }
  90. $sFormId = $oForm->getId();
  91. $sForm = $oForm->getCode(true);
  92. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_vector_store_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  93. 'form_id' => $sFormId,
  94. 'form' => $sForm,
  95. 'object' => $this->_sObject,
  96. 'action' => $sAction
  97. ]));
  98. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  99. }
  100. public function performActionFiles()
  101. {
  102. $iId = $this->_getId();
  103. $aVectorStore = $this->_oDb->getVectorStoreById($iId);
  104. $oGrid = BxDolGrid::getObjectInstance('sys_studio_agents_vector_store_data');
  105. $oGrid->addMarkers(['vector_store_id' => $iId]);
  106. $oGrid->setBrowseParams(['vector_store_id' => $iId]);
  107. $sGrid = $oGrid->getCode();
  108. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('popup_files_' . $iId, _t('_sys_agents_vector_store_popup_files'), $this->_oTemplate->parseHtmlByName('agents_popup_grid.html', [
  109. 'grid' => $sGrid,
  110. ]));
  111. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  112. }
  113. protected function _delete ($mixedId)
  114. {
  115. $mixedResult = parent::_delete($mixedId);
  116. if($mixedResult)
  117. $this->_oDb->deleteAutomatorHelpers(['helper_id' => (int)$mixedId]);
  118. return $mixedResult;
  119. }
  120. protected function _getForm($sAction = '', $aVectorStore = [])
  121. {
  122. $sJsObject = $this->getPageJsObject();
  123. if (empty($aVectorStore['params_user'])) {
  124. $aVectorStore['params_user'] = $aVectorStore['params'];
  125. }
  126. $data = json_decode($aVectorStore['params_user'], true);
  127. $aVectorStore['params_user'] = json_encode($data, JSON_PRETTY_PRINT);
  128. return [
  129. 'form_attrs' => [
  130. 'id' => 'bx_std_agents_helpers_' . $sAction,
  131. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_vector_store&a=' . $sAction,
  132. 'method' => 'post',
  133. ],
  134. 'params' => array (
  135. 'db' => array(
  136. 'table' => 'sys_agents_vector_store',
  137. 'key' => 'id',
  138. 'submit_name' => 'do_submit',
  139. ),
  140. ),
  141. 'inputs' => [
  142. 'topk' => [
  143. 'type' => 'text',
  144. 'name' => 'topk',
  145. 'caption' => '_sys_agents_vector_store_field_topk',
  146. 'required' => true,
  147. 'value' => !empty($aVectorStore['topk']) ? $aVectorStore['topk'] : '',
  148. 'db' => [
  149. 'pass' => 'Int'
  150. ]
  151. ],
  152. 'params_user' => [
  153. 'type' => 'textarea',
  154. 'name' => 'params_user',
  155. 'caption' => '_sys_agents_vector_store_field_params',
  156. 'required' => false,
  157. 'value' => !empty($aVectorStore['params_user']) ? $aVectorStore['params_user'] : '',
  158. 'checker' => [
  159. 'func' => 'Json',
  160. 'params' => ['allow_empty' => true],
  161. 'error' => _t('_sys_agents_json_field_err'),
  162. ],
  163. 'db' => [
  164. 'pass' => 'All'
  165. ]
  166. ],
  167. 'submit' => $this->_getFormControls(),
  168. ]
  169. ];
  170. }
  171. protected function _getFormAddData(string $sAction, array $aVectorStore)
  172. {
  173. $sJsObject = $this->getPageJsObject();
  174. $aExt = array_keys($this->_aDataExt);
  175. $sExtList = '.' . implode(', .', $aExt);
  176. return [
  177. 'form_attrs' => [
  178. 'id' => 'bx_std_agents_helpers_' . $sAction,
  179. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_vector_store&a=' . $sAction,
  180. 'method' => 'post',
  181. ],
  182. 'params' => array (
  183. 'db' => array(
  184. 'table' => 'sys_agents_vector_store_data',
  185. 'key' => 'id',
  186. 'submit_name' => 'do_submit',
  187. ),
  188. ),
  189. 'inputs' => [
  190. 'vector_store_id' => [
  191. 'type' => 'hidden',
  192. 'name' => 'vector_store_id',
  193. 'value' => $aVectorStore ? $aVectorStore['id'] : 0,
  194. ],
  195. 'chunk_size' => [
  196. 'type' => 'text',
  197. 'name' => 'chunk_size',
  198. 'caption' => '_sys_agents_vector_store_field_chunk_size',
  199. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  200. 'required' => true,
  201. 'value' => 512,
  202. 'db' => [
  203. 'pass' => 'Int'
  204. ]
  205. ],
  206. 'delimeter' => [
  207. 'type' => 'text',
  208. 'name' => 'delimeter',
  209. 'caption' => '_sys_agents_vector_store_field_delimeter',
  210. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  211. 'required' => true,
  212. 'value' => '.',
  213. 'db' => [
  214. 'pass' => 'Xss'
  215. ]
  216. ],
  217. 'overlap' => [
  218. 'type' => 'text',
  219. 'name' => 'overlap',
  220. 'caption' => '_sys_agents_vector_store_field_overlap',
  221. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  222. 'required' => true,
  223. 'value' => 2,
  224. 'db' => [
  225. 'pass' => 'Int'
  226. ]
  227. ],
  228. 'metadata' => [
  229. 'type' => 'textarea',
  230. 'name' => 'metadata',
  231. 'caption' => '_sys_agents_vector_store_field_metadata',
  232. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  233. 'required' => false,
  234. 'checker' => [
  235. 'func' => 'Json',
  236. 'params' => ['allow_empty' => true],
  237. 'error' => _t('_sys_agents_vector_store_json_field_err'),
  238. ],
  239. 'db' => [
  240. 'pass' => 'All'
  241. ]
  242. ],
  243. 'files' => [
  244. 'attrs' => [
  245. 'multiple' => true,
  246. 'accept' => $sExtList
  247. ],
  248. 'type' => 'file',
  249. 'name' => 'files[]',
  250. 'caption' => _t('_sys_agents_vector_store_field_files'),
  251. 'caption_info' => _t('_sys_agents_vector_store_field_files_info'),
  252. 'required' => true,
  253. ],
  254. 'submit' => $this->_getFormControls(),
  255. ]
  256. ];
  257. }
  258. protected function _getFormEdit($sAction, $aVectorStore = [])
  259. {
  260. $aForm = $this->_getForm($sAction, $aVectorStore);
  261. $aForm['form_attrs']['action'] .= '&id=' . $aVectorStore['id'];
  262. return $aForm;
  263. }
  264. protected function getFileContentPlain($sFilePath)
  265. {
  266. return file_get_contents($sFilePath);
  267. }
  268. protected function getFileContentHtml($sFilePath)
  269. {
  270. $s = file_get_contents($sFilePath);
  271. $html = new \Html2Text\Html2Text($s);
  272. return $html->getText();
  273. }
  274. }
  275. /** @} */