You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/develop/ai/langcache/api-examples.md
+267-7Lines changed: 267 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,30 +40,76 @@ curl -s -X POST "https://$HOST/v1/caches/$CACHE_ID/entires/search" \
40
40
This example uses `cURL` and Linux shell scripts to demonstrate the API; you can use any standard REST client or library.
41
41
{{% /info %}}
42
42
43
-
If your app is written in Javascript or Python, you can also use the LangCache Software Development Kits (SDKs) to access the API:
43
+
If your app is written in Python or Javascript, you can also use the LangCache Software Development Kits (SDKs) to access the API:
44
44
45
-
-[LangCache SDK for Javascript](https://www.npmjs.com/package/@redis-ai/langcache)
46
45
-[LangCache SDK for Python](https://pypi.org/project/langcache/)
46
+
-[LangCache SDK for Javascript](https://www.npmjs.com/package/@redis-ai/langcache)
47
47
48
48
## Examples
49
49
50
50
### Search LangCache for similar responses
51
51
52
-
Use `POST /v1/caches/{cacheId}/entries/search` to search the cache for matching responses to a user prompt.
52
+
Use [`POST /v1/caches/{cacheId}/entries/search`]({{< relref "/develop/ai/langcache/api-reference#tag/Cache-Entries/operation/search" >}}}) to search the cache for matching responses to a user prompt.
53
53
54
+
{{< multitabs id="search-basic"
55
+
tab1="REST API"
56
+
tab2="Python"
57
+
tab3="Javascript" >}}
54
58
```sh
55
59
POST https://[host]/v1/caches/{cacheId}/entries/search
res = lang_cache.search(prompt="User prompt text", similarity_threshold=0.9)
77
+
78
+
print(res)
79
+
```
80
+
-tab-sep-
81
+
```js
82
+
import { LangCache } from"@redis-ai/langcache";
83
+
84
+
constlangCache=newLangCache({
85
+
serverURL:"https://<host>",
86
+
cacheId:"<cacheId>",
87
+
serviceKey:"<LANGCACHE_SERVICE_KEY>",
88
+
});
89
+
90
+
asyncfunctionrun() {
91
+
constresult=awaitlangCache.search({
92
+
prompt:"User prompt text",
93
+
similarityThreshold:0.9
94
+
});
95
+
96
+
console.log(result);
97
+
}
98
+
99
+
run();
100
+
```
101
+
{{< /multitabs >}}
60
102
61
103
Place this call in your client app right before you call your LLM's REST API. If LangCache returns a response, you can send that response back to the user instead of calling the LLM.
62
104
63
105
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.
64
106
65
-
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.
107
+
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.
66
108
109
+
{{< multitabs id="search-attributes"
110
+
tab1="REST API"
111
+
tab2="Python"
112
+
tab3="Javascript" >}}
67
113
```sh
68
114
POST https://[host]/v1/caches/{cacheId}/entries/search
69
115
{
@@ -73,23 +119,118 @@ POST https://[host]/v1/caches/{cacheId}/entries/search
Use `POST /v1/caches/{cacheId}/entries` to store a new response in the cache.
170
+
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.
Use `DELETE /v1/caches/{cacheId}/entries/{entryId}` to delete a cached response from the cache.
270
+
Use [`DELETE /v1/caches/{cacheId}/entries/{entryId}`]({{< relref "/develop/ai/langcache/api-reference#tag/Cache-Entries/operation/delete" >}}) to delete a cached response from the cache.
107
271
108
-
You can also use `DELETE /v1/caches/{cacheId}/entries` to delete multiple cached responses at once. If you provide an `attributes` object, LangCache will delete all responses that match the attributes you specify.
res = lang_cache.delete_by_id(entry_id="{entryId}")
293
+
294
+
print(res)
295
+
```
296
+
-tab-sep-
297
+
```js
298
+
import { LangCache } from"@redis-ai/langcache";
299
+
300
+
constlangCache=newLangCache({
301
+
serverURL:"https://<host>",
302
+
cacheId:"<cacheId>",
303
+
serviceKey:"<LANGCACHE_SERVICE_KEY>",
304
+
});
305
+
306
+
asyncfunctionrun() {
307
+
constresult=awaitlangCache.deleteById({
308
+
entryId:"<entryId>",
309
+
});
310
+
311
+
console.log(result);
312
+
}
313
+
314
+
run();
315
+
```
316
+
{{< /multitabs >}}
317
+
318
+
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.
319
+
320
+
{{< warning >}}
321
+
If you do not specify any `attributes`, all responses in the cache will be deleted. This cannot be undone.
0 commit comments