
    }h	                     ~    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y).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y)RetrievalQAWithSourcesChainr   T)exclude	retrieverFreduce_k_below_max_tokensi/  max_tokens_limitdocsreturnc                 d   t        |      }| j                  rt        | j                  t              rv|D cg c]1  }| j                  j
                  j                  |j                        3 }}t        |d |       }|| j                  kD  r|dz  }|||   z  }|| j                  kD  r|d | S c c}w )N   )
lenr   
isinstancecombine_documents_chainr   	llm_chain_get_num_tokenspage_contentsumr   )selfr   num_docsdoctokenstoken_counts         d/var/www/html/engine/venv/lib/python3.12/site-packages/langchain/chains/qa_with_sources/retrieval.py_reduce_tokens_below_limitz6RetrievalQAWithSourcesChain._reduce_tokens_below_limit   s    t9))j((*=/

   ,,66FFsGWGWXF  fYh/0K 5 55Avh//  5 55 IXs   6B-inputsrun_managerc                    || j                      }| j                  j                  |d|j                         i      }| j	                  |      S N	callbacks)config)question_keyr   invoke	get_childr%   r   r&   r'   questionr   s        r$   	_get_docsz%RetrievalQAWithSourcesChain._get_docs-   sS     $++,~~$$k;+@+@+BC % 
 ..t44    c                   K   || j                      }| j                  j                  |d|j                         i       d {   }| j	                  |      S 7 wr)   )r,   r   ainvoker.   r%   r/   s        r$   
_aget_docsz&RetrievalQAWithSourcesChain._aget_docs6   sa      $++,^^++k;+@+@+BC , 
 
 ..t44
s   A AAAc                      y)zReturn the chain type.retrieval_qa_with_sources_chain )r   s    r$   _chain_typez'RetrievalQAWithSourcesChain._chain_type?   s     1r2   N)__name__
__module____qualname____doc__r   r   r
   __annotations__r   boolr   intr   r	   r%   r   strr   r   r1   r   r5   propertyr9   r8   r2   r$   r   r      s    8$T2I}2&+t+Q c XtH~ $x. "538n56P5	h5538n56U5	h5 1S 1 1r2   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   r8   r2   r$   <module>rJ      s1    4 " " . 3  H H11"8 11r2   