blob: 1a97d160bb4be78a59b19bde20d00c92ccb22378 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import os
from typing import Tuple
import faiss
import numpy as np
# TODO: inner product distance metric?
class Embeddings:
def __init__(self):
self.dim = int(os.environ["EMBEDDING_DIM"])
self.index = faiss.IndexFlatL2(self.dim)
# TODO: load from file
def add(self, embeddings: np.ndarray):
# TODO: save to file
self.index.add(embeddings)
def search(
self, query: np.ndarray, neighbors: int = 4
) -> Tuple[np.ndarray, np.ndarray]:
score, indices = self.index.search(query, neighbors)
return score, indices
|