
    Ngc
                     0   d Z ddlZddlmZmZmZmZ ddlZ ej	        e
          Zeeee                  eej                 ej        f         Zdededej        fdZ	 	 ddeded	ee         d
ee         deeeeef                  ee         f         f
dZdS )zMath utils.    N)ListOptionalTupleUnionXYreturnc                    t          |           dk    st          |          dk    rt          j        g           S t          j        |           } t          j        |          }| j        d         |j        d         k    r t	          d| j         d|j         d          	 ddl}t          j        | t          j                  } t          j        |t          j                  }dt          j        |                    | |d	                    z
  }|S # t          $ r t          
                    d
           t          j                            | d          }t          j                            |d          }t          j        dd          5  t          j        | |j                  t          j        ||          z  }ddd           n# 1 swxY w Y   d|t          j        |          t          j        |          z  <   |cY S w xY w)z<Row-wise cosine similarity between two equal-width matrices.r      z;Number of columns in X and Y must be the same. X has shape z and Y has shape .N)dtypecosine)metriczUnable to import simsimd, defaulting to NumPy implementation. If you want to use simsimd please install with `pip install simsimd`.axisignore)divideinvalidg        )lennparrayshape
ValueErrorsimsimdfloat32cdistImportErrorloggerdebuglinalgnormerrstatedotTouterisnanisinf)r   r   simdZX_normY_norm
similaritys          Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utils/math.pycosine_similarityr.      s2   
1vv{{c!ffkkx||
A
AwqzQWQZ*!' * * w* * *
 
 	
HQbj)))HQbj)))Aq::;;;   H	
 	
 	
 ****[(;;; 	C 	C13"(66*B*BBJ	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	CBE
28J''"(:*>*>>?s8    A2D A<H1G HG	HG	1HH   top_kscore_thresholdc                 f   t          |           dk    st          |          dk    rg g fS t          | |          }|pd}d|||k     <   t          |pt          |          t          j        |                    }t          j        || d          | d         }|t          j        |                                |                            ddd         }t          j        ||j	                  }|                                |         
                                }t          t          |           |fS )a  Row-wise cosine similarity with optional top-k and score threshold filtering.

    Args:
        X: Matrix.
        Y: Matrix, same width as X.
        top_k: Max number of results to return.
        score_threshold: Minimum cosine similarity of results.

    Returns:
        Tuple of two lists. First contains two-tuples of indices (X_idx, Y_idx),
            second contains corresponding cosine similarities.
    r   g      Nr   )r   r.   minr   count_nonzeroargpartitionargsortravelunravel_indexr   tolistlistzip)r   r   r0   r1   score_array
top_k_idxsret_idxsscoress           r-   cosine_similarity_top_krA   .   s   $ 1vv{{c!ffkk2v#Aq))K%-O12Ko-.)[))2+;K+H+HIIEuf4@@@%IJBJ{'8'8':'::'FGGH2NJ
K,=>>H  ,3355FX''    )r/   N)__doc__loggingtypingr   r   r   r   numpyr   	getLogger__name__r   floatndarrayMatrixr.   intrA    rB   r-   <module>rN      s!      / / / / / / / / / / / /    		8	$	$	tDK $rz"2BJ>	? F rz    H '+	( ((( C=( e_	(
 4c3h $u+-.( ( ( ( ( (rB   