BxDolAI.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_AI_ASSISTANT', 'assistant');
  10. define('BX_DOL_AI_AUTOMATOR_EVENT', 'event');
  11. define('BX_DOL_AI_AUTOMATOR_SCHEDULER', 'scheduler');
  12. define('BX_DOL_AI_AUTOMATOR_WEBHOOK', 'webhook');
  13. define('BX_DOL_AI_AUTOMATOR_STATUS_AUTO', 'auto');
  14. define('BX_DOL_AI_AUTOMATOR_STATUS_MANUAL', 'manual');
  15. define('BX_DOL_AI_AUTOMATOR_STATUS_READY', 'ready');
  16. class BxDolAI extends BxDolFactory implements iBxDolSingleton
  17. {
  18. protected $_oDb;
  19. protected $_iProfileId;
  20. protected $_aExcludeAlertUnits;
  21. protected $_sCmtsAutomators;
  22. protected $_sCmtsAssistantsChats;
  23. protected $_bWriteLog;
  24. protected function __construct()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. parent::__construct();
  29. $this->_oDb = new BxDolAIQuery();
  30. $this->_iProfileId = (int)getParam('sys_profile_bot');
  31. $this->_aExcludeAlertUnits = [
  32. 'system', 'module_template_method_call'
  33. ];
  34. $this->_sCmtsAutomators = 'sys_agents_automators';
  35. $this->_sCmtsAssistantsChats = 'sys_agents_assistants_chats';
  36. $this->_bWriteLog = true;
  37. }
  38. /**
  39. * Prevent cloning the instance
  40. */
  41. public function __clone()
  42. {
  43. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  44. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  45. }
  46. /**
  47. * Get singleton instance of the class
  48. */
  49. public static function getInstance()
  50. {
  51. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  52. $GLOBALS['bxDolClasses'][__CLASS__] = BxDolDb::getInstance()->isTableExists('sys_agents_automators') ? new BxDolAI() : null;
  53. }
  54. return $GLOBALS['bxDolClasses'][__CLASS__];
  55. }
  56. public static function getAiProviderInstance(int $iId):NeuronAI\Providers\AIProviderInterface
  57. {
  58. return self::getInstance()->getAiProviderInstance($iId);
  59. }
  60. public static function getAiEmbeddingsProviderInstance(int $iId):NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface
  61. {
  62. return self::getInstance()->getAiProviderInstance($iId);
  63. }
  64. public static function getModelInstance(int $iId): NeuronAI\Providers\AIProviderInterface | NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface
  65. {
  66. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_Model_' . $iId]))
  67. return $GLOBALS['bxDolClasses'][__CLASS__ . '_Model_' . $iId];
  68. $aProvidersWithKey = ['anthropic'];
  69. $a = BxDolAIQuery::getModelObject($iId);
  70. if (!$a) {
  71. bx_log('sys_agents', "Agent AI Model with id {$iId} not found", BX_LOG_ERR);
  72. throw new Exception("Agent AI Model with id {$iId} not found");
  73. }
  74. if (in_array($a['type'], $aProvidersWithKey) && empty($a['key'])) {
  75. bx_log('sys_agents', "Model with id {$iId} has empty key, can't be used", BX_LOG_ERR);
  76. throw new Exception("Model with id {$iId} has empty key, can't be used");
  77. }
  78. if (!$a['active']) {
  79. bx_log('sys_agents', "Model with id {$iId} is not active, can't be used", BX_LOG_ERR);
  80. throw new Exception("Model with id {$iId} is not active, can't be used");
  81. }
  82. $aParameters = !empty($a['params']) ? json_decode($a['params'], true) : [];
  83. switch($a['type']) {
  84. // TODO: add more providers
  85. case 'anthropic':
  86. $o = new NeuronAI\Providers\Anthropic\Anthropic(
  87. key: $a['key'],
  88. model: $a['model'],
  89. parameters: $aParameters['parameters'] ?? [],
  90. );
  91. break;
  92. case 'openai-responses':
  93. $o = new NeuronAI\Providers\OpenAI\Responses\OpenAIResponses(
  94. key: $a['key'],
  95. model: $a['model'],
  96. parameters: $aParameters['parameters'] ?? [],
  97. strict_response: false
  98. );
  99. break;
  100. case 'openai-embeddings':
  101. $o = new NeuronAI\RAG\Embeddings\OpenAIEmbeddingsProvider(
  102. key: $a['key'],
  103. model: $a['model'],
  104. dimensions: !empty($aParameters['dimensions']) ? (int)$aParameters['dimensions'] : 1024
  105. );
  106. break;
  107. default:
  108. bx_log('sys_agents', "Model type {$a['type']} is not supported", BX_LOG_ERR);
  109. throw new Exception("Model type {$a['type']} is not supported");
  110. }
  111. $GLOBALS['bxDolClasses'][__CLASS__ . '_Model_' . $iId] = $o;
  112. return $o;
  113. }
  114. public static function getVectorStoreInstance(int $iId): NeuronAI\RAG\VectorStore\VectorStoreInterface
  115. {
  116. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId]))
  117. return $GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId];
  118. $a = BxDolAIQuery::getVectorStoreObject($iId);
  119. if (!$a) {
  120. bx_log('sys_agents', "Vector store with id {$iId} not found", BX_LOG_ERR);
  121. throw new Exception("Vector store with id {$iId} not found");
  122. }
  123. $aParameters = !empty($a['params']) ? json_decode($a['params'], true) : [];
  124. switch($a['type']) {
  125. // TODO: add more vector store types
  126. case 'file':
  127. $o = new NeuronAI\RAG\VectorStore\FileVectorStore(
  128. directory: BX_DIRECTORY_STORAGE . 'vector_stores/' . $iId,
  129. topK: $a['topk'],
  130. );
  131. break;
  132. default:
  133. bx_log('sys_agents', "Vector store type {$a['type']} is not supported", BX_LOG_ERR);
  134. throw new Exception("Vector store type {$a['type']} is not supported");
  135. }
  136. $GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId] = $o;
  137. return $o;
  138. }
  139. public static function callHelper($mixedHelper, $sMessage)
  140. {
  141. $oAI = BxDolAI::getInstance();
  142. if (is_numeric($mixedHelper))
  143. $aHelper = $oAI->getHelperById($mixedHelper);
  144. else
  145. $aHelper = $oAI->getHelperByName($mixedHelper);
  146. $oAIModel = $oAI->getModelObject($aHelper['model_id']);
  147. return $oAIModel->getResponseText($aHelper['prompt'], $sMessage);
  148. }
  149. public static function pruning()
  150. {
  151. BxDolAIAssistant::pruning();
  152. }
  153. public static function getDefaultApiKey()
  154. {
  155. return getParam('sys_agents_api_key');
  156. }
  157. public static function getDefaultModel()
  158. {
  159. return (int)getParam('sys_agents_model');
  160. }
  161. public static function getAssistantForStudio()
  162. {
  163. return ($iId = (int)getParam('sys_agents_studio_assistant')) != 0 ? $iId : 0;
  164. }
  165. public static function getAssistantForLiveSearch()
  166. {
  167. return ($iId = (int)getParam('sys_agents_live_search_assistant')) != 0 ? $iId : 0;
  168. }
  169. public static function getAssistantForAskBlock()
  170. {
  171. return ($iId = (int)getParam('sys_agents_ask_block_assistant')) != 0 ? $iId : 0;
  172. }
  173. public function getProfileId()
  174. {
  175. return $this->_iProfileId;
  176. }
  177. public function getModels($aParams = [])
  178. {
  179. $aParamsDb = ['sample' => 'all_pairs'];
  180. if(isset($aParams['active']))
  181. $aParamsDb['active'] = $aParams['active'] === true ? 1 : 0;
  182. if(isset($aParams['capabilities']))
  183. $aParamsDb['capabilities'] = $aParams['capabilities'];
  184. return $aModel = $this->_oDb->getModelsBy($aParamsDb);
  185. }
  186. public function getModel($iId)
  187. {
  188. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iId]);
  189. if(!empty($aModel['params']))
  190. $aModel['params'] = json_decode($aModel['params'], true);
  191. return $aModel;
  192. }
  193. public function getModelObject($iId)
  194. {
  195. if(!$iId)
  196. $iId = $this->getDefaultModel();
  197. if(!$iId)
  198. return false;
  199. return BxDolAIModel::getObjectInstance($iId);
  200. }
  201. public function getProviderObject($iId)
  202. {
  203. if(!$iId)
  204. return false;
  205. return BxDolAIProvider::getObjectInstance($iId);
  206. }
  207. public function getAssistants($aParams = [])
  208. {
  209. $aParamsDb = ['sample' => 'all_pairs'];
  210. if(isset($aParams['active']))
  211. $aParamsDb['active'] = $aParams['active'] === true ? 1 : 0;
  212. if(isset($aParams['hidden']))
  213. $aParamsDb['hidden'] = $aParams['hidden'] === true ? 1 : 0;
  214. return $aModel = $this->_oDb->getAssistantsBy($aParamsDb);
  215. }
  216. public function getAssistantById($iId)
  217. {
  218. return $this->_oDb->getAssistantsBy(['sample' => 'id', 'id' => $iId]);
  219. }
  220. public function getAssistantByName($sName)
  221. {
  222. return $this->_oDb->getAssistantsBy(['sample' => 'name', 'name' => $sName]);
  223. }
  224. public function getAssistantChatById($iId)
  225. {
  226. return $this->_oDb->getChatsBy(['sample' => 'id', 'id' => $iId]);
  227. }
  228. public function getAssistantChatsTransient($iLifetime = 0)
  229. {
  230. return $this->_oDb->getChatsBy(['sample' => 'type', 'type' => BX_DOL_AI_ASST_TYPE_TRANSIENT, 'lifetime' => $iLifetime]);
  231. }
  232. public function updateAssistantChatById($iId, $aSet)
  233. {
  234. return $this->_oDb->updateChats($aSet, ['id' => $iId]);
  235. }
  236. public function getAssistantChatCmts()
  237. {
  238. return $this->_sCmtsAssistantsChats;
  239. }
  240. public function getAssistantChatCmtsObject($iId, $oTemplate = false)
  241. {
  242. $oCmts = BxDolCmts::getObjectInstance($this->_sCmtsAssistantsChats, (int)$iId, true, $oTemplate);
  243. if(!$oCmts || !$oCmts->isEnabled())
  244. return false;
  245. return $oCmts;
  246. }
  247. public function getHelperById($iId)
  248. {
  249. return $this->_oDb->getHelpersBy(['sample' => 'id', 'id' => $iId]);
  250. }
  251. public function getHelperByName($sName)
  252. {
  253. return $this->_oDb->getHelpersBy(['sample' => 'name', 'name' => $sName]);
  254. }
  255. public function getAutomator($iId, $bFullInfo = false)
  256. {
  257. $aAutomator = $this->_oDb->getAutomatorsBy(['sample' => 'id' . ($bFullInfo ? '_full' : ''), 'id' => $iId]);
  258. if(!empty($aAutomator['params']))
  259. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  260. if($bFullInfo && !empty($aAutomator['model_params']))
  261. $aAutomator['model_params'] = json_decode($aAutomator['model_params'], true);
  262. return $aAutomator;
  263. }
  264. public function getAutomatorInstruction($sType, $mixedParams = false)
  265. {
  266. $mixedResult = '';
  267. switch($sType) {
  268. case 'profile':
  269. $mixedResult = "\n ProfileId for system actions = " . $mixedParams;
  270. break;
  271. case 'providers':
  272. $aProviders = $this->_oDb->getProvidersBy(['sample' => 'ids', 'ids' => $mixedParams]);
  273. if(!empty($aProviders) && is_array($aProviders)) {
  274. $mixedResult = "\n Proividers list = [";
  275. foreach($aProviders as $aProvider)
  276. $mixedResult .= "\n {'ProviderName' => '" . $aProvider['name'] . "', 'ProviderType' => '" . $aProvider['type_name'] . "'}";
  277. $mixedResult .= "\n ]";
  278. }
  279. break;
  280. case 'helpers':
  281. $aHelpers = $this->_oDb->getHelpersBy(['sample' => 'ids', 'ids' => $mixedParams]);
  282. if(!empty($aHelpers) && is_array($aHelpers)) {
  283. $mixedResult = "\n Helpers list = [";
  284. foreach($aHelpers as $aHelper)
  285. $mixedResult .= "\n {'" . $aHelper['name'] . "', 'HelperDescription' => '" . $aHelper['description'] . "'}";
  286. $mixedResult .= "\n ]";
  287. }
  288. break;
  289. case 'assistants':
  290. $aAssistants = $this->_oDb->getAssistantsBy(['sample' => 'ids', 'ids' => $mixedParams]);
  291. if(!empty($aAssistants) && is_array($aAssistants)) {
  292. $mixedResult = "\n Assistants list = [";
  293. foreach($aAssistants as $aAssistant)
  294. $mixedResult .= "\n {'" . $aAssistant['name'] . "', 'AssistantDescription' => '" . $aAssistant['description'] . "'}";
  295. $mixedResult .= "\n ]";
  296. }
  297. break;
  298. }
  299. return $mixedResult;
  300. }
  301. public function getAutomatorCmts()
  302. {
  303. return $this->_sCmtsAutomators;
  304. }
  305. public function getAutomatorCmtsObject($iId, $oTemplate = false)
  306. {
  307. $oCmts = BxDolCmts::getObjectInstance($this->_sCmtsAutomators, (int)$iId, true, $oTemplate);
  308. if(!$oCmts || !$oCmts->isEnabled())
  309. return false;
  310. return $oCmts;
  311. }
  312. public function hasAutomators($sType, $bActive = null)
  313. {
  314. $aParams = [
  315. 'sample' => 'type',
  316. 'type' => $sType
  317. ];
  318. if($bActive !== null)
  319. $aParams['active'] = $bActive;
  320. return ($aAutomators = $this->_oDb->getAutomatorsBy($aParams)) && is_array($aAutomators);
  321. }
  322. public function getAutomatorsEvent($sUnit, $sAction)
  323. {
  324. if(in_array($sUnit, $this->_aExcludeAlertUnits))
  325. return [];
  326. return $this->_oDb->getAutomatorsBy([
  327. 'sample' => 'events',
  328. 'alert_unit' => $sUnit,
  329. 'alert_action' => $sAction,
  330. 'active' => true
  331. ]);
  332. }
  333. public function getAutomatorsScheduler()
  334. {
  335. $aAutomators = $this->_oDb->getAutomatorsBy(['sample' => 'schedulers', 'active' => true]);
  336. foreach($aAutomators as &$aAutomator)
  337. if(!empty($aAutomator['params']))
  338. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  339. return $aAutomators;
  340. }
  341. public function getAutomatorsWebhook($iProviderId)
  342. {
  343. $aAutomators = $this->_oDb->getAutomatorsBy(['sample' => 'webhooks', 'provider_id' => $iProviderId, 'active' => true]);
  344. foreach($aAutomators as &$aAutomator)
  345. if(!empty($aAutomator['params']))
  346. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  347. return $aAutomators;
  348. }
  349. public function callAutomator($sType, $aParams = [])
  350. {
  351. $sMethod = '_callAutomator' . bx_gen_method_name($sType);
  352. if(!method_exists($this, $sMethod))
  353. return false;
  354. return $this->$sMethod($aParams);
  355. }
  356. protected function _callAutomatorEvent($aParams = [])
  357. {
  358. if(!isset($aParams['automator'], $aParams['alert']) || !is_a($aParams['alert'], 'BxDolAlerts'))
  359. return false;
  360. $oAlert = &$aParams['alert'];
  361. $this->evalCode($aParams['automator'], ['alert' => $oAlert]);
  362. }
  363. protected function _callAutomatorScheduler($aParams = [])
  364. {
  365. if(!isset($aParams['automator']))
  366. return false;
  367. $this->evalCode($aParams['automator']);
  368. }
  369. protected function _callAutomatorWebhook($aParams = [])
  370. {
  371. if(!isset($aParams['automator']))
  372. return false;
  373. $this->evalCode($aParams['automator']);
  374. }
  375. public function evalCode($aAutomator, $aParams = [])
  376. {
  377. try {
  378. $this->_evalCode($aAutomator, $aParams);
  379. }
  380. catch (Exception $oException) {
  381. $this->log($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
  382. }
  383. catch (Error $oError) {
  384. $this->log($oError->getFile() . ':' . $oError->getLine() . ' ' . $oError->getMessage());
  385. }
  386. }
  387. public function emulCode($aAutomator, $aParams = [])
  388. {
  389. ob_start();
  390. try {
  391. $this->_evalCode($aAutomator, $aParams);
  392. }
  393. catch (Exception $oException) {
  394. return $oException->getMessage();
  395. }
  396. catch (Error $oError) {
  397. return $oError->getMessage();
  398. }
  399. finally {
  400. $sOutput = ob_get_clean();
  401. if(!empty($sOutput))
  402. return $sOutput;
  403. }
  404. }
  405. public function log($mixedContents, $sSection = '')
  406. {
  407. if(!$this->_bWriteLog)
  408. return;
  409. if(is_array($mixedContents))
  410. $mixedContents = var_export($mixedContents, true);
  411. else if(is_object($mixedContents))
  412. $mixedContents = json_encode($mixedContents);
  413. if(empty($sSection))
  414. $sSection = "Core";
  415. bx_log('sys_agents', ":\n[" . $sSection . "] " . $mixedContents, BX_LOG_ERR);
  416. }
  417. protected function _evalCode($aAutomator, $aParams = [])
  418. {
  419. $sCode = '';
  420. switch($aAutomator['type']) {
  421. case BX_DOL_AI_AUTOMATOR_EVENT:
  422. $sCode = $aAutomator['code']. '; onAlert($aParams["alert"]->iObject , $aParams["alert"]->iSender , $aParams["alert"]->aExtras);';
  423. break;
  424. case BX_DOL_AI_AUTOMATOR_SCHEDULER:
  425. $sCode = $aAutomator['code'] . '; onCron();';
  426. break;
  427. case BX_DOL_AI_AUTOMATOR_WEBHOOK:
  428. $sCode = $aAutomator['code'] . '; onHook();';
  429. break;
  430. }
  431. eval($sCode);
  432. }
  433. }
  434. class BxDolAIMessage
  435. {
  436. /**
  437. * @var string - message Type with following values: hb, ai
  438. */
  439. protected $_sType;
  440. /**
  441. * @var mixed - an array of message parts (text, image_url) or a string.
  442. */
  443. protected $_mixedContent;
  444. /**
  445. * @var array - an array of of files attached to the message.
  446. */
  447. protected $_aAttachments;
  448. public function __construct($sType)
  449. {
  450. $this->_sType = $sType;
  451. }
  452. public function isAi()
  453. {
  454. return $this->_sType == 'ai';
  455. }
  456. public function getContent()
  457. {
  458. return $this->_mixedContent;
  459. }
  460. public function getAttachments()
  461. {
  462. return $this->_aAttachments;
  463. }
  464. }
  465. class BxDolAIMessageString extends BxDolAIMessage
  466. {
  467. public function __construct($sType, $sContent)
  468. {
  469. parent::__construct($sType);
  470. $this->_mixedContent = is_string($sContent) ? $sContent : '';
  471. }
  472. }
  473. class BxDolAIMessageArray extends BxDolAIMessage
  474. {
  475. public function __construct($sType, $aContent = '')
  476. {
  477. parent::__construct($sType);
  478. $this->_mixedContent = is_array($aContent) ? $aContent : [];
  479. }
  480. public function addText($sText)
  481. {
  482. $this->_mixedContent[] = [
  483. 'type' => 'text',
  484. 'text' => $sText
  485. ];
  486. }
  487. public function addImageUrl($sUrl, $sDetail = 'high')
  488. {
  489. $this->_mixedContent[] = [
  490. 'type' => 'image_url',
  491. 'image_url' => [
  492. 'url' => $sUrl,
  493. 'detail' => $sDetail
  494. ]
  495. ];
  496. }
  497. public function addAttachments($mixedAttachments, $mixedTools = false)
  498. {
  499. if(!is_array($mixedAttachments))
  500. $mixedAttachments = [$mixedAttachments];
  501. if(!$mixedTools)
  502. $mixedTools = [['type' => 'file_search']];
  503. foreach($mixedAttachments as $sAttachment)
  504. $this->_aAttachments[] = [
  505. 'file_id' => $sAttachment,
  506. 'tools' => $mixedTools
  507. ];
  508. }
  509. }
  510. class BxDolAIMessages
  511. {
  512. /**
  513. * @var array - an array of items (messages)
  514. */
  515. protected $_aItems;
  516. public function __construct($aItems = [])
  517. {
  518. $this->_aItems = !empty($aItems) && is_array($aItems) ? $aItems : [];
  519. }
  520. public function add($sType, $mixedMessage)
  521. {
  522. $sClass = 'BxDolAIMessage' . (is_string($mixedMessage) ? 'String' : 'Array');
  523. $this->_aItems[] = new $sClass($sType, $mixedMessage);
  524. }
  525. public function getAll()
  526. {
  527. return $this->_aItems;
  528. }
  529. public function getLast()
  530. {
  531. return end($this->_aItems);
  532. }
  533. }