a
    agI!                     @  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                	   @  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,   promptZ	summaries)	llm_chainr-   document_variable_name)r    context)r3   reduce_documents_chainr4   r    )r   r   r   r   )clsr,   r-   r.   r/   r0   Zllm_question_chainZllm_combine_chainZcombine_results_chainr6   r     r8   s/var/www/html/cobodadashboardai.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_kwargsr0   r1   c                 K  s.   |pi }t |fd|i|}| f d|i|S )zLoad chain from chain type.r<   r    r   )r7   r,   r<   r=   r0   Z_chain_kwargsr    r8   r8   r9   from_chain_typeU   s    	z&BaseQAWithSourcesChain.from_chain_typeTZforbid)Zarbitrary_types_allowedextra	List[str]r1   c                 C  s   | j gS z2Expect input key.

        :meta private:
        )r#   selfr8   r8   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_keysr8   r8   r9   output_keysq   s    
z"BaseQAWithSourcesChain.output_keysbefore)moder   )valuesr1   c                 C  s   d|v r| d|d< |S )z&Fix backwards compatibility in naming.Zcombine_document_chainr    )pop)r7   rJ   r8   r8   r9   validate_naming|   s    z&BaseQAWithSourcesChain.validate_namingzTuple[str, str])r&   r1   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)rD   r&   r(   r8   r8   r9   _split_sources   s    z%BaseQAWithSourcesChain._split_sourcesDict[str, Any]r   List[Document]inputsrun_managerr1   c                C  s   dS !Get docs to run questioning over.Nr8   rD   rY   rZ   r8   r8   r9   	_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 NrZ   )rZ   )Zinput_documents	callbacksrF   )r   get_noop_managerinspect	signaturer^   
parametersr    run	get_childrU   r'   r)   r+   	rD   rY   rZ   Z_run_managerZaccepts_run_managerr$   r&   r(   resultr8   r8   r9   _call   s$    
zBaseQAWithSourcesChain._callr   c                  s   dS r[   r8   r]   r8   r8   r9   
_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   ra   rb   rc   rj   rd   r    Zarunrf   rU   r'   r)   r+   rg   r8   r8   r9   _acall   s$    zBaseQAWithSourcesChain._acall)r;   N)N)N)__name__
__module____qualname____doc____annotations__r#   r%   r'   r)   r+   classmethodr   r   r   r:   r>   r   Zmodel_configpropertyrE   rG   r   rL   rU   r   r^   ri   rj   rk   r8   r8   r8   r9   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%   r@   rA   c                 C  s   | j | jgS rB   )r%   r#   rC   r8   r8   r9   rE      s    zQAWithSourcesChain.input_keysrV   r   rW   rX   c                C  s   | | jS r\   rK   r%   r]   r8   r8   r9   r^      s    zQAWithSourcesChain._get_docsr   c                  s   | | jS rt   ru   r]   r8   r8   r9   rj      s    zQAWithSourcesChain._aget_docsc                 C  s   dS )NZqa_with_sources_chainr8   rC   r8   r8   r9   _chain_type   s    zQAWithSourcesChain._chain_typeN)rl   rm   rn   ro   r%   rp   rr   rE   r^   rj   rv   r8   r8   r8   r9   rs      s   

		rs   )0ro   
__future__r   rb   rP   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   rs   r8   r8   r8   r9   <module>   s@   	 +	