a
    ag	                     @   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_chainZ	llm_chainZ_get_num_tokensZpage_content).0docselfr   x/var/www/html/cobodadashboardai.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   Z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   Zainvoker(   r!   r)   r   r   r   
_aget_docs6   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_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      s   

	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   