sparse.py 1020 B

123456789101112131415161718192021222324252627282930313233343536
  1. import numpy as np
  2. from qdrant_client.http.models import SparseVector
  3. def empty_sparse_vector() -> SparseVector:
  4. return SparseVector(
  5. indices=[],
  6. values=[],
  7. )
  8. def validate_sparse_vector(vector: SparseVector) -> None:
  9. assert len(vector.indices) == len(
  10. vector.values
  11. ), "Indices and values must have the same length"
  12. assert not np.isnan(vector.values).any(), "Values must not contain NaN"
  13. assert len(vector.indices) == len(set(vector.indices)), "Indices must be unique"
  14. def is_sorted(vector: SparseVector) -> bool:
  15. for i in range(1, len(vector.indices)):
  16. if vector.indices[i] < vector.indices[i - 1]:
  17. return False
  18. return True
  19. def sort_sparse_vector(vector: SparseVector) -> SparseVector:
  20. if is_sorted(vector):
  21. return vector
  22. sorted_indices = np.argsort(vector.indices)
  23. return SparseVector(
  24. indices=[vector.indices[i] for i in sorted_indices],
  25. values=[vector.values[i] for i in sorted_indices],
  26. )