o
    Zh	                     @   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_chainZ	llm_chainZ_get_num_tokensZpage_content).0docselfr   a/var/www/html/lang_env/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   Znum_docstokensZ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   Z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   Zainvoker(   r!   r)   r   r   r   
_aget_docs6   s   

z&RetrievalQAWithSourcesChain._aget_docsc                 C   s   dS )zReturn the chain type.Z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   r+   r   r,   propertyr-   r   r   r   r   r      s2   
 

	

	r   N)r1   typingr   r   r   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    