| 25 | |
| 26 | |
| 27 | class ThreadSafeFaiss(ThreadSafeObject): |
| 28 | def __repr__(self) -> str: |
| 29 | cls = type(self).__name__ |
| 30 | return f"<{cls}: key: {self.key}, obj: {self._obj}, docs_count: {self.docs_count()}>" |
| 31 | |
| 32 | def docs_count(self) -> int: |
| 33 | return len(self._obj.docstore._dict) |
| 34 | |
| 35 | def save(self, path: str, create_path: bool = True): |
| 36 | with self.acquire(): |
| 37 | if not os.path.isdir(path) and create_path: |
| 38 | os.makedirs(path) |
| 39 | ret = self._obj.save_local(path) |
| 40 | # logger.info(f"已将向量库 {self.key} 保存到磁盘") |
| 41 | return ret |
| 42 | |
| 43 | def clear(self): |
| 44 | ret = [] |
| 45 | with self.acquire(): |
| 46 | ids = list(self._obj.docstore._dict.keys()) |
| 47 | if ids: |
| 48 | ret = self._obj.delete(ids) |
| 49 | assert len(self._obj.docstore._dict) == 0 |
| 50 | # logger.info(f"已将向量库 {self.key} 清空") |
| 51 | return ret |
| 52 | |
| 53 | |
| 54 | class _FaissPool(CachePool): |
no outgoing calls
no test coverage detected