
    hh                        d Z ddlZddlmZmZmZ ddl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  G d
 de          Zdeeef         deeef         fdZ G d dee          Z G d dee          ZdS )z(Tools for interacting with vectorstores.    N)AnyDictOptional)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseLanguageModel)BaseTool)VectorStore)	BaseModel
ConfigDictFieldOpenAIc                   r    e Zd ZU dZ ed          Zeed<    ed           Ze	ed<    e
d          Zd	S )
BaseVectorStoreToolz,Base class for tools that use a VectorStore.T)excludevectorstorec                  "    t          d          S )Nr   )temperaturer        g/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/tools/vectorstore/tool.py<lambda>zBaseVectorStoreTool.<lambda>   s    6a;P;P;P r   )default_factoryllm)arbitrary_types_allowedN)__name__
__module____qualname____doc__r   r   r
   __annotations__r   r   r   model_configr   r   r   r   r      sn         66$uT222K222"U3P3PQQQC	QQQ: $  LLLr   r   valuesreturnc                 P    | d                              | d                   | d<   | S )Ntemplatename)r'   descriptionformat)r#   s    r   !_create_description_from_templater+      s+    ":.556&>5JJF=Mr   c                   ~    e Zd ZdZedededefd            Z	 ddedee         defd	Z		 ddedee
         defd
ZdS )VectorStoreQAToolzETool for the VectorDBQA chain. To be initialized with name and chain.r'   r(   r$   c                 4    d}|                     | |          S )NzUseful for when you need to answer questions about {name}. Whenever you need information about {description} you should ALWAYS use this. Input should be a fully formed question.r'   r(   r)   r'   r(   r&   s      r   get_descriptionz!VectorStoreQATool.get_description%   s$    7 	 DkBBBr   Nqueryrun_managerc                     ddl m} |                    | j        | j                                                  }|                    |j        |id|r|                                ndi          |j	                 S )Use the tool.r   RetrievalQA	retriever	callbacksNconfig)
"langchain.chains.retrieval_qa.baser7   from_chain_typer   r   as_retrieverinvoke	input_key	get_child
output_keyselfr2   r3   r7   chains        r   _runzVectorStoreQATool._run/   s     	CBBBBB++H 0 = = ? ? , 
 
 ||_e$K!Q!6!6!8!8!8TR  
 
 
 	r   c                   K   ddl m} |                    | j        | j                                                  }|                    |j        |id|r|                                ndi           d{V |j	                 S )Use the tool asynchronously.r   r6   r8   r:   Nr;   )
r=   r7   r>   r   r   r?   ainvokerA   rB   rC   rD   s        r   _arunzVectorStoreQATool._arun?   s       	CBBBBB++H 0 = = ? ? , 
 
 --%(#%U[%:%:%<%<%<QUV           
 	r   Nr   r   r   r    staticmethodstrr1   r   r   rG   r   rK   r   r   r   r-   r-   "   s        OOCc C C C C C \C <@  78 
	   & AE  <= 
	     r   r-   c                   ~    e Zd ZdZedededefd            Z	 ddedee         defd	Z		 ddedee
         defd
ZdS )VectorStoreQAWithSourcesToolz)Tool for the VectorDBQAWithSources chain.r'   r(   r$   c                 4    d}|                     | |          S )Nad  Useful for when you need to answer questions about {name} and the sources used to construct the answer. Whenever you need information about {description} you should ALWAYS use this.  Input should be a fully formed question. Output is a json serialized dictionary with keys `answer` and `sources`. Only use this tool if the user explicitly asks for sources.r/   r)   r0   s      r   r1   z,VectorStoreQAWithSourcesTool.get_descriptionU   s%    J 	 DkBBBr   Nr2   r3   c           	         ddl m} |                    | j        | j                                                  }t          j        |                    |j	        |idd|r|
                                ndi                    S )r5   r   RetrievalQAWithSourcesChainr8   Tr:   Nreturn_only_outputsr<   )*langchain.chains.qa_with_sources.retrievalrU   r>   r   r   r?   jsondumpsr@   question_keyrB   rE   r2   r3   rU   rF   s        r   rG   z!VectorStoreQAWithSourcesTool._runb   s    	
 	
 	
 	
 	
 	
 ,;;H 0 = = ? ? < 
 
 zLL#U+$(#%U[%:%:%<%<%<QUV   
 
 	
r   c           	        K   ddl m} |                    | j        | j                                                  }t          j        |                    |j	        |idd|r|
                                ndi           d{V           S )rI   r   rT   r8   Tr:   NrV   )rX   rU   r>   r   r   r?   rY   rZ   rJ   r[   rB   r\   s        r   rK   z"VectorStoreQAWithSourcesTool._arunx   s      	
 	
 	
 	
 	
 	
 ,;;H 0 = = ? ? < 
 
 z--#U+$(#%U[%:%:%<%<%<QUV          
 
 	
r   rL   rM   r   r   r   rQ   rQ   R   s        33
Cc 
C 
C 
C 
C 
C \
C <@
 

 78
 
	
 
 
 
2 AE
 

 <=
 
	
 
 
 
 
 
r   rQ   )r    rY   typingr   r   r   langchain_core.callbacksr   r   langchain_core.language_modelsr   langchain_core.toolsr	   langchain_core.vectorstoresr
   pydanticr   r   r   langchain_community.llms.openair   r   rO   r+   r-   rQ   r   r   r   <module>re      s   . .  & & & & & & & & & &        = < < < < < ) ) ) ) ) ) 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2    )   d38n c3h    
- - - - -+X - - -`9
 9
 9
 9
 9
#6 9
 9
 9
 9
 9
r   