Skip to content
Open
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
33 changes: 32 additions & 1 deletion test/collection/test_byteops.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from weaviate.collections.grpc.shared import _ByteOps
import pytest

from weaviate.collections.grpc.query import _QueryGRPC
from weaviate.collections.grpc.shared import _ByteOps, _Pack, _Unpack
from weaviate.exceptions import WeaviateInvalidInputError
from weaviate.util import _ServerVersion


def test_decode_float32s():
Expand All @@ -22,3 +27,29 @@ def test_decode_int64s():
assert _ByteOps.decode_int64s(
b"\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00"
) == [1, 2]


def test_multi_vector_pack_round_trip():
vector = [[1.0, 2.0], [3.0, 4.0]]

assert _Unpack.multi(_Pack.multi(vector)) == vector


def test_multi_vector_pack_rejects_ragged_vectors():
with pytest.raises(WeaviateInvalidInputError, match="consistent dimensions"):
_Pack.multi([[1.0, 2.0], [3.0]])


def test_near_vector_request_rejects_ragged_multi_vector():
query = _QueryGRPC(
_ServerVersion.from_string("1.29.0"),
"TestCollection",
None,
None,
True,
True,
True,
)

with pytest.raises(WeaviateInvalidInputError, match="consistent dimensions"):
query.near_vector(near_vector=[[1.0, 2.0], [3.0]])
23 changes: 21 additions & 2 deletions weaviate/collections/grpc/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,27 @@ def single(vector: OneDimensionalVectorType) -> bytes:

@staticmethod
def multi(vector: TwoDimensionalVectorType) -> bytes:
vector_list = [item for sublist in vector for item in sublist]
return struct.pack("<H", len(vector[0])) + struct.pack(
if len(vector) == 0:
raise WeaviateInvalidInputError("Multi-vector embeddings must not be empty.")

first_vector = _get_vector_v4(vector[0])
dimension = len(first_vector)
if dimension == 0:
raise WeaviateInvalidInputError(
"Multi-vector embeddings must not contain empty vectors."
)

vector_list: List[float] = []
for subvector in vector:
subvector_list = _get_vector_v4(subvector)
if len(subvector_list) != dimension:
raise WeaviateInvalidInputError(
"Multi-vector embeddings must have consistent dimensions. "
f"Expected dimension {dimension}, got {len(subvector_list)}."
)
vector_list.extend(subvector_list)

return struct.pack("<H", dimension) + struct.pack(
"{}f".format(len(vector_list)), *vector_list
)

Expand Down