Skip to content

refactor: simplify vector embedding implementation#1674

Closed
vkozyura wants to merge 13 commits into
feat/vector-parityfrom
feat/vector-simplified
Closed

refactor: simplify vector embedding implementation#1674
vkozyura wants to merge 13 commits into
feat/vector-parityfrom
feat/vector-simplified

Conversation

@vkozyura

@vkozyura vkozyura commented Jul 2, 2026

Copy link
Copy Markdown
Contributor
  • 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.

- 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
vkozyura marked this pull request as draft July 2, 2026 13:17
vkozyura added 12 commits July 2, 2026 15:41
- 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
@vkozyura vkozyura closed this Jul 3, 2026
@vkozyura

vkozyura commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

switched to #1676

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant