Skip to content

Cache Gemini embeddings by content hash #9

@devangb3

Description

@devangb3

Every chunk's embedding costs a Gemini API call. If the same paragraph appears in two uploads (or the same file is uploaded twice), we pay twice. A SHA-256-of-chunk-text → embedding cache in SQLite cuts cost and latency on repeated content at no accuracy loss.

Current state:
No cache. Every chunk is always embedded.

Proposed implementation:

  1. SQLite table embedding_cache(chunk_hash TEXT PRIMARY KEY, model TEXT, dimension INT, embedding BLOB).
  2. Before calling Gemini: look up sha256(text)[:32]. If present, reuse.
  3. After calling Gemini: insert the new embeddings.
  4. Namespace the cache by model + dimension so upgrading gemini-embedding-001 to a newer model forces a rebuild.
  5. Log cache hit rate; surface in /metrics.

Files likely affected:

  • backend/rag_utils.py
  • backend/embedding_cache.py (new)
  • Tests.

Acceptance criteria:

  • Uploading the same file twice yields zero Gemini embed API calls on the second upload.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions