diff --git a/content/develop/ai/langcache/api-examples.md b/content/develop/ai/langcache/api-examples.md index a4482798e..4bd145e9f 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,22 @@ 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 only. You can specify one or more search strategies in the request body. + +{{< 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 +124,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 +140,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 be51468d2..bf2b2ba16 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 3ccda7491..a49e7fc02 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",