-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_wrapper.py
More file actions
165 lines (130 loc) · 6.06 KB
/
Copy pathtest_wrapper.py
File metadata and controls
165 lines (130 loc) · 6.06 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Demonstration script using the new VectorDB wrapper class
"""
import sys
import time
# Try to import from the installed package
try:
from simple_vectordb import VectorDB
except ImportError:
print("Warning: simple_vectordb package not installed. Falling back to build directory.")
try:
sys.path.insert(0, './build')
from SimpleHNSW import SimpleHNSWIndex
print("Using C++ bindings directly from build directory.")
print("For better experience, install the package:")
print(" cd packages/simple-vectordb-python && pip install -e .")
sys.exit(1)
except ImportError:
print("Error: Could not find SimpleHNSW module.")
print("Please build the C++ extension first:")
print(" mkdir -p build && cd build")
print(" cmake -DPYTHON_BINDINGS=ON .. && make")
sys.exit(1)
# Optional: Use sentence transformers for embeddings
try:
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
HAS_SENTENCE_TRANSFORMERS = True
except ImportError:
print("Note: sentence-transformers not installed. Skipping embedding example.")
print("Install with: pip install sentence-transformers")
HAS_SENTENCE_TRANSFORMERS = False
def basic_example():
"""Basic usage of VectorDB"""
print("=== Basic VectorDB Example ===\n")
# Create a VectorDB instance
db = VectorDB(L=5, mL=0.62, efc=10, max_connections=16)
# Insert some vectors with metadata
vectors = [
([1.0, 2.0, 3.0], {"id": "vec1", "label": "first"}),
([1.0, 2.0, 3.1], {"id": "vec2", "label": "second"}),
([1.1, 2.1, 3.0], {"id": "vec3", "label": "third"}),
([1.1, 2.1, 3.1], {"id": "vec4", "label": "fourth"}),
]
print(f"Inserting {len(vectors)} vectors...")
for vec, meta in vectors:
idx = db.insert(vec, metadata=meta)
print(f" Inserted vector at index {idx}: {meta}")
print(f"\nDatabase size: {db.size()}")
print(f"Database info: {db}")
# Search for nearest neighbors
query = [1.0, 2.0, 3.0]
print(f"\nSearching for top 3 nearest neighbors to {query}:")
start_time = time.time()
results = db.search(query, k=3, return_metadata=True)
elapsed = time.time() - start_time
for distance, idx, metadata in results:
print(f" Distance: {distance:.6f}, Index: {idx}, Metadata: {metadata}")
print(f"Search completed in {elapsed*1000:.3f} ms\n")
def sentence_embedding_example():
"""Example using sentence transformers for semantic search"""
if not HAS_SENTENCE_TRANSFORMERS:
return
print("=== Sentence Embedding Example ===\n")
sentences = [
"How do I get a replacement Medicare card?",
"What is the monthly premium for Medicare Part B?",
"How do I terminate my Medicare Part B (medical insurance)?",
"How do I sign up for Medicare?",
"Can I sign up for Medicare Part B if I am working and have health insurance through an employer?",
"How do I sign up for Medicare Part B if I already have Part A?",
"What are Medicare late enrollment penalties?",
"What is Medicare and who can get it?",
"How can I get help with my Medicare Part A and Part B premiums?",
"What are the different parts of Medicare?",
"Will my Medicare premiums be higher because of my higher income?",
"What is TRICARE?",
"Should I sign up for Medicare Part B if I have Veterans' Benefits?"
]
print(f"Loading sentence transformer model...")
model = SentenceTransformer('thenlper/gte-small')
print(f"Encoding {len(sentences)} sentences...")
embeddings = model.encode(sentences)
print(f"Generated embeddings with shape: {embeddings.shape}")
print(f"First embedding sample: {embeddings[0][:5]}... (showing first 5 dims)")
# Create database with appropriate parameters for this size
db = VectorDB(L=5, mL=0.62, efc=20, max_connections=16)
# Insert all embeddings with metadata
print(f"\nInserting embeddings into database...")
for i, (embedding, sentence) in enumerate(zip(embeddings, sentences)):
db.insert(
embedding.tolist(),
metadata={"index": i, "text": sentence}
)
print(f"Database contains {db.size()} vectors")
# Test query
query_text = "impact of higher income on medicare"
print(f"\nQuery: '{query_text}'")
query_embedding = model.encode(query_text)
# Compare with cosine similarity (for reference)
cos_similarities = [cos_sim(query_embedding, emb).item() for emb in embeddings]
top_cos_idx = sorted(range(len(cos_similarities)), key=lambda i: cos_similarities[i], reverse=True)[:3]
print("\nTop 3 by cosine similarity (reference):")
for i, idx in enumerate(top_cos_idx[:3], 1):
print(f" {i}. [{idx}] (sim: {cos_similarities[idx]:.4f}) {sentences[idx]}")
# Search using VectorDB
print("\nTop 3 by HNSW search:")
start_time = time.time()
results = db.search(query_embedding, k=3, ef=50, return_metadata=True)
elapsed = time.time() - start_time
for i, (distance, idx, metadata) in enumerate(results, 1):
print(f" {i}. [{idx}] (dist: {distance:.4f}) {metadata['text']}")
print(f"\nSearch completed in {elapsed*1000:.3f} ms")
# Test save and load
print("\nTesting persistence...")
filepath = "embeddings_db.json"
db.save(filepath)
print(f"Saved database to {filepath}")
loaded_db = VectorDB.load(filepath)
print(f"Loaded database with {loaded_db.size()} vectors")
# Verify loaded database works
results_loaded = loaded_db.search(query_embedding, k=3, ef=50, return_metadata=True)
print("\nVerifying loaded database (top 3):")
for i, (distance, idx, metadata) in enumerate(results_loaded, 1):
print(f" {i}. [{idx}] (dist: {distance:.4f}) {metadata['text'][:50]}...")
print()
if __name__ == "__main__":
basic_example()
sentence_embedding_example()
print("=== All examples completed successfully ===")