a
    •˜ëjE/  ã                   @  sÂ   d Z ddl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 dd	l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 edddd�G dd„ deƒƒZdS )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)Úoverride)ÚBaseCombineDocumentsChain)ÚReduceDocumentsChain)ÚLLMChainz0.3.1z1.0zªThis class is deprecated. Please see the migration guide here for a recommended replacement: https://python.langchain.com/docs/versions/migrating_chains/map_reduce_chain/)ZsinceZremovalÚmessagec                      s6  e Zd ZU dZded< ded< ded< dZd	ed
< ed2dddœ‡ fdd„ƒZeddœ‡ fdd„ƒZ	e
ddd�Zedd�edddœdd„ƒƒZedd�edddœdd „ƒƒZedd�edddœd!d"„ƒƒZeddœd#d$„ƒZeddœd%d&„ƒZd3d'd(d)dd*d+œd,d-„Zd4d'd(d)dd*d+œd.d/„Zeddœd0d1„ƒZ‡  ZS )5Ú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_stepsNzOptional[RunnableConfig]ztype[BaseModel])ÚconfigÚreturnc                   s6   | 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   ÚlistÚsuperÚget_output_schema)Úselfr   ©Ú	__class__© úl/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/combine_documents/map_reduce.pyr    o   s     ÿ
þþz)MapReduceDocumentsChain.get_output_schemaz	list[str])r   c                   s   t ƒ j}| jrg |¢d‘}|S )z2Expect input key.

        :meta private:
        r   )r   Úoutput_keysr   )r!   Z_output_keysr"   r$   r%   r&      s    z#MapReduceDocumentsChain.output_keysTZforbid)Zarbitrary_types_allowedÚextraÚbefore)ÚmodeÚdictr   )Úvaluesr   c                 C  sX   d|v rTd|v rd}t |ƒ‚|d }| d¡}t||d�}||d< |d= | dd¡ |S )úFor backwards compatibility.Úcombine_document_chainr   z´Both `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)Úclsr+   ÚmsgZcombine_chainZcollapse_chainZreduce_chainr$   r$   r%   Úget_reduce_chain�   s    ÿ
þz(MapReduceDocumentsChain.get_reduce_chainc                 C  s   d|v r|d |d< |d= |S )r,   Zreturn_map_stepsr   r$   )r4   r+   r$   r$   r%   Úget_return_intermediate_steps§   s    z5MapReduceDocumentsChain.get_return_intermediate_stepsc                 C  s|   d|vrd}t |ƒ‚|d jj}d|vrPt|ƒdkrB|d |d< qxd}t |ƒ‚n(|d |vrxd|d › d|› �}t |ƒ‚|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: )r1   ÚpromptZinput_variablesÚlen)r4   r+   r5   Zllm_chain_variablesr$   r$   r%   Ú"get_default_document_variable_name°   s"    ÿ
ÿÿz:MapReduceDocumentsChain.get_default_document_variable_namec                 C  sB   t | jtƒr$| jjr| jjS | jjS dt| jƒ› d�}t|ƒ‚dS ©z Kept for backward compatibility.z$`reduce_documents_chain` is of type z$ so it does not have this attribute.N)Ú
isinstancer   r   r0   r/   Útyper1   ©r!   r5   r$   r$   r%   r.   Ê   s    ÿÿz/MapReduceDocumentsChain.collapse_document_chainc                 C  s2   t | jtƒr| jjS dt| jƒ› d�}t|ƒ‚dS r<   )r=   r   r   r/   r>   r1   r?   r$   r$   r%   r-   Ø   s    ÿÿz.MapReduceDocumentsChain.combine_document_chainzlist[Document]zOptional[int]r   ztuple[str, dict])ÚdocsÚ	token_maxÚ	callbacksÚkwargsr   c           
        s†   ˆj j‡‡fdd„ˆ D ƒ|d�}ˆj j‰‡ ‡fdd„t|ƒD ƒ}ˆjj|f||dœˆ¤Ž\}}ˆjr~‡fd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                   s   g | ]}ˆj |jiˆ ¥‘qS r$   ©r   Úpage_content©Ú.0Úd©rC   r!   r$   r%   Ú
<listcomp>ò   ó    z8MapReduceDocumentsChain.combine_docs.<locals>.<listcomp>©rB   c                   s&   g | ]\}}t |ˆ ˆ | jd �‘qS ©)rF   Úmetadata©r   rO   ©rH   ÚiÚr©r@   Úquestion_result_keyr$   r%   rK   ö   s   þ©rA   rB   c                   s   g | ]}|ˆ  ‘qS r$   r$   ©rH   rS   ©rU   r$   r%   rK     rL   r   )r   Úapplyr   Ú	enumerater   Úcombine_docsr   ©
r!   r@   rA   rB   rC   Zmap_resultsZresult_docsÚresultZextra_return_dictr   r$   ©r@   rC   rU   r!   r%   r[   ä   s(    ýýÿýü
z$MapReduceDocumentsChain.combine_docsc           
      ‹  s’   ˆj j‡‡fdd„ˆ D ƒ|d�I dH }ˆj j‰‡ ‡fdd„t|ƒD ƒ}ˆjj|f||dœˆ¤ŽI dH \}}ˆjrŠ‡fdd„|D ƒ}	|	|d< ||fS )	rD   c                   s   g | ]}ˆj |jiˆ ¥‘qS r$   rE   rG   rJ   r$   r%   rK     rL   z9MapReduceDocumentsChain.acombine_docs.<locals>.<listcomp>rM   Nc                   s&   g | ]\}}t |ˆ ˆ | jd �‘qS rN   rP   rQ   rT   r$   r%   rK     s   þrV   c                   s   g | ]}|ˆ  ‘qS r$   r$   rW   rX   r$   r%   rK   $  rL   r   )r   Zaapplyr   rZ   r   Úacombine_docsr   r\   r$   r^   r%   r_     s(    ýýÿýüz%MapReduceDocumentsChain.acombine_docsc                 C  s   dS )NZmap_reduce_documents_chainr$   )r!   r$   r$   r%   Ú_chain_type(  s    z#MapReduceDocumentsChain._chain_type)N)NN)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r    Úpropertyr&   r   Zmodel_configr   Úclassmethodr6   r7   r;   r.   r-   r[   r_   r`   Ú__classcell__r$   r$   r"   r%   r      sF   

F þ
þ  ü%  ü"r   N)rd   Ú
__future__r   Útypingr   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.runnables.configr   Zlangchain_core.utils.pydanticr	   Zpydanticr
   r   r   Ztyping_extensionsr   Z'langchain.chains.combine_documents.baser   Z)langchain.chains.combine_documents.reducer   Zlangchain.chains.llmr   r   r$   r$   r$   r%   Ú<module>   s$   ü	