o
    JjgS!                     @  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/v0.2/docs/how_to/qa_sources/)since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.   prompt	summaries)	llm_chainr/   document_variable_name)r"   context)r6   reduce_documents_chainr7   r"   N )r   r   r   r   )clsr.   r/   r0   r1   r2   llm_question_chainllm_combine_chaincombine_results_chainr9   r"   r:   r:   ]/var/www/html/zoom/venv/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.rB   r"   Nr:   r   )r;   r.   rB   rC   r2   _chain_kwargsr"   r:   r:   r?   from_chain_typeU   s   	z&BaseQAWithSourcesChain.from_chain_typeTforbid)arbitrary_types_allowedextra	List[str]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-   )rM   _output_keysr:   r:   r?   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.combine_document_chainr"   )pop)r;   rT   r:   r:   r?   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)rM   r(   r*   r:   r:   r?   _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.Nr:   rM   rb   rd   r:   r:   r?   	_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 Nrd   )rd   )input_documents	callbacksrO   r:   )r   get_noop_managerinspect	signaturerj   
parametersr"   run	get_childra   r)   r+   r-   	rM   rb   rd   _run_manageraccepts_run_managerr&   r(   r*   resultr:   r:   r?   _call   s$   
zBaseQAWithSourcesChain._callr   c                  s   dS rg   r:   ri   r:   r:   r?   
_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 rm   )r   rp   rq   rr   r{   rs   r"   arunru   ra   r)   r+   r-   rv   r:   r:   r?   _acall   s&   zBaseQAWithSourcesChain._acall)r.   r   r/   r   r0   r   r1   r   r2   r   r3   r!   )rA   N)
r.   r   rB   r$   rC   rD   r2   r   r3   r!   r3   rJ   )rT   r   r3   r   )r(   r$   r3   rX   rb   rc   rd   r   r3   re   )N)rb   rc   rd   rk   r3   rl   rb   rc   rd   r   r3   re   )rb   rc   rd   r|   r3   rc   )__name__
__module____qualname____doc____annotations__r%   r'   r)   r+   r-   classmethodr   r   r   r@   rF   r   model_configpropertyrN   rQ   r   rW   ra   r   rj   rz   r{   r~   r:   r:   r:   r?   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'   r3   rJ   c                 C  s   | j | jgS rK   )r'   r%   rL   r:   r:   r?   rN      s   zQAWithSourcesChain.input_keysrb   rc   rd   r   re   c                C  s   | | jS rh   rV   r'   ri   r:   r:   r?   rj      s   zQAWithSourcesChain._get_docsr   c                  s   | | jS r   r   ri   r:   r:   r?   r{      s   zQAWithSourcesChain._aget_docsc                 C  rf   )Nqa_with_sources_chainr:   rL   r:   r:   r?   _chain_type   s   zQAWithSourcesChain._chain_typeNr   r   r   )r3   r$   )r   r   r   r   r'   r   r   rN   rj   r{   r   r:   r:   r:   r?   r      s   
 


		r   )0r   
__future__r   rq   r\   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   2langchain.chains.qa_with_sources.map_reduce_promptr   r   r   r!   r   r:   r:   r:   r?   <module>   sB    	 +	