
    Ngb/                        d Z ddlmZ ddlmZmZmZmZ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 dd	lmZmZmZ dd
lmZ ddlmZ ddlmZ  eddd           G d de                      ZdS )zOCombining documents by mapping a chain over them first, then combining results.    )annotations)AnyDictListOptionalTupleType)
deprecated)	Callbacks)Document)RunnableConfig)create_model)	BaseModel
ConfigDictmodel_validator)BaseCombineDocumentsChain)ReduceDocumentsChain)LLMChainz0.3.1z1.0zThis class is deprecated. Please see the migration guide here for a recommended replacement: https://python.langchain.com/docs/versions/migrating_chains/map_reduce_chain/)sinceremovalmessagec                      e Zd ZU dZded<   	 ded<   	 ded<   	 dZd	ed
<   	 	 d+d, fdZed- fd            Z e	dd          Z
 ed          ed.d                        Z ed          ed.d                        Z ed          ed.d                        Zed/d            Zed/d            Z	 	 d0d1d(Z	 	 d0d1d)Zed2d*            Z xZS )3MapReduceDocumentsChaina-  Combining documents by mapping a chain over them, then combining results.

    We first call `llm_chain` on each document individually, passing in the
    `page_content` and any other kwargs. This is the `map` step.

    We then process the results of that `map` step in a `reduce` step. This should
    likely be a ReduceDocumentsChain.

    Example:
        .. code-block:: python

            from langchain.chains import (
                StuffDocumentsChain,
                LLMChain,
                ReduceDocumentsChain,
                MapReduceDocumentsChain,
            )
            from langchain_core.prompts import PromptTemplate
            from langchain_community.llms import OpenAI

            # This controls how each document will be formatted. Specifically,
            # it will be passed to `format_document` - see that function for more
            # details.
            document_prompt = PromptTemplate(
                input_variables=["page_content"],
                 template="{page_content}"
            )
            document_variable_name = "context"
            llm = OpenAI()
            # The prompt here should take as an input variable the
            # `document_variable_name`
            prompt = PromptTemplate.from_template(
                "Summarize this content: {context}"
            )
            llm_chain = LLMChain(llm=llm, prompt=prompt)
            # We now define how to combine these summaries
            reduce_prompt = PromptTemplate.from_template(
                "Combine these summaries: {context}"
            )
            reduce_llm_chain = LLMChain(llm=llm, prompt=reduce_prompt)
            combine_documents_chain = StuffDocumentsChain(
                llm_chain=reduce_llm_chain,
                document_prompt=document_prompt,
                document_variable_name=document_variable_name
            )
            reduce_documents_chain = ReduceDocumentsChain(
                combine_documents_chain=combine_documents_chain,
            )
            chain = MapReduceDocumentsChain(
                llm_chain=llm_chain,
                reduce_documents_chain=reduce_documents_chain,
            )
            # If we wanted to, we could also pass in collapse_documents_chain
            # which is specifically aimed at collapsing documents BEFORE
            # the final call.
            prompt = PromptTemplate.from_template(
                "Collapse this content: {context}"
            )
            llm_chain = LLMChain(llm=llm, prompt=prompt)
            collapse_documents_chain = StuffDocumentsChain(
                llm_chain=llm_chain,
                document_prompt=document_prompt,
                document_variable_name=document_variable_name
            )
            reduce_documents_chain = ReduceDocumentsChain(
                combine_documents_chain=combine_documents_chain,
                collapse_documents_chain=collapse_documents_chain,
            )
            chain = MapReduceDocumentsChain(
                llm_chain=llm_chain,
                reduce_documents_chain=reduce_documents_chain,
            )
    r   	llm_chainr   reduce_documents_chainstrdocument_variable_nameFboolreturn_intermediate_stepsNconfigOptional[RunnableConfig]returnType[BaseModel]c           	         | j         r/t          	 di | j        t          d fdt          t                   d fiS t                                          |          S )NMapReduceDocumentsOutputintermediate_steps)r%   )r   r   
output_keyr   r   superget_output_schema)selfr    	__class__s     i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/chains/combine_documents/map_reduce.pyr)   z)MapReduceDocumentsChain.get_output_schemar   si     ) 	*  Oc4[(49d*;   ww((000    	List[str]c                H    t                      j        }| j        r|dgz   }|S )z2Expect input key.

        :meta private:
        r&   )r(   output_keysr   )r*   _output_keysr+   s     r,   r0   z#MapReduceDocumentsChain.output_keys   s1     ww*) 	A'+?*@@Lr-   Tforbid)arbitrary_types_allowedextrabefore)modevaluesr   r   c                    d|v rPd|v rt          d          |d         }|                    d          }t          ||          }||d<   |d= d|v r|d= |S )For backwards compatibility.combine_document_chainr   zBoth `reduce_documents_chain` and `combine_document_chain` cannot be provided at the same time. `combine_document_chain` is deprecated, please only provide `reduce_documents_chain`collapse_document_chain)combine_documents_chaincollapse_documents_chain)
ValueErrorgetr   )clsr7   combine_chaincollapse_chainreduce_chains        r,   get_reduce_chainz(MapReduceDocumentsChain.get_reduce_chain   s     $v--'611 R  
 ##;<M#ZZ(ABBN/(5)7  L 0<F+,/0(F2245r-   c                *    d|v r|d         |d<   |d= |S )r9   return_map_stepsr    )r@   r7   s     r,   get_return_intermediate_stepsz5MapReduceDocumentsChain.get_return_intermediate_steps   s.     ''289K2LF./)*r-   c                    d|vrt          d          |d         j        j        }d|vr.t          |          dk    r|d         |d<   n4t          d          |d         |vrt          d|d          d|           |S )	z4Get default document variable name, if not provided.r   zllm_chain must be providedr      r   zWdocument_variable_name must be provided if there are multiple llm_chain input_variableszdocument_variable_name z- was not found in llm_chain input_variables: )r>   promptinput_variableslen)r@   r7   llm_chain_variabless      r,   "get_default_document_variable_namez:MapReduceDocumentsChain.get_default_document_variable_name   s     f$$9:::$[18H#611&''1,,3Fq3I/00 9  
 ./7JJJ Uf5M.N U U?RU U   r-   c                    t          | j        t                    r$| j        j        r| j        j        S | j        j        S t          dt          | j                   d          z Kept for backward compatibility.z$`reduce_documents_chain` is of type z$ so it does not have this attribute.)
isinstancer   r   r=   r<   r>   typer*   s    r,   r;   z/MapReduceDocumentsChain.collapse_document_chain   su     d13GHH 
	*C K2KK2JJ#344# # #  r-   c                    t          | j        t                    r| j        j        S t	          dt          | j                   d          rQ   )rR   r   r   r<   r>   rS   rT   s    r,   r:   z.MapReduceDocumentsChain.combine_document_chain   sZ     d13GHH 	.FF#344# # #  r-   docsList[Document]	token_maxOptional[int]	callbacksr   kwargsTuple[str, dict]c                   
  j                              fdD             |          } j         j        

fdt          |          D             }  j        j        |f||d\  }} j        r
fd|D             }	|	|d<   ||fS )Combine documents in a map reduce manner.

        Combine by mapping first chain over all documents, then reducing the results.
        This reducing can be done recursively if needed (if there are many documents).
        c                0    g | ]}j         |j        iS rG   r   page_content.0dr[   r*   s     r,   
<listcomp>z8MapReduceDocumentsChain.combine_docs.<locals>.<listcomp>   s(    SSSd)1>DVDSSSr-   rZ   c                Z    g | ]'\  }}t          |         |         j                   (S )ra   metadatar   ri   rc   irrV   question_result_keys      r,   re   z8MapReduceDocumentsChain.combine_docs.<locals>.<listcomp>   I     
 
 
 1 !$7"847CSTTT
 
 
r-   rX   rZ   c                     g | ]
}|         S rG   rG   rc   rm   rn   s     r,   re   z8MapReduceDocumentsChain.combine_docs.<locals>.<listcomp>       !N!N!NQ!$7"8!N!N!Nr-   r&   )r   applyr'   	enumerater   combine_docsr   r*   rV   rX   rZ   r[   map_resultsresult_docsresultextra_return_dictr&   rn   s   ``  `     @r,   rv   z$MapReduceDocumentsChain.combine_docs   s     n**SSSSSdSSS + 
 

 #n7
 
 
 
 
 "+..
 
 

 %MD$?$L%
#,	%
 %
EK%
 %
!! ) 	I!N!N!N!N+!N!N!N6H23(((r-   c                6   
K    j                              fdD             |           d{V } j         j        

fdt          |          D             }  j        j        |f||d d{V \  }} j        r
fd|D             }	|	|d<   ||fS )r^   c                4    g | ]}i j         |j        iS rG   r`   rb   s     r,   re   z9MapReduceDocumentsChain.acombine_docs.<locals>.<listcomp>  s.    WWWQH,an=HHWWWr-   rf   Nc                Z    g | ]'\  }}t          |         |         j                   (S rh   rj   rk   s      r,   re   z9MapReduceDocumentsChain.acombine_docs.<locals>.<listcomp>  ro   r-   rp   c                     g | ]
}|         S rG   rG   rr   s     r,   re   z9MapReduceDocumentsChain.acombine_docs.<locals>.<listcomp>  rs   r-   r&   )r   aapplyr'   ru   r   acombine_docsr   rw   s   ``  `     @r,   r   z%MapReduceDocumentsChain.acombine_docs  s@      !N11WWWWWRVWWW 2 
 
 
 
 
 
 
 

 #n7
 
 
 
 
 "+..
 
 

 +T$*E*S+
#,	+
 +
EK+
 +
 %
 %
 %
 %
 %
 %
!! ) 	I!N!N!N!N+!N!N!N6H23(((r-   c                    dS )Nmap_reduce_documents_chainrG   rT   s    r,   _chain_typez#MapReduceDocumentsChain._chain_type"  s    ++r-   )N)r    r!   r"   r#   )r"   r.   )r7   r   r"   r   )r"   r   )NN)
rV   rW   rX   rY   rZ   r   r[   r   r"   r\   )r"   r   )__name__
__module____qualname____doc____annotations__r   r)   propertyr0   r   model_configr   classmethodrD   rH   rO   r;   r:   rv   r   r   __classcell__)r+   s   @r,   r   r      s3        H HT 75555JI&+++++< 261 1 1 1 1 1 1      X : $  L
 _(###   [ $#, _(###   [ $# _(###   [ $#,    X 	 	 	 X	 $(#	) ) ) ) )D $(#	) ) ) ) )> , , , X, , , , ,r-   r   N)r   
__future__r   typingr   r   r   r   r   r	   langchain_core._apir
   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.runnables.configr   langchain_core.runnables.utilsr   pydanticr   r   r   'langchain.chains.combine_documents.baser   )langchain.chains.combine_documents.reducer   langchain.chains.llmr   r   rG   r-   r,   <module>r      sf   U U " " " " " " 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 * * * * * * . . . . . . - - - - - - : : : : : : 7 7 7 7 7 7 ; ; ; ; ; ; ; ; ; ; M M M M M M J J J J J J ) ) ) ) ) ) 
	X	  H, H, H, H, H,7 H, H, H, H, H,r-   