a
    •˜ëjÐ!  ã                   @  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
 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( 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ÚOptional)Ú
deprecated)ÚAsyncCallbackManagerForChainRunÚCallbackManagerForChainRun)ÚDocument)ÚBaseLanguageModel)ÚBasePromptTemplate)Ú
ConfigDictÚmodel_validator)Úoverride)ÚReduceDocumentsChain)ÚChain)ÚBaseCombineDocumentsChain)ÚMapReduceDocumentsChain)ÚStuffDocumentsChain)ÚLLMChain©Úload_qa_with_sources_chain)ÚCOMBINE_PROMPTÚEXAMPLE_PROMPTÚQUESTION_PROMPTz0.2.13z1.0z”This class is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/docs/how_to/qa_sources/)ZsinceZ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_chainZ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)   ÚpromptZ	summaries)Ú	llm_chainr*   Údocument_variable_name)r   Úcontext)r0   Úreduce_documents_chainr1   r   )r   r   r   r   )Úclsr)   r*   r+   r,   r-   Zllm_question_chainZllm_combine_chainZcombine_results_chainr3   r   © r5   úd/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/qa_with_sources/base.pyÚfrom_llm8   s(    
ýÿýÿþzBaseQAWithSourcesChain.from_llmÚstuffNzOptional[dict])r)   Ú
chain_typeÚchain_type_kwargsr-   r.   c                 K  s.   |pi }t |fd|i|¤Ž}| f d|i|¤ŽS )zLoad chain from chain type.r9   r   r   )r4   r)   r9   r:   r-   Z_chain_kwargsr   r5   r5   r6   Úfrom_chain_typeV   s    	ÿþýz&BaseQAWithSourcesChain.from_chain_typeTZforbid)Zarbitrary_types_allowedÚextraú	list[str]©r.   c                 C  s   | j gS ©z2Expect input key.

        :meta private:
        )r    ©Úselfr5   r5   r6   Ú
input_keysl   s    z!BaseQAWithSourcesChain.input_keysc                 C  s"   | j | jg}| jrg |¢d‘}|S )z3Return output key.

        :meta private:
        Úsource_documents)r$   r&   r(   )rA   Z_output_keysr5   r5   r6   Úoutput_keyst   s    z"BaseQAWithSourcesChain.output_keysÚbefore)ÚmodeÚdict)Úvaluesr.   c                 C  s   d|v r|  d¡|d< |S )z&Fix backwards compatibility in naming.Zcombine_document_chainr   )Úpop)r4   rH   r5   r5   r6   Úvalidate_naming   s    z&BaseQAWithSourcesChain.validate_namingztuple[str, str])r#   r.   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)rA   r#   r%   r5   r5   r6   Ú_split_sources‡   s    ýüz%BaseQAWithSourcesChain._split_sourcesúdict[str, Any]r
   úlist[Document]©ÚinputsÚrun_managerr.   c                C  s   dS ©ú!Get docs to run questioning over.Nr5   ©rA   rW   rX   r5   r5   r6   Ú	_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 ©NrX   )rX   )Zinput_documentsÚ	callbacksrC   )r
   Úget_noop_managerÚinspectÚ	signaturer\   Ú
parametersr   ÚrunÚ	get_childrS   r$   r&   r(   ©	rA   rW   rX   Z_run_managerZaccepts_run_managerr!   r#   r%   Úresultr5   r5   r6   Ú_call�   s&    ÿ
þýþzBaseQAWithSourcesChain._callr	   c                Ã  s   dS rY   r5   r[   r5   r5   r6   Ú
_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 r]   )r	   r_   r`   ra   rh   rb   r   Zarunrd   rS   r$   r&   r(   re   r5   r5   r6   Ú_acallÂ   s&    ÿþýþzBaseQAWithSourcesChain._acall)r8   N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r    r"   r$   r&   r(   Úclassmethodr   r   r   r7   r;   r   Zmodel_configÚpropertyrB   rD   r   rJ   rS   r   r\   rg   rh   ri   r5   r5   r5   r6   r   #   sH   

û  üþ
 ý ýr   c                   @  sn   e Zd ZU dZdZded< eddœdd„ƒZed	d
ddœdd„ƒZ	ed	dddœdd„ƒZ
eddœdd„ƒZdS )ÚQAWithSourcesChainr   r!   r   r"   r=   r>   c                 C  s   | j | jgS r?   )r"   r    r@   r5   r5   r6   rB   ì   s    zQAWithSourcesChain.input_keysrT   r
   rU   rV   c                C  s   |  | j¡S ©rZ   ©rI   r"   r[   r5   r5   r6   r\   ô   s    zQAWithSourcesChain._get_docsr	   c                Ã  s   |  | j¡S rr   rs   r[   r5   r5   r6   rh   þ   s    zQAWithSourcesChain._aget_docsc                 C  s   dS )NZqa_with_sources_chainr5   r@   r5   r5   r6   Ú_chain_type  s    zQAWithSourcesChain._chain_typeN)rj   rk   rl   rm   r"   rn   rp   rB   r   r\   rh   rt   r5   r5   r5   r6   rq   Þ   s   

		rq   )/rm   Ú
__future__r   r`   rN   Úabcr   r   Útypingr   r   Zlangchain_core._apir   Zlangchain_core.callbacksr	   r
   Zlangchain_core.documentsr   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr   Zpydanticr   r   Ztyping_extensionsr   Zlangchain.chainsr   Zlangchain.chains.baser   Z'langchain.chains.combine_documents.baser   Z-langchain.chains.combine_documents.map_reducer   Z(langchain.chains.combine_documents.stuffr   Zlangchain.chains.llmr   Z(langchain.chains.qa_with_sources.loadingr   Z2langchain.chains.qa_with_sources.map_reduce_promptr   r   r   r   rq   r5   r5   r5   r6   Ú<module>   sB   ü	 3ü	