|
24 | 24 | Tuple, |
25 | 25 | Union, |
26 | 26 | TypeAlias, |
| 27 | + Annotated, |
27 | 28 | ) |
| 29 | +from beartype.vale import IsAttr, IsEqual, Is |
28 | 30 |
|
29 | 31 | import numpy as np |
30 | 32 | import numpy.typing as npt |
|
47 | 49 |
|
48 | 50 | NDArray = npt.NDArray # required for beartype |
49 | 51 | ArrayLike = npt.ArrayLike # required for beartype |
50 | | -UInt8Array: TypeAlias = NDArray[np.uint8] # 2D |
51 | | -UInt8Vector: TypeAlias = NDArray[np.uint8] # 1D |
| 52 | +UInt8Array: TypeAlias = Annotated[ |
| 53 | + NDArray[np.uint8], |
| 54 | + IsAttr["ndim", IsEqual[2]] & IsAttr["dtype", IsEqual[np.dtype(np.uint8)]], |
| 55 | +] # 2D binary array with uint8 dtype |
| 56 | +UInt8Vector: TypeAlias = Annotated[ |
| 57 | + NDArray[np.uint8], |
| 58 | + IsAttr["ndim", IsEqual[1]] & IsAttr["dtype", IsEqual[np.dtype(np.uint8)]], |
| 59 | +] # 1D binary vector with uint8 dtype |
| 60 | + |
| 61 | +# Type alias for numeric input arrays that will be converted to binary |
| 62 | +NumericArrayLike: TypeAlias = Annotated[ |
| 63 | + Union[ArrayLike, List[float], List[List[float]]], |
| 64 | + Is[lambda x: len(np.array(x).shape) <= 2] # Max 2D |
| 65 | + & Is[lambda x: np.issubdtype(np.array(x).dtype, np.number)], # Numeric values only |
| 66 | +] |
52 | 67 |
|
53 | 68 |
|
54 | 69 | class CompressedFAISS(FAISS): |
@@ -317,7 +332,7 @@ async def new_aembedding_function(self, texts: List[str]) -> UInt8Array: |
317 | 332 |
|
318 | 333 | @staticmethod |
319 | 334 | def _vec_to_binary( |
320 | | - vectors: Union[ArrayLike, List[float], List[List[float]]], |
| 335 | + vectors: NumericArrayLike, |
321 | 336 | ) -> UInt8Array: |
322 | 337 | """Convert vectors to binary format using global zero threshold. |
323 | 338 |
|
|
0 commit comments