agentdb 3.0.0-alpha.10 → 3.0.0-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2821 -225
- package/dist/src/backends/VectorBackend.d.ts +31 -6
- package/dist/src/backends/VectorBackend.d.ts.map +1 -1
- package/dist/src/backends/detector.js.map +1 -1
- package/dist/src/backends/factory.d.ts +16 -33
- package/dist/src/backends/factory.d.ts.map +1 -1
- package/dist/src/backends/factory.js +138 -108
- package/dist/src/backends/factory.js.map +1 -1
- package/dist/src/backends/hnswlib/HNSWLibBackend.d.ts.map +1 -1
- package/dist/src/backends/hnswlib/HNSWLibBackend.js +29 -2
- package/dist/src/backends/hnswlib/HNSWLibBackend.js.map +1 -1
- package/dist/src/backends/ruvector/RuVectorBackend.d.ts +198 -45
- package/dist/src/backends/ruvector/RuVectorBackend.d.ts.map +1 -1
- package/dist/src/backends/ruvector/RuVectorBackend.js +612 -277
- package/dist/src/backends/ruvector/RuVectorBackend.js.map +1 -1
- package/dist/src/backends/ruvector/RuVectorLearning.d.ts +0 -4
- package/dist/src/backends/ruvector/RuVectorLearning.d.ts.map +1 -1
- package/dist/src/backends/ruvector/RuVectorLearning.js +0 -6
- package/dist/src/backends/ruvector/RuVectorLearning.js.map +1 -1
- package/dist/src/backends/rvf/AdaptiveIndexTuner.d.ts +175 -0
- package/dist/src/backends/rvf/AdaptiveIndexTuner.d.ts.map +1 -0
- package/dist/src/backends/rvf/AdaptiveIndexTuner.js +559 -0
- package/dist/src/backends/rvf/AdaptiveIndexTuner.js.map +1 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.d.ts +148 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.d.ts.map +1 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.js +438 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.js.map +1 -0
- package/dist/src/backends/rvf/FederatedSessionManager.d.ts +211 -0
- package/dist/src/backends/rvf/FederatedSessionManager.d.ts.map +1 -0
- package/dist/src/backends/rvf/FederatedSessionManager.js +389 -0
- package/dist/src/backends/rvf/FederatedSessionManager.js.map +1 -0
- package/dist/src/backends/rvf/FilterBuilder.d.ts +136 -0
- package/dist/src/backends/rvf/FilterBuilder.d.ts.map +1 -0
- package/dist/src/backends/rvf/FilterBuilder.js +157 -0
- package/dist/src/backends/rvf/FilterBuilder.js.map +1 -0
- package/dist/src/backends/rvf/NativeAccelerator.d.ts +140 -0
- package/dist/src/backends/rvf/NativeAccelerator.d.ts.map +1 -0
- package/dist/src/backends/rvf/NativeAccelerator.js +630 -0
- package/dist/src/backends/rvf/NativeAccelerator.js.map +1 -0
- package/dist/src/backends/rvf/RvfBackend.d.ts +194 -0
- package/dist/src/backends/rvf/RvfBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/RvfBackend.js +607 -0
- package/dist/src/backends/rvf/RvfBackend.js.map +1 -0
- package/dist/src/backends/rvf/RvfSolver.d.ts +175 -0
- package/dist/src/backends/rvf/RvfSolver.d.ts.map +1 -0
- package/dist/src/backends/rvf/RvfSolver.js +176 -0
- package/dist/src/backends/rvf/RvfSolver.js.map +1 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.d.ts +143 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.js +569 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.js.map +1 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.d.ts +128 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.d.ts.map +1 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.js +415 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.js.map +1 -0
- package/dist/src/backends/rvf/SimdFallbacks.d.ts +25 -0
- package/dist/src/backends/rvf/SimdFallbacks.d.ts.map +1 -0
- package/dist/src/backends/rvf/SimdFallbacks.js +287 -0
- package/dist/src/backends/rvf/SimdFallbacks.js.map +1 -0
- package/dist/src/backends/rvf/SolverBandit.d.ts +100 -0
- package/dist/src/backends/rvf/SolverBandit.d.ts.map +1 -0
- package/dist/src/backends/rvf/SolverBandit.js +216 -0
- package/dist/src/backends/rvf/SolverBandit.js.map +1 -0
- package/dist/src/backends/rvf/SonaLearningBackend.d.ts +157 -0
- package/dist/src/backends/rvf/SonaLearningBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/SonaLearningBackend.js +283 -0
- package/dist/src/backends/rvf/SonaLearningBackend.js.map +1 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.d.ts +77 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.js +382 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.js.map +1 -0
- package/dist/src/backends/rvf/WasmStoreBridge.d.ts +27 -0
- package/dist/src/backends/rvf/WasmStoreBridge.d.ts.map +1 -0
- package/dist/src/backends/rvf/WasmStoreBridge.js +87 -0
- package/dist/src/backends/rvf/WasmStoreBridge.js.map +1 -0
- package/dist/src/backends/rvf/validation.d.ts +20 -0
- package/dist/src/backends/rvf/validation.d.ts.map +1 -0
- package/dist/src/backends/rvf/validation.js +78 -0
- package/dist/src/backends/rvf/validation.js.map +1 -0
- package/dist/src/benchmark/BenchmarkSuite.d.ts +318 -0
- package/dist/src/benchmark/BenchmarkSuite.d.ts.map +1 -0
- package/dist/src/benchmark/BenchmarkSuite.js +984 -0
- package/dist/src/benchmark/BenchmarkSuite.js.map +1 -0
- package/dist/src/benchmark/index.d.ts +29 -0
- package/dist/src/benchmark/index.d.ts.map +1 -0
- package/dist/src/benchmark/index.js +39 -0
- package/dist/src/benchmark/index.js.map +1 -0
- package/dist/src/browser/AttentionBrowser.d.ts.map +1 -1
- package/dist/src/browser/AttentionBrowser.js +1 -0
- package/dist/src/browser/AttentionBrowser.js.map +1 -1
- package/dist/src/cli/agentdb-cli.d.ts +13 -0
- package/dist/src/cli/agentdb-cli.d.ts.map +1 -1
- package/dist/src/cli/agentdb-cli.js +803 -94
- package/dist/src/cli/agentdb-cli.js.map +1 -1
- package/dist/src/cli/commands/hyperbolic.d.ts +11 -0
- package/dist/src/cli/commands/hyperbolic.d.ts.map +1 -0
- package/dist/src/cli/commands/hyperbolic.js +428 -0
- package/dist/src/cli/commands/hyperbolic.js.map +1 -0
- package/dist/src/cli/commands/learn.d.ts +11 -0
- package/dist/src/cli/commands/learn.d.ts.map +1 -0
- package/dist/src/cli/commands/learn.js +404 -0
- package/dist/src/cli/commands/learn.js.map +1 -0
- package/dist/src/cli/commands/migrate.d.ts +15 -0
- package/dist/src/cli/commands/migrate.d.ts.map +1 -1
- package/dist/src/cli/commands/migrate.js +176 -8
- package/dist/src/cli/commands/migrate.js.map +1 -1
- package/dist/src/cli/commands/route.d.ts +11 -0
- package/dist/src/cli/commands/route.d.ts.map +1 -0
- package/dist/src/cli/commands/route.js +414 -0
- package/dist/src/cli/commands/route.js.map +1 -0
- package/dist/src/cli/commands/rvf.d.ts +21 -0
- package/dist/src/cli/commands/rvf.d.ts.map +1 -0
- package/dist/src/cli/commands/rvf.js +448 -0
- package/dist/src/cli/commands/rvf.js.map +1 -0
- package/dist/src/cli/lib/report-store.d.ts +2 -1
- package/dist/src/cli/lib/report-store.d.ts.map +1 -1
- package/dist/src/cli/lib/report-store.js +85 -65
- package/dist/src/cli/lib/report-store.js.map +1 -1
- package/dist/src/cli/lib/simulation-runner.d.ts +40 -3
- package/dist/src/cli/lib/simulation-runner.d.ts.map +1 -1
- package/dist/src/cli/lib/simulation-runner.js +248 -6
- package/dist/src/cli/lib/simulation-runner.js.map +1 -1
- package/dist/src/compatibility/DeprecationWarnings.d.ts +54 -0
- package/dist/src/compatibility/DeprecationWarnings.d.ts.map +1 -0
- package/dist/src/compatibility/DeprecationWarnings.js +131 -0
- package/dist/src/compatibility/DeprecationWarnings.js.map +1 -0
- package/dist/src/compatibility/MigrationUtilities.d.ts +30 -0
- package/dist/src/compatibility/MigrationUtilities.d.ts.map +1 -0
- package/dist/src/compatibility/MigrationUtilities.js +184 -0
- package/dist/src/compatibility/MigrationUtilities.js.map +1 -0
- package/dist/src/compatibility/V1toV2Adapter.d.ts +76 -0
- package/dist/src/compatibility/V1toV2Adapter.d.ts.map +1 -0
- package/dist/src/compatibility/V1toV2Adapter.js +185 -0
- package/dist/src/compatibility/V1toV2Adapter.js.map +1 -0
- package/dist/src/compatibility/VersionDetector.d.ts +32 -0
- package/dist/src/compatibility/VersionDetector.d.ts.map +1 -0
- package/dist/src/compatibility/VersionDetector.js +144 -0
- package/dist/src/compatibility/VersionDetector.js.map +1 -0
- package/dist/src/compatibility/index.d.ts +14 -0
- package/dist/src/compatibility/index.d.ts.map +1 -0
- package/dist/src/compatibility/index.js +21 -0
- package/dist/src/compatibility/index.js.map +1 -0
- package/dist/src/compatibility/types.d.ts +103 -0
- package/dist/src/compatibility/types.d.ts.map +1 -0
- package/dist/src/compatibility/types.js +5 -0
- package/dist/src/compatibility/types.js.map +1 -0
- package/dist/src/controllers/AttentionService.d.ts +99 -202
- package/dist/src/controllers/AttentionService.d.ts.map +1 -1
- package/dist/src/controllers/AttentionService.js +450 -558
- package/dist/src/controllers/AttentionService.js.map +1 -1
- package/dist/src/controllers/CausalMemoryGraph.d.ts +30 -5
- package/dist/src/controllers/CausalMemoryGraph.d.ts.map +1 -1
- package/dist/src/controllers/CausalMemoryGraph.js +89 -13
- package/dist/src/controllers/CausalMemoryGraph.js.map +1 -1
- package/dist/src/controllers/CausalRecall.d.ts +4 -0
- package/dist/src/controllers/CausalRecall.d.ts.map +1 -1
- package/dist/src/controllers/CausalRecall.js +19 -2
- package/dist/src/controllers/CausalRecall.js.map +1 -1
- package/dist/src/controllers/EmbeddingService.d.ts.map +1 -1
- package/dist/src/controllers/EmbeddingService.js +28 -11
- package/dist/src/controllers/EmbeddingService.js.map +1 -1
- package/dist/src/controllers/EnhancedEmbeddingService.d.ts +4 -0
- package/dist/src/controllers/EnhancedEmbeddingService.d.ts.map +1 -1
- package/dist/src/controllers/EnhancedEmbeddingService.js +17 -3
- package/dist/src/controllers/EnhancedEmbeddingService.js.map +1 -1
- package/dist/src/controllers/ExplainableRecall.d.ts +1 -1
- package/dist/src/controllers/ExplainableRecall.d.ts.map +1 -1
- package/dist/src/controllers/ExplainableRecall.js +1 -1
- package/dist/src/controllers/ExplainableRecall.js.map +1 -1
- package/dist/src/controllers/HNSWIndex.d.ts +20 -2
- package/dist/src/controllers/HNSWIndex.d.ts.map +1 -1
- package/dist/src/controllers/HNSWIndex.js +82 -6
- package/dist/src/controllers/HNSWIndex.js.map +1 -1
- package/dist/src/controllers/LearningSystem.d.ts +1 -17
- package/dist/src/controllers/LearningSystem.d.ts.map +1 -1
- package/dist/src/controllers/LearningSystem.js +18 -135
- package/dist/src/controllers/LearningSystem.js.map +1 -1
- package/dist/src/controllers/MemoryController.d.ts +161 -0
- package/dist/src/controllers/MemoryController.d.ts.map +1 -0
- package/dist/src/controllers/MemoryController.js +303 -0
- package/dist/src/controllers/MemoryController.js.map +1 -0
- package/dist/src/controllers/MincutService.d.ts +92 -0
- package/dist/src/controllers/MincutService.d.ts.map +1 -0
- package/dist/src/controllers/MincutService.js +336 -0
- package/dist/src/controllers/MincutService.js.map +1 -0
- package/dist/src/controllers/NightlyLearner.d.ts +5 -1
- package/dist/src/controllers/NightlyLearner.d.ts.map +1 -1
- package/dist/src/controllers/NightlyLearner.js +17 -3
- package/dist/src/controllers/NightlyLearner.js.map +1 -1
- package/dist/src/controllers/QUICClient.d.ts +45 -0
- package/dist/src/controllers/QUICClient.d.ts.map +1 -1
- package/dist/src/controllers/QUICClient.js +190 -0
- package/dist/src/controllers/QUICClient.js.map +1 -1
- package/dist/src/controllers/ReasoningBank.d.ts +6 -3
- package/dist/src/controllers/ReasoningBank.d.ts.map +1 -1
- package/dist/src/controllers/ReasoningBank.js +47 -41
- package/dist/src/controllers/ReasoningBank.js.map +1 -1
- package/dist/src/controllers/ReflexionMemory.d.ts +115 -6
- package/dist/src/controllers/ReflexionMemory.d.ts.map +1 -1
- package/dist/src/controllers/ReflexionMemory.js +541 -213
- package/dist/src/controllers/ReflexionMemory.js.map +1 -1
- package/dist/src/controllers/SkillLibrary.d.ts +27 -3
- package/dist/src/controllers/SkillLibrary.d.ts.map +1 -1
- package/dist/src/controllers/SkillLibrary.js +187 -91
- package/dist/src/controllers/SkillLibrary.js.map +1 -1
- package/dist/src/controllers/SparsificationService.d.ts +151 -0
- package/dist/src/controllers/SparsificationService.d.ts.map +1 -0
- package/dist/src/controllers/SparsificationService.js +375 -0
- package/dist/src/controllers/SparsificationService.js.map +1 -0
- package/dist/src/controllers/SyncCoordinator.d.ts.map +1 -1
- package/dist/src/controllers/SyncCoordinator.js +115 -3
- package/dist/src/controllers/SyncCoordinator.js.map +1 -1
- package/dist/src/controllers/WASMVectorSearch.d.ts +12 -30
- package/dist/src/controllers/WASMVectorSearch.d.ts.map +1 -1
- package/dist/src/controllers/WASMVectorSearch.js +146 -93
- package/dist/src/controllers/WASMVectorSearch.js.map +1 -1
- package/dist/src/controllers/attention/AttentionCache.d.ts +38 -0
- package/dist/src/controllers/attention/AttentionCache.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionCache.js +80 -0
- package/dist/src/controllers/attention/AttentionCache.js.map +1 -0
- package/dist/src/controllers/attention/AttentionConfig.d.ts +127 -0
- package/dist/src/controllers/attention/AttentionConfig.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionConfig.js +124 -0
- package/dist/src/controllers/attention/AttentionConfig.js.map +1 -0
- package/dist/src/controllers/attention/AttentionCore.d.ts +89 -0
- package/dist/src/controllers/attention/AttentionCore.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionCore.js +289 -0
- package/dist/src/controllers/attention/AttentionCore.js.map +1 -0
- package/dist/src/controllers/attention/AttentionHelpers.d.ts +55 -0
- package/dist/src/controllers/attention/AttentionHelpers.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionHelpers.js +148 -0
- package/dist/src/controllers/attention/AttentionHelpers.js.map +1 -0
- package/dist/src/controllers/attention/AttentionMetrics.d.ts +55 -0
- package/dist/src/controllers/attention/AttentionMetrics.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionMetrics.js +72 -0
- package/dist/src/controllers/attention/AttentionMetrics.js.map +1 -0
- package/dist/src/controllers/attention/AttentionWASM.d.ts +98 -0
- package/dist/src/controllers/attention/AttentionWASM.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionWASM.js +147 -0
- package/dist/src/controllers/attention/AttentionWASM.js.map +1 -0
- package/dist/src/controllers/attention/CrossAttentionController.d.ts +141 -0
- package/dist/src/controllers/attention/CrossAttentionController.d.ts.map +1 -0
- package/dist/src/controllers/attention/CrossAttentionController.js +325 -0
- package/dist/src/controllers/attention/CrossAttentionController.js.map +1 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.d.ts +137 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.d.ts.map +1 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.js +336 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.js.map +1 -0
- package/dist/src/controllers/attention/SelfAttentionController.d.ts +117 -0
- package/dist/src/controllers/attention/SelfAttentionController.d.ts.map +1 -0
- package/dist/src/controllers/attention/SelfAttentionController.js +205 -0
- package/dist/src/controllers/attention/SelfAttentionController.js.map +1 -0
- package/dist/src/controllers/attention/index.d.ts +24 -0
- package/dist/src/controllers/attention/index.d.ts.map +1 -0
- package/dist/src/controllers/attention/index.js +20 -0
- package/dist/src/controllers/attention/index.js.map +1 -0
- package/dist/src/controllers/index.d.ts +14 -27
- package/dist/src/controllers/index.d.ts.map +1 -1
- package/dist/src/controllers/index.js +10 -18
- package/dist/src/controllers/index.js.map +1 -1
- package/dist/src/controllers/prerequisites.d.ts +76 -0
- package/dist/src/controllers/prerequisites.d.ts.map +1 -0
- package/dist/src/controllers/prerequisites.js +235 -0
- package/dist/src/controllers/prerequisites.js.map +1 -0
- package/dist/src/core/AgentDB.d.ts +27 -19
- package/dist/src/core/AgentDB.d.ts.map +1 -1
- package/dist/src/core/AgentDB.js +76 -124
- package/dist/src/core/AgentDB.js.map +1 -1
- package/dist/src/core/QueryCache.d.ts +138 -0
- package/dist/src/core/QueryCache.d.ts.map +1 -0
- package/dist/src/core/QueryCache.js +309 -0
- package/dist/src/core/QueryCache.js.map +1 -0
- package/dist/src/db-fallback.d.ts +10 -1
- package/dist/src/db-fallback.d.ts.map +1 -1
- package/dist/src/db-fallback.js +205 -28
- package/dist/src/db-fallback.js.map +1 -1
- package/dist/src/examples/quic-sync-example.js +2 -2
- package/dist/src/examples/quic-sync-example.js.map +1 -1
- package/dist/src/index.d.ts +15 -15
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +39 -11
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/agentdb-mcp-server.js +47 -2
- package/dist/src/mcp/agentdb-mcp-server.js.map +1 -1
- package/dist/src/model/ModelCacheLoader.d.ts +24 -0
- package/dist/src/model/ModelCacheLoader.d.ts.map +1 -0
- package/dist/src/model/ModelCacheLoader.js +121 -0
- package/dist/src/model/ModelCacheLoader.js.map +1 -0
- package/dist/src/observability/index.d.ts +8 -0
- package/dist/src/observability/index.d.ts.map +1 -0
- package/dist/src/observability/index.js +8 -0
- package/dist/src/observability/index.js.map +1 -0
- package/dist/src/observability/integration.d.ts +32 -0
- package/dist/src/observability/integration.d.ts.map +1 -0
- package/dist/src/observability/integration.js +159 -0
- package/dist/src/observability/integration.js.map +1 -0
- package/dist/src/observability/telemetry.d.ts +137 -0
- package/dist/src/observability/telemetry.d.ts.map +1 -0
- package/dist/src/observability/telemetry.js +416 -0
- package/dist/src/observability/telemetry.js.map +1 -0
- package/dist/src/optimizations/BatchOperations.d.ts +47 -0
- package/dist/src/optimizations/BatchOperations.d.ts.map +1 -1
- package/dist/src/optimizations/BatchOperations.js +183 -31
- package/dist/src/optimizations/BatchOperations.js.map +1 -1
- package/dist/src/optimizations/Quantization.d.ts +330 -0
- package/dist/src/optimizations/Quantization.d.ts.map +1 -0
- package/dist/src/optimizations/Quantization.js +793 -0
- package/dist/src/optimizations/Quantization.js.map +1 -0
- package/dist/src/optimizations/index.d.ts +1 -3
- package/dist/src/optimizations/index.d.ts.map +1 -1
- package/dist/src/optimizations/index.js +0 -1
- package/dist/src/optimizations/index.js.map +1 -1
- package/dist/src/quantization/index.d.ts +7 -0
- package/dist/src/quantization/index.d.ts.map +1 -0
- package/dist/src/quantization/index.js +15 -0
- package/dist/src/quantization/index.js.map +1 -0
- package/dist/src/quantization/vector-quantization.d.ts +388 -0
- package/dist/src/quantization/vector-quantization.d.ts.map +1 -0
- package/dist/src/quantization/vector-quantization.js +1132 -0
- package/dist/src/quantization/vector-quantization.js.map +1 -0
- package/dist/src/search/HybridSearch.d.ts +311 -0
- package/dist/src/search/HybridSearch.d.ts.map +1 -0
- package/dist/src/search/HybridSearch.js +712 -0
- package/dist/src/search/HybridSearch.js.map +1 -0
- package/dist/src/search/index.d.ts +12 -0
- package/dist/src/search/index.d.ts.map +1 -0
- package/dist/src/search/index.js +16 -0
- package/dist/src/search/index.js.map +1 -0
- package/dist/src/services/AttentionService.d.ts +303 -0
- package/dist/src/services/AttentionService.d.ts.map +1 -0
- package/dist/src/services/AttentionService.js +1165 -0
- package/dist/src/services/AttentionService.js.map +1 -0
- package/dist/src/services/LLMRouter.d.ts +61 -7
- package/dist/src/services/LLMRouter.d.ts.map +1 -1
- package/dist/src/services/LLMRouter.js +286 -22
- package/dist/src/services/LLMRouter.js.map +1 -1
- package/dist/src/services/audit-logger.service.d.ts +168 -0
- package/dist/src/services/audit-logger.service.d.ts.map +1 -0
- package/dist/src/services/audit-logger.service.js +331 -0
- package/dist/src/services/audit-logger.service.js.map +1 -0
- package/dist/src/services/enhanced-embeddings.d.ts +271 -0
- package/dist/src/services/enhanced-embeddings.d.ts.map +1 -0
- package/dist/src/services/enhanced-embeddings.js +1035 -0
- package/dist/src/services/enhanced-embeddings.js.map +1 -0
- package/dist/src/services/federated-learning.d.ts +172 -0
- package/dist/src/services/federated-learning.d.ts.map +1 -0
- package/dist/src/services/federated-learning.js +342 -0
- package/dist/src/services/federated-learning.js.map +1 -0
- package/dist/src/simd/index.d.ts +9 -0
- package/dist/src/simd/index.d.ts.map +1 -0
- package/dist/src/simd/index.js +27 -0
- package/dist/src/simd/index.js.map +1 -0
- package/dist/src/simd/simd-vector-ops.d.ts +332 -0
- package/dist/src/simd/simd-vector-ops.d.ts.map +1 -0
- package/dist/src/simd/simd-vector-ops.js +945 -0
- package/dist/src/simd/simd-vector-ops.js.map +1 -0
- package/dist/src/types/database.types.d.ts +275 -0
- package/dist/src/types/database.types.d.ts.map +1 -0
- package/dist/src/types/database.types.js +55 -0
- package/dist/src/types/database.types.js.map +1 -0
- package/dist/src/types/graph.d.ts +29 -0
- package/dist/src/types/graph.d.ts.map +1 -0
- package/dist/src/types/graph.js +8 -0
- package/dist/src/types/graph.js.map +1 -0
- package/dist/src/utils/chalk-fallback.d.ts +3 -0
- package/dist/src/utils/chalk-fallback.d.ts.map +1 -0
- package/dist/src/utils/chalk-fallback.js +20 -0
- package/dist/src/utils/chalk-fallback.js.map +1 -0
- package/dist/src/utils/similarity.d.ts +11 -0
- package/dist/src/utils/similarity.d.ts.map +1 -0
- package/dist/src/utils/similarity.js +25 -0
- package/dist/src/utils/similarity.js.map +1 -0
- package/dist/src/wasm-loader.d.ts +66 -0
- package/dist/src/wasm-loader.d.ts.map +1 -0
- package/dist/src/wasm-loader.js +76 -0
- package/dist/src/wasm-loader.js.map +1 -0
- package/dist/src/wrappers/agentdb-fast.d.ts +139 -0
- package/dist/src/wrappers/agentdb-fast.d.ts.map +1 -0
- package/dist/src/wrappers/agentdb-fast.js +307 -0
- package/dist/src/wrappers/agentdb-fast.js.map +1 -0
- package/dist/src/wrappers/attention-fallbacks.d.ts +343 -0
- package/dist/src/wrappers/attention-fallbacks.d.ts.map +1 -0
- package/dist/src/wrappers/attention-fallbacks.js +1484 -0
- package/dist/src/wrappers/attention-fallbacks.js.map +1 -0
- package/dist/src/wrappers/attention-native.d.ts +111 -0
- package/dist/src/wrappers/attention-native.d.ts.map +1 -0
- package/dist/src/wrappers/attention-native.js +273 -0
- package/dist/src/wrappers/attention-native.js.map +1 -0
- package/dist/src/wrappers/embedding-service.d.ts +117 -0
- package/dist/src/wrappers/embedding-service.d.ts.map +1 -0
- package/dist/src/wrappers/embedding-service.js +370 -0
- package/dist/src/wrappers/embedding-service.js.map +1 -0
- package/dist/src/wrappers/gnn-wrapper.d.ts +71 -0
- package/dist/src/wrappers/gnn-wrapper.d.ts.map +1 -0
- package/dist/src/wrappers/gnn-wrapper.js +236 -0
- package/dist/src/wrappers/gnn-wrapper.js.map +1 -0
- package/dist/src/wrappers/index.d.ts +48 -0
- package/dist/src/wrappers/index.d.ts.map +1 -0
- package/dist/src/wrappers/index.js +83 -0
- package/dist/src/wrappers/index.js.map +1 -0
- package/package.json +77 -76
- package/scripts/postinstall.cjs +13 -144
- package/dist/agentdb.browser.js +0 -1685
- package/dist/agentdb.browser.js.map +0 -7
- package/dist/agentdb.browser.min.js +0 -3
- package/dist/agentdb.browser.min.js.map +0 -7
- package/dist/agentdb.wasm-loader.js +0 -207
- package/dist/backends/VectorBackend.d.ts +0 -120
- package/dist/backends/VectorBackend.js +0 -14
- package/dist/backends/VectorBackend.js.map +0 -1
- package/dist/optimizations/RVFOptimizer.d.ts +0 -225
- package/dist/optimizations/RVFOptimizer.js +0 -541
- package/dist/optimizations/RVFOptimizer.js.map +0 -1
- package/dist/schemas/frontier-schema.sql +0 -378
- package/dist/schemas/schema.sql +0 -382
- package/dist/simulation/cli.d.ts +0 -9
- package/dist/simulation/cli.d.ts.map +0 -1
- package/dist/simulation/cli.js +0 -68
- package/dist/simulation/cli.js.map +0 -1
- package/dist/simulation/runner.d.ts +0 -21
- package/dist/simulation/runner.d.ts.map +0 -1
- package/dist/simulation/runner.js +0 -241
- package/dist/simulation/runner.js.map +0 -1
- package/dist/simulation/scenarios/aidefence-integration.d.ts +0 -24
- package/dist/simulation/scenarios/aidefence-integration.d.ts.map +0 -1
- package/dist/simulation/scenarios/aidefence-integration.js +0 -124
- package/dist/simulation/scenarios/aidefence-integration.js.map +0 -1
- package/dist/simulation/scenarios/bmssp-integration.d.ts +0 -23
- package/dist/simulation/scenarios/bmssp-integration.d.ts.map +0 -1
- package/dist/simulation/scenarios/bmssp-integration.js +0 -104
- package/dist/simulation/scenarios/bmssp-integration.js.map +0 -1
- package/dist/simulation/scenarios/causal-reasoning.d.ts +0 -16
- package/dist/simulation/scenarios/causal-reasoning.d.ts.map +0 -1
- package/dist/simulation/scenarios/causal-reasoning.js +0 -108
- package/dist/simulation/scenarios/causal-reasoning.js.map +0 -1
- package/dist/simulation/scenarios/consciousness-explorer.d.ts +0 -25
- package/dist/simulation/scenarios/consciousness-explorer.d.ts.map +0 -1
- package/dist/simulation/scenarios/consciousness-explorer.js +0 -108
- package/dist/simulation/scenarios/consciousness-explorer.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.d.ts +0 -135
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.js +0 -158
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/index.d.ts +0 -14
- package/dist/simulation/scenarios/domain-examples/index.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/index.js +0 -14
- package/dist/simulation/scenarios/domain-examples/index.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.d.ts +0 -178
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.js +0 -218
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/medical-imaging.d.ts +0 -122
- package/dist/simulation/scenarios/domain-examples/medical-imaging.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/medical-imaging.js +0 -129
- package/dist/simulation/scenarios/domain-examples/medical-imaging.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.d.ts +0 -166
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.js +0 -161
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/scientific-research.d.ts +0 -151
- package/dist/simulation/scenarios/domain-examples/scientific-research.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/scientific-research.js +0 -187
- package/dist/simulation/scenarios/domain-examples/scientific-research.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/trading-systems.d.ts +0 -102
- package/dist/simulation/scenarios/domain-examples/trading-systems.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/trading-systems.js +0 -100
- package/dist/simulation/scenarios/domain-examples/trading-systems.js.map +0 -1
- package/dist/simulation/scenarios/goalie-integration.d.ts +0 -24
- package/dist/simulation/scenarios/goalie-integration.d.ts.map +0 -1
- package/dist/simulation/scenarios/goalie-integration.js +0 -121
- package/dist/simulation/scenarios/goalie-integration.js.map +0 -1
- package/dist/simulation/scenarios/graph-traversal.d.ts +0 -17
- package/dist/simulation/scenarios/graph-traversal.d.ts.map +0 -1
- package/dist/simulation/scenarios/graph-traversal.js +0 -101
- package/dist/simulation/scenarios/graph-traversal.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/attention-analysis.d.ts +0 -45
- package/dist/simulation/scenarios/latent-space/attention-analysis.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/attention-analysis.js +0 -435
- package/dist/simulation/scenarios/latent-space/attention-analysis.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/clustering-analysis.d.ts +0 -55
- package/dist/simulation/scenarios/latent-space/clustering-analysis.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/clustering-analysis.js +0 -606
- package/dist/simulation/scenarios/latent-space/clustering-analysis.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.d.ts +0 -74
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.js +0 -391
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.d.ts +0 -51
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.js +0 -536
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/index.d.ts +0 -35
- package/dist/simulation/scenarios/latent-space/index.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/index.js +0 -35
- package/dist/simulation/scenarios/latent-space/index.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/neural-augmentation.d.ts +0 -52
- package/dist/simulation/scenarios/latent-space/neural-augmentation.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/neural-augmentation.js +0 -472
- package/dist/simulation/scenarios/latent-space/neural-augmentation.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.d.ts +0 -53
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.js +0 -391
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.d.ts +0 -52
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.js +0 -514
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/traversal-optimization.d.ts +0 -53
- package/dist/simulation/scenarios/latent-space/traversal-optimization.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/traversal-optimization.js +0 -595
- package/dist/simulation/scenarios/latent-space/traversal-optimization.js.map +0 -1
- package/dist/simulation/scenarios/lean-agentic-swarm.d.ts +0 -18
- package/dist/simulation/scenarios/lean-agentic-swarm.d.ts.map +0 -1
- package/dist/simulation/scenarios/lean-agentic-swarm.js +0 -132
- package/dist/simulation/scenarios/lean-agentic-swarm.js.map +0 -1
- package/dist/simulation/scenarios/multi-agent-swarm.d.ts +0 -17
- package/dist/simulation/scenarios/multi-agent-swarm.d.ts.map +0 -1
- package/dist/simulation/scenarios/multi-agent-swarm.js +0 -111
- package/dist/simulation/scenarios/multi-agent-swarm.js.map +0 -1
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.d.ts +0 -23
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.d.ts.map +0 -1
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.js +0 -98
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.js.map +0 -1
- package/dist/simulation/scenarios/reflexion-learning.d.ts +0 -16
- package/dist/simulation/scenarios/reflexion-learning.d.ts.map +0 -1
- package/dist/simulation/scenarios/reflexion-learning.js +0 -98
- package/dist/simulation/scenarios/reflexion-learning.js.map +0 -1
- package/dist/simulation/scenarios/research-swarm.d.ts +0 -24
- package/dist/simulation/scenarios/research-swarm.d.ts.map +0 -1
- package/dist/simulation/scenarios/research-swarm.js +0 -146
- package/dist/simulation/scenarios/research-swarm.js.map +0 -1
- package/dist/simulation/scenarios/skill-evolution.d.ts +0 -16
- package/dist/simulation/scenarios/skill-evolution.d.ts.map +0 -1
- package/dist/simulation/scenarios/skill-evolution.js +0 -109
- package/dist/simulation/scenarios/skill-evolution.js.map +0 -1
- package/dist/simulation/scenarios/stock-market-emergence.d.ts +0 -33
- package/dist/simulation/scenarios/stock-market-emergence.d.ts.map +0 -1
- package/dist/simulation/scenarios/stock-market-emergence.js +0 -246
- package/dist/simulation/scenarios/stock-market-emergence.js.map +0 -1
- package/dist/simulation/scenarios/strange-loops.d.ts +0 -18
- package/dist/simulation/scenarios/strange-loops.d.ts.map +0 -1
- package/dist/simulation/scenarios/strange-loops.js +0 -133
- package/dist/simulation/scenarios/strange-loops.js.map +0 -1
- package/dist/simulation/scenarios/sublinear-solver.d.ts +0 -22
- package/dist/simulation/scenarios/sublinear-solver.d.ts.map +0 -1
- package/dist/simulation/scenarios/sublinear-solver.js +0 -82
- package/dist/simulation/scenarios/sublinear-solver.js.map +0 -1
- package/dist/simulation/scenarios/temporal-lead-solver.d.ts +0 -23
- package/dist/simulation/scenarios/temporal-lead-solver.d.ts.map +0 -1
- package/dist/simulation/scenarios/temporal-lead-solver.js +0 -90
- package/dist/simulation/scenarios/temporal-lead-solver.js.map +0 -1
- package/dist/simulation/scenarios/voting-system-consensus.d.ts +0 -27
- package/dist/simulation/scenarios/voting-system-consensus.d.ts.map +0 -1
- package/dist/simulation/scenarios/voting-system-consensus.js +0 -175
- package/dist/simulation/scenarios/voting-system-consensus.js.map +0 -1
- package/dist/simulation/types.d.ts +0 -162
- package/dist/simulation/types.d.ts.map +0 -1
- package/dist/simulation/types.js +0 -5
- package/dist/simulation/types.js.map +0 -1
- package/dist/simulation/utils/PerformanceOptimizer.d.ts +0 -115
- package/dist/simulation/utils/PerformanceOptimizer.d.ts.map +0 -1
- package/dist/simulation/utils/PerformanceOptimizer.js +0 -223
- package/dist/simulation/utils/PerformanceOptimizer.js.map +0 -1
- package/dist/src/backends/index.cjs +0 -6
- package/dist/src/backends/ruvector/GuardedVectorBackend.d.ts +0 -93
- package/dist/src/backends/ruvector/GuardedVectorBackend.d.ts.map +0 -1
- package/dist/src/backends/ruvector/GuardedVectorBackend.js +0 -182
- package/dist/src/backends/ruvector/GuardedVectorBackend.js.map +0 -1
- package/dist/src/consensus/RaftConsensus.d.ts +0 -220
- package/dist/src/consensus/RaftConsensus.d.ts.map +0 -1
- package/dist/src/consensus/RaftConsensus.js +0 -762
- package/dist/src/consensus/RaftConsensus.js.map +0 -1
- package/dist/src/controllers/HierarchicalMemory.d.ts +0 -197
- package/dist/src/controllers/HierarchicalMemory.d.ts.map +0 -1
- package/dist/src/controllers/HierarchicalMemory.js +0 -519
- package/dist/src/controllers/HierarchicalMemory.js.map +0 -1
- package/dist/src/controllers/MemoryConsolidation.d.ts +0 -142
- package/dist/src/controllers/MemoryConsolidation.d.ts.map +0 -1
- package/dist/src/controllers/MemoryConsolidation.js +0 -479
- package/dist/src/controllers/MemoryConsolidation.js.map +0 -1
- package/dist/src/controllers/QUICConnection.d.ts +0 -122
- package/dist/src/controllers/QUICConnection.d.ts.map +0 -1
- package/dist/src/controllers/QUICConnection.js +0 -329
- package/dist/src/controllers/QUICConnection.js.map +0 -1
- package/dist/src/controllers/QUICConnectionPool.d.ts +0 -83
- package/dist/src/controllers/QUICConnectionPool.d.ts.map +0 -1
- package/dist/src/controllers/QUICConnectionPool.js +0 -256
- package/dist/src/controllers/QUICConnectionPool.js.map +0 -1
- package/dist/src/controllers/QUICStreamManager.d.ts +0 -114
- package/dist/src/controllers/QUICStreamManager.d.ts.map +0 -1
- package/dist/src/controllers/QUICStreamManager.js +0 -267
- package/dist/src/controllers/QUICStreamManager.js.map +0 -1
- package/dist/src/controllers/StreamingEmbeddingService.d.ts +0 -82
- package/dist/src/controllers/StreamingEmbeddingService.d.ts.map +0 -1
- package/dist/src/controllers/StreamingEmbeddingService.js +0 -243
- package/dist/src/controllers/StreamingEmbeddingService.js.map +0 -1
- package/dist/src/controllers/index.cjs +0 -6
- package/dist/src/coordination/MultiDatabaseCoordinator.d.ts +0 -348
- package/dist/src/coordination/MultiDatabaseCoordinator.d.ts.map +0 -1
- package/dist/src/coordination/MultiDatabaseCoordinator.js +0 -803
- package/dist/src/coordination/MultiDatabaseCoordinator.js.map +0 -1
- package/dist/src/coordination/index.d.ts +0 -10
- package/dist/src/coordination/index.d.ts.map +0 -1
- package/dist/src/coordination/index.js +0 -10
- package/dist/src/coordination/index.js.map +0 -1
- package/dist/src/index.cjs +0 -6
- package/dist/src/optimizations/RVFOptimizer.d.ts +0 -226
- package/dist/src/optimizations/RVFOptimizer.d.ts.map +0 -1
- package/dist/src/optimizations/RVFOptimizer.js +0 -541
- package/dist/src/optimizations/RVFOptimizer.js.map +0 -1
- package/dist/src/security/AttestationLog.d.ts +0 -70
- package/dist/src/security/AttestationLog.d.ts.map +0 -1
- package/dist/src/security/AttestationLog.js +0 -174
- package/dist/src/security/AttestationLog.js.map +0 -1
- package/dist/src/security/MutationGuard.d.ts +0 -83
- package/dist/src/security/MutationGuard.d.ts.map +0 -1
- package/dist/src/security/MutationGuard.js +0 -364
- package/dist/src/security/MutationGuard.js.map +0 -1
- package/dist/src/security/index.cjs +0 -6
- package/dist/src/security/index.d.ts +0 -15
- package/dist/src/security/index.d.ts.map +0 -1
- package/dist/src/security/index.js +0 -18
- package/dist/src/security/index.js.map +0 -1
- package/dist/src/services/GNNService.d.ts +0 -173
- package/dist/src/services/GNNService.d.ts.map +0 -1
- package/dist/src/services/GNNService.js +0 -639
- package/dist/src/services/GNNService.js.map +0 -1
- package/dist/src/services/GraphTransformerService.d.ts +0 -80
- package/dist/src/services/GraphTransformerService.d.ts.map +0 -1
- package/dist/src/services/GraphTransformerService.js +0 -369
- package/dist/src/services/GraphTransformerService.js.map +0 -1
- package/dist/src/services/SemanticRouter.d.ts +0 -83
- package/dist/src/services/SemanticRouter.d.ts.map +0 -1
- package/dist/src/services/SemanticRouter.js +0 -160
- package/dist/src/services/SemanticRouter.js.map +0 -1
- package/dist/src/services/SonaTrajectoryService.d.ts +0 -224
- package/dist/src/services/SonaTrajectoryService.d.ts.map +0 -1
- package/dist/src/services/SonaTrajectoryService.js +0 -539
- package/dist/src/services/SonaTrajectoryService.js.map +0 -1
- package/dist/src/utils/LegacyAttentionAdapter.d.ts +0 -93
- package/dist/src/utils/LegacyAttentionAdapter.d.ts.map +0 -1
- package/dist/src/utils/LegacyAttentionAdapter.js +0 -241
- package/dist/src/utils/LegacyAttentionAdapter.js.map +0 -1
- package/dist/src/utils/vector-math.d.ts +0 -29
- package/dist/src/utils/vector-math.d.ts.map +0 -1
- package/dist/src/utils/vector-math.js +0 -66
- package/dist/src/utils/vector-math.js.map +0 -1
|
@@ -0,0 +1,793 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector Quantization Module for AgentDB
|
|
3
|
+
*
|
|
4
|
+
* Provides memory-efficient vector storage through quantization techniques:
|
|
5
|
+
* - Scalar Quantization (4-bit and 8-bit)
|
|
6
|
+
* - Product Quantization with trainable codebooks
|
|
7
|
+
* - Quantized Vector Store with asymmetric distance computation
|
|
8
|
+
*
|
|
9
|
+
* @module optimizations/Quantization
|
|
10
|
+
*/
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// Scalar Quantization
|
|
13
|
+
// ============================================================================
|
|
14
|
+
/**
|
|
15
|
+
* Scalar quantization reduces memory by mapping floating-point values
|
|
16
|
+
* to lower-bit integer representations.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const sq = new ScalarQuantization();
|
|
21
|
+
* const vectors = [new Float32Array([0.1, 0.5, 0.9])];
|
|
22
|
+
* const quantized = sq.quantize(vectors, 8);
|
|
23
|
+
* const restored = sq.dequantize(quantized);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export class ScalarQuantization {
|
|
27
|
+
/**
|
|
28
|
+
* Quantize vectors to reduced bit representation
|
|
29
|
+
*
|
|
30
|
+
* @param vectors - Array of vectors to quantize
|
|
31
|
+
* @param bits - Target bit depth (4 or 8)
|
|
32
|
+
* @returns Quantized vector container
|
|
33
|
+
*/
|
|
34
|
+
quantize(vectors, bits) {
|
|
35
|
+
if (vectors.length === 0) {
|
|
36
|
+
throw new Error('Cannot quantize empty vector array');
|
|
37
|
+
}
|
|
38
|
+
const dimension = vectors[0].length;
|
|
39
|
+
const maxVal = bits === 8 ? 255 : 15;
|
|
40
|
+
// Compute min/max per dimension for optimal range utilization
|
|
41
|
+
const mins = new Float32Array(dimension).fill(Infinity);
|
|
42
|
+
const maxs = new Float32Array(dimension).fill(-Infinity);
|
|
43
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
44
|
+
const vec = vectors[i];
|
|
45
|
+
for (let d = 0; d < dimension; d++) {
|
|
46
|
+
if (vec[d] < mins[d])
|
|
47
|
+
mins[d] = vec[d];
|
|
48
|
+
if (vec[d] > maxs[d])
|
|
49
|
+
maxs[d] = vec[d];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Compute scales per dimension
|
|
53
|
+
const scales = new Float32Array(dimension);
|
|
54
|
+
for (let d = 0; d < dimension; d++) {
|
|
55
|
+
const range = maxs[d] - mins[d];
|
|
56
|
+
scales[d] = range > 0 ? maxVal / range : 0;
|
|
57
|
+
}
|
|
58
|
+
// Allocate output buffer
|
|
59
|
+
const bytesPerVector = bits === 8 ? dimension : Math.ceil(dimension / 2);
|
|
60
|
+
const data = new Uint8Array(vectors.length * bytesPerVector);
|
|
61
|
+
// Quantize each vector
|
|
62
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
63
|
+
const vec = vectors[i];
|
|
64
|
+
const offset = i * bytesPerVector;
|
|
65
|
+
if (bits === 8) {
|
|
66
|
+
// 8-bit: one byte per value
|
|
67
|
+
for (let d = 0; d < dimension; d++) {
|
|
68
|
+
const normalized = (vec[d] - mins[d]) * scales[d];
|
|
69
|
+
data[offset + d] = Math.min(maxVal, Math.max(0, Math.round(normalized)));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// 4-bit: pack two values per byte
|
|
74
|
+
for (let d = 0; d < dimension; d += 2) {
|
|
75
|
+
const val1 = Math.min(maxVal, Math.max(0, Math.round((vec[d] - mins[d]) * scales[d])));
|
|
76
|
+
const val2 = d + 1 < dimension
|
|
77
|
+
? Math.min(maxVal, Math.max(0, Math.round((vec[d + 1] - mins[d + 1]) * scales[d + 1])))
|
|
78
|
+
: 0;
|
|
79
|
+
data[offset + Math.floor(d / 2)] = (val1 << 4) | val2;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
type: bits === 8 ? 'scalar-8bit' : 'scalar-4bit',
|
|
85
|
+
dimension,
|
|
86
|
+
count: vectors.length,
|
|
87
|
+
data,
|
|
88
|
+
mins,
|
|
89
|
+
maxs,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Dequantize vectors back to floating-point representation
|
|
94
|
+
*
|
|
95
|
+
* @param quantized - Quantized vector container
|
|
96
|
+
* @returns Array of reconstructed vectors
|
|
97
|
+
*/
|
|
98
|
+
dequantize(quantized) {
|
|
99
|
+
const { type, dimension, count, data, mins, maxs } = quantized;
|
|
100
|
+
if (!mins || !maxs) {
|
|
101
|
+
throw new Error('Scalar quantized vectors require mins and maxs');
|
|
102
|
+
}
|
|
103
|
+
const bits = type === 'scalar-8bit' ? 8 : 4;
|
|
104
|
+
const maxVal = bits === 8 ? 255 : 15;
|
|
105
|
+
const bytesPerVector = bits === 8 ? dimension : Math.ceil(dimension / 2);
|
|
106
|
+
// Compute inverse scales
|
|
107
|
+
const invScales = new Float32Array(dimension);
|
|
108
|
+
for (let d = 0; d < dimension; d++) {
|
|
109
|
+
const range = maxs[d] - mins[d];
|
|
110
|
+
invScales[d] = range / maxVal;
|
|
111
|
+
}
|
|
112
|
+
const vectors = new Array(count);
|
|
113
|
+
for (let i = 0; i < count; i++) {
|
|
114
|
+
const vec = new Float32Array(dimension);
|
|
115
|
+
const offset = i * bytesPerVector;
|
|
116
|
+
if (bits === 8) {
|
|
117
|
+
// 8-bit: one byte per value
|
|
118
|
+
for (let d = 0; d < dimension; d++) {
|
|
119
|
+
vec[d] = data[offset + d] * invScales[d] + mins[d];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// 4-bit: unpack two values per byte
|
|
124
|
+
for (let d = 0; d < dimension; d += 2) {
|
|
125
|
+
const packed = data[offset + Math.floor(d / 2)];
|
|
126
|
+
vec[d] = ((packed >> 4) & 0x0f) * invScales[d] + mins[d];
|
|
127
|
+
if (d + 1 < dimension) {
|
|
128
|
+
vec[d + 1] = (packed & 0x0f) * invScales[d + 1] + mins[d + 1];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
vectors[i] = vec;
|
|
133
|
+
}
|
|
134
|
+
return vectors;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Compute approximate squared Euclidean distance using quantized representation
|
|
138
|
+
*
|
|
139
|
+
* @param quantized - Quantized vectors
|
|
140
|
+
* @param index - Index of vector in quantized store
|
|
141
|
+
* @param query - Full-precision query vector
|
|
142
|
+
* @returns Approximate squared distance
|
|
143
|
+
*/
|
|
144
|
+
computeDistance(quantized, index, query) {
|
|
145
|
+
const { type, dimension, data, mins, maxs } = quantized;
|
|
146
|
+
if (!mins || !maxs) {
|
|
147
|
+
throw new Error('Scalar quantized vectors require mins and maxs');
|
|
148
|
+
}
|
|
149
|
+
const bits = type === 'scalar-8bit' ? 8 : 4;
|
|
150
|
+
const maxVal = bits === 8 ? 255 : 15;
|
|
151
|
+
const bytesPerVector = bits === 8 ? dimension : Math.ceil(dimension / 2);
|
|
152
|
+
const offset = index * bytesPerVector;
|
|
153
|
+
// Compute inverse scales
|
|
154
|
+
let distanceSq = 0;
|
|
155
|
+
if (bits === 8) {
|
|
156
|
+
for (let d = 0; d < dimension; d++) {
|
|
157
|
+
const range = maxs[d] - mins[d];
|
|
158
|
+
const invScale = range / maxVal;
|
|
159
|
+
const reconstructed = data[offset + d] * invScale + mins[d];
|
|
160
|
+
const diff = reconstructed - query[d];
|
|
161
|
+
distanceSq += diff * diff;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
for (let d = 0; d < dimension; d += 2) {
|
|
166
|
+
const packed = data[offset + Math.floor(d / 2)];
|
|
167
|
+
const range1 = maxs[d] - mins[d];
|
|
168
|
+
const invScale1 = range1 / maxVal;
|
|
169
|
+
const reconstructed1 = ((packed >> 4) & 0x0f) * invScale1 + mins[d];
|
|
170
|
+
const diff1 = reconstructed1 - query[d];
|
|
171
|
+
distanceSq += diff1 * diff1;
|
|
172
|
+
if (d + 1 < dimension) {
|
|
173
|
+
const range2 = maxs[d + 1] - mins[d + 1];
|
|
174
|
+
const invScale2 = range2 / maxVal;
|
|
175
|
+
const reconstructed2 = (packed & 0x0f) * invScale2 + mins[d + 1];
|
|
176
|
+
const diff2 = reconstructed2 - query[d + 1];
|
|
177
|
+
distanceSq += diff2 * diff2;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return distanceSq;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// ============================================================================
|
|
185
|
+
// Product Quantization
|
|
186
|
+
// ============================================================================
|
|
187
|
+
/**
|
|
188
|
+
* Product quantization divides vectors into subvectors and learns
|
|
189
|
+
* codebooks for each subspace, enabling high compression ratios.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* const pq = new ProductQuantization(8, 256);
|
|
194
|
+
* pq.train(trainingVectors);
|
|
195
|
+
* const encoded = pq.encode(vectors);
|
|
196
|
+
* const decoded = pq.decode(encoded);
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export class ProductQuantization {
|
|
200
|
+
/** Number of subvectors to divide each vector into */
|
|
201
|
+
numSubvectors;
|
|
202
|
+
/** Number of centroids per subvector codebook */
|
|
203
|
+
numCentroids;
|
|
204
|
+
/** Maximum training iterations */
|
|
205
|
+
maxIterations;
|
|
206
|
+
/** Codebooks: one per subvector, each containing numCentroids centroids */
|
|
207
|
+
codebooks = [];
|
|
208
|
+
/** Dimension of each subvector */
|
|
209
|
+
subvectorDim = 0;
|
|
210
|
+
/** Total vector dimension */
|
|
211
|
+
dimension = 0;
|
|
212
|
+
/** Whether the model has been trained */
|
|
213
|
+
trained = false;
|
|
214
|
+
/**
|
|
215
|
+
* Create a product quantization instance
|
|
216
|
+
*
|
|
217
|
+
* @param numSubvectors - Number of subvectors (default: 8)
|
|
218
|
+
* @param numCentroids - Centroids per subvector (default: 256, max 256 for byte encoding)
|
|
219
|
+
* @param maxIterations - Maximum k-means iterations (default: 25)
|
|
220
|
+
*/
|
|
221
|
+
constructor(numSubvectors = 8, numCentroids = 256, maxIterations = 25) {
|
|
222
|
+
if (numCentroids > 256) {
|
|
223
|
+
throw new Error('Number of centroids cannot exceed 256 for byte encoding');
|
|
224
|
+
}
|
|
225
|
+
this.numSubvectors = numSubvectors;
|
|
226
|
+
this.numCentroids = numCentroids;
|
|
227
|
+
this.maxIterations = maxIterations;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Train codebooks from sample vectors using k-means clustering
|
|
231
|
+
*
|
|
232
|
+
* @param vectors - Training vectors
|
|
233
|
+
*/
|
|
234
|
+
train(vectors) {
|
|
235
|
+
if (vectors.length === 0) {
|
|
236
|
+
throw new Error('Cannot train on empty vector set');
|
|
237
|
+
}
|
|
238
|
+
this.dimension = vectors[0].length;
|
|
239
|
+
if (this.dimension % this.numSubvectors !== 0) {
|
|
240
|
+
throw new Error(`Vector dimension ${this.dimension} must be divisible by numSubvectors ${this.numSubvectors}`);
|
|
241
|
+
}
|
|
242
|
+
this.subvectorDim = this.dimension / this.numSubvectors;
|
|
243
|
+
this.codebooks = new Array(this.numSubvectors);
|
|
244
|
+
// Train each subvector codebook independently
|
|
245
|
+
for (let m = 0; m < this.numSubvectors; m++) {
|
|
246
|
+
// Extract subvectors for this segment
|
|
247
|
+
const subvectors = vectors.map((vec) => {
|
|
248
|
+
const start = m * this.subvectorDim;
|
|
249
|
+
return vec.slice(start, start + this.subvectorDim);
|
|
250
|
+
});
|
|
251
|
+
// Train codebook using k-means
|
|
252
|
+
this.codebooks[m] = this.trainCodebook(subvectors);
|
|
253
|
+
}
|
|
254
|
+
this.trained = true;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Train a single codebook using k-means clustering
|
|
258
|
+
*
|
|
259
|
+
* @param subvectors - Subvectors for this segment
|
|
260
|
+
* @returns Codebook as flattened Float32Array
|
|
261
|
+
*/
|
|
262
|
+
trainCodebook(subvectors) {
|
|
263
|
+
const k = Math.min(this.numCentroids, subvectors.length);
|
|
264
|
+
const dim = this.subvectorDim;
|
|
265
|
+
// Initialize centroids using k-means++ strategy
|
|
266
|
+
const centroids = new Float32Array(k * dim);
|
|
267
|
+
const usedIndices = new Set();
|
|
268
|
+
// First centroid: random
|
|
269
|
+
let idx = Math.floor(Math.random() * subvectors.length);
|
|
270
|
+
usedIndices.add(idx);
|
|
271
|
+
centroids.set(subvectors[idx], 0);
|
|
272
|
+
// Remaining centroids: probability proportional to squared distance
|
|
273
|
+
for (let c = 1; c < k; c++) {
|
|
274
|
+
const distances = new Float32Array(subvectors.length);
|
|
275
|
+
let totalDist = 0;
|
|
276
|
+
for (let i = 0; i < subvectors.length; i++) {
|
|
277
|
+
if (usedIndices.has(i)) {
|
|
278
|
+
distances[i] = 0;
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
// Find minimum distance to existing centroids
|
|
282
|
+
let minDist = Infinity;
|
|
283
|
+
for (let j = 0; j < c; j++) {
|
|
284
|
+
const dist = this.squaredDistance(subvectors[i], centroids.subarray(j * dim, (j + 1) * dim));
|
|
285
|
+
if (dist < minDist)
|
|
286
|
+
minDist = dist;
|
|
287
|
+
}
|
|
288
|
+
distances[i] = minDist;
|
|
289
|
+
totalDist += minDist;
|
|
290
|
+
}
|
|
291
|
+
// Select next centroid proportionally
|
|
292
|
+
let threshold = Math.random() * totalDist;
|
|
293
|
+
for (let i = 0; i < subvectors.length; i++) {
|
|
294
|
+
threshold -= distances[i];
|
|
295
|
+
if (threshold <= 0) {
|
|
296
|
+
usedIndices.add(i);
|
|
297
|
+
centroids.set(subvectors[i], c * dim);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// K-means iterations
|
|
303
|
+
const assignments = new Uint16Array(subvectors.length);
|
|
304
|
+
const counts = new Uint32Array(k);
|
|
305
|
+
const sums = new Float32Array(k * dim);
|
|
306
|
+
for (let iter = 0; iter < this.maxIterations; iter++) {
|
|
307
|
+
// Assignment step
|
|
308
|
+
let changed = false;
|
|
309
|
+
for (let i = 0; i < subvectors.length; i++) {
|
|
310
|
+
let bestCentroid = 0;
|
|
311
|
+
let bestDist = Infinity;
|
|
312
|
+
for (let c = 0; c < k; c++) {
|
|
313
|
+
const dist = this.squaredDistance(subvectors[i], centroids.subarray(c * dim, (c + 1) * dim));
|
|
314
|
+
if (dist < bestDist) {
|
|
315
|
+
bestDist = dist;
|
|
316
|
+
bestCentroid = c;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (assignments[i] !== bestCentroid) {
|
|
320
|
+
assignments[i] = bestCentroid;
|
|
321
|
+
changed = true;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (!changed)
|
|
325
|
+
break;
|
|
326
|
+
// Update step
|
|
327
|
+
counts.fill(0);
|
|
328
|
+
sums.fill(0);
|
|
329
|
+
for (let i = 0; i < subvectors.length; i++) {
|
|
330
|
+
const c = assignments[i];
|
|
331
|
+
counts[c]++;
|
|
332
|
+
for (let d = 0; d < dim; d++) {
|
|
333
|
+
sums[c * dim + d] += subvectors[i][d];
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
for (let c = 0; c < k; c++) {
|
|
337
|
+
if (counts[c] > 0) {
|
|
338
|
+
for (let d = 0; d < dim; d++) {
|
|
339
|
+
centroids[c * dim + d] = sums[c * dim + d] / counts[c];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return centroids;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Encode vectors using trained codebooks
|
|
348
|
+
*
|
|
349
|
+
* @param vectors - Vectors to encode
|
|
350
|
+
* @returns Quantized vector container
|
|
351
|
+
*/
|
|
352
|
+
encode(vectors) {
|
|
353
|
+
if (!this.trained) {
|
|
354
|
+
throw new Error('Product quantization model must be trained before encoding');
|
|
355
|
+
}
|
|
356
|
+
const data = new Uint8Array(vectors.length * this.numSubvectors);
|
|
357
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
358
|
+
const vec = vectors[i];
|
|
359
|
+
for (let m = 0; m < this.numSubvectors; m++) {
|
|
360
|
+
const start = m * this.subvectorDim;
|
|
361
|
+
const subvector = vec.slice(start, start + this.subvectorDim);
|
|
362
|
+
const codebook = this.codebooks[m];
|
|
363
|
+
// Find nearest centroid
|
|
364
|
+
let bestIdx = 0;
|
|
365
|
+
let bestDist = Infinity;
|
|
366
|
+
for (let c = 0; c < this.numCentroids; c++) {
|
|
367
|
+
const centroid = codebook.subarray(c * this.subvectorDim, (c + 1) * this.subvectorDim);
|
|
368
|
+
const dist = this.squaredDistance(subvector, centroid);
|
|
369
|
+
if (dist < bestDist) {
|
|
370
|
+
bestDist = dist;
|
|
371
|
+
bestIdx = c;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
data[i * this.numSubvectors + m] = bestIdx;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
type: 'product',
|
|
379
|
+
dimension: this.dimension,
|
|
380
|
+
count: vectors.length,
|
|
381
|
+
data,
|
|
382
|
+
codebooks: this.codebooks,
|
|
383
|
+
numSubvectors: this.numSubvectors,
|
|
384
|
+
subvectorDim: this.subvectorDim,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Decode quantized vectors back to approximate floating-point representation
|
|
389
|
+
*
|
|
390
|
+
* @param quantized - Quantized vector container
|
|
391
|
+
* @returns Array of reconstructed vectors
|
|
392
|
+
*/
|
|
393
|
+
decode(quantized) {
|
|
394
|
+
const { dimension, count, data, codebooks, numSubvectors, subvectorDim } = quantized;
|
|
395
|
+
if (!codebooks || numSubvectors === undefined || subvectorDim === undefined) {
|
|
396
|
+
throw new Error('Product quantized vectors require codebooks');
|
|
397
|
+
}
|
|
398
|
+
const vectors = new Array(count);
|
|
399
|
+
for (let i = 0; i < count; i++) {
|
|
400
|
+
const vec = new Float32Array(dimension);
|
|
401
|
+
for (let m = 0; m < numSubvectors; m++) {
|
|
402
|
+
const centroidIdx = data[i * numSubvectors + m];
|
|
403
|
+
const codebook = codebooks[m];
|
|
404
|
+
const centroid = codebook.subarray(centroidIdx * subvectorDim, (centroidIdx + 1) * subvectorDim);
|
|
405
|
+
vec.set(centroid, m * subvectorDim);
|
|
406
|
+
}
|
|
407
|
+
vectors[i] = vec;
|
|
408
|
+
}
|
|
409
|
+
return vectors;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Compute squared Euclidean distance between two vectors
|
|
413
|
+
*/
|
|
414
|
+
squaredDistance(a, b) {
|
|
415
|
+
let sum = 0;
|
|
416
|
+
for (let i = 0; i < a.length; i++) {
|
|
417
|
+
const diff = a[i] - b[i];
|
|
418
|
+
sum += diff * diff;
|
|
419
|
+
}
|
|
420
|
+
return sum;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Check if the model has been trained
|
|
424
|
+
*/
|
|
425
|
+
isTrained() {
|
|
426
|
+
return this.trained;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Get codebooks for serialization
|
|
430
|
+
*/
|
|
431
|
+
getCodebooks() {
|
|
432
|
+
return this.codebooks;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Load pre-trained codebooks
|
|
436
|
+
*
|
|
437
|
+
* @param codebooks - Array of codebook Float32Arrays
|
|
438
|
+
* @param dimension - Original vector dimension
|
|
439
|
+
*/
|
|
440
|
+
loadCodebooks(codebooks, dimension) {
|
|
441
|
+
this.codebooks = codebooks;
|
|
442
|
+
this.dimension = dimension;
|
|
443
|
+
this.subvectorDim = dimension / this.numSubvectors;
|
|
444
|
+
this.trained = true;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
// ============================================================================
|
|
448
|
+
// Quantized Vector Store
|
|
449
|
+
// ============================================================================
|
|
450
|
+
/**
|
|
451
|
+
* A vector store that maintains vectors in quantized form for memory efficiency.
|
|
452
|
+
* Uses asymmetric distance computation for improved accuracy during search.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```typescript
|
|
456
|
+
* const store = new QuantizedVectorStore({ type: 'scalar-8bit' });
|
|
457
|
+
* store.add('vec1', new Float32Array([0.1, 0.2, 0.3]));
|
|
458
|
+
* const results = store.search(new Float32Array([0.1, 0.2, 0.3]), 5);
|
|
459
|
+
* ```
|
|
460
|
+
*/
|
|
461
|
+
export class QuantizedVectorStore {
|
|
462
|
+
/** Quantization configuration */
|
|
463
|
+
config;
|
|
464
|
+
/** Scalar quantization instance */
|
|
465
|
+
scalarQuantizer = null;
|
|
466
|
+
/** Product quantization instance */
|
|
467
|
+
productQuantizer = null;
|
|
468
|
+
/** ID to index mapping */
|
|
469
|
+
idToIndex = new Map();
|
|
470
|
+
/** Index to ID mapping */
|
|
471
|
+
indexToId = [];
|
|
472
|
+
/** Raw vectors buffer (before quantization) */
|
|
473
|
+
rawVectors = [];
|
|
474
|
+
/** Quantized vectors */
|
|
475
|
+
quantizedData = null;
|
|
476
|
+
/** Whether the store needs reindexing */
|
|
477
|
+
dirty = false;
|
|
478
|
+
/** Batch size for auto-reindexing */
|
|
479
|
+
batchSize = 1000;
|
|
480
|
+
/**
|
|
481
|
+
* Create a quantized vector store
|
|
482
|
+
*
|
|
483
|
+
* @param config - Quantization configuration
|
|
484
|
+
*/
|
|
485
|
+
constructor(config) {
|
|
486
|
+
this.config = {
|
|
487
|
+
type: config.type,
|
|
488
|
+
numSubvectors: config.numSubvectors ?? 8,
|
|
489
|
+
numCentroids: config.numCentroids ?? 256,
|
|
490
|
+
trainingIterations: config.trainingIterations ?? 25,
|
|
491
|
+
};
|
|
492
|
+
if (config.type === 'scalar-4bit' || config.type === 'scalar-8bit') {
|
|
493
|
+
this.scalarQuantizer = new ScalarQuantization();
|
|
494
|
+
}
|
|
495
|
+
else if (config.type === 'product') {
|
|
496
|
+
this.productQuantizer = new ProductQuantization(this.config.numSubvectors, this.config.numCentroids, this.config.trainingIterations);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Add a vector to the store
|
|
501
|
+
*
|
|
502
|
+
* @param id - Unique identifier for the vector
|
|
503
|
+
* @param vector - The vector to add
|
|
504
|
+
*/
|
|
505
|
+
add(id, vector) {
|
|
506
|
+
if (this.idToIndex.has(id)) {
|
|
507
|
+
// Update existing vector
|
|
508
|
+
const index = this.idToIndex.get(id);
|
|
509
|
+
this.rawVectors[index] = vector;
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
// Add new vector
|
|
513
|
+
const index = this.rawVectors.length;
|
|
514
|
+
this.idToIndex.set(id, index);
|
|
515
|
+
this.indexToId.push(id);
|
|
516
|
+
this.rawVectors.push(vector);
|
|
517
|
+
}
|
|
518
|
+
this.dirty = true;
|
|
519
|
+
// Auto-reindex periodically for large stores
|
|
520
|
+
if (this.rawVectors.length % this.batchSize === 0) {
|
|
521
|
+
this.reindex();
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Add multiple vectors in batch
|
|
526
|
+
*
|
|
527
|
+
* @param entries - Array of [id, vector] pairs
|
|
528
|
+
*/
|
|
529
|
+
addBatch(entries) {
|
|
530
|
+
for (const [id, vector] of entries) {
|
|
531
|
+
if (this.idToIndex.has(id)) {
|
|
532
|
+
const index = this.idToIndex.get(id);
|
|
533
|
+
this.rawVectors[index] = vector;
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
const index = this.rawVectors.length;
|
|
537
|
+
this.idToIndex.set(id, index);
|
|
538
|
+
this.indexToId.push(id);
|
|
539
|
+
this.rawVectors.push(vector);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
this.dirty = true;
|
|
543
|
+
this.reindex();
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Remove a vector from the store
|
|
547
|
+
*
|
|
548
|
+
* @param id - ID of vector to remove
|
|
549
|
+
* @returns True if vector was removed
|
|
550
|
+
*/
|
|
551
|
+
remove(id) {
|
|
552
|
+
const index = this.idToIndex.get(id);
|
|
553
|
+
if (index === undefined)
|
|
554
|
+
return false;
|
|
555
|
+
// Swap with last element for O(1) removal
|
|
556
|
+
const lastIndex = this.rawVectors.length - 1;
|
|
557
|
+
if (index !== lastIndex) {
|
|
558
|
+
const lastId = this.indexToId[lastIndex];
|
|
559
|
+
this.rawVectors[index] = this.rawVectors[lastIndex];
|
|
560
|
+
this.indexToId[index] = lastId;
|
|
561
|
+
this.idToIndex.set(lastId, index);
|
|
562
|
+
}
|
|
563
|
+
this.rawVectors.pop();
|
|
564
|
+
this.indexToId.pop();
|
|
565
|
+
this.idToIndex.delete(id);
|
|
566
|
+
this.dirty = true;
|
|
567
|
+
return true;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Force reindexing of quantized data
|
|
571
|
+
*/
|
|
572
|
+
reindex() {
|
|
573
|
+
if (this.rawVectors.length === 0) {
|
|
574
|
+
this.quantizedData = null;
|
|
575
|
+
this.dirty = false;
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (this.config.type === 'product') {
|
|
579
|
+
// Train product quantizer if needed
|
|
580
|
+
if (!this.productQuantizer.isTrained()) {
|
|
581
|
+
this.productQuantizer.train(this.rawVectors);
|
|
582
|
+
}
|
|
583
|
+
this.quantizedData = this.productQuantizer.encode(this.rawVectors);
|
|
584
|
+
}
|
|
585
|
+
else {
|
|
586
|
+
// Scalar quantization
|
|
587
|
+
const bits = this.config.type === 'scalar-8bit' ? 8 : 4;
|
|
588
|
+
this.quantizedData = this.scalarQuantizer.quantize(this.rawVectors, bits);
|
|
589
|
+
}
|
|
590
|
+
this.dirty = false;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Search for nearest neighbors using asymmetric distance computation
|
|
594
|
+
*
|
|
595
|
+
* @param query - Query vector (full precision)
|
|
596
|
+
* @param k - Number of neighbors to return
|
|
597
|
+
* @returns Array of search results sorted by distance
|
|
598
|
+
*/
|
|
599
|
+
search(query, k) {
|
|
600
|
+
if (this.rawVectors.length === 0) {
|
|
601
|
+
return [];
|
|
602
|
+
}
|
|
603
|
+
// Ensure quantized data is up to date
|
|
604
|
+
if (this.dirty || !this.quantizedData) {
|
|
605
|
+
this.reindex();
|
|
606
|
+
}
|
|
607
|
+
const results = [];
|
|
608
|
+
const n = Math.min(k, this.rawVectors.length);
|
|
609
|
+
if (this.config.type === 'product') {
|
|
610
|
+
// Precompute distance tables for asymmetric distance computation (ADC)
|
|
611
|
+
const distanceTables = this.computeDistanceTables(query);
|
|
612
|
+
for (let i = 0; i < this.quantizedData.count; i++) {
|
|
613
|
+
const distance = this.computeADCDistance(i, distanceTables);
|
|
614
|
+
results.push({
|
|
615
|
+
id: this.indexToId[i],
|
|
616
|
+
distance,
|
|
617
|
+
index: i,
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
// Scalar quantization with direct distance computation
|
|
623
|
+
for (let i = 0; i < this.quantizedData.count; i++) {
|
|
624
|
+
const distance = this.scalarQuantizer.computeDistance(this.quantizedData, i, query);
|
|
625
|
+
results.push({
|
|
626
|
+
id: this.indexToId[i],
|
|
627
|
+
distance,
|
|
628
|
+
index: i,
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
// Sort by distance and return top k
|
|
633
|
+
results.sort((a, b) => a.distance - b.distance);
|
|
634
|
+
return results.slice(0, n);
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Precompute distance tables for asymmetric distance computation
|
|
638
|
+
* This enables efficient lookup during search
|
|
639
|
+
*
|
|
640
|
+
* @param query - Query vector
|
|
641
|
+
* @returns Distance lookup tables
|
|
642
|
+
*/
|
|
643
|
+
computeDistanceTables(query) {
|
|
644
|
+
const { numSubvectors, subvectorDim, codebooks } = this.quantizedData;
|
|
645
|
+
if (!codebooks || numSubvectors === undefined || subvectorDim === undefined) {
|
|
646
|
+
throw new Error('Invalid product quantized data');
|
|
647
|
+
}
|
|
648
|
+
const tables = new Array(numSubvectors);
|
|
649
|
+
for (let m = 0; m < numSubvectors; m++) {
|
|
650
|
+
const table = new Float32Array(this.config.numCentroids);
|
|
651
|
+
const querySubvector = query.slice(m * subvectorDim, (m + 1) * subvectorDim);
|
|
652
|
+
const codebook = codebooks[m];
|
|
653
|
+
for (let c = 0; c < this.config.numCentroids; c++) {
|
|
654
|
+
const centroid = codebook.subarray(c * subvectorDim, (c + 1) * subvectorDim);
|
|
655
|
+
let distSq = 0;
|
|
656
|
+
for (let d = 0; d < subvectorDim; d++) {
|
|
657
|
+
const diff = querySubvector[d] - centroid[d];
|
|
658
|
+
distSq += diff * diff;
|
|
659
|
+
}
|
|
660
|
+
table[c] = distSq;
|
|
661
|
+
}
|
|
662
|
+
tables[m] = table;
|
|
663
|
+
}
|
|
664
|
+
return tables;
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Compute asymmetric distance using precomputed tables
|
|
668
|
+
*
|
|
669
|
+
* @param index - Vector index
|
|
670
|
+
* @param tables - Precomputed distance tables
|
|
671
|
+
* @returns Squared distance
|
|
672
|
+
*/
|
|
673
|
+
computeADCDistance(index, tables) {
|
|
674
|
+
const { data, numSubvectors } = this.quantizedData;
|
|
675
|
+
let totalDist = 0;
|
|
676
|
+
for (let m = 0; m < numSubvectors; m++) {
|
|
677
|
+
const centroidIdx = data[index * numSubvectors + m];
|
|
678
|
+
totalDist += tables[m][centroidIdx];
|
|
679
|
+
}
|
|
680
|
+
return totalDist;
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* Get a vector by ID (reconstructed from quantized form)
|
|
684
|
+
*
|
|
685
|
+
* @param id - Vector ID
|
|
686
|
+
* @returns Reconstructed vector or null if not found
|
|
687
|
+
*/
|
|
688
|
+
get(id) {
|
|
689
|
+
const index = this.idToIndex.get(id);
|
|
690
|
+
if (index === undefined)
|
|
691
|
+
return null;
|
|
692
|
+
// Return raw vector for accuracy
|
|
693
|
+
return this.rawVectors[index];
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Check if a vector exists in the store
|
|
697
|
+
*
|
|
698
|
+
* @param id - Vector ID
|
|
699
|
+
* @returns True if vector exists
|
|
700
|
+
*/
|
|
701
|
+
has(id) {
|
|
702
|
+
return this.idToIndex.has(id);
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Get the number of vectors in the store
|
|
706
|
+
*/
|
|
707
|
+
get size() {
|
|
708
|
+
return this.rawVectors.length;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Get memory usage statistics
|
|
712
|
+
*/
|
|
713
|
+
getMemoryStats() {
|
|
714
|
+
const rawBytes = this.rawVectors.reduce((sum, v) => sum + v.byteLength, 0);
|
|
715
|
+
const quantizedBytes = this.quantizedData?.data.byteLength ?? 0;
|
|
716
|
+
const compressionRatio = quantizedBytes > 0 ? rawBytes / quantizedBytes : 0;
|
|
717
|
+
return {
|
|
718
|
+
rawBytes,
|
|
719
|
+
quantizedBytes,
|
|
720
|
+
compressionRatio,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Clear all vectors from the store
|
|
725
|
+
*/
|
|
726
|
+
clear() {
|
|
727
|
+
this.idToIndex.clear();
|
|
728
|
+
this.indexToId = [];
|
|
729
|
+
this.rawVectors = [];
|
|
730
|
+
this.quantizedData = null;
|
|
731
|
+
this.dirty = false;
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Export the store for serialization
|
|
735
|
+
*/
|
|
736
|
+
export() {
|
|
737
|
+
if (this.dirty) {
|
|
738
|
+
this.reindex();
|
|
739
|
+
}
|
|
740
|
+
return {
|
|
741
|
+
config: this.config,
|
|
742
|
+
ids: [...this.indexToId],
|
|
743
|
+
quantized: this.quantizedData,
|
|
744
|
+
codebooks: this.productQuantizer?.getCodebooks(),
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
// ============================================================================
|
|
749
|
+
// Utility Functions
|
|
750
|
+
// ============================================================================
|
|
751
|
+
/**
|
|
752
|
+
* Calculate the theoretical compression ratio for a quantization type
|
|
753
|
+
*
|
|
754
|
+
* @param type - Quantization type
|
|
755
|
+
* @param dimension - Vector dimension
|
|
756
|
+
* @param numSubvectors - Number of subvectors for product quantization
|
|
757
|
+
* @returns Compression ratio (original size / compressed size)
|
|
758
|
+
*/
|
|
759
|
+
export function calculateCompressionRatio(type, dimension, numSubvectors = 8) {
|
|
760
|
+
const originalBytes = dimension * 4; // Float32 = 4 bytes
|
|
761
|
+
switch (type) {
|
|
762
|
+
case 'scalar-8bit':
|
|
763
|
+
return originalBytes / dimension; // 1 byte per value = 4x compression
|
|
764
|
+
case 'scalar-4bit':
|
|
765
|
+
return originalBytes / Math.ceil(dimension / 2); // 0.5 bytes per value = 8x compression
|
|
766
|
+
case 'product':
|
|
767
|
+
return originalBytes / numSubvectors; // 1 byte per subvector
|
|
768
|
+
default:
|
|
769
|
+
return 1;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Estimate memory savings for a given number of vectors
|
|
774
|
+
*
|
|
775
|
+
* @param type - Quantization type
|
|
776
|
+
* @param dimension - Vector dimension
|
|
777
|
+
* @param numVectors - Number of vectors
|
|
778
|
+
* @param numSubvectors - Number of subvectors for product quantization
|
|
779
|
+
* @returns Memory savings in bytes
|
|
780
|
+
*/
|
|
781
|
+
export function estimateMemorySavings(type, dimension, numVectors, numSubvectors = 8) {
|
|
782
|
+
const originalBytes = dimension * 4 * numVectors;
|
|
783
|
+
const ratio = calculateCompressionRatio(type, dimension, numSubvectors);
|
|
784
|
+
const compressedBytes = Math.ceil(originalBytes / ratio);
|
|
785
|
+
const savedBytes = originalBytes - compressedBytes;
|
|
786
|
+
return {
|
|
787
|
+
originalBytes,
|
|
788
|
+
compressedBytes,
|
|
789
|
+
savedBytes,
|
|
790
|
+
savedPercentage: (savedBytes / originalBytes) * 100,
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
//# sourceMappingURL=Quantization.js.map
|