
    7|h$                        d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZ d d	lmZmZ  G d
 de      Zy)    )annotationsN)Path)AnyDictListOptionalTupleUnion)CallbackManagerForRetrieverRun)Document)BaseRetriever)convert_to_secret_strget_from_dict_or_envpre_init)
ConfigDict	SecretStrc                     e Zd ZU dZded<   	 dZded<   	  ed      Zeddd	       Z	e
	 d	 	 	 	 	 dd
       Ze
	 d	 	 	 	 	 dd       Zedd       Z	 	 d	 	 	 	 	 	 	 	 	 ddZddZddZddZddZd dZ	 	 	 	 	 	 	 	 d!dZd"dZy)#NeuralDBRetrieverz0Document retriever that uses ThirdAI's NeuralDB.r   thirdai_keyNr   dbforbid)extrac                    	 ddl m} t        j                  j	                  d       |j                  | xs t        j                  d             y # t        $ r t        d      w xY w)Nr   )	licensingzthirdai.neural_dbTHIRDAI_KEYz{Could not import thirdai python package and neuraldb dependencies. Please install it with `pip install thirdai[neural_db]`.)	thirdair   	importlibutil	find_specactivateosgetenvImportError)r   r   s     n/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/retrievers/thirdai_neuraldb.py_verify_thirdai_libraryz)NeuralDBRetriever._verify_thirdai_library   s[    
	)NN$$%89{Fbii.FG 	K 	s   AA A%c                l    t         j                  |       ddlm}  | | |j                  di |      S )a  
        Create a NeuralDBRetriever from scratch.

        To use, set the ``THIRDAI_KEY`` environment variable with your ThirdAI
        API key, or pass ``thirdai_key`` as a named parameter.

        Example:
            .. code-block:: python

                from langchain_community.retrievers import NeuralDBRetriever

                retriever = NeuralDBRetriever.from_scratch(
                    thirdai_key="your-thirdai-key",
                )

                retriever.insert([
                    "/path/to/doc.pdf",
                    "/path/to/doc.docx",
                    "/path/to/doc.csv",
                ])

                documents = retriever.invoke("AI-driven music therapy")
        r   	neural_dbr   r    )r   r%   r   r(   NeuralDB)clsr   model_kwargsndbs       r$   from_scratchzNeuralDBRetriever.from_scratch*   s0    : 	11+>,{|s||/Kl/KLL    c                ~    t         j                  |       ddlm}  | ||j                  j                  |            S )a!  
        Create a NeuralDBRetriever with a base model from a saved checkpoint

        To use, set the ``THIRDAI_KEY`` environment variable with your ThirdAI
        API key, or pass ``thirdai_key`` as a named parameter.

        Example:
            .. code-block:: python

                from langchain_community.retrievers import NeuralDBRetriever

                retriever = NeuralDBRetriever.from_checkpoint(
                    checkpoint="/path/to/checkpoint.ndb",
                    thirdai_key="your-thirdai-key",
                )

                retriever.insert([
                    "/path/to/doc.pdf",
                    "/path/to/doc.docx",
                    "/path/to/doc.csv",
                ])

                documents = retriever.invoke("AI-driven music therapy")
        r   r'   r)   )r   r%   r   r(   r+   from_checkpoint)r,   
checkpointr   r.   s       r$   r2   z!NeuralDBRetriever.from_checkpointL   s1    < 	11+>,{s||/K/KJ/WXXr0   c                8    t        t        |dd            |d<   |S )z'Validate ThirdAI environment variables.r   r   )r   r   )r,   valuess     r$   validate_environmentsz'NeuralDBRetriever.validate_environmentso   s+     !6 !
} r0   c                f    | j                  |      } | j                  j                  d|||d| y)as  Inserts files / document sources into the retriever.

        Args:
            train: When True this means that the underlying model in the
            NeuralDB will undergo unsupervised pretraining on the inserted files.
            Defaults to True.
            fast_mode: Much faster insertion with a slight drop in performance.
            Defaults to True.
        )sourcestrainfast_approximationNr*   )_preprocess_sourcesr   insert)selfr8   r9   	fast_modekwargss        r$   r<   zNeuralDBRetriever.insert{   s>      **73 	
(	
 		
r0   c                   ddl m} |s|S g }|D ]  }t        |t              s|j	                  |       %|j                         j                  d      r!|j	                  |j                  |             e|j                         j                  d      r!|j	                  |j                  |             |j                         j                  d      r!|j	                  |j                  |             t        d| d       |S )zChecks if the provided sources are string paths. If they are, convert
        to NeuralDB document objects.

        Args:
            sources: list of either string paths to PDF, DOCX or CSV files, or
            NeuralDB document objects.
        r   r'   z.pdfz.docxz.csvzCould not automatically load z. Only files with .pdf, .docx, or .csv extensions can be loaded automatically. For other formats, please use the appropriate document object from the ThirdAI library.)r   r(   
isinstancestrappendlowerendswithPDFDOCXCSVRuntimeError)r=   r8   r.   preprocessed_sourcesdocs        r$   r;   z%NeuralDBRetriever._preprocess_sources   s     	-N! 	Cc3'$++C099;''/(//=YY[))'2(//>YY[))&1(//=&7u =P P 	" $#r0   c                <    | j                   j                  ||       y)a!  The retriever upweights the score of a document for a specific query.
        This is useful for fine-tuning the retriever to user behavior.

        Args:
            query: text to associate with `document_id`
            document_id: id of the document to associate query with.
        N)r   text_to_result)r=   querydocument_ids      r$   upvotezNeuralDBRetriever.upvote   s     	uk2r0   c                :    | j                   j                  |       y)a  Given a batch of (query, document id) pairs, the retriever upweights
        the scores of the document for the corresponding queries.
        This is useful for fine-tuning the retriever to user behavior.

        Args:
            query_id_pairs: list of (query, document id) pairs. For each pair in
            this list, the model will upweight the document id for the query.
        N)r   text_to_result_batch)r=   query_id_pairss     r$   upvote_batchzNeuralDBRetriever.upvote_batch   s     	$$^4r0   c                <    | j                   j                  ||       y)a=  The retriever associates a source phrase with a target phrase.
        When the retriever sees the source phrase, it will also consider results
        that are relevant to the target phrase.

        Args:
            source: text to associate to `target`.
            target: text to associate `source` to.
        N)r   	associate)r=   sourcetargets      r$   rV   zNeuralDBRetriever.associate   s     	&&)r0   c                :    | j                   j                  |       y)a.  Given a batch of (source, target) pairs, the retriever associates
        each source phrase with the corresponding target phrase.

        Args:
            text_pairs: list of (source, target) text pairs. For each pair in
            this list, the source will be associated with the target.
        N)r   associate_batch)r=   
text_pairss     r$   rZ   z!NeuralDBRetriever.associate_batch   s     	
+r0   c                v   	 d|vrd|d<    | j                   j                  d	d|i|}|D cg c]a  }t        |j                  |j                  |j
                  |j                  |j                  |j                  |j                  d      d      c c}S c c}w # t        $ r}t        d|       |d}~ww xY w)
zRetrieve {top_k} contexts with your retriever for a given query

        Args:
            query: Query to submit to the model
            top_k: The max number of context results to retrieve. Defaults to 10.
        top_k
   rN      )id
upvote_idsrW   metadatascorecontext)page_contentrb   z"Error while retrieving documents: Nr*   )r   searchr   textr`   ra   rW   rb   rc   rd   	Exception
ValueError)r=   rN   run_managerr?   
referencesrefes          r$   _get_relevant_documentsz)NeuralDBRetriever._get_relevant_documents   s    	Nf$"$w'>e>v>J &  !$!ff&)nn"%**$'LL!$#&;;q>
    	NA!EFAM	Ns)   +B A&BB B 	B8$B33B8c                :    | j                   j                  |       y)zSaves a NeuralDB instance to disk. Can be loaded into memory by
        calling NeuralDB.from_checkpoint(path)

        Args:
            path: path on disk to save the NeuralDB instance to.
        N)r   save)r=   paths     r$   rp   zNeuralDBRetriever.save   s     	Tr0   )N)r   Optional[str]returnNone)r   rr   r-   dictrs   r   )r3   zUnion[str, Path]r   rr   rs   r   )r5   r   rs   r   )TT)
r8   z	List[Any]r9   boolr>   rv   r?   ru   rs   rt   )r8   listrs   rw   )rN   rB   rO   intrs   rt   )rS   zList[Tuple[str, int]]rs   rt   )rW   rB   rX   rB   rs   rt   )r[   zList[Tuple[str, str]]rs   rt   )rN   rB   rj   r   r?   r   rs   zList[Document])rq   rB   rs   rt   )__name__
__module____qualname____doc____annotations__r   r   model_configstaticmethodr%   classmethodr/   r2   r   r6   r<   r;   rP   rT   rV   rZ   rn   rp   r*   r0   r$   r   r      sK   :BNL    &*M"M M 
	M MB  &* Y$ Y # Y 
	 Y  YD 	 	 	

 
 	

 
 

0$@3	5	*,NN'ENQTN	N<r0   r   )
__future__r   r   r!   pathlibr   typingr   r   r   r   r	   r
   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.retrieversr   langchain_core.utilsr   r   r   pydanticr   r   r   r*   r0   r$   <module>r      s7    "  	  : : C - 3 V V *s sr0   