BxDolAIVectorStoreFactory.php 5.1 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 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. // TODO: fix params
  28. switch($a['type']) {
  29. case 'file':
  30. $o = new NeuronAI\RAG\VectorStore\FileVectorStore(
  31. directory: BX_DIRECTORY_STORAGE . 'vector_stores/' . $iId,
  32. topK: $a['topk'] ?? 4
  33. );
  34. break;
  35. case 'pinecon':
  36. $o = new NeuronAI\RAG\VectorStore\PineconeVectorStore(
  37. key: $a['key'],
  38. indexUrl: $aParameters['indexUrl'],
  39. topK: $a['topk'] ?? 4,
  40. version: $aParameters['version'] ?? '2025-04',
  41. namespace: $aParameters['namespace'] ?? '__default__'
  42. );
  43. break;
  44. case 'elasticsearch':
  45. $oClient = Elastic\Elasticsearch\ClientBuilder::create()
  46. ->setHosts([$aParameters['client_params']['endpoint']])
  47. ->setApiKey($aParameters['client_params']['key'])
  48. ->build();
  49. $o = new NeuronAI\RAG\VectorStore\ElasticsearchVectorStore(
  50. client: $oClient,
  51. index: $aParameters['index'],
  52. topK: $a['topk'] ?? 4
  53. );
  54. break;
  55. case 'opensearch':
  56. $oClient = (new OpenSearch\GuzzleClientFactory())->create([
  57. 'base_uri' => $aParameters['client_params']['endpoint'] ?? 'http://localhost:9200',
  58. ]);
  59. $o = NeuronAI\RAG\VectorStore\OpenSearchVectorStore(
  60. client: $oClient,
  61. index: $aParameters['index'],
  62. topK: $a['topk'] ?? 4,
  63. );
  64. break;
  65. case 'typesense':
  66. $oClient = new \Typesense\Client($aParameters['client_params']);
  67. $o = new NeuronAI\RAG\VectorStore\TypesenseVectorStore(
  68. client: $oClient,
  69. collection: $aParameters['collection'],
  70. vectorDimension: $aParameters['vectorDimension'] ?? 1024,
  71. topK: $a['topk'] ?? 4,
  72. );
  73. break;
  74. case 'qdrant':
  75. $o = new NeuronAI\RAG\VectorStore\QdrantVectorStore(
  76. collectionUrl: $aParameters['collectionUrl'],
  77. key: $a['key'],
  78. topK: $a['topk'] ?? 4,
  79. dimension: $aParameters['dimension'] ?? 1024
  80. );
  81. break;
  82. case 'chromadb':
  83. $o = new NeuronAI\RAG\VectorStore\ChromaVectorStore(
  84. collection: $aParameters['collection'],
  85. host: $aParameters['host'] ?? 'http://localhost:8000',
  86. tenant: $aParameters['tenant'] ?? 'default_tenant',
  87. database: $aParameters['database'] ?? 'default_database',
  88. key: $a['key'] ?? null,
  89. topK: $a['topk'] ?? 4
  90. );
  91. break;
  92. case 'meilisearch':
  93. $o = new NeuronAI\RAG\VectorStore\MeilisearchVectorStore(
  94. indexUid: $aParameters['indexUid'],
  95. host: $aParameters['host'] ?? 'http://localhost:8000',
  96. key: $a['key'] ?? null,
  97. embedder: $aParameters['embedder'] ?? 'default',
  98. topK: $a['topk'] ?? 4,
  99. dimension: $aParameters['dimension'] ?? 1024
  100. );
  101. break;
  102. default:
  103. bx_log('sys_agents', "Vector store type {$a['type']} is not supported", BX_LOG_ERR);
  104. throw new Exception("Vector store type {$a['type']} is not supported");
  105. }
  106. $GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId] = $o;
  107. return $o;
  108. }
  109. }