pinecone_text.hybrid.hybrid_convex

 1from typing import List, Tuple
 2from pinecone_text.sparse import SparseVector
 3
 4
 5def hybrid_convex_scale(
 6    query_dense: List[float], query_sparse: SparseVector, alpha: float
 7) -> Tuple[List[float], SparseVector]:
 8    """Hybrid vector scaling using a convex combination
 9    Args:
10        query_dense: a query dense vector represented as a list of floats
11        query_sparse: a query sparse vector represented as a dict of indices and values
12        alpha: float between 0 and 1 where 0 == sparse only and 1 == dense only
13
14    Returns:
15        a tuple of dense and sparse vectors scaled by alpha as a convex combination:
16        ((dense * alpha), (sparse * (1 - alpha)))
17    """
18
19    if not 0 <= alpha <= 1:
20        raise ValueError("Alpha must be between 0 and 1")
21
22    scaled_sparse = {
23        "indices": query_sparse["indices"],
24        "values": [v * (1 - alpha) for v in query_sparse["values"]],
25    }
26    scaled_dense = [v * alpha for v in query_dense]
27    return scaled_dense, scaled_sparse
def hybrid_convex_scale( query_dense: List[float], query_sparse: Dict[str, Union[List[int], List[float]]], alpha: float) -> Tuple[List[float], Dict[str, Union[List[int], List[float]]]]:
 6def hybrid_convex_scale(
 7    query_dense: List[float], query_sparse: SparseVector, alpha: float
 8) -> Tuple[List[float], SparseVector]:
 9    """Hybrid vector scaling using a convex combination
10    Args:
11        query_dense: a query dense vector represented as a list of floats
12        query_sparse: a query sparse vector represented as a dict of indices and values
13        alpha: float between 0 and 1 where 0 == sparse only and 1 == dense only
14
15    Returns:
16        a tuple of dense and sparse vectors scaled by alpha as a convex combination:
17        ((dense * alpha), (sparse * (1 - alpha)))
18    """
19
20    if not 0 <= alpha <= 1:
21        raise ValueError("Alpha must be between 0 and 1")
22
23    scaled_sparse = {
24        "indices": query_sparse["indices"],
25        "values": [v * (1 - alpha) for v in query_sparse["values"]],
26    }
27    scaled_dense = [v * alpha for v in query_dense]
28    return scaled_dense, scaled_sparse

Hybrid vector scaling using a convex combination

Arguments:
  • query_dense: a query dense vector represented as a list of floats
  • query_sparse: a query sparse vector represented as a dict of indices and values
  • alpha: float between 0 and 1 where 0 == sparse only and 1 == dense only
Returns:

a tuple of dense and sparse vectors scaled by alpha as a convex combination: ((dense * alpha), (sparse * (1 - alpha)))