feat: add embedding generators and cache#1111
Merged
dluc merged 6 commits intomicrosoft:mainfrom Dec 2, 2025
Merged
Conversation
Implement embedding generation infrastructure with caching support: Core Components: - IEmbeddingGenerator interface for all providers - EmbeddingConstants with known model dimensions - CachedEmbeddingGenerator decorator for transparent caching Cache System: - IEmbeddingCache interface with SQLite implementation - EmbeddingCacheKey with SHA256 hashing (privacy: text never stored) - CachedEmbedding model with provider/model metadata - CacheModes enum: ReadWrite, ReadOnly, WriteOnly Embedding Providers: - OllamaEmbeddingGenerator for local models - OpenAIEmbeddingGenerator for OpenAI API - AzureOpenAIEmbeddingGenerator for Azure OpenAI - HuggingFaceEmbeddingGenerator for HuggingFace Inference API Configuration: - HuggingFaceEmbeddingsConfig with JSON discriminator - Updated EmbeddingsTypes enum with HuggingFace value Test coverage: 86.33% (133 new tests)
Changed from single TEXT key to composite primary key with individual columns (provider, model, dimensions, normalized, text_hash) for: - Easier debugging when inspecting SQLite database contents - Selective SELECT/DELETE with individual discriminators - Better query flexibility for cache management Added indexes on provider and (provider, model) for common query patterns.
dluc
commented
Dec 2, 2025
dluc
commented
Dec 2, 2025
dluc
commented
Dec 2, 2025
dluc
commented
Dec 2, 2025
dluc
commented
Dec 2, 2025
dluc
commented
Dec 2, 2025
There was a problem hiding this comment.
Pull request overview
This PR implements a comprehensive embedding generation and caching infrastructure for the KernelMemory project, adding support for multiple embedding providers with transparent caching capabilities.
Key Changes:
- Adds four embedding provider implementations (OpenAI, Azure OpenAI, Ollama, HuggingFace)
- Implements SQLite-based caching with SHA256 hashing for privacy (input text never stored)
- Provides decorator pattern for transparent caching with configurable modes (ReadWrite, ReadOnly, WriteOnly)
Reviewed changes
Copilot reviewed 29 out of 29 changed files in this pull request and generated 26 comments.
Show a summary per file
| File | Description |
|---|---|
tests/Core.Tests/GlobalUsings.cs |
Adds CA2000 and CA1861 suppressions for test code |
tests/Core.Tests/Embeddings/Providers/*.cs |
Comprehensive unit tests for all four embedding providers |
tests/Core.Tests/Embeddings/EmbeddingConstantsTests.cs |
Tests for known model dimensions and defaults |
tests/Core.Tests/Embeddings/EmbeddingCacheKeyTests.cs |
Tests for SHA256 hashing and cache key generation |
tests/Core.Tests/Embeddings/CachedEmbeddingTests.cs |
Tests for cached embedding model |
tests/Core.Tests/Embeddings/CachedEmbeddingGeneratorTests.cs |
Tests for caching decorator with mixed cache hits/misses |
tests/Core.Tests/Embeddings/Cache/SqliteEmbeddingCacheTests.cs |
Integration tests for SQLite cache with all modes |
tests/Core.Tests/Config/*.cs |
Tests for HuggingFace config and cache modes enum |
src/Core/Embeddings/Providers/*.cs |
Implementation of OpenAI, Ollama, HuggingFace, and Azure OpenAI generators |
src/Core/Embeddings/IEmbeddingGenerator.cs |
Core interface for all embedding providers |
src/Core/Embeddings/EmbeddingConstants.cs |
Known model dimensions and default configurations |
src/Core/Embeddings/CachedEmbeddingGenerator.cs |
Decorator for transparent caching with mode support |
src/Core/Embeddings/Cache/*.cs |
Cache interface, SQLite implementation, cache key, and cached embedding models |
src/Core/Config/Enums/*.cs |
CacheModes enum and HuggingFace addition to EmbeddingsTypes |
src/Core/Config/Embeddings/*.cs |
HuggingFace configuration and type discriminator updates |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tests/Core.Tests/Embeddings/Providers/AzureOpenAIEmbeddingGeneratorTests.cs
Show resolved
Hide resolved
tests/Core.Tests/Embeddings/Providers/AzureOpenAIEmbeddingGeneratorTests.cs
Show resolved
Hide resolved
tests/Core.Tests/Embeddings/Providers/AzureOpenAIEmbeddingGeneratorTests.cs
Show resolved
Hide resolved
tests/Core.Tests/Embeddings/Providers/OpenAIEmbeddingGeneratorTests.cs
Outdated
Show resolved
Hide resolved
Changes: - Allow null text in EmbeddingCacheKey.Create(), normalize to empty string - Remove unnecessary indexes (idx_timestamp, idx_provider, idx_model) - Rename 'normalized' column to 'is_normalized' for clarity - Rename 'timestamp' column to 'created_at' for clarity - Simplify timestamp documentation (debugging only, no invalidation)
b9ec398 to
12eb472
Compare
Embeddings are immutable - same text always produces the same vector. No need for cache invalidation or timestamps. Simplifies the schema.
CachedEmbedding now only stores the vector: - Removed TokenCount (token counting should be done before embedding) - Removed timestamp (embeddings are immutable, no invalidation needed) - StoreAsync signature simplified: (key, vector, ct) Final schema: provider, model, dimensions, is_normalized, text_length, text_hash, vector PRIMARY KEY (provider, model, dimensions, is_normalized, text_hash)
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.
Summary
Implements embedding generation infrastructure with caching support:
Core Components
Cache System
Embedding Providers
Security
Test Plan
format.shpassesbuild.shpasses with 0 warnings, 0 errorscoverage.shpasses