Skip to content

Conversation

@abrookins
Copy link
Collaborator

@abrookins abrookins commented Oct 17, 2025

Summary

Add a LangCache wrapper to RedisVL LLM cache extensions so users can leverage LangCache for semantic caching via the familiar BaseLLMCache interface. Also make the wrapper’s distance threshold semantics explicit and configurable so users can swap it out for an existing SemanticCache instance easily.

Motivation

  • Use LangCache while keeping RedisVL’s developer experience
  • Avoid surprises by making threshold scale explicit

What’s included

  • New LangCacheWrapper implementing BaseLLMCache (sync + async) that talks to the LangCache service:
    • Exact + semantic search toggles, TTL, and standard store/check APIs
    • Consistent result shape (including vector_distance) compatible with existing callers
    • Lazily imports LangCache and gives a friendly error if it's not installed
  • Configurable distance threshold scale via distance_scale:
    • "normalized": interpret distance_threshold as 0–1 semantic distance; converts to/from similarity (1 - d)
    • "redis": interpret distance_threshold as native COSINE [0–2]; converts using norm_cosine_distance / denorm_cosine_distance

Usage

from redisvl.extensions.cache.llm.langcache import LangCacheWrapper

cache = LangCacheWrapper(
    name="langcache",
    cache_id="<id>",
    api_key="<key>",
    distance_scale="normalized"  # or "redis" for COSINE [0–2]
)

cache.store(prompt="Hello?", response="Hi\!")

hits = cache.check(
    prompt="Hello\!",
    distance_threshold=0.15,  # interpreted per distance_scale
)

Docs

  • Follow-up docs PR will add LangCache wrapper docs and clarify threshold scales

Related

- Implement LangCacheWrapper as BaseLLMCache extension
- Support semantic and exact search strategies
- Add distance_threshold support (converts to similarity_threshold)
- Add attributes filtering for metadata-based searches
- Fix SDK integration issues:
  - Use correct response model attributes (entry_id, data)
  - Remove incorrect context manager usage
  - Convert similarity to distance (1.0 - similarity)
  - Handle optional attributes in store operations
- Add comprehensive unit tests (16 tests)
- All 444 unit tests passing
- Type checking, formatting, and linting passing
…redis) and convert accordingly\n\n- Add distance_scale option to LangCacheWrapper (default: normalized)\n- Convert distance_threshold to similarity_threshold based on scale\n- Convert returned similarity to vector_distance based on scale\n- No change to SemanticCache behavior; opened issue #407 to track threshold inconsistencies there
@abrookins abrookins changed the title LangCacheWrapper: explicit distance_threshold scale (normalized|redis) Add a LangCache implementation of BaseLLMCache Oct 18, 2025
@abrookins abrookins changed the title Add a LangCache implementation of BaseLLMCache Add LangCache wrapper (LLM cache extension) with configurable distance scale Oct 18, 2025
@abrookins abrookins requested a review from Copilot October 18, 2025 00:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a LangCache-based LLM cache wrapper with configurable distance scale and fixes an async connection parameter in AsyncSearchIndex. It also introduces unit/integration tests and updates packaging to include an optional langcache extra.

  • Add LangCacheWrapper with exact/semantic search, consistent hit shape, and distance scale support ("normalized" or "redis")
  • Fix AsyncSearchIndex.connect to pass url kwarg to the connection factory; add integration test
  • Update packaging: add langcache extra; adjust Python requirement

Reviewed Changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/unit/test_langcache_wrapper.py New unit tests for LangCacheWrapper covering init, store/check paths, threshold conversion, and delete/update behavior.
tests/integration/test_async_search_index.py Adds regression test ensuring AsyncSearchIndex.connect accepts redis_url and connects successfully despite deprecation.
redisvl/index/index.py Fixes async connect to call RedisConnectionFactory with url=redis_url (consistent with factory’s API).
redisvl/extensions/cache/llm/langcache.py Implements LangCacheWrapper with distance scale conversions, sync/async store/check/delete, and consistent CacheHit output.
redisvl/extensions/cache/llm/init.py Re-exports LangCacheWrapper in the llm package namespace.
pyproject.toml Adds optional dependency extra for langcache and tightens the minimum Python version to 3.9.2.

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

…type in check/acheck to Optional[Dict[str, Any]]\n- Clarify docstrings for distance_threshold conversion for both scales\n- Factor helpers: _similarity_threshold, _build_search_kwargs, _hits_from_response to reduce duplication
@abrookins
Copy link
Collaborator Author

abrookins commented Oct 20, 2025

Addressed review feedback:

  • Broadened attributes type in check/acheck to Optional[Dict[str, Any]]
  • Clarified docstrings to document both conversion paths (normalized vs redis)
  • Reduced duplication by factoring helpers: _similarity_threshold, _build_search_kwargs, _hits_from_response

… in Redis connection factory to allow cluster/non-cluster assignments
Copy link
Collaborator

@rbs333 rbs333 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me! Left a couple comments that I think we should sync on

@abrookins
Copy link
Collaborator Author

abrookins commented Oct 27, 2025

Fully deprecated url as a parameter to the async connection functions per review.

@abrookins abrookins force-pushed the feat/langcache-extension branch from 0c5761a to b760113 Compare October 27, 2025 21:58
Copy link
Collaborator

@rbs333 rbs333 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Copy link
Collaborator

@bsbodden bsbodden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@abrookins abrookins merged commit 69407f6 into main Oct 28, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants