a
    ~gI!                     @  sF  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dG dd deeZ.eddddG d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                	   @  sH  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dddd dddZe
d?ddddd dddZedddZedd d!d"Zedd d#d$Zed%d&e
d'dd(d)d*Zdd+d,d-d.Zed/d0d1d2d3d4Zd@d/d5d6d2d7d8Zed/d9d1d2d:d;ZdAd/d<d/d2d=d>ZdS )B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_documentsr   r   r   )llmdocument_promptquestion_promptcombine_promptkwargsreturnc                 K  sP   t ||d}t ||d}t||dd}t|d}	t||	dd}
| f 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/   r0   r1   r2   Zllm_question_chainZllm_combine_chainZcombine_results_chainr9   r"    r;   j/var/www/html/emsaiapi.evdpl.com/venv/lib/python3.9/site-packages/langchain/chains/qa_with_sources/base.pyfrom_llm7   s(    
zBaseQAWithSourcesChain.from_llmstuffNzOptional[dict])r.   
chain_typechain_type_kwargsr2   r3   c                 K  s.   |pi }t |fd|i|}| f d|i|S )zLoad chain from chain type.r?   r"   r   )r:   r.   r?   r@   r2   Z_chain_kwargsr"   r;   r;   r<   from_chain_typeU   s    	z&BaseQAWithSourcesChain.from_chain_typeTforbid)arbitrary_types_allowedextra	List[str]r3   c                 C  s   | j gS z2Expect input key.

        :meta private:
        )r%   selfr;   r;   r<   
input_keysi   s    z!BaseQAWithSourcesChain.input_keysc                 C  s    | j | jg}| jr|dg }|S )z3Return output key.

        :meta private:
        source_documents)r)   r+   r-   )rI   _output_keysr;   r;   r<   output_keysq   s    
z"BaseQAWithSourcesChain.output_keysbefore)moder   )valuesr3   c                 C  s   d|v r| d|d< |S )z&Fix backwards compatibility in naming.combine_document_chainr"   )pop)r:   rP   r;   r;   r<   validate_naming|   s    z&BaseQAWithSourcesChain.validate_namingzTuple[str, str])r(   r3   c                 C  sP   t d|t jrDt jd|t jddd \}}t 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)rI   r(   r*   r;   r;   r<   _split_sources   s    z%BaseQAWithSourcesChain._split_sourcesDict[str, Any]r   List[Document]inputsrun_managerr3   c                C  s   dS !Get docs to run questioning over.Nr;   rI   r`   ra   r;   r;   r<   	_get_docs   s    z BaseQAWithSourcesChain._get_docsz$Optional[CallbackManagerForChainRun]zDict[str, str]c           	      C  s   |p
t  }dt| jjv }|r2| j||d}n
| |}| jjf || d|}| 	|\}}| j
|| j|i}| jr||d< |S Nra   )ra   )input_documents	callbacksrK   )r   get_noop_managerinspect	signaturere   
parametersr"   run	get_childr\   r)   r+   r-   	rI   r`   ra   _run_manageraccepts_run_managerr&   r(   r*   resultr;   r;   r<   _call   s$    
zBaseQAWithSourcesChain._callr   c                  s   dS rb   r;   rd   r;   r;   r<   
_aget_docs   s    z!BaseQAWithSourcesChain._aget_docsz)Optional[AsyncCallbackManagerForChainRun]c           	        s   |p
t  }dt| jjv }|r8| j||dI d H }n| |I d H }| jjf || d|I d H }| 	|\}}| j
|| j|i}| jr||d< |S rf   )r   ri   rj   rk   rt   rl   r"   arunrn   r\   r)   r+   r-   ro   r;   r;   r<   _acall   s$    zBaseQAWithSourcesChain._acall)r>   N)N)N)__name__
__module____qualname____doc____annotations__r%   r'   r)   r+   r-   classmethodr   r   r   r=   rA   r   model_configpropertyrJ   rM   r   rS   r\   r   re   rs   rt   rv   r;   r;   r;   r<   r!   "   sH   

  
  r!   c                   @  sf   e Zd ZU dZdZded< eddddZd	d
ddddZd	dddddZ	eddddZ
dS )QAWithSourcesChainr   r&   r$   r'   rE   rF   c                 C  s   | j | jgS rG   )r'   r%   rH   r;   r;   r<   rJ      s    zQAWithSourcesChain.input_keysr]   r   r^   r_   c                C  s   | | jS rc   rR   r'   rd   r;   r;   r<   re      s    zQAWithSourcesChain._get_docsr   c                  s   | | jS r   r   rd   r;   r;   r<   rt      s    zQAWithSourcesChain._aget_docsc                 C  s   dS )Nqa_with_sources_chainr;   rH   r;   r;   r<   _chain_type   s    zQAWithSourcesChain._chain_typeN)rw   rx   ry   rz   r'   r{   r~   rJ   re   rt   r   r;   r;   r;   r<   r      s   

		r   )0rz   
__future__r   rj   rW   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   Z2langchain.chains.qa_with_sources.map_reduce_promptr   r   r   r!   r   r;   r;   r;   r<   <module>   s@   	 +	