
    Ng$                        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dS )    )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d4d5d            Z	e
	 d4d6d            Ze
	 d4d7d            Zed8d            Z	 	 d9d:dZd;dZd<d$Zd=d'Zd>d*Zd?d-Zd@d1ZdAd3ZdS )BNeuralDBRetrieverz0Document retriever that uses ThirdAI's NeuralDB.r   thirdai_keyNr   dbforbid)extraOptional[str]returnNonec                    	 ddl m} t          j                            d           |                    | pt          j        d                     d S # 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     k/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/retrievers/thirdai_neuraldb.py_verify_thirdai_libraryz)NeuralDBRetriever._verify_thirdai_library   s    
	))))))N$$%8999{Fbi.F.FGGGGG 	 	 	K  	s   AA A,model_kwargsdictc                r    t                               |           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   r)   ndbs       r'   from_scratchzNeuralDBRetriever.from_scratch*   sQ    : 	11+>>>,,,,,,s{|s|/K/Kl/K/KLLLL    
checkpointUnion[str, Path]c                    t                               |           ddlm}  | ||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-   r0   from_checkpoint)r1   r5   r   r2   s       r'   r8   z!NeuralDBRetriever.from_checkpointL   sO    < 	11+>>>,,,,,,s{s|/K/KJ/W/WXXXXr4   valuesr   c                H    t          t          |dd                    |d<   |S )z'Validate ThirdAI environment variables.r   r   )r   r   )r1   r9   s     r'   validate_environmentsz'NeuralDBRetriever.validate_environmentso   s5     !6  !
 !
} r4   Tsources	List[Any]trainbool	fast_modekwargsc                \    |                      |          } | j        j        d|||d| dS )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.
        )r<   r>   fast_approximationNr/   )_preprocess_sourcesr   insert)selfr<   r>   r@   rA   s        r'   rE   zNeuralDBRetriever.insert{   sV      **733 	
(	
 	
 		
 	
 	
 	
 	
r4   listc                   ddl m} |s|S g }|D ]0}t          |t                    s|                    |           .|                                                    d          r)|                    |                    |                     ~|                                                    d          r)|                    |                    |                     |                                                    d          r*|                    |	                    |                     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)rF   r<   r2   preprocessed_sourcesdocs        r'   rD   z%NeuralDBRetriever._preprocess_sources   sR    	-,,,,, 	N! 	 	Cc3'' $++C000099;;''// (//====YY[[))'22 
(//>>>>YY[[))&11 (//====&P P P P   $#r4   queryrJ   document_idintc                <    | j                             ||           dS )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)rF   rT   rU   s      r'   upvotezNeuralDBRetriever.upvote   s"     	uk22222r4   query_id_pairsList[Tuple[str, int]]c                :    | j                             |           dS )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)rF   rZ   s     r'   upvote_batchzNeuralDBRetriever.upvote_batch   s      	$$^44444r4   sourcetargetc                <    | j                             ||           dS )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)rF   r_   r`   s      r'   rb   zNeuralDBRetriever.associate   s"     	&&)))))r4   
text_pairsList[Tuple[str, str]]c                :    | j                             |           dS )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)rF   rc   s     r'   rf   z!NeuralDBRetriever.associate_batch   s      	
+++++r4   run_managerr   List[Document]c                    	 d|vrd|d<    | j         j        dd|i|}d |D             S # 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
   rT   c                    g | ]K}t          |j        |j        |j        |j        |j        |j        |                    d           d          LS )   )id
upvote_idsr_   metadatascorecontext)page_contentrp   )r   textrn   ro   r_   rp   rq   rr   ).0refs     r'   
<listcomp>z=NeuralDBRetriever._get_relevant_documents.<locals>.<listcomp>   sn         !$!f&)n"%*$'L!$#&;;q>> 
 
 
  r4   z"Error while retrieving documents: Nr/   )r   search	Exception
ValueError)rF   rT   rg   rA   
referenceses         r'   _get_relevant_documentsz)NeuralDBRetriever._get_relevant_documents   s    	Nf$$"$w'>>e>v>>J  &     	N 	N 	NE!EEFFAM	Ns   (+ 
AAApathc                :    | j                             |           dS )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)rF   r~   s     r'   r   zNeuralDBRetriever.save   s     	Tr4   )N)r   r   r   r   )r   r   r)   r*   r   r   )r5   r6   r   r   r   r   )r9   r   r   r   )TT)
r<   r=   r>   r?   r@   r?   rA   r*   r   r   )r<   rG   r   rG   )rT   rJ   rU   rV   r   r   )rZ   r[   r   r   )r_   rJ   r`   rJ   r   r   )rc   rd   r   r   )rT   rJ   rg   r   rA   r   r   rh   )r~   rJ   r   r   )__name__
__module____qualname____doc____annotations__r   r   model_configstaticmethodr(   classmethodr3   r8   r   r;   rE   rD   rY   r^   rb   rf   r}   r   r/   r4   r'   r   r      s        ::BNNNN:  L     \  &*M M M M [MB  &* Y  Y  Y  Y [ YD 	 	 	 X	 	
 
 
 
 
0$ $ $ $@3 3 3 3	5 	5 	5 	5	* 	* 	* 	*, , , ,N N N N<     r4   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/   r4   r'   <module>r      s   " " " " " "     				       : : : : : : : : : : : : : : : : C C C C C C - - - - - - 3 3 3 3 3 3 V V V V V V V V V V * * * * * * * *s s s s s s s s s sr4   