BxBaseStudioAgentsVectorStore.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. $sGrid = $oGrid->getCode();
  106. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('popup_files_' . $iId, _t('_sys_agents_vector_store_popup_files'), $this->_oTemplate->parseHtmlByName('agents_popup_grid.html', [
  107. 'grid' => $sGrid,
  108. ]));
  109. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  110. }
  111. protected function _delete ($mixedId)
  112. {
  113. $mixedResult = parent::_delete($mixedId);
  114. if($mixedResult)
  115. $this->_oDb->deleteAutomatorHelpers(['helper_id' => (int)$mixedId]);
  116. return $mixedResult;
  117. }
  118. protected function _getForm($sAction = '', $aVectorStore = [])
  119. {
  120. $sJsObject = $this->getPageJsObject();
  121. if (empty($aVectorStore['params_user'])) {
  122. $aVectorStore['params_user'] = $aVectorStore['params'];
  123. }
  124. $data = json_decode($aVectorStore['params_user'], true);
  125. $aVectorStore['params_user'] = json_encode($data, JSON_PRETTY_PRINT);
  126. return [
  127. 'form_attrs' => [
  128. 'id' => 'bx_std_agents_helpers_' . $sAction,
  129. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_vector_store&a=' . $sAction,
  130. 'method' => 'post',
  131. ],
  132. 'params' => array (
  133. 'db' => array(
  134. 'table' => 'sys_agents_vector_store',
  135. 'key' => 'id',
  136. 'submit_name' => 'do_submit',
  137. ),
  138. ),
  139. 'inputs' => [
  140. 'topk' => [
  141. 'type' => 'text',
  142. 'name' => 'topk',
  143. 'caption' => '_sys_agents_vector_store_field_topk',
  144. 'required' => true,
  145. 'value' => !empty($aVectorStore['topk']) ? $aVectorStore['topk'] : '',
  146. 'db' => [
  147. 'pass' => 'Int'
  148. ]
  149. ],
  150. 'params_user' => [
  151. 'type' => 'textarea',
  152. 'name' => 'params_user',
  153. 'caption' => '_sys_agents_vector_store_field_params',
  154. 'required' => false,
  155. 'value' => !empty($aVectorStore['params_user']) ? $aVectorStore['params_user'] : '',
  156. 'checker' => [
  157. 'func' => 'Json',
  158. 'params' => ['allow_empty' => true],
  159. 'error' => _t('_sys_agents_vector_store_json_field_err'),
  160. ],
  161. 'db' => [
  162. 'pass' => 'All'
  163. ]
  164. ],
  165. 'submit' => $this->_getFormControls(),
  166. ]
  167. ];
  168. }
  169. protected function _getFormAddData(string $sAction, array $aVectorStore)
  170. {
  171. $sJsObject = $this->getPageJsObject();
  172. $aExt = array_keys($this->_aDataExt);
  173. $sExtList = '.' . implode(', .', $aExt);
  174. return [
  175. 'form_attrs' => [
  176. 'id' => 'bx_std_agents_helpers_' . $sAction,
  177. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_vector_store&a=' . $sAction,
  178. 'method' => 'post',
  179. ],
  180. 'params' => array (
  181. 'db' => array(
  182. 'table' => 'sys_agents_vector_store_data',
  183. 'key' => 'id',
  184. 'submit_name' => 'do_submit',
  185. ),
  186. ),
  187. 'inputs' => [
  188. 'vector_store_id' => [
  189. 'type' => 'hidden',
  190. 'name' => 'vector_store_id',
  191. 'value' => $aVectorStore ? $aVectorStore['id'] : 0,
  192. ],
  193. 'chunk_size' => [
  194. 'type' => 'text',
  195. 'name' => 'chunk_size',
  196. 'caption' => '_sys_agents_vector_store_field_chunk_size',
  197. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  198. 'required' => true,
  199. 'value' => 512,
  200. 'db' => [
  201. 'pass' => 'Int'
  202. ]
  203. ],
  204. 'delimeter' => [
  205. 'type' => 'text',
  206. 'name' => 'delimeter',
  207. 'caption' => '_sys_agents_vector_store_field_delimeter',
  208. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  209. 'required' => true,
  210. 'value' => '.',
  211. 'db' => [
  212. 'pass' => 'Xss'
  213. ]
  214. ],
  215. 'overlap' => [
  216. 'type' => 'text',
  217. 'name' => 'overlap',
  218. 'caption' => '_sys_agents_vector_store_field_overlap',
  219. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  220. 'required' => true,
  221. 'value' => 2,
  222. 'db' => [
  223. 'pass' => 'Int'
  224. ]
  225. ],
  226. 'metadata' => [
  227. 'type' => 'textarea',
  228. 'name' => 'metadata',
  229. 'caption' => '_sys_agents_vector_store_field_metadata',
  230. 'info' => '_sys_agents_vector_store_field_chunk_size_info',
  231. 'required' => false,
  232. 'checker' => [
  233. 'func' => 'Json',
  234. 'params' => ['allow_empty' => true],
  235. 'error' => _t('_sys_agents_vector_store_json_field_err'),
  236. ],
  237. 'db' => [
  238. 'pass' => 'All'
  239. ]
  240. ],
  241. 'files' => [
  242. 'attrs' => [
  243. 'multiple' => true,
  244. 'accept' => $sExtList
  245. ],
  246. 'type' => 'file',
  247. 'name' => 'files[]',
  248. 'caption' => _t('_sys_agents_vector_store_field_files'),
  249. 'caption_info' => _t('_sys_agents_vector_store_field_files_info'),
  250. 'required' => true,
  251. ],
  252. 'submit' => $this->_getFormControls(),
  253. ]
  254. ];
  255. }
  256. protected function _getFormEdit($sAction, $aVectorStore = [])
  257. {
  258. $aForm = $this->_getForm($sAction, $aVectorStore);
  259. $aForm['form_attrs']['action'] .= '&id=' . $aVectorStore['id'];
  260. return $aForm;
  261. }
  262. protected function getFileContentPlain($sFilePath)
  263. {
  264. return file_get_contents($sFilePath);
  265. }
  266. protected function getFileContentHtml($sFilePath)
  267. {
  268. $s = file_get_contents($sFilePath);
  269. $html = new \Html2Text\Html2Text($s);
  270. return $html->getText();
  271. }
  272. }
  273. /** @} */