Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
sparse_embedding_utils.py50 linesDownload Raw Back to utils
1from typing import List, Optional, Tuple
2from chromadb.base_types import SparseVector
3
4
5def normalize_sparse_vector(
6    indices: List[int], 
7    values: List[float],
8    labels: Optional[List[str]] = None
9) -> SparseVector:
10    """Normalize and create a SparseVector by sorting indices and values together.
11
12    This function takes raw indices and values (which may be unsorted or have duplicates)
13    and returns a properly constructed SparseVector with sorted indices.
14
15    Args:
16        indices: List of dimension indices (may be unsorted)
17        values: List of values corresponding to each index
18        labels: Optional list of string labels corresponding to each index
19
20    Returns:
21        SparseVector with indices sorted in ascending order
22
23    Raises:
24        ValueError: If indices and values have different lengths
25        ValueError: If there are duplicate indices (after sorting)
26        ValueError: If indices are negative
27        ValueError: If values are not numeric
28        ValueError: If labels is provided and has different length than indices
29    """
30    if not indices:
31        return SparseVector(indices=[], values=[], labels=None)
32
33    # Sort indices, values, and labels together by index
34    if labels is not None:
35        sorted_triples = sorted(zip(indices, values, labels), key=lambda x: x[0])
36        sorted_indices, sorted_values, sorted_labels = zip(*sorted_triples)
37        return SparseVector(
38            indices=list(sorted_indices), 
39            values=list(sorted_values),
40            labels=list(sorted_labels)
41        )
42    else:
43        sorted_pairs = sorted(zip(indices, values), key=lambda x: x[0])
44        sorted_indices, sorted_values = zip(*sorted_pairs)
45        return SparseVector(
46            indices=list(sorted_indices), 
47            values=list(sorted_values),
48            labels=None
49        )
50 
codekingpro/portable-devtools · Team Ai