
    @hZ/                        S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJrJrJr  SS
KJr  SSKJr  SSKJr  \" SSSS9 " S S\5      5       rg)zOCombining documents by mapping a chain over them first, then combining results.    )annotations)AnyOptional)
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                    ^  \ rS rSr% SrS\S'    S\S'    S\S'    S	rS
\S'     S   SU 4S jjjr\SU 4S jj5       r	\
" SSS9r\" SS9\SS j5       5       r\" SS9\SS j5       5       r\" SS9\SS j5       5       r\S S j5       r\S S j5       r  S!         S"S jjr  S!         S"S jjr\S#S j5       rSrU =r$ )$MapReduceDocumentsChain   a!  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_stepsc           	        > U R                   (       a-  [         S0 U R                  [        S 4S[        [           S 40D6$ [
        TU ]  U5      $ )Nintermediate_steps)MapReduceDocumentsOutput)r   r
   
output_keyr   listsuperget_output_schema)selfconfig	__class__s     e/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/chains/combine_documents/map_reduce.pyr#   )MapReduceDocumentsChain.get_output_schemar   sW     ))* OOc4[(49d*;  w(00    c                J   > [         TU ]  nU R                  (       a  / UQSPnU$ )z"Expect input key.

:meta private:
r   )r"   output_keysr   )r$   _output_keysr&   s     r'   r+   #MapReduceDocumentsChain.output_keys   s.     w*))@\@+?@Lr)   Tforbid)arbitrary_types_allowedextrabefore)modec                    SU;   aL  SU;   a  Sn[        U5      eUS   nUR                  S5      n[        UUS9nXQS'   US	 UR                  SS5        U$ )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_chainN)
ValueErrorgetr   pop)clsvaluesmsgcombine_chaincollapse_chainreduce_chains         r'   get_reduce_chain(MapReduceDocumentsChain.get_reduce_chain   s}     $v-'61R 
 !o%"#;<M#ZZ(ABN/(5)7L 0<+,/0JJ0$7r)   c                (    SU;   a  US   US'   US	 U$ )r4   return_map_stepsr    )r<   r=   s     r'   get_return_intermediate_steps5MapReduceDocumentsChain.get_return_intermediate_steps   s+     '289K2LF./)*r)   c                    SU;  a  Sn[        U5      eUS   R                  R                  nSU;  a&  [        U5      S:X  a
  US   US'   U$ Sn[        U5      eUS   U;  a  SUS    SU 3n[        U5      eU$ )	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: )r9   promptinput_variableslen)r<   r=   r>   llm_chain_variabless       r'   "get_default_document_variable_name:MapReduceDocumentsChain.get_default_document_variable_name   s     f$.CS/!$[188HH#61&'1,3Fq3I/0 9  !o%,-5HH)&1I*J)K L;;N:OQ  S/!r)   c                   [        U R                  [        5      (       aG  U R                  R                  (       a  U R                  R                  $ U R                  R                  $ S[        U R                  5       S3n[        U5      ez Kept for backward compatibility.z$`reduce_documents_chain` is of type z$ so it does not have this attribute.)
isinstancer   r   r8   r7   typer9   r$   r>   s     r'   r6   /MapReduceDocumentsChain.collapse_document_chain   sz     d113GHH**CC22KKK..FFF2D//01 2 	
 or)   c                    [        U R                  [        5      (       a  U R                  R                  $ S[	        U R                  5       S3n[        U5      erR   )rS   r   r   r7   rT   r9   rU   s     r'   r5   .MapReduceDocumentsChain.combine_document_chain   sY     d113GHH..FFF2D//01 2 	
 or)   c           	        U R                   R                  U Vs/ sH  oPR                  UR                  0UEPM     snUS9nU R                   R                  n[        U5       VV	s/ sH  u  p[        X   X   R                  S9PM     n
nn	U R                  R                  " U
4UUS.UD6u  pU R                  (       a  U V	s/ sH  oU   PM	     nn	XS'   X4$ s  snf s  sn	nf s  sn	f )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).
	callbackspage_contentmetadata	token_maxr\   r   )r   applyr   r^   r    	enumerater   r_   r   combine_docsr   r$   docsra   r\   kwargsdmap_resultsquestion_result_keyirresult_docsresultextra_return_dictr   s                 r'   rd   $MapReduceDocumentsChain.combine_docs   s    nn**NRSd))1>>DVDdS + 

 #nn77 "+.
 / !"847CSCST. 	 

 %)$?$?$L$L%
%
 	%
! ))BM!N+Q$7"8+!N6H23((% T
 "Os   "C&#CC$c           	       #    U R                   R                  U Vs/ sH  oPR                  UR                  0UEPM     snUS9I Sh  vN nU R                   R                  n[        U5       VV	s/ sH  u  p[        X   X   R                  S9PM     n
nn	U R                  R                  " U
4UUS.UD6I Sh  vN u  pU R                  (       a  U V	s/ sH  oU   PM	     nn	XS'   X4$ s  snf  Ns  sn	nf  N>s  sn	f 7f)rZ   r[   Nr]   r`   r   )r   aapplyr   r^   r    rc   r   r_   r   acombine_docsr   re   s                 r'   rs   %MapReduceDocumentsChain.acombine_docs  s$     !NN11NRSd))1>>DVDdS 2 
 

 #nn77 "+.
 / !"847CSCST. 	 

 +/*E*E*S*S+
+
 	+
 %
! ))BM!N+Q$7"8+!N6H23((% T


%
 "OsK   C?"C+	C?C0(C?0#C2&C?9C8:C?C:"C?2C?:C?c                    g)Nmap_reduce_documents_chainrF   )r$   s    r'   _chain_type#MapReduceDocumentsChain._chain_type*  s    +r)   rF   )N)r%   zOptional[RunnableConfig]returnztype[BaseModel])ry   z	list[str])r=   dictry   r   )ry   r   )NN)
rf   zlist[Document]ra   zOptional[int]r\   r   rg   r   ry   ztuple[str, dict])ry   r   )__name__
__module____qualname____firstlineno____doc____annotations__r   r#   propertyr+   r   model_configr   classmethodrB   rG   rO   r6   r5   rd   rs   rw   __static_attributes____classcell__)r&   s   @r'   r   r      s   HT 755JI&+t+< ,01(1 
1 1    $L
 (#  $, (#  $ (#  $0   	 	 $(#	 ) ) ! ) 	 )
  ) 
 )J $(#	 ) ) ! ) 	 )
  ) 
 )D , ,r)   r   N)r   
__future__r   typingr   r   langchain_core._apir   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.runnables.configr	   langchain_core.utils.pydanticr
   pydanticr   r   r   'langchain.chains.combine_documents.baser   )langchain.chains.combine_documents.reducer   langchain.chains.llmr   r   rF   r)   r'   <module>r      s\    U "   * . - : 6 ; ; M J ) 
	X	P,7 P,P,r)   