Skip to content

Commit f38c3f9

Browse files
committed
Add LangCache Search strategies example
1 parent f9c96b1 commit f38c3f9

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

content/develop/ai/langcache/api-examples.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ title: Use the LangCache API and SDK
1111
weight: 10
1212
---
1313

14-
Use the [LangCache API]({{< relref "/develop/ai/langcache/api-reference" >}}) from your client app to store and retrieve LLM, RAG, or agent responses.
14+
Use the [LangCache API]({{< relref "/develop/ai/langcache/api-reference" >}}) from your client app to store and retrieve LLM, RAG, or agent responses.
1515

1616
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:
1717

@@ -60,6 +60,7 @@ Place this call in your client app right before you call your LLM's REST API. If
6060

6161
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.
6262

63+
#### Attributes
6364
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.
6465

6566
{{< 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
7273
}
7374
{{< /clients-example >}}
7475

76+
#### Search strategies
77+
LangCache supports two search strategies when looking for a cached response:
78+
79+
- **EXACT**: Returns a result only when the stored prompt matches the query exactly (case insensitive).
80+
- **SEMANTIC**: Uses vector similarity to find semantically similar prompts and responses.
81+
82+
By default, LangCache uses SEMANTIC search. You can specify the search strategies you want to use in the request body.
83+
This can be set as default in the cache settings page.
84+
85+
{{< clients-example set="langcache_sdk" step="search_strategies" dft_tab_name="REST API" footer="hide" >}}
86+
POST https://[host]/v1/caches/{cacheId}/entries/search
87+
{
88+
"prompt": "User prompt text",
89+
"searchStrategies": ["exact", "semantic"]
90+
}
91+
{{< /clients-example >}}
92+
7593
### Store a new response in LangCache
7694

7795
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
107125
DELETE https://[host]/v1/caches/{cacheId}/entries/{entryId}
108126
{{< /clients-example >}}
109127

110-
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.
128+
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.
111129

112130
{{< warning >}}
113131
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
123141
}
124142
}
125143
{{< /clients-example >}}
126-

local_examples/langcache/langcache_sdk.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ async function searchAttributes() {
3838
searchAttributes();
3939
// STEP_END
4040

41+
// STEP_START search_strategies
42+
import { SearchStrategy } from '@redis-ai/langcache/models/searchstrategy.js';
43+
44+
async function searchStrategies() {
45+
const result = await langCache.search({
46+
prompt: "User prompt text",
47+
searchStrategies: [SearchStrategy.Exact, SearchStrategy.Semantic],
48+
similarityThreshold: 0.9,
49+
});
50+
51+
console.log(result);
52+
}
53+
54+
searchStrategies();
55+
// STEP_END
56+
4157
// STEP_START store_basic
4258
async function storeBasic() {
4359
const result = await langCache.set({
@@ -91,4 +107,4 @@ async function deleteQuery() {
91107
}
92108

93109
deleteQuery();
94-
// STEP_END
110+
// STEP_END

local_examples/langcache/langcache_sdk.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
print(res)
3030
# STEP_END
3131

32+
# STEP_START search_strategies
33+
from langcache.models import SearchStrategy
34+
35+
res = lang_cache.search(
36+
prompt="User prompt text",
37+
search_strategies=[SearchStrategy.EXACT, SearchStrategy.SEMANTIC],
38+
similarity_threshold=0.9,
39+
)
40+
41+
print(res)
42+
# STEP_END
43+
3244
# STEP_START store_basic
3345
res = lang_cache.set(
3446
prompt="User prompt text",

0 commit comments

Comments
 (0)