Skip to content

Commit 6b01d0a

Browse files
committed
Make Langcache SDK python and javascript TCE files and add footer argument to hide footer
1 parent a2fd1f7 commit 6b01d0a

File tree

5 files changed

+180
-115
lines changed

5 files changed

+180
-115
lines changed

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

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

14-
Use the LangCache API 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.
15+
16+
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:
17+
18+
- [LangCache SDK for Python](https://pypi.org/project/langcache/)
19+
- [LangCache SDK for Javascript](https://www.npmjs.com/package/@redis-ai/langcache)
1520

1621
To access the LangCache API, you need:
1722

1823
- LangCache API base URL
1924
- LangCache service API key
2025
- Cache ID
2126

22-
When you call the API, you need to pass the LangCache API key in the `Authorization` header as a Bearer token and the Cache ID as the `cacheId` path parameter.
27+
When you call the API, you need to pass the LangCache API key in the `Authorization` header as a Bearer token and the Cache ID as the `cacheId` path parameter.
2328

24-
For example, to search the cache using `cURL`:
29+
For example:
2530

26-
```bash
27-
curl -s -X POST "https://$HOST/v1/caches/$CACHE_ID/entires/search" \
31+
{{< clients-example set="langcache_sdk" step="imports_setup" dft_tab_name="cURL" footer="hide" >}}
32+
curl -s -X POST "https://$HOST/v1/caches/$CACHE_ID/entries/search" \
2833
-H "accept: application/json" \
2934
-H "Authorization: Bearer $API_KEY" \
3035
-d "{ 'prompt': 'What is semantic caching' }"
31-
```
36+
{{< /clients-example >}}
3237

3338
- The example expects several variables to be set in the shell:
3439

3540
- **$HOST** - the LangCache API base URL
3641
- **$CACHE_ID** - the Cache ID of your cache
3742
- **$API_KEY** - The LangCache API token
3843

39-
{{% info %}}
40-
This example uses `cURL` and Linux shell scripts to demonstrate the API; you can use any standard REST client or library.
41-
{{% /info %}}
42-
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-
45-
- [LangCache SDK for Python](https://pypi.org/project/langcache/)
46-
- [LangCache SDK for Javascript](https://www.npmjs.com/package/@redis-ai/langcache)
47-
4844
## Examples
4945

5046
### Search LangCache for similar responses
5147

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.
48+
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.
5349

54-
{{< multitabs id="search-basic"
55-
tab1="REST API"
56-
tab2="Python"
57-
tab3="Javascript" >}}
58-
```sh
50+
{{< clients-example set="langcache_sdk" step="search_basic" dft_tab_name="REST API" footer="hide" >}}
5951
POST https://[host]/v1/caches/{cacheId}/entries/search
6052
{
6153
"prompt": "User prompt text"
6254
}
63-
```
64-
-tab-sep-
65-
```python
66-
from langcache import LangCache
67-
import os
68-
69-
70-
with LangCache(
71-
server_url="https://<host>",
72-
cache_id="<cacheId>",
73-
service_key=os.getenv("LANGCACHE_SERVICE_KEY", ""),
74-
) as lang_cache:
75-
76-
res = lang_cache.search(
77-
prompt="User prompt text",
78-
similarity_threshold=0.9
79-
)
80-
81-
print(res)
82-
```
83-
-tab-sep-
84-
```js
85-
import { LangCache } from "@redis-ai/langcache";
86-
87-
const langCache = new LangCache({
88-
serverURL: "https://<host>",
89-
cacheId: "<cacheId>",
90-
serviceKey: "<LANGCACHE_SERVICE_KEY>",
91-
});
92-
93-
async function run() {
94-
const result = await langCache.search({
95-
prompt: "User prompt text",
96-
similarityThreshold: 0.9
97-
});
98-
99-
console.log(result);
100-
}
101-
102-
run();
103-
```
104-
{{< /multitabs >}}
55+
{{< /clients-example >}}
10556

10657
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.
10758

10859
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.
10960

11061
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.
11162

112-
{{< multitabs id="search-attributes"
113-
tab1="REST API"
114-
tab2="Python"
115-
tab3="Javascript" >}}
116-
```sh
63+
{{< clients-example set="langcache_sdk" step="search_attributes" dft_tab_name="REST API" footer="hide" >}}
11764
POST https://[host]/v1/caches/{cacheId}/entries/search
11865
{
11966
"prompt": "User prompt text",
12067
"attributes": {
12168
"customAttributeName": "customAttributeValue"
12269
}
12370
}
124-
```
125-
-tab-sep-
126-
```python
127-
from langcache import LangCache
128-
import os
129-
130-
131-
with LangCache(
132-
server_url="https://<host>",
133-
cache_id="<cacheId>",
134-
service_key=os.getenv("LANGCACHE_SERVICE_KEY", ""),
135-
) as lang_cache:
136-
137-
res = lang_cache.search(
138-
prompt="User prompt text",
139-
attributes={"customAttributeName": "customAttributeValue"},
140-
similarity_threshold=0.9,
141-
)
142-
143-
print(res)
144-
```
145-
-tab-sep-
146-
```js
147-
import { LangCache } from "@redis-ai/langcache";
148-
149-
const langCache = new LangCache({
150-
serverURL: "https://<host>",
151-
cacheId: "<cacheId>",
152-
serviceKey: "<LANGCACHE_SERVICE_KEY>",
153-
});
154-
155-
async function run() {
156-
const result = await langCache.search({
157-
prompt: "User prompt text",
158-
similarityThreshold: 0.9,
159-
attributes: {
160-
"customAttributeName": "customAttributeValue",
161-
},
162-
});
163-
164-
console.log(result);
165-
}
166-
167-
run();
168-
```
169-
{{< /multitabs >}}
71+
{{< /clients-example >}}
17072

17173
### Store a new response in LangCache
17274

layouts/partials/tabbed-clients-example.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{{ $cliTabName := (or (.Scratch.Get "cli_tab_name") ">_ Redis CLI") }}
77
{{ $cliFooterLinkText := .Scratch.Get "cli_footer_link_text" }}
88
{{ $cliFooterLinkUrl := .Scratch.Get "cli_footer_link_url" }}
9+
{{ $showFooter := .Scratch.Get "show_footer" | default true }}
910

1011
{{ if not (isset $.Site.Data.examples $id) }}
1112
{{ warnf "[tabbed-clients-example] Example not found %q for %q" $id $.Page }}
@@ -51,5 +52,5 @@
5152
{{ end }}
5253
{{ end }}
5354

54-
{{ $params := dict "id" (printf "%s-step%s" $id $step) "tabs" $tabs "showFooter" true }}
55+
{{ $params := dict "id" (printf "%s-step%s" $id $step) "tabs" $tabs "showFooter" $showFooter }}
5556
{{ partial "tabs/wrapper.html" $params }}

layouts/shortcodes/clients-example.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- dft_tab_name: Custom first tab name (optional, default: ">_ Redis CLI")
1010
- dft_tab_link_title: Custom first tab footer link title (optional)
1111
- dft_tab_url: Custom first tab footer link URL (optional)
12+
- show_footer: Show footer (optional, default: true)
1213

1314
Positional parameters (for backward compatibility):
1415
- 0: example set name
@@ -25,6 +26,7 @@
2526
{{ if .Get "set" }}
2627
{{ $usingNamedParams = true }}
2728
{{ end }}
29+
{{ .Scratch.Set "show_footer" true }}
2830

2931
{{/* Set parameters based on whether we're using named or positional */}}
3032
{{ if $usingNamedParams }}
@@ -36,6 +38,9 @@
3638
{{ .Scratch.Set "cli_tab_name" (.Get "dft_tab_name") }}
3739
{{ .Scratch.Set "cli_footer_link_text" (.Get "dft_tab_link_title") }}
3840
{{ .Scratch.Set "cli_footer_link_url" (.Get "dft_tab_url") }}
41+
{{ if eq (.Get "footer") "hide" }}
42+
{{ .Scratch.Set "show_footer" false }}
43+
{{ end }}
3944
{{ else }}
4045
{{/* Positional parameters (backward compatibility) */}}
4146
{{ .Scratch.Set "example" (.Get 0) }}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// EXAMPLE: langcache_sdk
2+
// STEP_START imports_setup
3+
import { LangCache } from "@redis-ai/langcache";
4+
5+
const langCache = new LangCache({
6+
serverURL: "https://" + process.env.HOST,
7+
cacheId: process.env.CACHE_ID,
8+
serviceKey: process.env.API_KEY,
9+
});
10+
// STEP_END
11+
12+
// STEP_START search_basic
13+
async function searchBasic() {
14+
const result = await langCache.search({
15+
prompt: "User prompt text",
16+
similarityThreshold: 0.9,
17+
});
18+
19+
console.log(result);
20+
}
21+
22+
searchBasic();
23+
// STEP_END
24+
25+
// STEP_START search_attributes
26+
async function searchAttributes() {
27+
const result = await langCache.search({
28+
prompt: "User prompt text",
29+
attributes: {
30+
"customAttributeName": "customAttributeValue",
31+
},
32+
similarityThreshold: 0.9,
33+
});
34+
35+
console.log(result);
36+
}
37+
38+
searchAttributes();
39+
// STEP_END
40+
41+
// STEP_START store_basic
42+
async function storeBasic() {
43+
const result = await langCache.set({
44+
prompt: "User prompt text",
45+
response: "LLM response text",
46+
});
47+
48+
console.log(result);
49+
}
50+
51+
storeBasic();
52+
// STEP_END
53+
54+
// STEP_START store_attributes
55+
async function storeAttributes() {
56+
const result = await langCache.set({
57+
prompt: "User prompt text",
58+
response: "LLM response text",
59+
attributes: {
60+
"customAttributeName": "customAttributeValue",
61+
},
62+
});
63+
64+
console.log(result);
65+
}
66+
67+
storeAttributes();
68+
// STEP_END
69+
70+
// STEP_START delete_entry
71+
async function deleteEntry() {
72+
const result = await langCache.deleteById({
73+
entryId: "<entryId>",
74+
});
75+
76+
console.log(result);
77+
}
78+
79+
deleteEntry();
80+
// STEP_END
81+
82+
// STEP_START delete_query
83+
async function deleteQuery() {
84+
const result = await langCache.deleteQuery({
85+
attributes: {
86+
"customAttributeName": "customAttributeValue",
87+
},
88+
});
89+
90+
console.log(result);
91+
}
92+
93+
deleteQuery();
94+
// STEP_END
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# EXAMPLE: langcache_sdk
2+
# STEP_START imports_setup
3+
from langcache import LangCache
4+
import os
5+
6+
lang_cache = LangCache(
7+
server_url=f"https://{os.getenv('HOST', '')}",
8+
cache_id=os.getenv("CACHE_ID", ""),
9+
service_key=os.getenv("API_KEY", "")
10+
)
11+
# STEP_END
12+
13+
# STEP_START search_basic
14+
res = lang_cache.search(
15+
prompt="User prompt text",
16+
similarity_threshold=0.9
17+
)
18+
19+
print(res)
20+
# STEP_END
21+
22+
# STEP_START search_attributes
23+
res = lang_cache.search(
24+
prompt="User prompt text",
25+
attributes={"customAttributeName": "customAttributeValue"},
26+
similarity_threshold=0.9,
27+
)
28+
29+
print(res)
30+
# STEP_END
31+
32+
# STEP_START store_basic
33+
res = lang_cache.set(
34+
prompt="User prompt text",
35+
response="LLM response text",
36+
)
37+
38+
print(res)
39+
# STEP_END
40+
41+
# STEP_START store_attributes
42+
res = lang_cache.set(
43+
prompt="User prompt text",
44+
response="LLM response text",
45+
attributes={"customAttributeName": "customAttributeValue"},
46+
)
47+
48+
print(res)
49+
# STEP_END
50+
51+
# STEP_START delete_entry
52+
res = lang_cache.delete_by_id(entry_id="<entryId>")
53+
54+
print(res)
55+
# STEP_END
56+
57+
# STEP_START delete_query
58+
res = lang_cache.delete_query(
59+
attributes={"customAttributeName": "customAttributeValue"},
60+
)
61+
62+
print(res)
63+
# STEP_END

0 commit comments

Comments
 (0)