-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata_filtering.py
More file actions
69 lines (55 loc) · 1.79 KB
/
metadata_filtering.py
File metadata and controls
69 lines (55 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Metadata filtering example for sqlite-vec-client.
Demonstrates:
- Adding records with metadata
- Querying records with get_all
- Updating metadata
"""
from sqlite_vec_client import SQLiteVecClient
def main():
client = SQLiteVecClient(table="articles", db_path=":memory:")
client.create_table(dim=128, distance="cosine")
# Add articles with metadata
texts = [
"Introduction to Python programming",
"Advanced machine learning techniques",
"Python for data science",
]
embeddings = [
[0.1] * 128,
[0.2] * 128,
[0.15] * 128,
]
metadata = [
{"category": "programming", "author": "Alice", "year": 2023},
{"category": "ai", "author": "Bob", "year": 2024},
{"category": "data-science", "author": "Alice", "year": 2024},
]
rowids = client.add(texts=texts, embeddings=embeddings, metadata=metadata)
print(f"Added {len(rowids)} articles")
# Query all articles and filter by author
print("\nAlice's articles:")
for rowid, text, meta, _ in client.get_all():
if meta.get("author") == "Alice":
print(f" [{rowid}] {text} - {meta}")
# Query all articles and filter by text
print("\nPython-related articles:")
for rowid, text, meta, _ in client.get_all():
if "Python" in text:
print(f" [{rowid}] {text}")
# Update metadata
if rowids:
client.update(
rowids[0],
metadata={
"category": "programming",
"author": "Alice",
"year": 2024,
"updated": True,
},
)
updated = client.get(rowids[0])
if updated:
print(f"\nUpdated metadata: {updated[2]}")
client.close()
if __name__ == "__main__":
main()