-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.py
215 lines (187 loc) · 9.03 KB
/
cache.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
import json
import utils
import mlflow
import logging
from uuid import uuid4
from datetime import datetime
from databricks.vector_search.client import VectorSearchClient
class Cache:
def __init__(self, vsc, config):
mlflow.set_tracking_uri("databricks")
self.vsc = vsc
self.config = config
def create_cache(self):
# Create or wait for the endpoint
utils.create_or_wait_for_endpoint(self.vsc, self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE)
logging.info(f"Vector search endpoint '{self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE}' is ready")
# Create or update the main index
utils.create_or_update_direct_index(
self.vsc,
self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE,
self.config.VS_INDEX_FULLNAME_CACHE,
self.config.VECTOR_SEARCH_INDEX_SCHEMA_CACHE,
self.config.VECTOR_SEARCH_INDEX_CONFIG_CACHE,
)
logging.info(f"Main index '{self.config.VS_INDEX_FULLNAME_CACHE}' created/updated and is ready")
logging.info("Environment setup completed successfully")
@staticmethod
def load_data(file_path):
data = []
with open(file_path, 'r') as file:
for line in file:
data.append(json.loads(line))
return data
def get_embedding(self, text):
from mlflow.deployments import get_deploy_client
client = get_deploy_client("databricks")
response = client.predict(
endpoint=self.config.EMBEDDING_MODEL_SERVING_ENDPOINT_NAME,
inputs={"input": [text]})
return response.data[0]['embedding']
def warm_cache(self, batch_size=100):
vs_index_cache = self.vsc.get_index(
index_name=self.config.VS_INDEX_FULLNAME_CACHE,
endpoint_name=self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE,
)
# Load dataset
data = Cache.load_data(self.config.CACHE_WARMING_FILE_PATH)
logging.info(f"Loaded {len(data)} documents from {self.config.CACHE_WARMING_FILE_PATH}")
documents = []
for idx, item in enumerate(data):
if 'question' in item and 'answer' in item:
embedding = self.get_embedding(item['question'])
doc = {
"id": str(idx),
"creator": "system",
"question": item["question"],
"answer": item["answer"],
"access_level": 0,
"created_at": datetime.now().isoformat(),
"text_vector": embedding
}
documents.append(doc)
# Upsert when batch size is reached
if len(documents) >= batch_size:
try:
vs_index_cache.upsert(documents)
print(f"Successfully upserted batch of {len(documents)} documents.")
except Exception as e:
print(f"Error upserting batch: {str(e)}")
documents = [] # Clear the batch
# Upsert any remaining documents
if documents:
try:
vs_index_cache.upsert(documents)
print(f"Successfully upserted final batch of {len(documents)} documents.")
except Exception as e:
print(f"Error upserting final batch: {str(e)}")
logging.info("Index details:")
logging.info(f" Type: {type(vs_index_cache)}")
logging.info(f" Name: {vs_index_cache.name}")
logging.info(f" Endpoint name: {vs_index_cache.endpoint_name}")
logging.info(f"Finished loading documents into the index.")
logging.info("Cache warming completed successfully")
# Get response from cache
def get_from_cache(self, question, creator="user", access_level=0):
vs_index_cache = self.vsc.get_index(
index_name=self.config.VS_INDEX_FULLNAME_CACHE,
endpoint_name=self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE,
)
# Check if the question exists in the cache
qa = {"question": question, "answer": ""}
results = vs_index_cache.similarity_search(
query_vector=self.get_embedding(question),
columns=["id", "question", "answer"],
num_results=1
)
if results and results['result']['row_count'] > 0:
score = results['result']['data_array'][0][3] # Get the score
logging.info(f"Score: {score}")
try:
if float(score) >= self.config.SIMILARITY_THRESHOLD:
# Cache hit
qa["answer"] = results['result']['data_array'][0][2]
record_id = results['result']['data_array'][0][0] # Assuming 'id' is the first column
logging.info("Cache hit: True")
else:
logging.info("Cache hit: False")
except ValueError:
logging.info(f"Warning: Invalid score value: {score}")
return qa
# Store response to the cache
def store_in_cache(self, question, answer, creator="user", access_level=0):
vs_index_cache = self.vsc.get_index(
index_name=self.config.VS_INDEX_FULLNAME_CACHE,
endpoint_name=self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE,
)
document = {
"id": str(uuid4()),
"creator": creator,
"question": question,
"answer": answer,
"access_level": access_level,
"created_at": datetime.now().isoformat(),
"text_vector": self.get_embedding(question),
}
vs_index_cache.upsert([document])
def evict(self, strategy='FIFO', max_documents=1000, batch_size=100):
total_docs = self.get_indexed_row_count()
if total_docs <= max_documents:
logging.info(f"Cache size ({total_docs}) is within limit ({max_documents}). No eviction needed.")
return
docs_to_remove = total_docs - max_documents
logging.info(f"Evicting {docs_to_remove} documents from cache using {strategy} strategy...")
index = self.vsc.get_index(
index_name=self.config.VS_INDEX_FULLNAME_CACHE,
endpoint_name=self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE
)
if strategy == 'FIFO':
self._evict_fifo(index, docs_to_remove, batch_size)
elif strategy == 'LRU':
self._evict_lru(index, docs_to_remove, batch_size)
else:
raise ValueError(f"Unknown eviction strategy: {strategy}")
logging.info("Cache eviction completed.")
def _evict_fifo(self, index, docs_to_remove, batch_size):
while docs_to_remove > 0:
results = index.similarity_search(
query_vector=[0] * self.config.EMBEDDING_DIMENSION,
columns=["id", "created_at"],
num_results=min(docs_to_remove, batch_size),
)
if not results or results['result']['row_count'] == 0:
break
ids_to_remove = [row[0] for row in results['result']['data_array']]
index.delete(ids_to_remove)
docs_to_remove -= len(ids_to_remove)
logging.info(f"Removed {len(ids_to_remove)} documents from cache (FIFO).")
def _evict_lru(self, index, docs_to_remove, batch_size):
while docs_to_remove > 0:
results = index.similarity_search(
query_vector=[0] * self.config.EMBEDDING_DIMENSION,
columns=["id", "last_accessed"],
num_results=min(docs_to_remove, batch_size),
)
if not results or results['result']['row_count'] == 0:
break
ids_to_remove = [row[0] for row in results['result']['data_array']]
index.delete(ids_to_remove)
docs_to_remove -= len(ids_to_remove)
logging.info(f"Removed {len(ids_to_remove)} documents from cache (LRU).")
def get_indexed_row_count(self):
index = self.vsc.get_index(
index_name=self.config.VS_INDEX_FULLNAME_CACHE,
endpoint_name=self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE,
)
description = index.describe()
return description.get('status', {}).get('indexed_row_count', 0)
def clear_cache(self):
logging.info(f"Cleaning cache on endpoint {self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE}...")
if utils.index_exists(self.vsc, self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE, self.config.VS_INDEX_FULLNAME_CACHE):
try:
self.vsc.delete_index(self.config.VECTOR_SEARCH_ENDPOINT_NAME_CACHE, self.config.VS_INDEX_FULLNAME_CACHE)
logging.info(f"Cache index {self.config.VS_INDEX_FULLNAME_CACHE} deleted successfully")
except Exception as e:
logging.error(f"Error deleting cache index {self.config.VS_INDEX_FULLNAME_CACHE}: {str(e)}")
else:
logging.info(f"Cache index {self.config.VS_INDEX_FULLNAME_CACHE} does not exist")