|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from evadb.third_party.vector_stores.types import ( |
| 4 | + FeaturePayload, |
| 5 | + VectorIndexQuery, |
| 6 | + VectorIndexQueryResult, |
| 7 | + VectorStore, |
| 8 | +) |
| 9 | +from evadb.utils.generic_utils import try_to_import_marqo_client |
| 10 | + |
| 11 | +_marqo_client_instance = None |
| 12 | + |
| 13 | +required_params = ["url", "index_name"] |
| 14 | + |
| 15 | + |
| 16 | +def get_marqo_client(url: str, api_key: str=None): |
| 17 | + global _marqo_client_instance |
| 18 | + if _marqo_client_instance is None: |
| 19 | + try_to_import_marqo_client() |
| 20 | + import marqo as mq |
| 21 | + _marqo_client_instance = mq.Client(url=url, api_key=api_key) |
| 22 | + return _marqo_client_instance |
| 23 | + |
| 24 | + |
| 25 | +class MarqoVectorStore(VectorStore): |
| 26 | + def __init__(self, index_name: str, url: str = "http://0.0.0.0:8882", api_key=None) -> None: |
| 27 | + self._client = get_marqo_client(url=url) |
| 28 | + self._index_name = index_name |
| 29 | + |
| 30 | + def create(self, vector_dim: int): |
| 31 | + |
| 32 | + # Delete index if exists already |
| 33 | + if self._index_name in [i.index_name for i in self._client.get_indexes()['results']]: |
| 34 | + self.delete() |
| 35 | + |
| 36 | + # create fresh |
| 37 | + # Refer here for details - https://docs.marqo.ai/2.0.0/API-Reference/Indexes/create_index/ |
| 38 | + self._client.create_index( |
| 39 | + index_name=self._index_name, |
| 40 | + settings_dict={ |
| 41 | + 'index_defaults': { |
| 42 | + 'model': 'no_model', |
| 43 | + 'model_properties': { |
| 44 | + 'dimensions': vector_dim |
| 45 | + }, |
| 46 | + |
| 47 | + 'normalize_embeddings': True, |
| 48 | + 'ann_parameters':{ |
| 49 | + 'space_type': 'cosinesimil' |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + def add(self, payload: List[FeaturePayload]): |
| 56 | + |
| 57 | + ids = [int(row.id) for row in payload] |
| 58 | + embeddings = [row.embedding for row in payload] |
| 59 | + |
| 60 | + data = [] |
| 61 | + for _id, _emb in zip(ids, embeddings): |
| 62 | + _id = str(_id) |
| 63 | + data.append( |
| 64 | + { |
| 65 | + '_id': _id, |
| 66 | + 'evadb_data':{ |
| 67 | + 'vector': _emb |
| 68 | + } |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + # For reference and more information |
| 73 | + # check - https://docs.marqo.ai/1.4.0/Guides/Advanced-Usage/document_fields/#custom-vector-object |
| 74 | + self._client.index( |
| 75 | + index_name=self._index_name |
| 76 | + ).add_documents( |
| 77 | + documents=data, |
| 78 | + mappings={ |
| 79 | + 'evadb_data':{ |
| 80 | + 'type': 'custom_vector' |
| 81 | + } |
| 82 | + }, |
| 83 | + tensor_fields=['evadb_data'], |
| 84 | + auto_refresh=True, |
| 85 | + client_batch_size=64 |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | + def delete(self) -> None: |
| 90 | + self._client.delete_index(index_name=self._index_name) |
| 91 | + |
| 92 | + def query( |
| 93 | + self, |
| 94 | + query: VectorIndexQuery, |
| 95 | + ) -> VectorIndexQueryResult: |
| 96 | + response = self._client.index( |
| 97 | + self._index_name).search( |
| 98 | + context={ |
| 99 | + 'tensor':[ |
| 100 | + { |
| 101 | + 'vector': list(query.embedding), |
| 102 | + 'weight' : 1 |
| 103 | + } |
| 104 | + ], |
| 105 | + }, |
| 106 | + limit=query.top_k |
| 107 | + ) |
| 108 | + |
| 109 | + similarities, ids = [], [] |
| 110 | + |
| 111 | + for result in response['hits']: |
| 112 | + ids.append(result['_id']) |
| 113 | + |
| 114 | + # Because it is similarity score |
| 115 | + similarities.append(1-result['_score']) |
| 116 | + |
| 117 | + return VectorIndexQueryResult(similarities=similarities, ids=ids) |
| 118 | + |
0 commit comments