
    @h-%                        S 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  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 jr\" SSSS9 " S S\5      5       rg)zLCombine documents by doing a first pass and then refining on more documents.    )annotations)Any)
deprecated)	Callbacks)Document)BasePromptTemplateformat_documentPromptTemplate)
ConfigDictFieldmodel_validator)BaseCombineDocumentsChain)LLMChainc                     [        S/SS9$ )Npage_contentz{page_content})input_variablestemplater
        a/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/chains/combine_documents/refine.py_get_default_document_promptr      s    >*:EUVVr   z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/refine_docs_chain/)sinceremovalmessagec                  h  ^  \ rS rSr% SrS\S'    S\S'    S\S'    S\S'    \" \S	9rS
\S'    Sr	S\S'    \
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!S jjr S        S!S jjrS"S jrS#S jr      S$S jr\
S%S j5       rSrU =r$ )&RefineDocumentsChain   az  Combine documents by doing a first pass and then refining on more documents.

This algorithm first calls `initial_llm_chain` on the first document, passing
that first document in with the variable name `document_variable_name`, and
produces a new variable with the variable name `initial_response_name`.

Then, it loops over every remaining document. This is called the "refine" step.
It calls `refine_llm_chain`,
passing in that document with the variable name `document_variable_name`
as well as the previous response with the variable name `initial_response_name`.

Example:
    .. code-block:: python

        from langchain.chains import RefineDocumentsChain, LLMChain
        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}"
        )
        initial_llm_chain = LLMChain(llm=llm, prompt=prompt)
        initial_response_name = "prev_response"
        # The prompt here should take as an input variable the
        # `document_variable_name` as well as `initial_response_name`
        prompt_refine = PromptTemplate.from_template(
            "Here's your first summary: {prev_response}. "
            "Now add to it based on the following context: {context}"
        )
        refine_llm_chain = LLMChain(llm=llm, prompt=prompt_refine)
        chain = RefineDocumentsChain(
            initial_llm_chain=initial_llm_chain,
            refine_llm_chain=refine_llm_chain,
            document_prompt=document_prompt,
            document_variable_name=document_variable_name,
            initial_response_name=initial_response_name,
        )
r   initial_llm_chainrefine_llm_chainstrdocument_variable_nameinitial_response_name)default_factoryr   document_promptFboolreturn_intermediate_stepsc                J   > [         TU ]  nU R                  (       a  / UQSPnU$ )z"Expect input key.

:meta private:
intermediate_steps)superoutput_keysr'   )self_output_keys	__class__s     r   r+    RefineDocumentsChain.output_keysd   s.     w*))@\@+?@Lr   Tforbid)arbitrary_types_allowedextrabefore)modec                (    SU;   a  US   US'   US	 U$ )zFor backwards compatibility.return_refine_stepsr'   r   )clsvaluess     r   get_return_intermediate_steps2RefineDocumentsChain.get_return_intermediate_stepst   s+     !F*289N2OF./,-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"initial_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: )
ValueErrorpromptr   len)r7   r8   msgllm_chain_variabless       r   "get_default_document_variable_name7RefineDocumentsChain.get_default_document_variable_name}   s     f,6CS/!$%89@@PP#61&'1,3Fq3I/0 9  !o%,-5HH)&1I*J)K L;;N:OQ  S/!r   c                0   U R                   " U40 UD6nU R                  R                  " SSU0UD6nU/nUSS  HI  nU R                  Xu5      n0 UEUEnU R                  R                  " SSU0UD6nUR                  U5        MK     U R                  Xe5      $ )a  Combine by mapping first chain over all, then stuffing into final chain.

Args:
    docs: List of documents to combine
    callbacks: Callbacks to be passed through
    **kwargs: additional parameters to be passed to LLM calls (like other
        input variables besides the documents)

Returns:
    The first element returned is the single string output. The second
    element returned is a dictionary of other keys to return.
	callbacksr<   Nr   )_construct_initial_inputsr   predict_construct_refine_inputsr    append_construct_result	r,   docsrE   kwargsinputsresrefine_stepsdocbase_inputss	            r   combine_docs!RefineDocumentsChain.combine_docs   s    $ //??$$,,KyKFKu8C77AK..v.F''//N)NvNC$	 
 %%l88r   c                `  #    U R                   " U40 UD6nU R                  R                  " SSU0UD6I Sh  vN nU/nUSS  HQ  nU R                  Xu5      n0 UEUEnU R                  R                  " SSU0UD6I Sh  vN nUR                  U5        MS     U R                  Xe5      $  Nr N,7f)a  Async combine by mapping a first chain over all, then stuffing
 into a final chain.

Args:
    docs: List of documents to combine
    callbacks: Callbacks to be passed through
    **kwargs: additional parameters to be passed to LLM calls (like other
        input variables besides the documents)

Returns:
    The first element returned is the single string output. The second
    element returned is a dictionary of other keys to return.
rE   Nr<   r   )rF   r   apredictrH   r    rI   rJ   rK   s	            r   acombine_docs"RefineDocumentsChain.acombine_docs   s     & //??**33RiR6RRu8C77AK..v.F--66UUfUUC$	 
 %%l88 S
 Vs"   5B.B*AB.?B, +B.,B.c                <    U R                   (       a  SU0nX#4$ 0 nX#4$ )Nr)   )r'   )r,   rP   rO   extra_return_dicts       r   rJ   &RefineDocumentsChain._construct_result   s2    ))!5| D %% !#%%r   c                \    U R                   [        XR                  5      U R                  U0$ N)r"   r	   r%   r#   )r,   rQ   rO   s      r   rH   -RefineDocumentsChain._construct_refine_inputs   s-    ''>R>R)S&&
 	
r   c                   SUS   R                   0nUR                  US   R                  5        U R                  R                   Vs0 sH  oDX4   _M	     nnU R
                  U R                  R                  " S0 UD60n0 UEUE$ s  snf )Nr   r   r   )r   updatemetadatar%   r   r"   format)r,   rL   rM   	base_infokdocument_inforR   s          r   rF   .RefineDocumentsChain._construct_initial_inputs   s    
 $T!W%9%9:	a))*262F2F2V2VW2VQIL2VW'')=)=)D)D)U})U
 )+(((	 Xs   Bc                    g)Nrefine_documents_chainr   )r,   s    r   _chain_type RefineDocumentsChain._chain_type   s    'r   r   )return	list[str])r8   dictrk   r   r]   )rL   list[Document]rE   r   rM   r   rk   tuple[str, dict])rP   rl   rO   r!   rk   ro   )rQ   r   rO   r!   rk   dict[str, Any])rL   rn   rM   r   rk   rp   )rk   r!   )__name__
__module____qualname____firstlineno____doc____annotations__r   r   r%   r'   propertyr+   r   model_configr   classmethodr9   rB   rS   rW   rJ   rH   rF   ri   __static_attributes____classcell__)r.   s   @r   r   r      s`   0d  /)QL*/4+O'  S&+t+?   $L
 (#  $ (#  $6  $99 9 	9
 
9>  $99 9 	9
 
9:&
)) ) 
	) ( (r   r   N)rk   r   )ru   
__future__r   typingr   langchain_core._apir   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.promptsr   r	   langchain_core.prompts.promptr   pydanticr   r   r   'langchain.chains.combine_documents.baser   langchain.chains.llmr   r   r   r   r   r   <module>r      sc    R "  * . - F 8 7 7 *W 
	Y	K(4 K(K(r   