refactor: simplify vector embedding implementation#1674
Closed
vkozyura wants to merge 13 commits into
Closed
Conversation
- Replace manual ONNX implementation with @xenova/transformers - Add deterministic hash-based fallback (port of Java's HashEmbeddingService) - Move embedding computation to CAP db.before handlers (async-friendly) - Keep synchronous pure-math functions in SQLite (COSINE_SIMILARITY, L2DISTANCE, L2NORMALIZE) This approach: - Reduces code from ~619 lines to ~275 lines - Uses maintained library instead of copied ONNX internals - Provides deterministic fallback for testing (same input = same output) - Solves sync/async problem by pre-computing embeddings at CAP level The VECTOR_EMBEDDING SQL function now throws an error if called directly, as embeddings should be computed via CAP handlers using @cds.vectorSource annotation.
vkozyura
requested review from
BobdenOs,
danjoa,
johannes-vogel,
patricebender,
sjvans and
stewsk
as code owners
July 2, 2026 13:17
vkozyura
marked this pull request as draft
July 2, 2026 13:17
- Extract cosineSimilarity(), l2Distance(), l2Normalize() as shared functions - SQLite functions now use these shared implementations - hashEmbedding() also uses shared l2Normalize() - Removes code duplication between SQLite functions and internal use
- Test vector functions: COSINE_SIMILARITY, L2DISTANCE, L2NORMALIZE - Test automatic embedding computation on INSERT/UPDATE - Test semantic search with ORDER BY similarity - Test hash-based fallback determinism - Update vectors.cds with @cds.vectorSource annotation and 384 dimensions
- Replace closeTo assertions with custom approxEq helper - Update VECTOR_EMBEDDING test to expect error (computed via CAP handlers)
- Remove @xenova/transformers dependency (async, can't work in SQLite functions) - Make VECTOR_EMBEDDING a synchronous SQLite function using hash-based embedding - Remove before handlers for pre-computing embeddings - Hash-based embedding is deterministic and suitable for testing/development - Add note for future sync embedding library integration
Define vector functions (COSINE_SIMILARITY, L2DISTANCE, L2NORMALIZE, VECTOR_EMBEDDING) inline like other SQLite functions. The vector_handling module now just exports helper functions.
- Move comprehensive vector function tests to functions.test.js - Add L2NORMALIZE tests (was missing) - Add semantic search tests (ORDER BY similarity, dynamic embedding) - Remove separate vector.test.js
Contributor
Author
|
switched to #1676 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This approach:
The VECTOR_EMBEDDING SQL function now throws an error if called directly, as embeddings should be computed via CAP handlers using @cds.vectorSource annotation.