
    BTh                     V   d dl mZ d dlmZmZ d dlZd dlmZ eeee	      eej                     ej                  f   Z G d dee      Z	 	 ddej                  ded	e	d
edee   f
dZdededej                  fdZ	 	 ddedee   d	e	d
edee   f
dZdedee   dej                  fdZy)    )Enum)ListUnionN)SparseValuesc                       e Zd ZdZdZdZdZy)DistanceStrategyzTEnumerator of the Distance strategies for calculating distances
    between vectors.EUCLIDEAN_DISTANCEMAX_INNER_PRODUCTCOSINEN)__name__
__module____qualname____doc__r	   r
   r        o/var/www/catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/langchain_pinecone/_utilities.pyr   r   
   s     .+Fr   r   query_embeddingembedding_listlambda_multkreturnc                    t        |t        |            dk  rg S | j                  dk(  rt        j                  | d      } t        | |      d   }t        t        j                  |            }|g}t        j                  ||   g      }t        |      t        |t        |            k  rt        j                   }d}	t        ||      }
t        |      D ]0  \  }}||v rt        |
|         }||z  d|z
  |z  z
  }||kD  s-|}|}	2 |j                  |	       t        j                  |||	   gd      }t        |      t        |t        |            k  r|S )z%Calculate maximal marginal relevance.r      axis)minlenndimnpexpand_dimscosine_similarityintargmaxarrayinf	enumeratemaxappend)r   r   r   r   similarity_to_querymost_similaridxsselected
best_score
idx_to_addsimilarity_to_selectediquery_scoreredundant_scoreequation_scores                  r   maximal_marginal_relevancer5      s^    1c.!"a'	q ..qA+O^LQOryy!456L>Dxx567H
d)c!S01
1ffW

!2>8!L'(;< 		NA{Dy!"8";<Ok)Q_,OO  
*+

		 	J99Xz(B'C!L d)c!S01
1  Kr   XYc                    t        |       dk(  st        |      dk(  rt        j                  g       S t        j                  |       } t        j                  |      }| j                  d   |j                  d   k7  r&t	        d| j                   d|j                   d      	 ddl}t        j                  | t        j                        } t        j                  |t        j                        }dt        j                  |j                  | |d	            z
  }|S # t        $ r t        j                  j                  | d
      }t        j                  j                  |d
      }t        j                  dd      5  t        j                  | |j                        t        j                  ||      z  }ddd       n# 1 sw Y   nxY wdt        j                  |      t        j                   |      z  <   |cY S w xY w)z<Row-wise cosine similarity between two equal-width matrices.r   r   z;Number of columns in X and Y must be the same. X has shape z and Y has shape .N)dtypecosine)metricr   ignore)divideinvalid        )r   r    r%   shape
ValueErrorsimsimdfloat32cdistImportErrorlinalgnormerrstatedotTouterisnanisinf)r6   r7   simdZX_normY_norm
similaritys          r   r"   r"   5   s   
1v{c!fkxx|
A
AwwqzQWWQZI!'' S wwiq*
 	
HHQbjj)HHQbjj)Aq:;; **[[(; 	C133"((66*BBJ	C 	C 	CBE
288J'"((:*>>?s,   "A9D A"G>>8F?6	G>?G	7G>=G>c                 n   t        |t        |            dk  rg S t        | |      }t        t	        j
                  |            }|g}||   g}t        |      t        |t        |            k  rt        j                   }d}	t        |      D ]^  \  }
}|
|v rg }|D ]&  }t        ||
   |g      d   }|j                  |       ( |rt        |      nd}||z  d|z
  |z  z
  }||kD  s[|}|
}	` |j                  |	       |j                  ||	          t        |      t        |t        |            k  r|S )a  Calculate maximal marginal relevance for sparse vectors.

    Args:
        query_embedding: A sparse vector representation of the query
        embedding_list: A list of sparse vector representations to compare against
        lambda_mult: Controls the weight given to query similarity vs diversity
        k: The number of results to return

    Returns:
        A list of indices of the selected items in order of relevance
    r   r   r@   r   )
r   r   sparse_cosine_similarityr#   r    r$   r&   r'   r)   r(   )r   r   r   r   r*   r+   r,   r-   r.   r/   r1   r2   redundant_scoresselected_embeddingsimr3   r4   s                    r   !sparse_maximal_marginal_relevancerY   R   sn   " 1c.!"a'	 3?NS ryy!456L>D|,-H d)c!S01
1ffW

 ((;< 	NA{Dy  "&. -".~a/@CUBVW !'',- 8Hc"23SO k)Q_,OO  
*+

-	0 	Jz23= d)c!S01
1@ Kr   c                    |st        j                  g       S t        j                  t        d | j                  D                    }|dk(  rt        j
                  t        |            S t        j
                  t        |            }t        | j                  | j                        D ci c]  \  }}||
 }}}t        |      D ]t  \  }}d}	d}
t        |j                  |j                        D ]  \  }}|
||z  z  }
||v s|	||   |z  z  }	 t        j                  |
      }
|
dk(  rd||<   j|	||
z  z  ||<   v |S c c}}w )zCalculate cosine similarity between sparse vectors without converting to dense.

    Args:
        X: A single sparse vector
        Y: A list of sparse vectors

    Returns:
        A numpy array of similarity scores
    c              3   &   K   | ]	  }||z    y w)Nr   ).0vals     r   	<genexpr>z+sparse_cosine_similarity.<locals>.<genexpr>   s     7ss7s   r   r@   )
r    r%   sqrtsumvalueszerosr   zipindicesr'   )r6   r7   x_normsimilaritiesidxr]   x_dictr1   y_vecdot_producty_norms              r   rU   rU      s8    xx| WWS7ahh778F{xxA88CF#L (+199ahh'?@83c3h@F@aL >5EMM5<<8 	1HCcCiFf}vc{S00	1
  Q;!LO)Vf_=LO!>$ ) As   +D?)g      ?   )enumr   typingr   r   numpyr    pineconer   floatndarrayMatrixstrr   listr#   r5   r"   rY   rU   r   r   r   <module>rv      s      !	tDK $rzz"2BJJ>	?sD  	ZZ  	
 
#YD F rzz @ 	=!=&= = 	=
 
#Y=@) )l1C )

 )r   