feat(embed): role-aware prompts + cache key distinguishment (P0-E2)#112
Closed
ohdearquant wants to merge 1 commit into
Closed
feat(embed): role-aware prompts + cache key distinguishment (P0-E2)#112ohdearquant wants to merge 1 commit into
ohdearquant wants to merge 1 commit into
Conversation
Fix document_instruction() to return "passage: " for E5 multilingual variants (was unconditionally None). Add EmbeddingRole enum (Query, Passage, Generic) and embed_query/embed_passage trait methods that apply model-specific prompt prefixes before forwarding. Extend CacheKey hash inputs with role.cache_tag() so query and passage embeddings of the same raw text are stored as separate cache entries. CachedEmbeddingService overrides both role-aware methods with prompt-application + role-keyed cache logic. Existing embed() uses Generic role for backwards compat. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Owner
Author
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.
Layer
L2 — embed service public API (PR8 of 11)
What
document_instruction()onEmbeddingModel: E5 multilingual variants returnSome("passage: "). BGE/MiniLM returnNone. Qwen returnsNoneon document side (raw passage per model card).EmbeddingRoleenum (Query|Passage|Generic) with distinctcache_tag()strings injected into Blake3 hash incache.rs::compute_key().EmbeddingServicetrait methods:embed_query(),embed_passage(). Applyquery_instruction()/document_instruction()prefix then callembed().CachedEmbeddingServiceoverrides both methods to call internalembed_with_role()with the appropriateEmbeddingRole, so cache keys are role-distinct.embed()usesEmbeddingRole::Genericfor backwards compatibility.Why
Two inseparable issues: (a) E5/Qwen need their canonical prompt prefixes applied for production-quality embeddings, (b) without role-aware cache keys,
embed_query("hello")andembed_passage("hello")collide in cache.Result
cache.rs): query vs passage vs generic produce distinct keys, deterministic, isolatedservice/tests.rs): E5 document instruction, BGE/MiniLM no document instruction, Qwen no document instruction,apply_prefix, role cache tags distinctCache migration
Existing on-disk caches written with
role:genericwill be separate from newrole:query/role:passagekeys. Old caches remain readable under Generic. No migration needed; callers moving fromembed()toembed_passage()will see misses until repopulation.Stack
Base: #111 (PR7 tokenizer e2e test)
Umbrella: #104
🤖 Generated with Claude Code