a
    bg                     @  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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)
ConfigDict	List[str]r	   z
np.ndarray)contexts
embeddingsreturnc                 C  sF   t j (}tt||j| W  d   S 1 s80    Y  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)
concurrentfuturesThreadPoolExecutornparraylistmapembed_query)r   r   executor r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/retrievers/nanopq.pycreate_index   s    r   c                   @  s   e Zd ZU dZded< dZ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< e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   Nr   indexr   textszOptional[List[dict]]	metadatas   intkzOptional[float]relevancy_thresholdsubspace   clustersT)Zarbitrary_types_allowed)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_texts5   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>M       z1NanoPQRetriever.from_documents.<locals>.<genexpr>)r   r   r   )zipr)   )r(   r*   r   r'   r   r   r   r   r   from_documentsF   s    zNanoPQRetriever.from_documentsstrr   zList[Document])queryrun_managerr   c             	     s   zddl m} W n ty*   tdY n0 t j|}z$| j jdd	 j
d}W n@ ty   dj j
jd  j j j
jd d	}t|Y n0 |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>u   s
   z;NanoPQRetriever._get_relevant_documents.<locals>.<listcomp>)Znanopqr6   ImportErrorr   r   r   r   r$   r&   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_documentsR   s8    





z'NanoPQRetriever._get_relevant_documents)N)__name__
__module____qualname____doc____annotations__r   r   r"   r#   r$   r&   r   Zmodel_configclassmethodr)   r2   rI   r   r   r   r   r      s$   
 r   )
__future__r   concurrent.futuresr   typingr   r   r   r   numpyr   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr	   Zlangchain_core.retrieversr
   Zpydanticr   r   r   r   r   r   r   <module>   s   