Save a Vicinity instance in a fast format. The Vicinity fast format stores the words and vectors of a Vicinity instance separately in a JSON and numpy format, respectively. :param folder: The path to which to save the JSON file. The vectors are saved separately. Th
(
self,
folder: PathLike,
overwrite: bool = False,
)
| 165 | return out |
| 166 | |
| 167 | def save( |
| 168 | self, |
| 169 | folder: PathLike, |
| 170 | overwrite: bool = False, |
| 171 | ) -> None: |
| 172 | """ |
| 173 | Save a Vicinity instance in a fast format. |
| 174 | |
| 175 | The Vicinity fast format stores the words and vectors of a Vicinity instance |
| 176 | separately in a JSON and numpy format, respectively. |
| 177 | |
| 178 | :param folder: The path to which to save the JSON file. The vectors are saved separately. The JSON contains a path to the numpy file. |
| 179 | :param overwrite: Whether to overwrite the JSON and numpy files if they already exist. |
| 180 | :raises ValueError: If the path is not a directory. |
| 181 | :raises JSONEncodeError: If the items are not JSON-serializable. ``save()`` and ``load()`` |
| 182 | only support item types that orjson can encode (e.g. strings, numbers, dicts). |
| 183 | Use ``Vicinity[str]`` or another serializable type if you need persistence. |
| 184 | """ |
| 185 | path = Path(folder) |
| 186 | path.mkdir(parents=True, exist_ok=overwrite) |
| 187 | |
| 188 | if not path.is_dir(): |
| 189 | raise ValueError(f"Path {path} should be a directory.") |
| 190 | |
| 191 | items_dict = {"items": self.items, "metadata": self.metadata, "backend_type": self.backend.backend_type.value} |
| 192 | try: |
| 193 | with open(path / "data.json", "wb") as file_handle: |
| 194 | file_handle.write(orjson.dumps(items_dict)) |
| 195 | except JSONEncodeError as e: |
| 196 | raise JSONEncodeError(f"Items could not be encoded to JSON because they are not serializable: {e}") |
| 197 | |
| 198 | self.backend.save(path) |
| 199 | if self.vector_store is not None: |
| 200 | store_path = path / "store" |
| 201 | store_path.mkdir(exist_ok=overwrite) |
| 202 | self.vector_store.save(store_path) |
| 203 | |
| 204 | @classmethod |
| 205 | def load(cls, filename: PathLike) -> Vicinity[Any]: |
no outgoing calls