Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ jobs:
strategy:
matrix:
os: ["ubuntu-latest", "windows-latest"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12"]
exclude:
- os: windows-latest
python-version: "3.9"
- os: windows-latest
python-version: "3.11"
- os: windows-latest
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "vicinity"
description = "Lightweight Nearest Neighbors with Flexible Backends"
readme = { file = "README.md", content-type = "text/markdown" }
dynamic = ["version"]
requires-python = ">=3.9"
requires-python = ">=3.10"
license = { file = "LICENSE" }
authors = [{ name = "Stéphan Tulkens", email = "[email protected]"}, {name = "Thomas van Dongen", email = "[email protected]"}]

Expand All @@ -15,7 +15,6 @@ classifiers = [
"Topic :: Software Development :: Libraries",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand Down
244 changes: 3 additions & 241 deletions uv.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions vicinity/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from importlib.util import find_spec
from typing import Union

from vicinity.backends.base import AbstractBackend
from vicinity.backends.basic import BasicBackend, BasicVectorStore
Expand All @@ -20,7 +19,7 @@ def _require(module_name: str, backend: Backend, extra: str) -> None:
raise OptionalDependencyError(backend, extra)


def get_backend_class(backend: Union[Backend, str]) -> type[AbstractBackend]:
def get_backend_class(backend: Backend | str) -> type[AbstractBackend]:
"""Get the requested backend and ensure its dependencies are installed."""
backend = Backend(backend)

Expand Down
4 changes: 2 additions & 2 deletions vicinity/backends/annoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Any, Union
from typing import Any

import numpy as np
from annoy import AnnoyIndex
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
def from_vectors(
cls: type[AnnoyBackend],
vectors: npt.NDArray,
metric: Union[str, Metric],
metric: str | Metric,
trees: int,
**kwargs: Any,
) -> AnnoyBackend:
Expand Down
2 changes: 1 addition & 1 deletion vicinity/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def dump(self, file: Path) -> None:
@classmethod
def load(cls: type[ArgType], file: Path) -> ArgType:
"""Load the arguments from a file."""
with open(file, "r") as f:
with open(file) as f:
data = json.load(f)
data["metric"] = Metric.from_string(data["metric"])
return cls(**data)
Expand Down
4 changes: 2 additions & 2 deletions vicinity/backends/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Union
from typing import Any

import numpy as np
from numpy import typing as npt
Expand Down Expand Up @@ -115,7 +115,7 @@ def _dist(self, x: npt.NDArray) -> npt.NDArray:
raise NotImplementedError()

@classmethod
def from_vectors(cls, vectors: npt.NDArray, metric: Union[str, Metric] = "cosine", **kwargs: Any) -> BasicBackend:
def from_vectors(cls, vectors: npt.NDArray, metric: str | Metric = "cosine", **kwargs: Any) -> BasicBackend:
"""Create a new instance from vectors."""
metric_enum = Metric.from_string(metric)
if metric_enum not in cls.supported_metrics:
Expand Down
4 changes: 2 additions & 2 deletions vicinity/backends/faiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Union
from typing import Any

import faiss
from numpy import typing as npt
Expand Down Expand Up @@ -65,7 +65,7 @@ def from_vectors( # noqa: C901
cls: type[FaissBackend],
vectors: npt.NDArray,
index_type: str = "flat",
metric: Union[str, Metric] = "cosine",
metric: str | Metric = "cosine",
nlist: int = 100,
m: int = 8,
nbits: int = 8,
Expand Down
4 changes: 2 additions & 2 deletions vicinity/backends/hnsw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Any, Union
from typing import Any

from hnswlib import Index as HnswIndex
from numpy import typing as npt
Expand Down Expand Up @@ -41,7 +41,7 @@ def __init__(
def from_vectors(
cls: type[HNSWBackend],
vectors: npt.NDArray,
metric: Union[str, Metric],
metric: str | Metric,
ef_construction: int,
m: int,
**kwargs: Any,
Expand Down
4 changes: 2 additions & 2 deletions vicinity/backends/pynndescent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Any, Union
from typing import Any

import numpy as np
from numpy import typing as npt
Expand Down Expand Up @@ -37,7 +37,7 @@ def from_vectors(
cls: type[PyNNDescentBackend],
vectors: npt.NDArray,
n_neighbors: int = 15,
metric: Union[str, Metric] = "cosine",
metric: str | Metric = "cosine",
**kwargs: Any,
) -> PyNNDescentBackend:
"""Create a new instance from vectors."""
Expand Down
4 changes: 2 additions & 2 deletions vicinity/backends/usearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Any, Union
from typing import Any

import numpy as np
from numpy import typing as npt
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
def from_vectors(
cls: type[UsearchBackend],
vectors: npt.NDArray,
metric: Union[str, Metric] = "cos",
metric: str | Metric = "cos",
connectivity: int = 16,
expansion_add: int = 128,
expansion_search: int = 64,
Expand Down
4 changes: 2 additions & 2 deletions vicinity/backends/voyager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Any, Union
from typing import Any

from numpy import typing as npt
from voyager import Index, Space
Expand Down Expand Up @@ -41,7 +41,7 @@ def __init__(
def from_vectors(
cls: type[VoyagerBackend],
vectors: npt.NDArray,
metric: Union[str, Metric],
metric: str | Metric,
ef_construction: int,
m: int,
**kwargs: Any,
Expand Down
14 changes: 7 additions & 7 deletions vicinity/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from collections.abc import Iterable
from enum import Enum
from pathlib import Path
from typing import Iterable, List, Tuple, Union

from numpy import typing as npt

PathLike = Union[str, Path]
Matrix = Union[npt.NDArray, List[npt.NDArray]]
SimilarityItem = List[Tuple[str, float]]
SimilarityResult = List[SimilarityItem]
PathLike = str | Path
Matrix = npt.NDArray | list[npt.NDArray]
SimilarityItem = list[tuple[str, float]]
SimilarityResult = list[SimilarityItem]
# Tuple of (indices, distances)
SingleQueryResult = Tuple[npt.NDArray, npt.NDArray]
QueryResult = List[SingleQueryResult]
SingleQueryResult = tuple[npt.NDArray, npt.NDArray]
QueryResult = list[SingleQueryResult]
Tokens = Iterable[str]


Expand Down
2 changes: 1 addition & 1 deletion vicinity/integrations/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def push_to_hub(
token: str | None = None,
private: bool = False,
**kwargs: Any,
) -> "CommitInfo":
) -> CommitInfo:
"""
Push the Vicinity instance to the Hugging Face Hub.

Expand Down
5 changes: 2 additions & 3 deletions vicinity/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import annotations

from enum import Enum
from typing import Union

import numpy as np
from numpy import typing as npt


def normalize(vectors: npt.NDArray, norms: Union[npt.NDArray, None] = None) -> npt.NDArray:
def normalize(vectors: npt.NDArray, norms: npt.NDArray | None = None) -> npt.NDArray:
"""
Normalize a matrix of row vectors to unit length.

Expand Down Expand Up @@ -66,7 +65,7 @@ class Metric(Enum):
TANIMOTO = "tanimoto"

@classmethod
def from_string(cls, metric: Union[str, Metric]) -> Metric:
def from_string(cls, metric: str | Metric) -> Metric:
"""Convert a string or Metric enum to a Metric enum member."""
if isinstance(metric, cls):
return metric
Expand Down
8 changes: 4 additions & 4 deletions vicinity/vicinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from __future__ import annotations

import logging
from io import open
from collections.abc import Iterable, Sequence
from pathlib import Path
from time import perf_counter
from typing import Any, Iterable, Sequence, Type, Union
from typing import Any

import numpy as np
import orjson
Expand All @@ -32,7 +32,7 @@ def __init__(
self,
items: Sequence[Any],
backend: AbstractBackend,
metadata: Union[dict[str, Any], None] = None,
metadata: dict[str, Any] | None = None,
vector_store: BasicVectorStore | None = None,
) -> None:
"""
Expand Down Expand Up @@ -309,7 +309,7 @@ def push_to_hub(
)

@classmethod
def load_from_hub(cls: Type[Vicinity], repo_id: str, token: str | None = None, **kwargs: Any) -> Vicinity:
def load_from_hub(cls: type[Vicinity], repo_id: str, token: str | None = None, **kwargs: Any) -> Vicinity:
"""
Load a Vicinity instance from the Hugging Face Hub.

Expand Down