File size: 6,777 Bytes
74b1bac |
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 |
import json
import time
import faiss
##############################Sentence_Bert#########################################
import numpy as np
from sentence_transformers import SentenceTransformer # pylint: disable=C0413
from abc import ABCMeta, abstractmethod
class BaseEmbedding(metaclass=ABCMeta):
"""
Base Embedding interface.
"""
@abstractmethod
def to_embeddings(self, data, **kwargs):
pass
@property
@abstractmethod
def dimension(self) -> int:
return 0
class SBERT(BaseEmbedding):
"""Generate sentence embedding for given text using pretrained models of Sentence Transformers.
:param model: model name, defaults to 'all-MiniLM-L6-v2'.
:type model: str
Example:
.. code-block:: python
from gptcache.embedding import SBERT
test_sentence = 'Hello, world.'
encoder = SBERT('all-MiniLM-L6-v2')
embed = encoder.to_embeddings(test_sentence)
"""
def __init__(self, model: str = "all-MiniLM-L6-v2"):
self.model = SentenceTransformer(model)
self.model.eval()
self.__dimension = None
def to_embeddings(self, data, **_):
"""Generate embedding given text input
:param data: text in string.
:type data: str
:return: a text embedding in shape of (dim,).
"""
if not isinstance(data, list):
data = [data]
emb = self.model.encode(data)
_, dim = emb.shape
if not self.__dimension:
self.__dimension = dim
return np.array(emb).astype("float32")
@property
def dimension(self):
"""Embedding dimension.
:return: embedding dimension
"""
if not self.__dimension:
embd = self.model.encode(["foo"])
_, self.__dimension = embd.shape
return self.__dimension
#################################### Adapter ########################################
def init_cache(embedding_model: str = "all-MiniLM-L6-v2"):
"""Initializes the cache with a Faiss index and an SBERT model.
Args:
embedding_model (str): The name of the SBERT model to use.
Returns:
tuple: (index, encoder) where
- index is a Faiss index for storing embeddings.
- encoder is an SBERT model instance.
"""
encoder = SBERT(embedding_model)
dimension = encoder.dimension
print(dimension)
index = faiss.IndexFlatL2(dimension)
if index.is_trained:
print('Index initialized and ready for use')
return index, encoder
def retrieve_cache(json_file):
try:
with open(json_file, 'r') as file:
cache = json.load(file)
except FileNotFoundError:
cache = {'questions': [], 'answers': []}
return cache
def store_cache(json_file, cache):
with open(json_file, 'w', encoding = 'utf-8') as file:
json.dump(cache, file)
#####################################################################3
class Cache:
def __init__(self, embedding = "all-MiniLM-L6-v2" , json_file="cache_file.json", thresold=0.5, max_response=100, eviction_policy='FIFO'):
"""Initializes the semantic cache.
Args:
json_file (str): The name of the JSON file where the cache is stored.
thresold (float): The threshold for the Euclidean distance to determine if a question is similar.
max_response (int): The maximum number of responses the cache can store.
eviction_policy (str): The policy for evicting items from the cache.
This can be any policy, but 'FIFO' (First In First Out) has been implemented for now.
If None, no eviction policy will be applied.
"""
# Initialize Faiss index with Euclidean distance
self.index, self.encoder = init_cache(embedding)
# Set Euclidean distance threshold
# a distance of 0 means identicals sentences
# We only return from cache sentences under this thresold
self.euclidean_threshold = thresold
self.is_missed = True
self.json_file = json_file
self.cache = retrieve_cache(self.json_file)
self.max_response = max_response
self.eviction_policy = eviction_policy
def evict(self):
"""Evicts an item from the cache based on the eviction policy."""
if self.eviction_policy and len(self.cache["questions"]) > self.max_response:
for _ in range((len(self.cache["questions"]) - self.max_response)):
if self.eviction_policy == 'FIFO':
self.cache["questions"].pop(0)
self.cache["answers"].pop(0)
def cached_hit(self, question: str) -> str:
"""Handles the cache hit logic by retrieving the answer from the cache.
Args:
question (str): The input question.
embedding: The embedding of the question.
Returns:
str: The cached answer.
"""
# Search for the nearest neighbor in the index
embedding = self.encoder.to_embeddings([question])
self.index.nprobe = 8
D, I = self.index.search(embedding, 1)
print(D)
if D[0] >= 0:
if I[0][0] >= 0 and D[0][0] / 100 <= self.euclidean_threshold:
row_id = int(I[0][0])
print('Answer recovered from Cache.')
print(f'Distance: {D[0][0]:.3f} (Threshold: {self.euclidean_threshold})')
print(f'Found in cache at row: {row_id} with score: {D[0][0]:.3f}')
self.is_missed =False
return self.cache['answers'][row_id]
self.is_missed = True
return embedding , self.is_missed
def cache_miss(self, question: str, embedding , answer) -> str:
"""Handles the cache miss logic by querying the model and updating the cache.
Args:
question (str): The input question.
embedding: The embedding of the question take from cache_hit if hit nothing
answer (str) : The answer from LLMs
Returns:
Append to cache and return answer.
"""
# Update the cache with the new question, embedding, and answer
self.cache['questions'].append(question)
self.cache['answers'].append(answer)
print('Answer not found in cache, appending new answer.')
print(f'Response: {answer}')
# Add the new embedding to the index
self.index.add(embedding)
# Evict items if necessary
self.evict()
# Save the updated cache to the JSON file
store_cache(self.json_file, self.cache)
self.is_missed = False
return answer
|