-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectordb.py
304 lines (254 loc) · 11 KB
/
vectordb.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import math
import os
from typing import List
import numpy as np
import pinecone
import redis
from datasets import load_dataset
from loguru import logger
from qdrant_client import QdrantClient
from qdrant_client.http import models
from qdrant_client.http.models import CollectionStatus, PointStruct, UpdateStatus
from redis.commands.search.field import TextField, VectorField
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import Query
class VectorDatabase:
"""VectorDatabase class initializes the Vector Database index_name and loads the dataset
for the usage of the subclasses."""
def __init__(self, index_name, top_k: int = 3):
self.index_name = index_name
logger.info(f"Index name: {self.index_name} initialized")
# Load the dataset
self.dataset = load_dataset(
"Cohere/wikipedia-22-12-simple-embeddings", split="train"
).select(range(1000))
logger.info(f"Dataset loaded with {len(self.dataset)} records")
self.top_k = top_k
self.dimension = 768
def upsert(self) -> str:
raise NotImplementedError
def query(self, query_embedding: List[float]) -> dict:
raise NotImplementedError
class PineconeDB(VectorDatabase):
"""PineconeDB class is a subclass of VectorDatabase that
interacts with the Pinecone Cloud Vector Database index and
has the following methods:
- upsert: Upserts the embedding into the Pinecone index along with the metadata
- query: Queries the Pinecone index with the query embedding along with the metadata
- delete_index: Deletes the Pinecone index
"""
def __init__(self, index_name):
super().__init__(index_name)
self.batch_size = 100 # Adjust the batch size as per your requirements
pinecone.init(
api_key=os.environ["PINECONE_API_KEY"],
environment=os.environ["PINECONE_ENVIRONMENT"],
)
# Create the index if it doesn't exist
if self.index_name not in pinecone.list_indexes():
pinecone.create_index(index_name, dimension=self.dimension, metric="cosine")
# Connect to the index
self.index = pinecone.Index(index_name=index_name)
def upsert(self) -> str:
logger.info(f"total vectors from upsert: {len(self.dataset)}")
num_vectors = len(self.dataset)
logger.info(f"total num of vectors from upsert: {num_vectors}")
num_batches = math.ceil(num_vectors / self.batch_size)
logger.info(f"Upserting {num_vectors} vectors in {num_batches} batches")
for i in range(num_batches):
start_idx = i * self.batch_size
end_idx = min((i + 1) * self.batch_size, num_vectors)
vectors_batch = [
(
f"{self.dataset[j]['id']}",
self.dataset[j]["emb"],
{"text": self.dataset[j]["text"]},
)
for j in range(start_idx, end_idx)
]
logger.info(
f"Upserting batch {i + 1} of {num_batches}, from {start_idx} to {end_idx}"
)
self.index.upsert(vectors_batch)
logger.info(f"Upserted {num_vectors} vectors")
return "Upserted successfully"
def query(self, query_embedding: List[float]) -> dict:
# Pinecone Output:
# {
# "matches": [
# {
# "id": "9",
# "metadata": {
# "text": "In the Old Testament, Almighty God is the one who created the world. The God of the Old Testament is not always presented as the only God who exists Even though there may be other gods, the God of the Old Testament is always shown as the only God whom Israel is to worship. The God of the Old Testament is the one 'true God'; only Yahweh is Almighty. Both Jews and Christians have always interpreted the Bible (both the 'Old' and 'New' Testaments) as an affirmation of the oneness of Almighty God."
# },
# "score": 40.6401978,
# "values": [0.479291856, ..., 0.31344567],
# }
# ],
# "namespace": "",
# }
return self.index.query(
vector=query_embedding,
top_k=self.top_k,
include_values=True,
include_metadata=True,
)
def delete_index(self) -> str:
pinecone.delete_index(self.index_name)
return "Index deleted"
class QdrantDB(VectorDatabase):
"""QdrantDB class is a subclass of VectorDatabase that
interacts with the Qdrant Cloud Vector Database. It has the following methods:
- upsert: Upserts the dataset into the Qdrant collection(index) with the payload(metadata)
- query: Queries the Qdrant collection(index) with the query embedding along with
the payload(metadata)
- delete_index: Deletes the Qdrant collection(index)
"""
def __init__(self, index_name):
super().__init__(index_name)
self.batch_size = 1000 # Adjust the batch size as per your requirements
self.qdrant_client = QdrantClient(
os.environ["QDRANT_URL"],
prefer_grpc=True,
api_key=os.environ["QDRANT_API_KEY"],
)
collection_info = self.qdrant_client.get_collection(
collection_name=self.index_name
)
# Create the collection(index) if it doesn't exist
if collection_info.status != CollectionStatus.GREEN:
self.qdrant_client.recreate_collection(
collection_name=self.index_name,
vectors_config=models.VectorParams(
size=self.dimension, distance=models.Distance.COSINE
),
)
def upsert(self) -> str:
logger.info(f"total vectors from upsert: {len(self.dataset)}")
num_vectors = len(self.dataset)
logger.info(f"total num of vectors from upsert: {num_vectors}")
num_batches = math.ceil(num_vectors / self.batch_size)
logger.info(f"Upserting {num_vectors} vectors in {num_batches} batches")
for i in range(num_batches):
start_idx = i * self.batch_size
end_idx = min((i + 1) * self.batch_size, num_vectors)
vectors_batch = [
PointStruct(
id=self.dataset[j]["id"],
vector=self.dataset[j]["emb"],
payload={"text": self.dataset[j]["text"]},
)
for j in range(start_idx, end_idx)
]
logger.info(
f"Upserting batch {i + 1} of {num_batches}, from {start_idx} to {end_idx}"
)
operation_info = self.qdrant_client.upsert(
collection_name=self.index_name, wait=True, points=vectors_batch
)
if operation_info.status != UpdateStatus.COMPLETED:
raise Exception("Upsert failed")
logger.info(f"Upserted {num_vectors} vectors")
return "Upserted successfully"
def query(self, query_embedding: List[float]) -> dict:
# Qdrant Output:
# {
# "result": [
# {"id": 4, "score": 1.362},
# {"id": 1, "score": 1.273},
# {"id": 3, "score": 1.208},
# ],
# "status": "ok",
# "time": 0.000055785,
# }
return self.qdrant_client.search(
collection_name=self.index_name,
query_vector=query_embedding,
limit=self.top_k,
with_payload=True,
)
def delete_index(self) -> str:
self.qdrant_client.delete_collection(collection_name=self.index_name)
return "Index deleted"
class RedisDB(VectorDatabase):
def __init__(self, index_name):
super().__init__(index_name)
self.batch_size = 1000 # Adjust the batch size as per your requirements
# self.redis_client = redis.from_url(url="redis://localhost:6379")
self.redis_client = redis.from_url(url=os.environ["REDIS_URL"])
logger.info("Redis client initialized")
def check_redis_index() -> bool:
"""Check if Redis index exists."""
logger.info(f"Checking if index {self.index_name} exists")
try:
self.redis_client.ft(self.index_name).info()
except: # noqa: E722
logger.info(f"Index {self.index_name} does not exist")
return False
logger.info(f"Index {self.index_name} already exists")
return True
if not check_redis_index():
logger.info(f"Creating index {self.index_name}")
# Constants
schema = (
TextField(name="text"),
TextField(name="metadata"),
VectorField(
"embedding",
"FLAT",
{
"TYPE": "FLOAT32",
"DIM": self.dimension,
"DISTANCE_METRIC": "COSINE",
},
),
)
prefix = f"doc:{self.index_name}"
# Create Redis Index
self.redis_client.ft(index_name=self.index_name).create_index(
fields=schema,
definition=IndexDefinition(prefix=[prefix], index_type=IndexType.HASH),
)
def upsert(self) -> str:
# Write data to redis
pipeline = self.redis_client.pipeline(transaction=False)
for i, data in enumerate(self.dataset):
# Use provided values by default or fallback
key = data["id"]
metadata = data["title"]
embedding = data["emb"]
text = data["text"]
pipeline.hset(
key,
mapping={
"text": text,
"vector": np.array(embedding, dtype=np.float32).tobytes(),
"metadata": metadata,
},
)
# Write batch
if i % self.batch_size == 0:
pipeline.execute()
# Cleanup final batch
pipeline.execute()
return "Upserted successfully"
def query(self, query_embedding: List[float]) -> dict:
# base_query = (
# f"(@metadata:{ Old Testament })=>[KNN {self.top_k} @vector $query_embedding AS score]"
# )
# logger.info(f"base query: {base_query}")
query = (
Query("*=>[KNN 3 @vector $query_embedding AS score]")
.return_fields("text", "score")
.dialect(2)
)
query_params = {
"query_embedding": np.array(query_embedding, dtype=np.float32).tobytes()
}
# logger.info(f"byte embedding: {query_params['query_embedding']}")
result = self.redis_client.ft(self.index_name).search(query, query_params)
logger.info(f"result: {result}, type: {type(result)}")
return result
def delete_index(self) -> str:
self.redis_client.ft(self.index_name).drop_index(delete_documents=True)
return "Index deleted"