
    h!                    b   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,y)/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&d       Z	 d'	 	 	 	 	 d(dZe	 	 	 	 	 	 d)d       Z	 d'	 	 	 	 	 d*dZy)+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_documentsc                    t        ||      }t        ||      }t        ||d      }t        |      }	t        ||	d      }
 | dd|
i|S )	z Construct the chain from an LLM.)llmprompt	summaries)	llm_chaindocument_promptdocument_variable_name)r!   context)r1   reduce_documents_chainr3   r!    )r   r   r   r   )clsr.   r2   question_promptcombine_promptkwargsllm_question_chainllm_combine_chaincombine_results_chainr5   r!   s              c/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain/chains/qa_with_sources/base.pyfrom_llmzBaseQAWithSourcesChain.from_llm7   sw     &#oF$^D 3'+#.!

 "6$9"
 #:(#9#,#

  
$;

 	
    Nc                >    |xs i }t        |fd|i|} | dd|i|S )zLoad chain from chain type.
chain_typer!   r6   r   )r7   r.   rB   chain_type_kwargsr:   _chain_kwargsr!   s          r>   from_chain_typez&BaseQAWithSourcesChain.from_chain_typeU   sD     */R"<#
!#
 #

 M+BMfMMr@   Tforbid)arbitrary_types_allowedextrac                    | j                   gS z2Expect input key.

        :meta private:
        )r$   selfs    r>   
input_keysz!BaseQAWithSourcesChain.input_keysk   s     !!""r@   c                Z    | j                   | j                  g}| j                  rg |d}|S )z3Return output key.

        :meta private:
        source_documents)r(   r*   r,   )rL   _output_keyss     r>   output_keysz"BaseQAWithSourcesChain.output_keyss   s7     )@)@A''>\>+=>Lr@   before)modec                6    d|v r|j                  d      |d<   |S )z&Fix backwards compatibility in naming.combine_document_chainr!   )pop)r7   valuess     r>   validate_namingz&BaseQAWithSourcesChain.validate_naming~   s&     $v-06

;S0TF,-r@   c                   t        j                  d|t         j                        rWt        j                  d|t         j                        dd \  }}t        j                  d|      d   j	                         }||fS d}||fS )	zSplit sources from answer.z	SOURCES?:zSOURCES?:|QUESTION:\s)flagsN   z\nr    )research
IGNORECASEsplitstrip)rL   r'   r)   s      r>   _split_sourcesz%BaseQAWithSourcesChain._split_sources   s~    99\62==9 hh(mm q	OFG
 hhug.q1779G w Gwr@   c                    y!Get docs to run questioning over.Nr6   rL   inputsrun_managers      r>   	_get_docsz BaseQAWithSourcesChain._get_docs   s    r@   c                   |xs t        j                         }dt        j                  | j                        j
                  v }|r| j	                  ||      }n| j	                  |      } | j                  j                  d||j                         d|}| j                  |      \  }}| j                  || j                  |i}| j                  r||d<   |S Nrh   )rh   )input_documents	callbacksrO   r6   )r   get_noop_managerinspect	signatureri   
parametersr!   run	get_childrb   r(   r*   r,   	rL   rg   rh   _run_manageraccepts_run_managerr%   r'   r)   results	            r>   _callzBaseQAWithSourcesChain._call   s    
 #S&@&Q&Q&SW..t~~>III 	 >>&l>CD>>&)D1--11 
 ",,.
 

 --f5OOV##W"
 '')-F%&r@   c                  K   ywrd   r6   rf   s      r>   
_aget_docsz!BaseQAWithSourcesChain._aget_docs   s     s   c                  K   |xs t        j                         }dt        j                  | j                        j
                  v }|r| j	                  ||       d {   }n| j	                  |       d {   } | j                  j                  d||j                         d| d {   }| j                  |      \  }}| j                  || j                  |i}| j                  r||d<   |S 7 7 |7 Iwrk   )r
   rn   ro   rp   rz   rq   r!   arunrs   rb   r(   r*   r,   rt   s	            r>   _acallzBaseQAWithSourcesChain._acall   s     
 #X&E&V&V&XW..t?JJJ 	 \JJD00D8t3388 
 ",,.
 
 

 --f5OOV##W"
 '')-F%& K0
s7   AC9C3C98C594C9-C7.AC95C97C9)r.   r   r2   r   r8   r   r9   r   r:   r   returnr    )stuffN)
r.   r   rB   r#   rC   zOptional[dict]r:   r   r~   r    r~   z	list[str])rW   dictr~   r   )r'   r#   r~   ztuple[str, str]rg   dict[str, Any]rh   r   r~   list[Document])N)rg   r   rh   z$Optional[CallbackManagerForChainRun]r~   zdict[str, str]rg   r   rh   r
   r~   r   )rg   r   rh   z)Optional[AsyncCallbackManagerForChainRun]r~   r   )__name__
__module____qualname____doc____annotations__r$   r&   r(   r*   r,   classmethodr   r   r   r?   rE   r   model_configpropertyrM   rQ   r   rX   rb   r   ri   rx   rz   r}   r6   r@   r>   r    r    "   s    @66,"L#" NC J''$)T)& /=.=-;

 ,
 ,	

 +
 
 
 
 
:  ",0	NN N *	N
 N 
 N N   $L
 # #   (#  $ 00 0	0
 
0 0 =A : 
	8 00 5	0
 
0 0 BF ? 
	r@   r    c                  l    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
y	)QAWithSourcesChainr   r%   r#   r&   c                2    | j                   | j                  gS rJ   )r&   r$   rK   s    r>   rM   zQAWithSourcesChain.input_keys   s     ##T%6%677r@   c               8    |j                  | j                        S re   rV   r&   rf   s      r>   ri   zQAWithSourcesChain._get_docs   s     zz$--..r@   c               @   K   |j                  | j                        S wr   r   rf   s      r>   rz   zQAWithSourcesChain._aget_docs   s      zz$--..s   c                     y)Nqa_with_sources_chainr6   rK   s    r>   _chain_typezQAWithSourcesChain._chain_type  s    &r@   Nr   r   r   )r~   r#   )r   r   r   r   r&   r   r   rM   ri   rz   r   r6   r@   r>   r   r      sy     : NC 8 8// 0	/
 
/// 5	/
 
/ ' 'r@   r   )-r   
__future__r   ro   r]   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   r6   r@   r>   <module>r      s    5 "  	 #   * . < 5 0 1 ' M Q H ) O  
	?	oUC ood 
	?	!'/ !'!'r@   