Repository navigation
feat(retrieve): cache rerank results for identical query-document batches - #6135
Open
guvengursel wants to merge 1 commit into
Open
guvengursel wants to merge 1 commit into
guvengursel wants to merge 1 commit into
Conversation
…ches Process-wide LRU result cache (1024 entries, 24h TTL) in front of rerank provider calls. Key is a sha256 digest over length-prefixed scoring-identity fields (provider, model identity, host/api_base, jev mode) plus the exact post-truncation query and document texts in request order; only the digest is retained as key material. Provider failures are never cached and the vector-score fallback is unchanged. Cache hits skip the provider call and therefore emit no rerank token-usage or call metrics. Fixes volcengine#5200
Open
1 task done
This branch has not been deployed
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.
Description
Adds a process-wide LRU result cache in front of rerank provider calls, upstreaming the design proposed in #5200.
All provider clients flow through
HierarchicalRetriever._rerank_scores, which already normalizes inputs (blank-doc filtering, token-budget truncation) before callingrerank_batch. The cache wraps the constructed client transparently (CachedRerankClient), so it keys on exactly what the provider receives:model_name/model/model_version/host/api_base/mode), then the exact query and document texts in request order. Length-prefixing removes query/document boundary ambiguity; only the digest is retained as key material, and digests are never logged.time.monotonic); hits refresh recency. A per-entry byte cap skips caching oversized batches, since request limits are unbounded and truncation defaults off.threading.Lock—rerank_batchruns throughasyncio.to_thread, so concurrent requests reach the cache from multiple executor threads.Noneresponses propagate exactly as before and are never cached; the vector-score fallback path is untouched.Identical inputs deterministically produce identical provider scores, so caching is correctness-preserving.
Behavior before: every retrieve with rerank enabled issues a provider
rerank_batchcall, including repeated identical (query, documents) requests in steady-state auto-recall traffic.Behavior after: repeated identical requests within the TTL are served from cache; provider call count drops accordingly. Field telemetry from the deployment that motivated #5200: 10,047 served / 1,629 cache hits (~13.95%) over a multi-day window; hit rate is window-dependent (a window starting from a fresh restart showed substantially lower hit rates).
One behavioral note, stated explicitly: cache hits skip the provider call, so they emit no rerank token-usage or call metrics — per-account rerank counters under-report by the hit volume.
Human Involvement
Related Issue
Fixes #5200
Type of Change
Changes Made
openviking/retrieve/rerank_cache.py(new): sha256 length-prefixed key, OrderedDict LRU (1024 entries, 24h TTL, monotonic clock), byte cap on oversized payloads,threading.Lock-guarded operations, transparentCachedRerankClientwrapperopenviking/retrieve/hierarchical_retriever.py: wrap the constructed rerank client when configured; declare the attribute's union typetests/retrieve/test_hierarchical_retriever_rerank.py: contract tests — cache hit skips provider call, document-order sensitivity, TTL expiry, LRU eviction, client wiring with/without configdocs/en/guides/01-configuration.md+docs/zh/guides/01-configuration.md: rerank section notes the cache and the metrics behavior on hitsTesting
Validation commands and results (local environment has no Rust toolchain, so the project wheel was not built; this test file's import chain is pure Python and unaffected):
uv run --no-project python -m pytest tests/retrieve/test_hierarchical_retriever_rerank.py -q→ 24 passed (18 pre-existing + 6 new)ruff format --check+ruff checkon the changed paths (pinned via uvx) → cleanmypyon the changed paths → no new errors vs the pre-change tree (8 pre-existing errors inhierarchical_retriever.py, unchanged)Checklist
Additional Notes
The diff is ~211 lines (about 102 the new cache module, ~98 contract tests), above the usual ≤100-line fast-review size — the tests and both documentation languages are included deliberately rather than trimmed to fit; happy to split if reviewers prefer.
Follow-ups deliberately deferred to keep this PR one coherent problem: configurable size/TTL knobs, hit/miss telemetry counters.