File size: 1,010 Bytes
5a1b165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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]