|
| 1 | +from typing import List |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import logging |
| 5 | +from modelcache_mm.manager.vector_data.base import VectorBase, VectorData |
| 6 | +from modelcache_mm.utils import import_chromadb, import_torch |
| 7 | +from modelcache_mm.utils.index_util import get_mm_index_name |
| 8 | + |
| 9 | +import_torch() |
| 10 | +import_chromadb() |
| 11 | + |
| 12 | +import chromadb |
| 13 | + |
| 14 | + |
| 15 | +class Chromadb(VectorBase): |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + persist_directory="./chromadb", |
| 20 | + top_k: int = 1, |
| 21 | + ): |
| 22 | + self.top_k = top_k |
| 23 | + |
| 24 | + self._client = chromadb.PersistentClient(path=persist_directory) |
| 25 | + self._collection = None |
| 26 | + |
| 27 | + def create(self, model=None, mm_type=None): |
| 28 | + try: |
| 29 | + collection_name_model = get_mm_index_name(model, mm_type) |
| 30 | + # collection_name_model = self.collection_name + '_' + model |
| 31 | + self._client.get_or_create_collection(name=collection_name_model) |
| 32 | + except Exception as e: |
| 33 | + raise ValueError(str(e)) |
| 34 | + |
| 35 | + def add(self, datas: List[VectorData], model=None, mm_type=None): |
| 36 | + collection_name_model = get_mm_index_name(model, mm_type) |
| 37 | + self._collection = self._client.get_or_create_collection(name=collection_name_model) |
| 38 | + |
| 39 | + data_array, id_array = map(list, zip(*((data.data.tolist(), str(data.id)) for data in datas))) |
| 40 | + self._collection.add(embeddings=data_array, ids=id_array) |
| 41 | + |
| 42 | + def search(self, data: np.ndarray, top_k: int = -1, model=None, mm_type='mm'): |
| 43 | + collection_name_model = get_mm_index_name(model, mm_type) |
| 44 | + self._collection = self._client.get_or_create_collection(name=collection_name_model) |
| 45 | + |
| 46 | + if self._collection.count() == 0: |
| 47 | + return [] |
| 48 | + if top_k == -1: |
| 49 | + top_k = self.top_k |
| 50 | + results = self._collection.query( |
| 51 | + query_embeddings=[data.tolist()], |
| 52 | + n_results=top_k, |
| 53 | + include=["distances"], |
| 54 | + ) |
| 55 | + return list(zip(results["distances"][0], [int(x) for x in results["ids"][0]])) |
| 56 | + |
| 57 | + def delete(self, ids, model=None, mm_type=None): |
| 58 | + try: |
| 59 | + collection_name_model = get_mm_index_name(model, mm_type) |
| 60 | + self._collection = self._client.get_or_create_collection(name=collection_name_model) |
| 61 | + # 查询集合中实际存在的 ID |
| 62 | + ids_str = [str(x) for x in ids] |
| 63 | + existing_ids = set(self._collection.get(ids=ids_str).ids) |
| 64 | + |
| 65 | + # 删除存在的 ID |
| 66 | + if existing_ids: |
| 67 | + self._collection.delete(list(existing_ids)) |
| 68 | + |
| 69 | + # 返回实际删除的条目数量 |
| 70 | + return len(existing_ids) |
| 71 | + |
| 72 | + except Exception as e: |
| 73 | + logging.error('Error during deletion: {}'.format(e)) |
| 74 | + raise ValueError(str(e)) |
| 75 | + |
| 76 | + def rebuild_idx(self, model, mm_type=None): |
| 77 | + collection_name_model = get_mm_index_name(model, mm_type) |
| 78 | + |
| 79 | + # 检查集合是否存在,如果存在则删除 |
| 80 | + collections = self._client.list_collections() |
| 81 | + if any(col.name == collection_name_model for col in collections): |
| 82 | + self._client.delete_collection(collection_name_model) |
| 83 | + else: |
| 84 | + return 'model collection not found, please check!' |
| 85 | + |
| 86 | + try: |
| 87 | + self._client.create_collection(collection_name_model) |
| 88 | + except Exception as e: |
| 89 | + logging.info(f'rebuild_collection: {e}') |
| 90 | + raise ValueError(str(e)) |
| 91 | + |
| 92 | + def rebuild(self, ids=None): |
| 93 | + pass |
| 94 | + |
| 95 | + def flush(self): |
| 96 | + pass |
| 97 | + |
| 98 | + def close(self): |
| 99 | + pass |
0 commit comments