Skip to content

Crystlfly/AI-Vector-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

In-Memory AI Vector Search Engine

An ultra-low latency embedded vector search engine built from scratch in C++ and bound natively to the Node.js V8 runtime. Designed to perform high-dimensional semantic search with zero-copy memory translation and zero network overhead.

Architecture Algorithm Latency


Features

  • Native C++17 HNSW implementation for approximate nearest neighbor search.
  • Zero-copy memory transfer between JavaScript and C++ using Node-API (N-API).
  • Embedded in-memory architecture with no external vector database.
  • Sub-millisecond query latency.
  • Local text embeddings generated using Transformers.js.
  • React dashboard for interactive semantic search.

Architecture

HNSW (Hierarchical Navigable Small World) is a graph-based algorithm used for fast Approximate Nearest Neighbor (ANN) search in high-dimensional vector spaces.

Instead of performing a brute-force scan with O(N) complexity, HNSW organizes vectors into multiple graph layers, reducing search complexity to approximately O(log N).

The search engine core was implemented entirely in C++ to bypass the overhead introduced by the Node.js V8 garbage collector. Memory pointers and cache-friendly data structures provide significantly lower latency during intensive mathematical computations.


System Architecture

                     HTTP Request
┌─────────────────┐
│ React Frontend  │
│  (Dashboard UI) │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│ Express.js API  │
└────────┬────────┘
         │
         ▼
┌──────────────────────────────┐
│ Transformers.js              │
│ Local Embedding Model         │
│ all-mpnet-base-v2 (768D)      │
└────────┬─────────────────────┘
         │ Float32Array
         ▼
┌──────────────────────────────┐
│ Node-API (N-API) Bridge      │
│ Zero-Copy Pointer Extraction │
└────────┬─────────────────────┘
         │
         ▼
┌──────────────────────────────┐
│ Native C++ HNSW Engine       │
│ Graph Traversal              │
│ Top-K Nearest Search         │
└────────┬─────────────────────┘
         │
         ▼
      Match IDs

Zero-Copy V8 Bridge

The primary bottleneck in multi-language systems is data transfer between runtimes.

Instead of copying the 768-dimensional embedding from JavaScript into C++, the engine extracts a direct pointer to the underlying Float32Array memory block using Node-API, eliminating unnecessary memory copies.

Native Binding

// addon.cpp

napi_value SearchVector(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1];
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_typedarray_type type;
    size_t length;
    void* data;
    napi_value arraybuffer;
    size_t byte_offset;

    // Extract raw pointer from the V8 heap
    napi_get_typedarray_info(
        env,
        args[0],
        &type,
        &length,
        &data,
        &arraybuffer,
        &byte_offset
    );

    // Cast directly to float*
    float* query_vector = static_cast<float*>(data);

    // Execute HNSW search
    int top_match_id = hnsw_db->search(query_vector);

    napi_value result;
    napi_create_int32(env, top_match_id, &result);

    return result;
}

Performance Benchmarks

Metric Result
Dataset Size 10,000 vectors
Index Build Time 221 ms
Brute-Force Search 4.00 ms
HNSW Query Latency < 0.99 ms
Typical Query Time ~0.02 ms

Tech Stack

Core Engine

  • C++17
  • HNSW Algorithm

Native Build

  • Node-API (N-API)
  • node-gyp
  • MSVC / GCC

Backend

  • Node.js
  • Express.js

Machine Learning

  • Transformers.js
  • Hugging Face all-mpnet-base-v2 (768-dimensional embeddings)

Frontend

  • React
  • Vite
  • CSS Glassmorphism

Project Structure

AI-Vector-Engine/
│
├── vector-database-backend/
│   ├── addon.cpp              # Node-API bindings
│   ├── main.cpp               # Core vector engine
│   ├── HNSWDB.h               # HNSW graph implementation
│   ├── FlatDB.h               # Brute-force search implementation
│   ├── VectorMath.h           # Similarity calculations
│   ├── binding.gyp
│   ├── index.js
│   ├── server.js              # Express API server
│   └── package.json
│
├── vectordb-dashboard/
│   ├── src/
│   ├── public/
│   ├── index.html
│   ├── vite.config.js
│   └── package.json
│
└── README.md

Getting Started

1. Clone the Repository

git clone https://github.com/Crystlfly/AI-Vector-Engine.git
cd AI-Vector-Engine

2. Install Dependencies

npm install

3. Build the Native C++ Addon

Requires Visual Studio C++ Build Tools (Windows) or GCC (Linux).

npx node-gyp configure build

4. Start the Backend

node server.js

5. Start the React Frontend

npm run dev

Example Search Flow

  1. User submits a natural language query.
  2. Transformers.js converts the text into a 768-dimensional embedding.
  3. Node-API extracts a pointer to the underlying Float32Array.
  4. The C++ HNSW engine performs graph traversal.
  5. The nearest vector IDs are returned to Node.js.
  6. Results are displayed in the React dashboard.

Future Improvements

  • Top-K nearest neighbor retrieval.
  • Persistent disk storage.
  • Dynamic insertions and deletions.
  • Product Quantization (PQ).
  • SIMD vector optimization.
  • Multi-threaded indexing.
  • Distributed sharding.

License

MIT License.

About

Embedded vector search engine built in C++ with HNSW algorithm, bound to Node.js via N-API for sub-millisecond semantic search.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors