BxDolAIVectorStoreFactory.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. class BxDolAIVectorStoreFactory extends BxDolFactory
  10. {
  11. public static function getVectorStoreInstance(int $iId): NeuronAI\RAG\VectorStore\VectorStoreInterface
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId]))
  14. return $GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId];
  15. $a = BxDolAiQuery::getVectorStoreObject($iId);
  16. if (!$a) {
  17. bx_log('sys_agents', "Vector store with id {$iId} not found", BX_LOG_ERR);
  18. throw new Exception("Vector store with id {$iId} not found");
  19. }
  20. $aParametersSystem = !empty($a['params']) ? json_decode($a['params'], true) : [];
  21. $aParametersUser = !empty($a['params_user']) ? json_decode($a['params_user'], true) : [];
  22. $aParameters = array_merge($aParametersSystem, $aParametersUser);
  23. // replace marker {topk} in $aParameters recoursively
  24. $aParameters = bx_replace_markers($aParameters, [
  25. 'topk' => $a['topk'],
  26. ]);
  27. switch($a['type']) {
  28. case 'file':
  29. $sDirectoryStores = BX_DIRECTORY_STORAGE . 'vector_stores/';
  30. $sDirectory = $sDirectoryStores . $iId;
  31. $sFile = '/data.store';
  32. if (!file_exists($sDirectory . $sFile)) {
  33. @mkdir($sDirectoryStores, BX_DOL_DIR_RIGHTS);
  34. @chmod($sDirectoryStores, BX_DOL_DIR_RIGHTS);
  35. @mkdir($sDirectory, BX_DOL_DIR_RIGHTS);
  36. @chmod($sDirectory, BX_DOL_DIR_RIGHTS);
  37. @touch($sDirectory . $sFile);
  38. @chmod($sDirectory . $sFile, BX_DOL_FILE_RIGHTS);
  39. }
  40. $o = new NeuronAI\RAG\VectorStore\FileVectorStore(
  41. directory: $sDirectory,
  42. topK: $a['topk'] ?? 4,
  43. name: 'data',
  44. ext: '.store'
  45. );
  46. break;
  47. case 'pinecon':
  48. $o = new NeuronAI\RAG\VectorStore\PineconeVectorStore(
  49. key: $a['key'],
  50. indexUrl: $aParameters['indexUrl'],
  51. topK: $a['topk'] ?? 4,
  52. version: $aParameters['version'] ?? '2025-04',
  53. namespace: $aParameters['namespace'] ?? '__default__'
  54. );
  55. break;
  56. case 'elasticsearch':
  57. $oClient = Elastic\Elasticsearch\ClientBuilder::create()
  58. ->setHosts([$aParameters['client_params']['endpoint']])
  59. ->setApiKey($aParameters['client_params']['key'])
  60. ->build();
  61. $o = new NeuronAI\RAG\VectorStore\ElasticsearchVectorStore(
  62. client: $oClient,
  63. index: $aParameters['index'],
  64. topK: $a['topk'] ?? 4
  65. );
  66. break;
  67. case 'opensearch':
  68. $oClient = (new OpenSearch\GuzzleClientFactory())->create([
  69. 'base_uri' => $aParameters['client_params']['endpoint'] ?? 'http://localhost:9200',
  70. ]);
  71. $o = NeuronAI\RAG\VectorStore\OpenSearchVectorStore(
  72. client: $oClient,
  73. index: $aParameters['index'],
  74. topK: $a['topk'] ?? 4,
  75. );
  76. break;
  77. case 'typesense':
  78. $oClient = new \Typesense\Client($aParameters['client_params']);
  79. $o = new NeuronAI\RAG\VectorStore\TypesenseVectorStore(
  80. client: $oClient,
  81. collection: $aParameters['collection'],
  82. vectorDimension: $aParameters['vectorDimension'] ?? 1024,
  83. topK: $a['topk'] ?? 4,
  84. );
  85. break;
  86. case 'qdrant':
  87. $o = new NeuronAI\RAG\VectorStore\QdrantVectorStore(
  88. collectionUrl: $aParameters['collectionUrl'],
  89. key: $a['key'],
  90. topK: $a['topk'] ?? 4,
  91. dimension: $aParameters['dimension'] ?? 1024
  92. );
  93. break;
  94. case 'chromadb':
  95. $o = new NeuronAI\RAG\VectorStore\ChromaVectorStore(
  96. collection: $aParameters['collection'],
  97. host: $aParameters['host'] ?? 'http://localhost:8000',
  98. tenant: $aParameters['tenant'] ?? 'default_tenant',
  99. database: $aParameters['database'] ?? 'default_database',
  100. key: $a['key'] ?? null,
  101. topK: $a['topk'] ?? 4
  102. );
  103. break;
  104. case 'meilisearch':
  105. $o = new NeuronAI\RAG\VectorStore\MeilisearchVectorStore(
  106. indexUid: $aParameters['indexUid'],
  107. host: $aParameters['host'] ?? 'http://localhost:8000',
  108. key: $a['key'] ?? null,
  109. embedder: $aParameters['embedder'] ?? 'default',
  110. topK: $a['topk'] ?? 4,
  111. dimension: $aParameters['dimension'] ?? 1024
  112. );
  113. break;
  114. case 'mariadb':
  115. $o = BxDolAIVectorStoreMariaDb::open($iId, (int)($a['topk'] ?? 4));
  116. break;
  117. case 'una-proxy':
  118. bx_log('sys_agents', 'Remote knowledge is stored on the master', BX_LOG_ERR);
  119. throw new Exception('Remote knowledge is stored on the master');
  120. default:
  121. bx_log('sys_agents', "Vector store type {$a['type']} is not supported", BX_LOG_ERR);
  122. throw new Exception("Vector store type {$a['type']} is not supported");
  123. }
  124. $GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId] = $o;
  125. return $o;
  126. }
  127. }