Skip to content

perf(genai): cache PDF embeddings with bounded content reuse - #250

Open
divyanshAgarwal123 wants to merge 1 commit into
TheCodeHeist-Coder:mainfrom
divyanshAgarwal123:codex/nsoc-pdf-embedding-cache-244
Open

divyanshAgarwal123 wants to merge 1 commit into
TheCodeHeist-Coder:mainfrom
divyanshAgarwal123:codex/nsoc-pdf-embedding-cache-244

Conversation

@divyanshAgarwal123

@divyanshAgarwal123 divyanshAgarwal123 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Repeated /ask-pdf uploads of identical bytes now reuse document embeddings even when their temporary filenames differ. A SHA-256/model/chunk-configuration key identifies each PDF snapshot. The in-process LRU retains at most four PDFs / 2,000 total chunks; oversized stores serve the current flight without retention. Concurrent misses share a build, failures release all waiters for a later retry, and model initialization is serialized.

The cache strips source paths and returns independent record/vector/metadata copies with the current request's path. Hashing and parsing use the same bytes. Temporary uploads are still removed. Cached PDF text/vectors remain in RAM until eviction or restart; each process starts cold. There is no disk/shared cache or TTL. Query embedding and LLM answering still run per question; quiz generation's separate context-window issue is unchanged.

Related issue

Closes #244.

Type of change

  • Bug fix
  • Performance
  • Tests
  • Documentation

Which services does this touch?

  • apps/genAI

How did you test this?

Python3.11.15, canonical requirements-dev.txt: 43 pytest tests pass (31 parent + 12 new). Ruff passes for app and both new test/benchmark modules; compileall and staged secret scan pass. Regressions cover renamed/changed PDFs, immutable snapshots, model/chunk key changes, mutable-record/source isolation, both LRU limits, oversized bypass, concurrent miss plus eviction, failure fan-out/retry, concurrent model initialization and two actual multipart /ask-pdf test-client uploads with cleanup. The regular suite uses fake providers and the real vector-store implementation.

Real CPU fastembed BAAI/bge-small-en-v1.5, arm64, one synthetic20,144-byte PDF / 30 chunks, same initialized/warmed model:

PDF-to-vector-store phase First upload Same bytes, renamed upload Document embedding calls
Uncached parent implementation 476.282 ms 519.792 ms 2
This change 553.986 ms 3.756 ms 1

Reproduce with .venv/bin/python -m tests.benchmark_pdf_cache. This opt-in script downloads the model if absent. Model load/warmup, query embedding and LLM generation are excluded from timing; these numbers are not whole-request latency or a production guarantee. Immutable source/parent/environment and four-artifact verification manifest.

Hosted CI run45509929479 passed all10 reported jobs on aa3af64ff4788fb1a756ad2e8e00a7cbee93f1ad, including allfive container builds. Every executed step reported success; no failure was hidden by a continued step. Local monorepo/Compose checkboxes below remain accurately unticked. Maintainer review and carrying over the issue grading labels are still pending.

Checklist

  • I ran pnpm run lint
  • I ran pnpm run check-types
  • I ran pnpm run build
  • The app still starts with docker-compose up
  • Updated documentation
  • No .env, API keys or secrets committed

The monorepo/Docker boxes are left unchecked: this scoped Python change was validated with the full GenAI tests, Ruff and compileall rather than claiming unrun frontend/Node/Compose checks. No dependencies, schemas, workflows or thresholds changed.

Signed-off-by: Divyansh agarwal <85987665+divyanshAgarwal123@users.noreply.github.com>
@divyanshAgarwal123

Copy link
Copy Markdown
Contributor Author

@TheCodeHeist-Coder, #244 is implemented with 43 passing Python tests and the requested real-CPU before/after timing in the PR. The repeated PDF-to-store phase measured 519.792 ms uncached versus 3.756 ms on a warm cache, with no second document-embedding call; model startup and LLM time are excluded. Could you carry the existing nsoc26 and level2 labels from assigned issue #244 onto this PR when reviewing it? The cache limits and in-memory PDF retention are documented.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The AI service re-embeds the whole PDF on every request

2 participants