U
    hP                     @  s   d dl mZ d dlZd dlmZmZmZmZ d dl	Z
d dlmZ d dlmZ d dlmZ d dlmZ dd	d
dddZG dd deZdS )    )annotationsN)AnyIterableListOptional)CallbackManagerForRetrieverRun)Document)
Embeddings)BaseRetriever	List[str]r	   z
np.ndarray)contexts
embeddingsreturnc              
   C  s:   t j &}tt||j| W  5 Q R  S Q R X dS )z
    Create an index of embeddings for a list of contexts.

    Args:
        contexts: List of contexts to embed.
        embeddings: Embeddings model to use.

    Returns:
        Index of embeddings.
    N)
concurrentZfuturesZThreadPoolExecutornparraylistmapembed_query)r   r   executor r   I/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/retrievers/nanopq.pycreate_index   s    r   c                   @  s   e Zd ZU dZded< ded< ded< dZd	ed
< dZded< dZded< dZded< dZ	ded< G dd dZ
ed"ddd	dd dddZedddd dddZddddd d!ZdS )#NanoPQRetrieverz`NanoPQ retriever.r	   r   r   indexr   textsNzOptional[List[dict]]	metadatas   intkzOptional[float]relevancy_thresholdsubspace   clustersc                   @  s   e Zd ZdZdS )zNanoPQRetriever.ConfigTN)__name__
__module____qualname__Zarbitrary_types_allowedr   r   r   r   Config0   s   r'   )r   r   r   kwargsr   c                 K  s"   t ||}| f ||||d|S )N)r   r   r   r   )r   )clsr   r   r   r(   r   r   r   r   
from_texts3   s    
zNanoPQRetriever.from_textszIterable[Document])	documentsr   r(   r   c                 K  s.   t dd |D  \}}| jf |||d|S )Nc                 s  s   | ]}|j |jfV  qd S )NZpage_contentmetadata).0dr   r   r   	<genexpr>K   s     z1NanoPQRetriever.from_documents.<locals>.<genexpr>)r   r   r   )zipr*   )r)   r+   r   r(   r   r   r   r   r   from_documentsD   s      zNanoPQRetriever.from_documentsstrr   zList[Document])queryrun_managerr   c             	     s  zddl m} W n tk
r,   tdY nX t j|}z$| j jdd	 j
d}W nB tk
r   dj j
jd  j j j
jd d	}t|Y nX |j j
dd
}|j|dd}|j|d}	t|	}
 fdd|
d j D }|S )Nr   )PQzBCould not import nanopq, please install with `pip install nanopq`.T)MZKsverboseZfloat32zReceived params: training_sample={training_sample}, n_cluster={n_clusters}, subspace={subspace}, embedding_shape={embedding_shape}. Issue with the combination. Please retrace back to find the exact error   )Ztraining_sampleZ
n_clustersr!   Zembedding_shape)Zvecs)r4   )codesc                   s.   g | ]&}t  j|  jr" j| ni d qS )r,   )r   r   r   )r.   rowselfr   r   
<listcomp>s   s
   z;NanoPQRetriever._get_relevant_documents.<locals>.<listcomp>)Znanopqr6   ImportErrorr   r   r   r   r!   r#   Zfitr   ZastypeAssertionErrorformatshapeRuntimeErrorencodeZdtableZadistZargsortr   )r=   r4   r5   r6   Zquery_embedsZpqerror_messageZ
index_codedtdistsZ	sorted_ixZtop_k_resultsr   r<   r   _get_relevant_documentsP   s8    





z'NanoPQRetriever._get_relevant_documents)N)r$   r%   r&   __doc____annotations__r   r   r    r!   r#   r'   classmethodr*   r2   rH   r   r   r   r   r      s    
 r   )
__future__r   concurrent.futuresr   typingr   r   r   r   Znumpyr   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr	   Zlangchain_core.retrieversr
   r   r   r   r   r   r   <module>   s   