o
    ZhI!                     @  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/)ZsinceZremovalmessagec                   @  s  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dDddZe
		dEdFd d!Zed"d#d$ZedGd&d'ZedGd(d)Zed*d+e
dHd.d/ZdId1d2ZedJd8d9Z	dKdLd<d=ZedMd?d@Z	dKdNdBdCZdS )O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                 K  sP   t ||d}t ||d}t||dd}t|d}	t||	dd}
| d	d|
i|S )
z Construct the chain from an LLM.)r,   promptZ	summaries)	llm_chainr-   document_variable_name)r    context)r3   reduce_documents_chainr4   r    N )r   r   r   r   )clsr,   r-   r.   r/   r0   Zllm_question_chainZllm_combine_chainZcombine_results_chainr6   r    r7   r7   \/var/www/html/lang_env/lib/python3.10/site-packages/langchain/chains/qa_with_sources/base.pyfrom_llm7   s(   
zBaseQAWithSourcesChain.from_llmstuffN
chain_typechain_type_kwargsOptional[dict]c                 K  s.   |pi }t |fd|i|}| dd|i|S )zLoad chain from chain type.r<   r    Nr7   r   )r8   r,   r<   r=   r0   Z_chain_kwargsr    r7   r7   r9   from_chain_typeU   s   	z&BaseQAWithSourcesChain.from_chain_typeTZforbid)Zarbitrary_types_allowedextra	List[str]c                 C  s   | j gS z2Expect input key.

        :meta private:
        )r#   selfr7   r7   r9   
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+   )rD   Z_output_keysr7   r7   r9   output_keysq   s   
z"BaseQAWithSourcesChain.output_keysbefore)modevaluesr   c                 C  s   d|v r| d|d< |S )z&Fix backwards compatibility in naming.Zcombine_document_chainr    )pop)r8   rJ   r7   r7   r9   validate_naming|   s   z&BaseQAWithSourcesChain.validate_namingTuple[str, str]c                 C  sV   t d|t jr%t jd|t jddd \}}t d|d  }||fS d}||fS )	zSplit sources from answer.z	SOURCES?:zSOURCES?:|QUESTION:\s)flagsN   z\nr    )research
IGNORECASEsplitstrip)rD   r&   r(   r7   r7   r9   _split_sources   s   z%BaseQAWithSourcesChain._split_sourcesinputsDict[str, Any]run_managerr   List[Document]c                C     dS !Get docs to run questioning over.Nr7   rD   rW   rY   r7   r7   r9   	_get_docs   s    z BaseQAWithSourcesChain._get_docs$Optional[CallbackManagerForChainRun]Dict[str, str]c           	      C  s   |pt  }dt| jjv }|r| j||d}n| |}| jjd|| d|}| 	|\}}| j
|| j|i}| jrB||d< |S NrY   )rY   )Zinput_documents	callbacksrF   r7   )r   get_noop_managerinspect	signaturer_   
parametersr    run	get_childrV   r'   r)   r+   	rD   rW   rY   Z_run_managerZaccepts_run_managerr$   r&   r(   resultr7   r7   r9   _call   s$   
zBaseQAWithSourcesChain._callr   c                  s   dS r\   r7   r^   r7   r7   r9   
_aget_docs   s    z!BaseQAWithSourcesChain._aget_docs)Optional[AsyncCallbackManagerForChainRun]c           	        s   |pt  }dt| jjv }|r| j||dI d H }n| |I d H }| jjd|| d|I d H }| 	|\}}| j
|| j|i}| jrL||d< |S rb   )r   rd   re   rf   rm   rg   r    Zarunri   rV   r'   r)   r+   rj   r7   r7   r9   _acall   s&   zBaseQAWithSourcesChain._acall)r,   r   r-   r   r.   r   r/   r   r0   r   r1   r   )r;   N)
r,   r   r<   r"   r=   r>   r0   r   r1   r   r1   rA   )rJ   r   r1   r   )r&   r"   r1   rM   rW   rX   rY   r   r1   rZ   )N)rW   rX   rY   r`   r1   ra   rW   rX   rY   r   r1   rZ   )rW   rX   rY   rn   r1   rX   )__name__
__module____qualname____doc____annotations__r#   r%   r'   r)   r+   classmethodr   r   r   r:   r?   r   Zmodel_configpropertyrE   rG   r   rL   rV   r   r_   rl   rm   ro   r7   r7   r7   r9   r   "   sN   
 


r   c                   @  sN   e Zd ZU dZdZded< edddZdddZdddZ	edddZ
dS )QAWithSourcesChainr   r$   r"   r%   r1   rA   c                 C  s   | j | jgS rB   )r%   r#   rC   r7   r7   r9   rE      s   zQAWithSourcesChain.input_keysrW   rX   rY   r   rZ   c                C  s   | | jS r]   rK   r%   r^   r7   r7   r9   r_      s   zQAWithSourcesChain._get_docsr   c                  s   | | jS r{   r|   r^   r7   r7   r9   rm      s   zQAWithSourcesChain._aget_docsc                 C  r[   )NZqa_with_sources_chainr7   rC   r7   r7   r9   _chain_type   s   zQAWithSourcesChain._chain_typeNrp   rq   rr   )r1   r"   )rs   rt   ru   rv   r%   rw   ry   rE   r_   rm   r}   r7   r7   r7   r9   rz      s   
 


		rz   )0rv   
__future__r   re   rQ   abcr   r   typingr   r   r   r	   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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   rz   r7   r7   r7   r9   <module>   sB    	 +	