
    NgI!                       d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZmZ ddl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 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l&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z-  eddd           G d dee                      Z. eddd           G d de.                      Z/dS )/Question answering with sources over documents.    )annotationsN)ABCabstractmethod)AnyDictListOptionalTuple)
deprecated)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)Document)BaseLanguageModel)BasePromptTemplate)
ConfigDictmodel_validator)ReduceDocumentsChain)Chain)BaseCombineDocumentsChain)MapReduceDocumentsChain)StuffDocumentsChain)LLMChainload_qa_with_sources_chain)COMBINE_PROMPTEXAMPLE_PROMPTQUESTION_PROMPTz0.2.13z1.0zThis class is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/docs/how_to/qa_sources/)sinceremovalmessagec                  |   e Zd ZU 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<   dZ	ded<   	 e
eeefd:d            Ze
	 	 d;d<d            Z ed d!"          Zed=d$            Zed=d%            Z ed&'          e
d>d*                        Zd?d,Zed@d2            Z	 dAdBd5ZedCd7            Z	 dAdDd9ZdS )EBaseQAWithSourcesChainz5Question answering chain with sources over documents.r   combine_documents_chainquestionstrquestion_keydocsinput_docs_keyanswer
answer_keysourcessources_answer_keyFboolreturn_source_documentsllmr   document_promptr   question_promptcombine_promptkwargsr   returnc                    t          ||          }t          ||          }t          ||d          }t          |          }	t          ||	d          }
 | dd|
i|S )	z Construct the chain from an LLM.)r0   prompt	summaries)	llm_chainr1   document_variable_name)r$   context)r9   reduce_documents_chainr:   r$    )r   r   r   r   )clsr0   r1   r2   r3   r4   llm_question_chainllm_combine_chaincombine_results_chainr<   r$   s              a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/chains/qa_with_sources/base.pyfrom_llmzBaseQAWithSourcesChain.from_llm7   s     &#oFFF$^DDD 3'+#.!
 !
 !

 "6$9"
 "
 "
 #:(#9#,#
 #
 #

 s 
 
$;

 
 	
    stuffN
chain_typechain_type_kwargsOptional[dict]c                <    |pi }t          |fd|i|} | dd|i|S )zLoad chain from chain type.rF   r$   r=   r   )r>   r0   rF   rG   r4   _chain_kwargsr$   s          rB   from_chain_typez&BaseQAWithSourcesChain.from_chain_typeU   sT     */R"<#
 #
&#
*7#
 #
 sMM+BMfMMMrD   Tforbid)arbitrary_types_allowedextra	List[str]c                    | j         gS z2Expect input key.

        :meta private:
        )r'   selfs    rB   
input_keysz!BaseQAWithSourcesChain.input_keysi   s     !""rD   c                <    | j         | j        g}| j        r|dgz   }|S )z3Return output key.

        :meta private:
        source_documents)r+   r-   r/   )rS   _output_keyss     rB   output_keysz"BaseQAWithSourcesChain.output_keysq   s2     )@A' 	?'+=*>>LrD   before)modevaluesr   c                >    d|v r|                     d          |d<   |S )z&Fix backwards compatibility in naming.combine_document_chainr$   )pop)r>   r[   s     rB   validate_namingz&BaseQAWithSourcesChain.validate_naming|   s,     $v--06

;S0T0TF,-rD   Tuple[str, str]c                   t          j        d|t           j                  rZt          j        d|t           j                  dd         \  }}t          j        d|          d                                         }nd}||fS )	zSplit sources from answer.z	SOURCES?:zSOURCES?:|QUESTION:\s)flagsN   z\nr    )research
IGNORECASEsplitstrip)rS   r*   r,   s      rB   _split_sourcesz%BaseQAWithSourcesChain._split_sources   s~    9\62=99 	 h(&  qbOFG hug..q17799GGGwrD   inputsDict[str, Any]run_managerr   List[Document]c                   dS !Get docs to run questioning over.Nr=   rS   rk   rm   s      rB   	_get_docsz BaseQAWithSourcesChain._get_docs   s      rD   $Optional[CallbackManagerForChainRun]Dict[str, str]c                   |pt          j                    }dt          j        | j                  j        v }|r|                     ||          }n|                     |          } | j        j        d||                                d|}| 	                    |          \  }}| j
        || j        |i}| j        r||d<   |S Nrm   )rm   )input_documents	callbacksrV   r=   )r   get_noop_managerinspect	signaturers   
parametersr$   run	get_childrj   r+   r-   r/   	rS   rk   rm   _run_manageraccepts_run_managerr(   r*   r,   results	            rB   _callzBaseQAWithSourcesChain._call   s    
 #S&@&Q&S&SW.t~>>II 	  	*>>&l>CCDD>>&))D1-1 
 L,B,B,D,D
 
HN
 
 --f55OV#W"
 ' 	.)-F%&rD   r   c               
   K   dS rp   r=   rr   s      rB   
_aget_docsz!BaseQAWithSourcesChain._aget_docs   s
        rD   )Optional[AsyncCallbackManagerForChainRun]c                  K   |pt          j                    }dt          j        | j                  j        v }|r|                     ||           d {V }n|                     |           d {V } | j        j        d||                                d| d {V }| 	                    |          \  }}| j
        || j        |i}| j        r||d<   |S rw   )r   rz   r{   r|   r   r}   r$   arunr   rj   r+   r-   r/   r   s	            rB   _acallzBaseQAWithSourcesChain._acall   s3     
 #X&E&V&X&XW.t??JJ 	  	1\JJJJJJJJDD00000000D8t38 
 L,B,B,D,D
 
HN
 
 
 
 
 
 
 
 --f55OV#W"
 ' 	.)-F%&rD   )r0   r   r1   r   r2   r   r3   r   r4   r   r5   r#   )rE   N)
r0   r   rF   r&   rG   rH   r4   r   r5   r#   r5   rO   )r[   r   r5   r   )r*   r&   r5   r`   rk   rl   rm   r   r5   rn   )N)rk   rl   rm   rt   r5   ru   rk   rl   rm   r   r5   rn   )rk   rl   rm   r   r5   rl   )__name__
__module____qualname____doc____annotations__r'   r)   r+   r-   r/   classmethodr   r   r   rC   rK   r   model_configpropertyrT   rX   r   r_   rj   r   rs   r   r   r   r=   rD   rB   r#   r#   "   s         @?6666,"L"""" N    J'''''$)))))& /=.=-;
 
 
 
 [
:  ",0	N N N N [N : $  L
 # # # X#    X _(###   [ $#	 	 	 	 0 0 0 ^0 =A    4 0 0 0 ^0 BF      rD   r#   c                  b    e Zd ZU dZdZded<   edd            ZddZddZ	edd            Z
dS )QAWithSourcesChainr   r(   r&   r)   r5   rO   c                    | j         | j        gS rQ   )r)   r'   rR   s    rB   rT   zQAWithSourcesChain.input_keys   s     #T%677rD   rk   rl   rm   r   rn   c               6    |                     | j                  S rq   r^   r)   rr   s      rB   rs   zQAWithSourcesChain._get_docs   s     zz$-...rD   r   c               :   K   |                     | j                  S r   r   rr   s      rB   r   zQAWithSourcesChain._aget_docs   s       zz$-...rD   c                    dS )Nqa_with_sources_chainr=   rR   s    rB   _chain_typezQAWithSourcesChain._chain_type   s    &&rD   Nr   r   r   )r5   r&   )r   r   r   r   r)   r   r   rT   rs   r   r   r=   rD   rB   r   r      s          :9 N    8 8 8 X8/ / / // / / / ' ' ' X' ' 'rD   r   )0r   
__future__r   r{   re   abcr   r   typingr   r   r	   r
   r   langchain_core._apir   langchain_core.callbacksr   r   langchain_core.documentsr   langchain_core.language_modelsr   langchain_core.promptsr   pydanticr   r   langchain.chainsr   langchain.chains.baser   'langchain.chains.combine_documents.baser   -langchain.chains.combine_documents.map_reducer   (langchain.chains.combine_documents.stuffr   langchain.chains.llmr   (langchain.chains.qa_with_sources.loadingr   2langchain.chains.qa_with_sources.map_reduce_promptr   r   r   r#   r   r=   rD   rB   <module>r      sk   5 5 " " " " " "  				 # # # # # # # # 3 3 3 3 3 3 3 3 3 3 3 3 3 3 * * * * * *        . - - - - - < < < < < < 5 5 5 5 5 5 0 0 0 0 0 0 0 0 1 1 1 1 1 1 ' ' ' ' ' ' M M M M M M Q Q Q Q Q Q H H H H H H ) ) ) ) ) ) O O O O O O          
	?	  g g g g gUC g g gT 
	?	  !' !' !' !' !'/ !' !' !' !' !'rD   