a
    ~g	                     @   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< 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_chain	llm_chain_get_num_tokenspage_content).0docselfr   o/var/www/html/emsaiapi.evdpl.com/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   invoke	get_childr$   r   r%   r&   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   ainvoker,   r$   r-   r   r   r   
_aget_docs6   s
    
z&RetrievalQAWithSourcesChain._aget_docs)r   c                 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   r/   r   r1   propertyr3   r   r   r   r   r      s   

	r   N)r7   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   