Skip to content

Commit 35e9daa

Browse files
authored
👌 IMPROVE: memory example with filters (#93)
* 👌 IMPROVE: memory example with filters * 🐛 FIX: package.json * 🐛 FIX: comments for filter examples * 🐛 FIX: formatting issue * 🐛 FIX: formatting issue * 🐛 FIX: package.json
1 parent 442f1cf commit 35e9daa

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Basic example to demonstrate how to retrieve memories with filters.
3+
*
4+
* - And: This filter is used to retrieve memories that match all the filters.
5+
* - Eq: This filter is used to retrieve memories that match the exact value.
6+
*
7+
* In this example, we retrieve memories with the following filters:
8+
* - company: Langbase
9+
* - category: docs
10+
*
11+
* We expect to get all chunks of memory from the Langbase Docs memory that have the company Langbase and the category docs.
12+
*
13+
*/
14+
15+
import 'dotenv/config';
16+
import {Langbase} from 'langbase';
17+
18+
const langbase = new Langbase({
19+
apiKey: process.env.LANGBASE_API_KEY!,
20+
});
21+
22+
async function main() {
23+
const response = await langbase.memories.retrieve({
24+
memory: [
25+
{
26+
name: "langbase-docs",
27+
filters: ["And", [
28+
["company", "Eq", "Langbase"],
29+
["category", "Eq", "docs"]
30+
]]
31+
},
32+
],
33+
query: "What are pipes in Langbase Docs?",
34+
topK: 5
35+
});
36+
37+
console.log(response);
38+
}
39+
40+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Basic example to demonstrate how to retrieve memories with filters.
3+
*
4+
* - Eq: This filter is used to retrieve memories that match the exact value.
5+
*
6+
* In this example, we retrieve memories with the following filters:
7+
* - company: Langbase
8+
*
9+
* We expect to get all chunks of memory from the Langbase Docs memory that have the company Langbase.
10+
*
11+
*/
12+
13+
import 'dotenv/config';
14+
import {Langbase} from 'langbase';
15+
16+
const langbase = new Langbase({
17+
apiKey: process.env.LANGBASE_API_KEY!,
18+
});
19+
20+
async function main() {
21+
const response = await langbase.memories.retrieve({
22+
memory: [
23+
{
24+
name: "langbase-docs",
25+
filters: ["company", "Eq", "Langbase"],
26+
},
27+
],
28+
query: "What is Langbase?",
29+
topK: 5
30+
});
31+
32+
console.log(response);
33+
}
34+
35+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Basic example to demonstrate how to retrieve memories with filters.
3+
*
4+
* - In: This filter is used to retrieve memories that match any of the value/values in the array.
5+
*
6+
* In this example, we retrieve memories with the following filters:
7+
* - company: Langbase or Google
8+
*
9+
* We expect to get all chunks of memory from the Langbase Docs memory that have the company Langbase or Google.
10+
*
11+
*/
12+
13+
import 'dotenv/config';
14+
import {Langbase} from 'langbase';
15+
16+
const langbase = new Langbase({
17+
apiKey: process.env.LANGBASE_API_KEY!,
18+
});
19+
20+
async function main() {
21+
const response = await langbase.memories.retrieve({
22+
memory: [
23+
{
24+
name: "langbase-docs",
25+
filters: ["company", "In", ["Langbase","Google"]],
26+
},
27+
],
28+
query: "What are pipes in Langbase?",
29+
topK: 5
30+
});
31+
32+
console.log(response);
33+
}
34+
35+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Basic example to demonstrate how to retrieve memories with filters.
3+
*
4+
* - NotEq: This filter is used to retrieve memories that do not match the exact value.
5+
*
6+
* In this example, we retrieve memories with the following filters:
7+
* - company: Langbase
8+
*
9+
* We expect to get all chunks of memory from the Langbase Docs memory that do not have the company Langbase.
10+
*
11+
*/
12+
13+
import 'dotenv/config';
14+
import {Langbase} from 'langbase';
15+
16+
const langbase = new Langbase({
17+
apiKey: process.env.LANGBASE_API_KEY!,
18+
});
19+
20+
async function main() {
21+
const response = await langbase.memories.retrieve({
22+
memory: [
23+
{
24+
name: "langbase-docs",
25+
filters: ["company", "NotEq", "Google"],
26+
},
27+
],
28+
query: "What is Langbase?",
29+
topK: 5
30+
});
31+
32+
console.log(response);
33+
}
34+
35+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Basic example to demonstrate how to retrieve memories with filters.
3+
*
4+
* - NotIn: This filter is used to retrieve memories that do not match any of the value/values in the array.
5+
*
6+
* In this example, we retrieve memories with the following filters:
7+
* - company: Google
8+
*
9+
* We expect to get all chunks of memory from the Langbase Docs memory that do not have the company Google.
10+
*
11+
*/
12+
13+
import 'dotenv/config';
14+
import {Langbase} from 'langbase';
15+
16+
const langbase = new Langbase({
17+
apiKey: process.env.LANGBASE_API_KEY!,
18+
});
19+
20+
async function main() {
21+
const response = await langbase.memories.retrieve({
22+
memory: [
23+
{
24+
name: "langbase-docs",
25+
filters: ["company", "NotIn", "Google"],
26+
},
27+
],
28+
query: "What are pipes in Langbase?",
29+
topK: 5
30+
});
31+
32+
console.log(response);
33+
}
34+
35+
main();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Advanced example to demonstrate how to retrieve memories with filters.
3+
*
4+
* - And: This filter is used to retrieve memories that match all the filters.
5+
* - Or: This filter is used to retrieve memories that match any of the filters.
6+
* - In: This filter is used to retrieve memories that match any of the value/values in the array.
7+
* - Eq: This filter is used to retrieve memories that match the exact value.
8+
*
9+
* In this example, we retrieve memories with the following filters:
10+
* - company: Langbase
11+
* - category: docs or examples
12+
* - primitive: Chunk or Threads
13+
*
14+
* We expect to get all chunks of memory from the Langbase Docs memory
15+
* that have the company Langbase, the category docs or examples, and the primitive can be Chunk or Threads.
16+
*
17+
*/
18+
19+
import 'dotenv/config';
20+
import {Langbase} from 'langbase';
21+
22+
const langbase = new Langbase({
23+
apiKey: process.env.LANGBASE_API_KEY!,
24+
});
25+
26+
async function main() {
27+
const response = await langbase.memories.retrieve({
28+
memory: [
29+
{
30+
name: "langbase-docs",
31+
filters: [
32+
"And", [
33+
["company", "Eq", "Langbase"],
34+
["Or", [
35+
["category", "Eq", "docs"],
36+
["category", "Eq", "examples"]
37+
]],
38+
["primitive", "In", ["Chunk", "Threads"]]
39+
]
40+
]
41+
}
42+
],
43+
query: "What are primitives in Langbase?",
44+
topK: 5
45+
});
46+
47+
console.log(response);
48+
}
49+
50+
main();

examples/nodejs/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
"memory.list": "npx tsx ./examples/memory/memory.list.ts",
1414
"memory.delete": "npx tsx ./examples/memory/memory.delete.ts",
1515
"memory.retrieve": "npx tsx ./examples/memory/memory.retrieve.ts",
16+
"memory.retrieve.filters.In": "npx tsx ./examples/memory/memory.retrieve.filters.In.ts",
17+
"memory.retrieve.filters.NotIn": "npx tsx ./examples/memory/memory.retrieve.filters.NotIn.ts",
18+
"memory.retrieve.filters.Eq": "npx tsx ./examples/memory/memory.retrieve.filters.Eq.ts",
19+
"memory.retrieve.filters.NotEq": "npx tsx ./examples/memory/memory.retrieve.filters.NotEq.ts",
20+
"memory.retrieve.filters.Or": "npx tsx ./examples/memory/memory.retrieve.filters.Or.ts",
21+
"memory.retrieve.filters.advanced": "npx tsx ./examples/memory/memory.retrieve.filters.advanced.ts",
1622
"memory.docs.list": "npx tsx ./examples/memory/memory.docs.list.ts",
1723
"memory.docs.delete": "npx tsx ./examples/memory/memory.docs.delete.ts",
1824
"memory.docs.upload": "npx tsx ./examples/memory/memory.docs.upload.ts",

0 commit comments

Comments
 (0)