MCPcopy Create free account
hub / github.com/MinishLab/vicinity / Vicinity

Class Vicinity

vicinity/vicinity.py:23–393  ·  view source on GitHub ↗

Work with vector representations of items. Supports functions for calculating fast batched similarity between items or composite representations of items.

Source from the content-addressed store, hash-verified

21
22
23class Vicinity(Generic[T]):
24 """
25 Work with vector representations of items.
26
27 Supports functions for calculating fast batched similarity
28 between items or composite representations of items.
29 """
30
31 def __init__(
32 self,
33 items: Sequence[T],
34 backend: AbstractBackend,
35 metadata: dict[str, Any] | None = None,
36 vector_store: BasicVectorStore | None = None,
37 ) -> None:
38 """
39 Initialize a Vicinity instance with an array and list of items.
40
41 :param items: The items in the vector space.
42 A list of items. Length must be equal to the number of vectors, and
43 aligned with the vectors.
44 :param backend: The backend to use for the vector space.
45 :param metadata: A dictionary containing metadata about the vector space.
46 :param vector_store: A simple vector store only used for storing actual vectors.
47 :raises ValueError: If the length of the items and vectors are not the same.
48 """
49 if len(items) != len(backend):
50 raise ValueError(
51 f"Your vector space and list of items are not the same length: {len(backend)} != {len(items)}"
52 )
53 self.items: list[T] = list(items)
54 self.backend: AbstractBackend = backend
55 self.metadata = metadata or {}
56 self.vector_store = vector_store
57
58 def get_vector_by_index(self, index: int | Iterable[int]) -> npt.NDArray:
59 """Get a vector by index."""
60 if isinstance(index, int):
61 index = [index]
62 if not all(0 <= i < len(self.items) for i in index):
63 raise ValueError("Index out of bounds.")
64 if self.vector_store is None:
65 raise ValueError(
66 "No vector store was provided. To get items by index, create a vicinity index by passing store_vectors=True on index creation."
67 )
68 return self.vector_store.get_by_index(list(index))
69
70 def __len__(self) -> int:
71 """The number of the items in the vector space."""
72 return len(self.items)
73
74 @classmethod
75 def from_vectors_and_items(
76 cls: type[Vicinity[T]],
77 vectors: npt.NDArray,
78 items: Sequence[T],
79 backend_type: Backend | str = Backend.BASIC,
80 store_vectors: bool = False,

Callers 1

load_from_hubMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…