Repository navigation
Expand file tree
/
Copy pathmulti_collection.py
More file actions
131 lines (105 loc) · 4.26 KB
/
Copy pathmulti_collection.py
File metadata and controls
131 lines (105 loc) · 4.26 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
Multi-Collection Database Example
This example demonstrates using VecDatabase to manage multiple
isolated collections, similar to how you'd use ChromaDB or Qdrant.
Use cases:
- Multi-tenant applications (one collection per user/organization)
- Different document types (articles, code, images)
- Isolated namespaces for different projects
"""
import random
from vecstore import VecDatabase
def mock_embed(text: str, dim: int = 384) -> list[float]:
"""Mock embedding function"""
random.seed(hash(text) % (2**32))
return [random.random() for _ in range(dim)]
def main():
print("=" * 60)
print("VecStore - Multi-Collection Example")
print("=" * 60)
# Create database
print("\n1. Creating multi-collection database...")
db = VecDatabase("./multi_collection_db")
print(f" ✓ Database created: {db}")
# Create separate collections for different data types
print("\n2. Creating collections...")
docs_collection = db.create_collection("documents")
print(f" ✓ Created 'documents' collection: {docs_collection}")
code_collection = db.create_collection("code_snippets")
print(f" ✓ Created 'code_snippets' collection: {code_collection}")
notes_collection = db.create_collection("personal_notes")
print(f" ✓ Created 'personal_notes' collection: {notes_collection}")
# List all collections
collections = db.list_collections()
print(f"\n3. All collections: {collections}")
# Add data to each collection
print("\n4. Adding data to collections...")
# Documents collection
docs_collection.upsert(
"article_1",
mock_embed("Rust programming language overview"),
{"title": "Intro to Rust", "type": "article", "author": "Jane Doe"}
)
docs_collection.upsert(
"article_2",
mock_embed("Python for data science"),
{"title": "Python DS", "type": "article", "author": "John Smith"}
)
print(f" ✓ Added {docs_collection.count()} documents")
# Code collection
code_collection.upsert(
"snippet_1",
mock_embed("fn main() { println!(\"Hello\"); }"),
{"language": "rust", "description": "Hello world in Rust"}
)
code_collection.upsert(
"snippet_2",
mock_embed("def hello(): print('Hello')"),
{"language": "python", "description": "Hello function in Python"}
)
print(f" ✓ Added {code_collection.count()} code snippets")
# Notes collection
notes_collection.upsert(
"note_1",
mock_embed("Remember to buy groceries"),
{"category": "todo", "priority": "high"}
)
print(f" ✓ Added {notes_collection.count()} notes")
# Query each collection independently
print("\n5. Querying collections...")
# Query documents
query_vec = mock_embed("programming tutorials")
doc_results = docs_collection.query(query_vec, k=2)
print(f"\n Documents matching 'programming tutorials':")
for result in doc_results:
print(f" - {result.id}: {result.metadata['title']} (score: {result.score:.4f})")
# Query code
code_query = mock_embed("rust code examples")
code_results = code_collection.query(code_query, k=2)
print(f"\n Code snippets matching 'rust code examples':")
for result in code_results:
print(f" - {result.id}: {result.metadata['description']} (score: {result.score:.4f})")
# Get collection statistics
print("\n6. Collection statistics...")
for name in collections:
coll = db.get_collection(name)
if coll:
stats = coll.stats()
print(f"\n {name}:")
print(f" - Vectors: {stats['vector_count']}")
print(f" - Active: {stats['active_count']}")
print(f" - Deleted: {stats['deleted_count']}")
print(f" - Dimension: {stats['dimension']}")
# Delete a collection
print("\n7. Deleting 'personal_notes' collection...")
db.delete_collection("personal_notes")
print(f" ✓ Collections remaining: {db.list_collections()}")
print("\n" + "=" * 60)
print("✓ Multi-collection demo complete!")
print("=" * 60)
# Cleanup
import shutil
shutil.rmtree("./multi_collection_db", ignore_errors=True)
print("\n✓ Cleaned up demo database")
if __name__ == "__main__":
main()