BxDolAIVectorStoreFactory.php 5.1 KB

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