Repository navigation
fix: condition Gemini embedding queries and documents separately - #11012
Closed
hongdougao111 wants to merge 1 commit into
Closed
hongdougao111 wants to merge 1 commit into
hongdougao111 wants to merge 1 commit into
Conversation
Contributor
|
Sorry @hongdougao111, your pull request is larger than the review limit of 150,000 diff characters |
`gemini_embedding_source.py` embedded queries and documents with the same
configuration, so retrieval similarity had little to no discriminative power.
Two separate problems were involved.
Task conditioning was missing entirely:
- `gemini-embedding-001` supports `task_type` (`RETRIEVAL_QUERY` /
`RETRIEVAL_DOCUMENT`) but only `output_dimensionality` was sent.
- `gemini-embedding-2` dropped support for `task_type` and expects the task
baked into the text instead (`task: search result | query: {text}` for
queries, `title: none | text: {text}` for documents); neither was applied.
Separately, FAISS stored and searched raw provider vectors. `IndexFlatL2` with
`score = 1 - distance / 2` only equals cosine similarity for unit vectors, and
Gemini returns unnormalized vectors below 3072 dimensions whose magnitude
drifts with text length. The score was therefore dominated by vector magnitude
rather than meaning, which is why no similarity threshold could separate
relevant from irrelevant memories.
Changes:
- Add `EmbeddingProvider.get_query_embedding()`, defaulting to
`get_embedding()` so other providers are unaffected. `FaissVecDB.retrieve()`
and the dashboard query embedding now use it.
- Gemini adapter branches on model generation, with a
`embedding_task_type_mode` escape hatch (`auto` / `never` / `always`).
- L2-normalize vectors on insert, batch insert and search.
- Re-normalize existing indexes once on load so upgraded instances do not end
up with normalized queries against unnormalized stored vectors. The rewrite
is idempotent (zero vectors excluded from the check so they cannot retrigger
it), keeps the id-to-vector mapping intact, is skipped for index types that
cannot be reconstructed, and a failed write only warns instead of preventing
the knowledge base from starting.
- Accept `title: none` for untitled documents, and fall back to `search
result` for an unrecognized task name rather than failing the retrieval path.
Closes AstrBotDevs#11006
hongdougao111
force-pushed
the
fix/11006-gemini-embedding-task-type
branch
from
November 5, 2026 12:15
fa127ff to
b0e7dd9
Compare
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.
Problem
gemini_embedding_source.pyembedded queries and documents with identicalconfiguration, so retrieval similarity had almost no discriminative power.
Reported in #11006 with real numbers: a relevant memory and an unrelated one
scored 0.458 and 0.462, while a meaningless query scored 0.607.
There were two independent causes.
1. No task conditioning
gemini-embedding-001supportstask_type(RETRIEVAL_QUERY/RETRIEVAL_DOCUMENT), but onlyoutput_dimensionalitywas sent.gemini-embedding-2no longer acceptstask_typeand expects the task inthe text instead —
task: search result | query: {text}for queries,title: {title} | text: {text}for documents. Neither was applied.2. FAISS was not normalizing vectors
This is the part that actually explains the numbers in the report.
IndexFlatL2returns squared L2 distance, and1 - distance / 2only equalscosine similarity when the vectors are unit length. Gemini returns
unnormalized vectors below 3072 dimensions, and their magnitude drifts
with text length. The score was therefore dominated by vector magnitude rather
than meaning.
Measured on a two-document setup (related doc, magnitude 0.2; orthogonal doc,
magnitude 5.0):
After normalizing both sides, the same pair scores
0.600and0.000.Changes
EmbeddingProvider.get_query_embedding()— new hook defaulting toget_embedding(), so providers that don't support asymmetric retrieval areunaffected.
FaissVecDB.retrieve()and the dashboard query embedding nowuse it.
embedding_task_type_modeescape hatch (auto/never/always).get_embedding()keeps document semantics for existing callers.re-normalized once on load. Otherwise an upgraded instance would search with
normalized queries against unnormalized stored vectors.
Notes on the migration
This rewrites the user's
index.faisson first load, so it is worth beingexplicit about the safeguards:
otherwise their norm (0, not 1) would retrigger the rebuild on every startup.
and the in-memory index is already usable, so it must not prevent the
knowledge base from starting. The flag is retained so a later
initialize()/close()retries.username.
I have not validated the migration against a large real-world index — only
against constructed ones with sparse ids, zero vectors and mixed magnitudes.
Verification
tests/test_gemini_embedding_source.pyandtests/unit/test_faiss_vec_db.py.tests/unitsuite: 2238 passed vs 2201 on a clean checkout of the samecommit; the 4 remaining failures (
test_wakeup_pipeline.py) fail identicallybefore and after, and are ordering pollution unrelated to this change.
ruff format --check/ruff checkclean on every touched file.Closes #11006