
    Ng	                         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
e          ZdS ).Question-answering with sources over an index.    )AnyDictList)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)Document)BaseRetriever)Field)StuffDocumentsChain)BaseQAWithSourcesChainc                       e Zd ZU dZ ed          Zeed<   	 dZe	ed<   	 dZ
eed<   	 d	ee         d
ee         fdZdeeef         ded
ee         fdZdeeef         ded
ee         fdZed
efd            ZdS )RetrievalQAWithSourcesChainr   T)exclude	retrieverFreduce_k_below_max_tokensi/  max_tokens_limitdocsreturnc                     t          |          } j        ret           j        t                    rK fd|D             }t          |d |                   }| j        k    r|dz  }|||         z  }| j        k    |d |         S )Nc                 X    g | ]&}j         j                            |j                  'S  )combine_documents_chain	llm_chain_get_num_tokenspage_content).0docselfs     f/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/chains/qa_with_sources/retrieval.py
<listcomp>zJRetrievalQAWithSourcesChain._reduce_tokens_below_limit.<locals>.<listcomp>"   s@        ,6FFsGWXX         )lenr   
isinstancer   r   sumr   )r   r   num_docstokenstoken_counts   `    r    _reduce_tokens_below_limitz6RetrievalQAWithSourcesChain._reduce_tokens_below_limit   s    t99) 
	0j(*=/
 /
 
	0     F fYhY/00K 555Avh//  555 IXIr"   inputsrun_managerc                    || j                  }| j                            |d|                                i          }|                     |          S N	callbacks)config)question_keyr   invoke	get_childr*   r   r+   r,   questionr   s        r    	_get_docsz%RetrievalQAWithSourcesChain._get_docs-   sY     $+,~$$k;+@+@+B+BC % 
 
 ..t444r"   c                   K   || j                  }| j                            |d|                                i           d {V }|                     |          S r.   )r1   r   ainvoker3   r*   r4   s        r    
_aget_docsz&RetrievalQAWithSourcesChain._aget_docs6   s{       $+,^++k;+@+@+B+BC , 
 
 
 
 
 
 
 
 ..t444r"   c                     dS )zReturn the chain type.retrieval_qa_with_sources_chainr   )r   s    r    _chain_typez'RetrievalQAWithSourcesChain._chain_type?   s
     10r"   N)__name__
__module____qualname____doc__r   r   r
   __annotations__r   boolr   intr   r	   r*   r   strr   r   r6   r   r9   propertyr<   r   r"   r    r   r      s(        88$uT222I}222&+t+++Q c   XtH~ $x.    "538n56P5	h5 5 5 5538n56U5	h5 5 5 5 1S 1 1 1 X1 1 1r"   r   N)r@   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    <module>rM      s    4 4 " " " " " " " " " "        . - - - - - 3 3 3 3 3 3       H H H H H H H H H H H H11 11 11 11 11"8 11 11 11 11 11r"   