zinoubm's picture
refactoring the code on the SOLID principles
5a1b165
raw
history blame
1.01 kB
from abc import ABC, abstractmethod
import jsonlines
from gpt_3_manager import Gpt3Manager
from src.utils import dot_similarity
class Index(ABC):
@abstractmethod
def load(self, path):
pass
class JsonLinesIndex(Index):
def __init__(self):
pass
def load(self, path):
with jsonlines.open(path) as passages:
indexes = list(passages)
return indexes
class IndexSearchEngine:
def __init__(self, index):
index = index
def search(self, question, indexes, count=4):
question_embedding = Gpt3Manager.get_embedding(question)
simmilarities = []
for index in indexes:
embedding = index["embedding"]
score = dot_similarity(question_embedding, embedding)
simmilarities.append({"index": index, "score": score})
sorted_similarities = sorted(
simmilarities, key=lambda x: x["score"], reverse=True
)
return sorted_similarities[:count]