Add semantic-cache example (cache LLM responses by meaning) - #354
Conversation
…aning)
A tiny semantic cache built on Moss: embed each question, look up the nearest
one already answered, and return the stored answer if it clears a similarity
threshold — skipping the model call entirely. Two phrasings of the same
question ('what are your hours?' / 'when do you open?') hit the same cache
entry, where an exact-text cache would miss and pay the model twice.
Includes semantic_cache.py (the SemanticCache class + a runnable demo), README,
and config. Retrieval is on-device and sub-10ms, far cheaper than the call it
avoids.
…with Moss index term)
There was a problem hiding this comment.
Pull request overview
Adds a new runnable moss-live-labs example demonstrating a semantic cache pattern: reuse prior LLM answers when a new question is sufficiently similar in embedding space, avoiding repeated model calls.
Changes:
- Introduces
SemanticCachedemo script that queries a vector index for nearest prior question and reuses its stored answer above a similarity threshold. - Adds usage docs and environment template for Moss + OpenAI credentials.
- Adds a minimal
pyproject.tomland Python version hint for running the example.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| moss-live-labs/examples/semantic-cache/semantic_cache.py | Implements the semantic-cache demo logic and runnable CLI loop. |
| moss-live-labs/examples/semantic-cache/README.md | Explains the semantic-cache concept and how to run the example. |
| moss-live-labs/examples/semantic-cache/pyproject.toml | Declares minimal dependencies for running the example. |
| moss-live-labs/examples/semantic-cache/.python-version | Pins a local Python version for convenience. |
| moss-live-labs/examples/semantic-cache/.env.example | Provides env var template for Moss and OpenAI keys. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codex reviewNo issues found. |
…fensive metadata, clearer session/README notes
- Fail fast with a clear message if MOSS/OPENAI env vars are missing.
- Pin moss>=1.1.1 (matches other moss-live-labs examples).
- Use metadata.get('answer') defensively on cache hits.
- Note the session is in-memory for the run (deterministic MISS->HIT) and
document push_index() for cross-run persistence.
- Clarify in the README that the shown snippet is simplified vs the runnable ask().
|
Thanks for the review — addressed the actionable points:
On the |
- Query with alpha=1.0 (pure semantic) so the score reflects meaning, matching
the 'semantic similarity' threshold wording.
- Guard optional QueryResultDocumentInfo.metadata (use (metadata or {}).get()).
- Guard nullable OpenAI message.content.
- Use a unique session name per run so the demo is deterministic (clean MISS->HIT);
documented the stable-name + push_index() path for real persistence.
|
Follow-up review addressed:
The remaining |
…'cosine' to generic similarity score
- No import-time side effects: env validation + MossClient/AsyncOpenAI creation moved into main(); SemanticCache takes store+llm injected (reusable). - Note that the cache is keyed by question meaning only; production should scope by tenant/user/model/prompt. - README snippet mirrors the safe metadata access. - Add uv.lock for a reproducible install (matches other examples).
|
Note: the recurring |
… close AsyncOpenAI via async context manager
What
A small, runnable example under
moss-live-labs/examples/semantic-cachethat caches LLM responses by meaning instead of exact text.A normal cache keys on the literal request string, so two phrasings of the same question miss and you pay the model twice:
This example embeds each question, looks up the nearest one already answered, and returns the stored answer if it clears a similarity threshold — no model call:
The whole thing
Moss serves the nearest-match lookup on-device in <10 ms, far cheaper than the model call it avoids. The one knob that matters is
THRESHOLD(cosine similarity).Contents
semantic_cache.py—SemanticCacheclass + a runnable demo (first question misses, the paraphrased second hits)README.md,.env.example,pyproject.tomlRun