o
    Jjg	                     @   sx   d Z ddlmZmZ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DictList)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< 	 d	ee d
ee fddZdeeef ded
ee fddZdeeef ded
ee fddZed
efddZdS )RetrievalQAWithSourcesChainr   T)exclude	retrieverFreduce_k_below_max_tokensi/  max_tokens_limitdocsreturnc                    sp   t |} jr2t jtr2 fdd|D }t|d | }| jkr2|d8 }||| 8 }| jks#|d | S )Nc                    s   g | ]
} j j|jqS  )combine_documents_chain	llm_chain_get_num_tokenspage_content).0docselfr   b/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/chains/qa_with_sources/retrieval.py
<listcomp>"   s    zJRetrievalQAWithSourcesChain._reduce_tokens_below_limit.<locals>.<listcomp>   )lenr   
isinstancer   r   sumr   )r   r   num_docstokenstoken_countr   r   r   _reduce_tokens_below_limit   s   


z6RetrievalQAWithSourcesChain._reduce_tokens_below_limitinputsrun_managerc                C   s,   || j  }| jj|d| id}| |S N	callbacks)config)question_keyr   invoke	get_childr&   r   r'   r(   questionr   r   r   r   	_get_docs-   s
   

z%RetrievalQAWithSourcesChain._get_docsc                   s4   || j  }| jj|d| idI d H }| |S r)   )r,   r   ainvoker.   r&   r/   r   r   r   
_aget_docs6   s   

z&RetrievalQAWithSourcesChain._aget_docsc                 C   s   dS )zReturn the chain type.retrieval_qa_with_sources_chainr   r   r   r   r   _chain_type?   s   z'RetrievalQAWithSourcesChain._chain_typeN)__name__
__module____qualname____doc__r
   r   r	   __annotations__r   boolr   intr   r   r&   r   strr   r   r1   r   r3   propertyr5   r   r   r   r   r      s2   
 

	

	r   N)r9   typingr   r   r   langchain_core.callbacksr   r   langchain_core.documentsr   langchain_core.retrieversr	   pydanticr
   (langchain.chains.combine_documents.stuffr   %langchain.chains.qa_with_sources.baser   r   r   r   r   r   <module>   s    