BxBaseStudioAgentsVectorStoreData.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 BxBaseStudioAgentsVectorStoreData extends BxDolStudioAgentsVectorStoreData
  11. {
  12. public function __construct ($aOptions, $oTemplate = false)
  13. {
  14. parent::__construct ($aOptions, $oTemplate);
  15. $this->addMarkers($this->_aBrowseParams);
  16. }
  17. protected function _delete ($mixedId)
  18. {
  19. $r = $this->_oDb->getVectorStoreDataById($mixedId);
  20. $oVectorStore = BxDolAIVectorStoreFactory::getVectorStoreInstance($r['vector_store_id']);
  21. if ($oVectorStore) {
  22. $oVectorStore->deleteBySource($r['vector_store_id'], $r['id']);
  23. }
  24. $mixedResult = parent::_delete($mixedId);
  25. return $mixedResult;
  26. }
  27. protected function _getCellSize ($mixedValue, $sKey, $aField, $aRow)
  28. {
  29. $mixedValue = bx_format_bytes($mixedValue);
  30. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  31. }
  32. protected function _getCellStatus ($mixedValue, $sKey, $aField, $aRow)
  33. {
  34. switch ($mixedValue) {
  35. case 'pending':
  36. $mixedValue = '<span title="Pending">⏳</span>';
  37. break;
  38. case 'processing':
  39. $mixedValue = '<span title="Processing">⚙️</span>';
  40. break;
  41. case 'ready':
  42. $mixedValue = '<span title="Ready">✅</span>';
  43. break;
  44. case 'error':
  45. $mixedValue = '<span title="Error">❌</span>';
  46. break;
  47. }
  48. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  49. }
  50. /**
  51. * Process files which were added to the vector store.
  52. * It takes files converts it to the vector and store in the appropriate vector store.
  53. */
  54. static public function processPendingData()
  55. {
  56. $iLimit = 5; // TODO: add to settings
  57. for ($i = 0; $i < $iLimit; $i++) {
  58. $a = BxDolAiQuery::getVectorStorePendingData(1);
  59. if (!$a)
  60. break;
  61. foreach ($a as $r) {
  62. BxDolAiQuery::updateVectorStoreDataStatus($r['id'], 'processing');
  63. $aVectorStore = BxDolAiQuery::getVectorStoreObject($r['vector_store_id']);
  64. $oVectorStore = BxDolAIVectorStoreFactory::getVectorStoreInstance($aVectorStore['id']);
  65. $oEmbedder = BxDolAIModelFactory::getModelInstance($aVectorStore['embedding_provider_id']);
  66. $sError = '';
  67. if ($oEmbedder && $oVectorStore) {
  68. try {
  69. $aSettings = $r['settings'] ? json_decode($r['settings'], true) : [];
  70. $aMetadata = $r['metadata'] ? json_decode($r['metadata'], true) : [];
  71. $documents = NeuronAI\RAG\DataLoader\StringDataLoader::for($r['content'])
  72. ->withSplitter(
  73. new NeuronAI\RAG\Splitter\DelimiterTextSplitter(
  74. maxLength: isset($aSettings['chunk_size']) ? $aSettings['chunk_size'] : 512,
  75. separator: isset($aSettings['delimiter']) ? $aSettings['delimiter'] : '.',
  76. wordOverlap: isset($aSettings['overlap']) ? $aSettings['overlap'] : 2
  77. )
  78. )
  79. ->getDocuments();
  80. foreach ($documents as $document) {
  81. $document->sourceType = $r['vector_store_id'];
  82. $document->sourceName = $r['id'];
  83. if (!empty($aMetadata))
  84. $document->metadata = $aMetadata;
  85. }
  86. $oVectorStore->addDocuments(
  87. $oEmbedder->embedDocuments($documents)
  88. );
  89. }
  90. catch (Exception $e) {
  91. $sError = "Error process vector store data ({$r['id']}): " . $e->getMessage();
  92. }
  93. if (!$sError)
  94. BxDolAiQuery::updateVectorStoreDataStatus($r['id'], 'ready');
  95. }
  96. else {
  97. $sError = "Can't process vector store data with id {$r['id']}, missing embedder({$aVectorStore['embedding_provider_id']}) or vector store({$aVectorStore['id']}) instance";
  98. }
  99. if ($sError) {
  100. bx_log('sys_agents', $sError, BX_LOG_ERR);
  101. BxDolAiQuery::updateVectorStoreDataStatus($r['id'], 'error');
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /** @} */