From f38c3f9e62ac17fd93835fd968d8bab89c816b5d Mon Sep 17 00:00:00 2001 From: Antonio Mindov Date: Wed, 29 Oct 2025 13:55:21 +0200 Subject: [PATCH 1/3] Add LangCache Search strategies example --- content/develop/ai/langcache/api-examples.md | 23 +++++++++++++++++--- local_examples/langcache/langcache_sdk.js | 18 ++++++++++++++- local_examples/langcache/langcache_sdk.py | 12 ++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/content/develop/ai/langcache/api-examples.md b/content/develop/ai/langcache/api-examples.md index a4482798ef..27aa7a26c3 100644 --- a/content/develop/ai/langcache/api-examples.md +++ b/content/develop/ai/langcache/api-examples.md @@ -11,7 +11,7 @@ title: Use the LangCache API and SDK weight: 10 --- -Use the [LangCache API]({{< relref "/develop/ai/langcache/api-reference" >}}) from your client app to store and retrieve LLM, RAG, or agent responses. +Use the [LangCache API]({{< relref "/develop/ai/langcache/api-reference" >}}) from your client app to store and retrieve LLM, RAG, or agent responses. You can use any standard REST client or library to access the API. If your app is written in Python or Javascript, you can also use the LangCache Software Development Kits (SDKs) to access the API: @@ -60,6 +60,7 @@ Place this call in your client app right before you call your LLM's REST API. If If LangCache does not return a response, you should call your LLM's REST API to generate a new response. After you get a response from the LLM, you can [store it in LangCache](#store-a-new-response-in-langcache) for future use. +#### Attributes You can also scope the responses returned from LangCache by adding an `attributes` object to the request. LangCache will only return responses that match the attributes you specify. {{< clients-example set="langcache_sdk" step="search_attributes" dft_tab_name="REST API" footer="hide" >}} @@ -72,6 +73,23 @@ POST https://[host]/v1/caches/{cacheId}/entries/search } {{< /clients-example >}} +#### Search strategies +LangCache supports two search strategies when looking for a cached response: + +- **EXACT**: Returns a result only when the stored prompt matches the query exactly (case insensitive). +- **SEMANTIC**: Uses vector similarity to find semantically similar prompts and responses. + +By default, LangCache uses SEMANTIC search. You can specify the search strategies you want to use in the request body. +This can be set as default in the cache settings page. + +{{< clients-example set="langcache_sdk" step="search_strategies" dft_tab_name="REST API" footer="hide" >}} +POST https://[host]/v1/caches/{cacheId}/entries/search +{ + "prompt": "User prompt text", + "searchStrategies": ["exact", "semantic"] +} +{{< /clients-example >}} + ### Store a new response in LangCache Use [`POST /v1/caches/{cacheId}/entries`]({{< relref "/develop/ai/langcache/api-reference#tag/Cache-Entries/operation/set" >}}) to store a new response in the cache. @@ -107,7 +125,7 @@ Use [`DELETE /v1/caches/{cacheId}/entries/{entryId}`]({{< relref "/develop/ai/la DELETE https://[host]/v1/caches/{cacheId}/entries/{entryId} {{< /clients-example >}} -You can also use [`DELETE /v1/caches/{cacheId}/entries`]({{< relref "/develop/ai/langcache/api-reference#tag/Cache-Entries/operation/deleteQuery" >}}) to delete multiple cached responses based on the `attributes` you specify. If you specify multiple `attributes`, LangCache will delete entries that contain all given attributes. +You can also use [`DELETE /v1/caches/{cacheId}/entries`]({{< relref "/develop/ai/langcache/api-reference#tag/Cache-Entries/operation/deleteQuery" >}}) to delete multiple cached responses based on the `attributes` you specify. If you specify multiple `attributes`, LangCache will delete entries that contain all given attributes. {{< warning >}} If you do not specify any `attributes`, all responses in the cache will be deleted. This cannot be undone. @@ -123,4 +141,3 @@ DELETE https://[host]/v1/caches/{cacheId}/entries } } {{< /clients-example >}} - diff --git a/local_examples/langcache/langcache_sdk.js b/local_examples/langcache/langcache_sdk.js index be51468d27..bf2b2ba166 100644 --- a/local_examples/langcache/langcache_sdk.js +++ b/local_examples/langcache/langcache_sdk.js @@ -38,6 +38,22 @@ async function searchAttributes() { searchAttributes(); // STEP_END +// STEP_START search_strategies +import { SearchStrategy } from '@redis-ai/langcache/models/searchstrategy.js'; + +async function searchStrategies() { + const result = await langCache.search({ + prompt: "User prompt text", + searchStrategies: [SearchStrategy.Exact, SearchStrategy.Semantic], + similarityThreshold: 0.9, + }); + + console.log(result); +} + +searchStrategies(); +// STEP_END + // STEP_START store_basic async function storeBasic() { const result = await langCache.set({ @@ -91,4 +107,4 @@ async function deleteQuery() { } deleteQuery(); -// STEP_END \ No newline at end of file +// STEP_END diff --git a/local_examples/langcache/langcache_sdk.py b/local_examples/langcache/langcache_sdk.py index 3ccda74915..a49e7fc028 100644 --- a/local_examples/langcache/langcache_sdk.py +++ b/local_examples/langcache/langcache_sdk.py @@ -29,6 +29,18 @@ print(res) # STEP_END +# STEP_START search_strategies +from langcache.models import SearchStrategy + +res = lang_cache.search( + prompt="User prompt text", + search_strategies=[SearchStrategy.EXACT, SearchStrategy.SEMANTIC], + similarity_threshold=0.9, +) + +print(res) +# STEP_END + # STEP_START store_basic res = lang_cache.set( prompt="User prompt text", From fefa51f505dc3e64cb973a3515f4d8503ee7f5e2 Mon Sep 17 00:00:00 2001 From: Cameron Bates <102550101+cmilesb@users.noreply.github.com> Date: Wed, 29 Oct 2025 11:42:32 -0400 Subject: [PATCH 2/3] Apply suggestions from code review --- content/develop/ai/langcache/api-examples.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/develop/ai/langcache/api-examples.md b/content/develop/ai/langcache/api-examples.md index 27aa7a26c3..b79804d93f 100644 --- a/content/develop/ai/langcache/api-examples.md +++ b/content/develop/ai/langcache/api-examples.md @@ -76,10 +76,10 @@ POST https://[host]/v1/caches/{cacheId}/entries/search #### Search strategies LangCache supports two search strategies when looking for a cached response: -- **EXACT**: Returns a result only when the stored prompt matches the query exactly (case insensitive). -- **SEMANTIC**: Uses vector similarity to find semantically similar prompts and responses. +- **`exact`**: Returns a result only when the stored prompt matches the query exactly (case insensitive). +- `**semantic**`: Uses vector similarity to find semantically similar prompts and responses. -By default, LangCache uses SEMANTIC search. You can specify the search strategies you want to use in the request body. +By default, LangCache uses `semantic` search only. You can specify one or more search strategies in the request body. This can be set as default in the cache settings page. {{< clients-example set="langcache_sdk" step="search_strategies" dft_tab_name="REST API" footer="hide" >}} From 6c8a1db9f5f18d624e46d94409804b6bb97aa4a3 Mon Sep 17 00:00:00 2001 From: Cameron Bates <102550101+cmilesb@users.noreply.github.com> Date: Wed, 29 Oct 2025 11:48:42 -0400 Subject: [PATCH 3/3] Update content/develop/ai/langcache/api-examples.md --- content/develop/ai/langcache/api-examples.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/develop/ai/langcache/api-examples.md b/content/develop/ai/langcache/api-examples.md index b79804d93f..4bd145e9f5 100644 --- a/content/develop/ai/langcache/api-examples.md +++ b/content/develop/ai/langcache/api-examples.md @@ -80,7 +80,6 @@ LangCache supports two search strategies when looking for a cached response: - `**semantic**`: Uses vector similarity to find semantically similar prompts and responses. By default, LangCache uses `semantic` search only. You can specify one or more search strategies in the request body. -This can be set as default in the cache settings page. {{< clients-example set="langcache_sdk" step="search_strategies" dft_tab_name="REST API" footer="hide" >}} POST https://[host]/v1/caches/{cacheId}/entries/search