o
    Jjgo$                     @  s   d Z ddlmZ ddlmZmZmZ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  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 reranking results.    )annotations)	AnyDictListOptionalSequenceTupleTypeUnioncast)
deprecated)	Callbacks)Document)RunnableConfig)create_model)	BaseModel
ConfigDictmodel_validator)Self)BaseCombineDocumentsChain)LLMChain)RegexParserz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_rerank_docs_chain/)sinceremovalmessagec                      s   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 dZd	ed
< 	 dZded< 	 edddZ	d5d6ddZ	e
d7 fddZeddd8ddZedded9d#d$Z	d5d:d+d,Z	d5d:d-d.Zd;d1d2Ze
d<d3d4Z  ZS )=MapRerankDocumentsChaina  Combining documents by mapping a chain over them, then reranking results.

    This algorithm calls an LLMChain on each input document. The LLMChain is expected
    to have an OutputParser that parses the result into both an answer (`answer_key`)
    and a score (`rank_key`). The answer with the highest score is then returned.

    Example:
        .. code-block:: python

            from langchain.chains import MapRerankDocumentsChain, LLMChain
            from langchain_core.prompts import PromptTemplate
            from langchain_community.llms import OpenAI
            from langchain.output_parsers.regex import RegexParser

            document_variable_name = "context"
            llm = OpenAI()
            # The prompt here should take as an input variable the
            # `document_variable_name`
            # The actual prompt will need to be a lot more complex, this is just
            # an example.
            prompt_template = (
                "Use the following context to tell me the chemical formula "
                "for water. Output both your answer and a score of how confident "
                "you are. Context: {context}"
            )
            output_parser = RegexParser(
                regex=r"(.*?)
Score: (.*)",
                output_keys=["answer", "score"],
            )
            prompt = PromptTemplate(
                template=prompt_template,
                input_variables=["context"],
                output_parser=output_parser,
            )
            llm_chain = LLMChain(llm=llm, prompt=prompt)
            chain = MapRerankDocumentsChain(
                llm_chain=llm_chain,
                document_variable_name=document_variable_name,
                rank_key="score",
                answer_key="answer",
            )
    r   	llm_chainstrdocument_variable_namerank_key
answer_keyNzOptional[List[str]]metadata_keysFboolreturn_intermediate_stepsTforbid)arbitrary_types_allowedextraconfigOptional[RunnableConfig]returnType[BaseModel]c                 C  sN   | j td fi}| jrtt d f|d< | jr |dd | jD  tdi |S )Nintermediate_stepsc                 S  s   i | ]}|t d fqS N)r   ).0key r/   e/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/chains/combine_documents/map_rerank.py
<dictcomp>f   s    z=MapRerankDocumentsChain.get_output_schema.<locals>.<dictcomp>MapRerankOutput)r2   )
output_keyr   r#   r   r!   updater   )selfr'   schemar/   r/   r0   get_output_schema]   s   
z)MapRerankDocumentsChain.get_output_schema	List[str]c                   s0   t  j}| jr|dg }| jdur|| j7 }|S )z2Expect input key.

        :meta private:
        r+   N)superoutput_keysr#   r!   )r5   _output_keys	__class__r/   r0   r:   j   s   


z#MapRerankDocumentsChain.output_keysafter)moder   c                 C  sp   | j jj}t|tstd| |j}| j|vr%td| j d| d| j|vr6td| j d| d| S )z5Validate that the combine chain outputs a dictionary.z8Output parser of llm_chain should be a RegexParser, got zGot zF as key to rank on, but did not find it in the llm_chain output keys ()zE as key to return, but did not find it in the llm_chain output keys ()	r   promptoutput_parser
isinstancer   
ValueErrorr:   r   r    )r5   rB   r:   r/   r/   r0   validate_llm_outputw   s*   





z+MapRerankDocumentsChain.validate_llm_outputbeforevaluesr   r   c                 C  sp   d|vrt d|d jj}d|vr$t|dkr |d |d< |S t d|d |vr6t 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: )rD   rA   input_variableslen)clsrG   llm_chain_variablesr/   r/   r0   "get_default_document_variable_name   s"   z:MapRerankDocumentsChain.get_default_document_variable_namedocsList[Document]	callbacksr   kwargsTuple[str, dict]c                   s,   j j fdd|D |d}||S ).  Combine documents in a map rerank manner.

        Combine by mapping first chain over all documents, then reranking the results.

        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.
        c                       g | ]}i j |ji qS r/   r   page_contentr-   drQ   r5   r/   r0   
<listcomp>        z8MapRerankDocumentsChain.combine_docs.<locals>.<listcomp>rP   )r   apply_and_parse_process_resultsr5   rN   rP   rQ   resultsr/   rY   r0   combine_docs   s
   z$MapRerankDocumentsChain.combine_docsc                   s4   j j fdd|D |dI dH }||S )rS   c                   rT   r/   rU   rW   rY   r/   r0   rZ      r[   z9MapRerankDocumentsChain.acombine_docs.<locals>.<listcomp>r\   N)r   aapply_and_parser^   r_   r/   rY   r0   acombine_docs   s   z%MapRerankDocumentsChain.acombine_docsr`   /Sequence[Union[str, List[str], Dict[str, str]]]c           	        sx   t tt |}tt|| fddd}|d \}}i } jd ur. jD ]	}|j| ||< q$ jr5||d< | j |fS )Nc                   s   t | d  j  S )Nr   )intr   )xr5   r/   r0   <lambda>   s    z:MapRerankDocumentsChain._process_results.<locals>.<lambda>)r.   r   r+   )	r   r   dictsortedzipr!   metadatar#   r    )	r5   rN   r`   typed_results
sorted_resoutputdocument
extra_infor.   r/   rg   r0   r^      s   

z(MapRerankDocumentsChain._process_resultsc                 C  s   dS )Nmap_rerank_documents_chainr/   rg   r/   r/   r0   _chain_type   s   z#MapRerankDocumentsChain._chain_typer,   )r'   r(   r)   r*   )r)   r8   )r)   r   )rG   r   r)   r   )rN   rO   rP   r   rQ   r   r)   rR   )rN   rO   r`   rd   r)   rR   )r)   r   )__name__
__module____qualname____doc____annotations__r!   r#   r   model_configr7   propertyr:   r   rE   classmethodrM   ra   rc   r^   rs   __classcell__r/   r/   r<   r0   r      sD   
 
+
r   N)$rw   
__future__r   typingr   r   r   r   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   typing_extensionsr   'langchain.chains.combine_documents.baser   langchain.chains.llmr   langchain.output_parsers.regexr   r   r/   r/   r/   r0   <module>   s&    ,	