Skip to content

Integrate sqlite-vec for HNSW vector search (follow-on to #1219) #1221

Closed

Description

@malibio

Background

Issue #1219 migrated the local DB from SurrealDB to SQLite (libsql). The migration is complete and all operations are working correctly and faster.

One item was intentionally deferred: the search_embeddings method currently uses a linear scan (load all non-stale embeddings, decode in Rust, compute cosine similarity in a loop). SurrealDB used an HNSW index via the `<|K,EF|>` KNN operator.

What needs to be done

  1. Add `sqlite-vec` to workspace `Cargo.toml` and `packages/core/Cargo.toml`
  2. In `SqliteStore::initialize_schema`: load the `sqlite-vec` extension and create the `vec_embeddings` virtual table:
    ```sql
    CREATE VIRTUAL TABLE IF NOT EXISTS vec_embeddings USING vec0(
    embedding_id TEXT PRIMARY KEY,
    vector FLOAT[768]
    );
    ```
  3. In `upsert_embeddings`: after inserting into `embedding`, also insert into `vec_embeddings`
  4. In `delete_embeddings`: also delete from `vec_embeddings`
  5. Rewrite `search_embeddings` to use the `vec0` KNN query instead of linear scan:
    ```sql
    SELECT ve.embedding_id, ve.distance
    FROM vec_embeddings ve
    WHERE ve.vector MATCH ? AND k = ?
    ```
    Then group by node_id and compute composite score (same density formula as current).

Why deferred

Performance context

SurrealDB HNSW search was O(log N). Current linear scan is O(N). For a 10k-node corpus with ~3 chunks/node (~30k vectors × 3072 bytes = ~90MB) the linear scan takes ~100ms. With sqlite-vec HNSW it would be ~5ms.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions