a
    •˜ëjÞ	  ã                   @   sp   d 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 G d	d
„ d
eƒZdS )ú.Question-answering with sources over an index.é    )ÚAny)ÚAsyncCallbackManagerForChainRunÚCallbackManagerForChainRun)ÚDocument)ÚBaseRetriever)ÚField)ÚStuffDocumentsChain)ÚBaseQAWithSourcesChainc                   @   s¢   e Zd ZU dZedd�Zeed< dZe	ed< dZ
eed< ee ee d	œd
d„Zeeef eee dœdd„Zeeef eee dœdd„Zeedœdd„ƒZdS )ÚRetrievalQAWithSourcesChainr   T)ÚexcludeÚ	retrieverFÚreduce_k_below_max_tokensi/  Úmax_tokens_limit)ÚdocsÚreturnc                    sh   t |ƒ}ˆ jr\tˆ jtƒr\‡ fdd„|D ƒ}t|d |… ƒ}|ˆ jkr\|d8 }||| 8 }q<|d |… S )Nc                    s   g | ]}ˆ j j |j¡‘qS © )Úcombine_documents_chainZ	llm_chainZ_get_num_tokensZpage_content)Ú.0Údoc©Úselfr   úi/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/qa_with_sources/retrieval.pyÚ
<listcomp>#   s   ÿzJRetrievalQAWithSourcesChain._reduce_tokens_below_limit.<locals>.<listcomp>é   )Úlenr   Ú
isinstancer   r	   Úsumr   )r   r   Znum_docsÚtokensZtoken_countr   r   r   Ú_reduce_tokens_below_limit   s    þ
þ
z6RetrievalQAWithSourcesChain._reduce_tokens_below_limit)ÚinputsÚrun_managerr   c                C   s,   || j  }| jj|d| ¡ id�}|  |¡S ©NÚ	callbacks)Úconfig)Úquestion_keyr   ZinvokeÚ	get_childr   ©r   r    r!   Zquestionr   r   r   r   Ú	_get_docs.   s    

þz%RetrievalQAWithSourcesChain._get_docsc                Ã   s2   || j  }| jj|d| ¡ id�I d H }|  |¡S r"   )r%   r   Zainvoker&   r   r'   r   r   r   Ú
_aget_docs;   s    

þz&RetrievalQAWithSourcesChain._aget_docs)r   c                 C   s   dS )zReturn the chain type.Zretrieval_qa_with_sources_chainr   r   r   r   r   Ú_chain_typeH   s    z'RetrievalQAWithSourcesChain._chain_typeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ú__annotations__r   Úboolr   ÚintÚlistr   r   ÚdictÚstrr   r   r(   r   r)   Úpropertyr*   r   r   r   r   r      s   

û
ûr   N)r.   Útypingr   Zlangchain_core.callbacksr   r   Zlangchain_core.documentsr   Zlangchain_core.retrieversr   Zpydanticr   Z(langchain.chains.combine_documents.stuffr	   Z%langchain.chains.qa_with_sources.baser
   r   r   r   r   r   Ú<module>   s   