• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

georgia-tech-db / eva / #758

04 Sep 2023 08:37PM UTC coverage: 0.0% (-78.3%) from 78.333%
#758

push

circle-ci

hershd23
Increased underline length in at line 75 in text_summarization.rst
	modified:   docs/source/benchmarks/text_summarization.rst

0 of 11303 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/evadb/third_party/vector_stores/faiss.py
1
# coding=utf-8
2
# Copyright 2018-2023 EvaDB
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
from pathlib import Path
×
16
from typing import List
×
17

18
import numpy as np
×
19

20
from evadb.third_party.vector_stores.types import (
×
21
    FeaturePayload,
22
    VectorIndexQuery,
23
    VectorIndexQueryResult,
24
    VectorStore,
25
)
26
from evadb.utils.generic_utils import try_to_import_faiss
×
27

28
required_params = ["index_path"]
×
29

30

31
class FaissVectorStore(VectorStore):
×
32
    def __init__(self, index_name: str, index_path: str) -> None:
×
33
        # Reference to Faiss documentation.
34
        # IDMap: https://github.com/facebookresearch/faiss/wiki/Pre--and-post-processing#faiss-id-mapping
35
        # Other index types: https://github.com/facebookresearch/faiss/wiki/The-index-factory
36
        try_to_import_faiss()
×
37
        self._index_name = index_name
×
38
        self._index_path = index_path
×
39
        self._index = None
×
40

41
    def create(self, vector_dim: int):
×
42
        import faiss
×
43

44
        self._index = faiss.IndexIDMap2(faiss.IndexHNSWFlat(vector_dim, 32))
×
45

46
    def add(self, payload: List[FeaturePayload]):
×
47
        assert self._index is not None, "Please create an index before adding features."
×
48
        for row in payload:
×
49
            embedding = np.array(row.embedding, dtype="float32")
×
50
            if len(embedding.shape) != 2:
×
51
                embedding = embedding.reshape(1, -1)
×
52
            self._index.add_with_ids(embedding, np.array([row.id]))
×
53

54
    def persist(self):
×
55
        assert self._index is not None, "Please create an index before calling persist."
×
56
        import faiss
×
57

58
        faiss.write_index(self._index, self._index_path)
×
59

60
    def query(self, query: VectorIndexQuery) -> VectorIndexQueryResult:
×
61
        import faiss
×
62

63
        if self._index is None:
×
64
            self._index = faiss.read_index(self._index_path)
×
65
        assert self._index is not None, "Cannot query as index does not exists."
×
66
        embedding = np.array(query.embedding, dtype="float32")
×
67
        if len(embedding.shape) != 2:
×
68
            embedding = embedding.reshape(1, -1)
×
69

70
        dists, indices = self._index.search(embedding, query.top_k)
×
71
        distances, ids = [], []
×
72
        for dis, idx in zip(dists[0], indices[0]):
×
73
            distances.append(dis)
×
74
            ids.append(idx)
×
75
        return VectorIndexQueryResult(distances, ids)
×
76

77
    def delete(self):
×
78
        index_path = Path(self._index_path)
×
79
        if index_path.exists():
×
80
            index_path.unlink()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc