
    gh !                       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
 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Optional)
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.)r-   prompt	summaries)	llm_chainr.   document_variable_name)r!   context)r6   reduce_documents_chainr7   r!    )r   r   r   r   )clsr-   r.   r/   r0   r1   llm_question_chainllm_combine_chaincombine_results_chainr9   r!   s              b/var/www/FlaskApp/flask-venv/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.rC   r!   r:   r   )r;   r-   rC   rD   r1   _chain_kwargsr!   s          r?   from_chain_typez&BaseQAWithSourcesChain.from_chain_typeU   sT     */R"<#
 #
&#
*7#
 #
 sMM+BMfMMMrA   Tforbid)arbitrary_types_allowedextra	list[str]c                    | j         gS z2Expect input key.

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

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

;S0T0TF,-rA   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)rP   r'   r)   s      r?   _split_sourcesz%BaseQAWithSourcesChain._split_sources   s~    9\62=99 	 h(&  qbOFG hug..q17799GGGwrA   inputsdict[str, Any]run_managerr   list[Document]c                   dS !Get docs to run questioning over.Nr:   rP   ri   rk   s      r?   	_get_docsz BaseQAWithSourcesChain._get_docs   s      rA   $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 Nrk   )rk   )input_documents	callbacksrS   r:   )r   get_noop_managerinspect	signaturerq   
parametersr!   run	get_childrh   r(   r*   r,   	rP   ri   rk   _run_manageraccepts_run_managerr%   r'   r)   results	            r?   _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%&rA   r
   c               
   K   dS rn   r:   rp   s      r?   
_aget_docsz!BaseQAWithSourcesChain._aget_docs   s
        rA   )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 ru   )r
   rx   ry   rz   r   r{   r!   arunr}   rh   r(   r*   r,   r~   s	            r?   _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%&rA   )r-   r   r.   r   r/   r   r0   r   r1   r   r2   r    )rB   N)
r-   r   rC   r#   rD   rE   r1   r   r2   r    r2   rL   )rX   rY   r2   r   )r'   r#   r2   r^   ri   rj   rk   r   r2   rl   )N)ri   rj   rk   rr   r2   rs   ri   rj   rk   r
   r2   rl   )ri   rj   rk   r   r2   rj   )__name__
__module____qualname____doc____annotations__r$   r&   r(   r*   r,   classmethodr   r   r   r@   rH   r   model_configpropertyrQ   rU   r   r]   rh   r   rq   r   r   r   r:   rA   r?   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      rA   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&   r2   rL   c                    | j         | j        gS rN   )r&   r$   rO   s    r?   rQ   zQAWithSourcesChain.input_keys   s     #T%677rA   ri   rj   rk   r   rl   c               6    |                     | j                  S ro   r\   r&   rp   s      r?   rq   zQAWithSourcesChain._get_docs   s     zz$-...rA   r
   c               :   K   |                     | j                  S r   r   rp   s      r?   r   zQAWithSourcesChain._aget_docs   s       zz$-...rA   c                    dS )Nqa_with_sources_chainr:   rO   s    r?   _chain_typezQAWithSourcesChain._chain_type   s    &&rA   Nr   r   r   )r2   r#   )r   r   r   r   r&   r   r   rQ   rq   r   r   r:   rA   r?   r   r      s          :9 N    8 8 8 X8/ / / // / / / ' ' ' X' ' 'rA   r   )-r   
__future__r   ry   rc   abcr   r   typingr   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:   rA   r?   <module>r      sY   5 5 " " " " " "  				 # # # # # # # #                 * * * * * *        . - - - - - < < < < < < 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 
	?	  !' !' !' !' !'/ !' !' !' !' !'rA   