§
    ™Štj-  ã                   ó`  — d 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 ddlmZmZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZmZmZmZmZ ddl m!Z! ddeedœde
dededz  dedz  de"de"dee#e"ef         ef         fd„Z$ edddd¬¦  «         G d„ de¦  «        ¦   «         Z%dS )z7Chain that combines documents by stuffing into context.é    )ÚAny)Ú
deprecated)Ú	Callbacks)ÚDocument)ÚLanguageModelLike)ÚBaseOutputParserÚStrOutputParser)ÚBasePromptTemplateÚformat_document)ÚRunnableÚRunnablePassthrough)Ú
ConfigDictÚFieldÚmodel_validator)Úoverride)ÚDEFAULT_DOCUMENT_PROMPTÚDEFAULT_DOCUMENT_SEPARATORÚDOCUMENTS_KEYÚBaseCombineDocumentsChainÚ_validate_prompt)ÚLLMChainN)Úoutput_parserÚdocument_promptÚdocument_separatorÚdocument_variable_nameÚllmÚpromptr   r   r   r   Úreturnc                ó  ‡‡‡— t          |‰¦  «         |pt          Š|pt          ¦   «         }dt          dt          fˆˆˆfd„}t          j        di ‰|i¤Ž                     d¬¦  «        |z  | z  |z                       d¬¦  «        S )až  Create a chain for passing a list of Documents to a model.

    Args:
        llm: Language model.
        prompt: Prompt template. Must contain input variable `"context"` (override by
            setting document_variable), which will be used for passing in the formatted
            documents.
        output_parser: Output parser. Defaults to `StrOutputParser`.
        document_prompt: Prompt used for formatting each document into a string. Input
            variables can be "page_content" or any metadata keys that are in all
            documents. "page_content" will automatically retrieve the
            `Document.page_content`, and all other inputs variables will be
            automatically retrieved from the `Document.metadata` dictionary. Default to
            a prompt that only contains `Document.page_content`.
        document_separator: String separator to use between formatted document strings.
        document_variable_name: Variable name to use for the formatted documents in the
            prompt. Defaults to `"context"`.

    Returns:
        An LCEL Runnable. The input is a dictionary that must have a `"context"` key
        that maps to a `list[Document]`, and any other input variables expected in the
        prompt. The `Runnable` return type depends on `output_parser` used.

    Example:
        ```python
        # pip install -U langchain langchain-openai

        from langchain_openai import ChatOpenAI
        from langchain_core.documents import Document
        from langchain_core.prompts import ChatPromptTemplate
        from langchain_classic.chains.combine_documents import (
            create_stuff_documents_chain,
        )

        prompt = ChatPromptTemplate.from_messages(
            [("system", "What are everyone's favorite colors:\n\n{context}")]
        )
        model = ChatOpenAI(model="gpt-3.5-turbo")
        chain = create_stuff_documents_chain(model, prompt)

        docs = [
            Document(page_content="Jesse loves red but not yellow"),
            Document(
                page_content="Jamal loves green but not as much as he loves orange"
            ),
        ]

        chain.invoke({"context": docs})
        ```
    Úinputsr   c                 óR   •— ‰                      ˆfd„| ‰         D ¦   «         ¦  «        S )Nc              3   ó8   •K  — | ]}t          |‰¦  «        V — Œd S ©N)r   )Ú.0ÚdocÚ_document_prompts     €ún/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_classic/chains/combine_documents/stuff.pyú	<genexpr>zDcreate_stuff_documents_chain.<locals>.format_docs.<locals>.<genexpr>Y   sB   øè è € ð '
ð '
àõ ˜CÐ!1Ñ2Ô2ð'
ð '
ð '
ð '
ð '
ð '
ó    )Újoin)r    r&   r   r   s    €€€r'   Úformat_docsz1create_stuff_documents_chain.<locals>.format_docsX   sG   ø€ Ø!×&Ò&ð '
ð '
ð '
ð '
àÐ4Ô5ð'
ñ '
ô '
ñ 
ô 
ð 	
r)   Úformat_inputs)Úrun_nameÚstuff_documents_chain© )r   r   r	   ÚdictÚstrr   ÚassignÚwith_config)	r   r   r   r   r   r   Ú_output_parserr+   r&   s	       ``  @r'   Úcreate_stuff_documents_chainr5      sÕ   øøø€ õv �VÐ3Ñ4Ô4Ð4Ø&ÐAÕ*AÐØ"Ð7¥oÑ&7Ô&7€Nð
�Dð 
¥Sð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
õ 	Ô"ÐKÐKÐ&<¸kÐ%JÐKÐK×WÒWØ$ð 	Xñ 	
ô 	
ð ñ	ð ñ		ð
 ñ	÷ ‚kÐ2€kÑ3Ô3ð4r)   z0.2.13z2.0.0zlangchain.agents.create_agentzuBuild new RAG flows with `create_agent` and a retrieval tool. See https://docs.langchain.com/oss/python/langchain/rag)ÚsinceÚremovalÚalternativeÚaddendumc                   óÔ  ‡ — e Zd ZU dZeed<   	  ed„ ¬¦  «        Zeed<   	 e	ed<   	 dZ
e	ed<   	  ed	d
¬¦  «        Z ed¬¦  «        ededefd„¦   «         ¦   «         Zeedee	         fˆ fd„¦   «         ¦   «         Zdee         dedefd„Zdee         dededz  fd„Z	 ddee         dededee	ef         fd„Z	 ddee         dededee	ef         fd„Zede	fd„¦   «         Zˆ xZS )ÚStuffDocumentsChaina²  Chain that combines documents by stuffing into context.

    This chain takes a list of documents and first combines them into a single string.
    It does this by formatting each document into a string with the `document_prompt`
    and then joining them together with `document_separator`. It then adds that new
    string to the inputs with the variable name set by `document_variable_name`.
    Those inputs are then passed to the `llm_chain`.

    Example:
        ```python
        from langchain_classic.chains import StuffDocumentsChain, LLMChain
        from langchain_core.prompts import PromptTemplate
        from langchain_openai 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"
        model = 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=model, prompt=prompt)
        chain = StuffDocumentsChain(
            llm_chain=llm_chain,
            document_prompt=document_prompt,
            document_variable_name=document_variable_name,
        )
        ```
    Ú	llm_chainc                  ó   — t           S r#   )r   r/   r)   r'   ú<lambda>zStuffDocumentsChain.<lambda>˜   s   € Õ 7€ r)   )Údefault_factoryr   r   z

r   TÚforbid)Úarbitrary_types_allowedÚextraÚbefore)ÚmodeÚvaluesr   c                 óà   — |d         j         j        }d|vr0t          |¦  «        dk    r|d         |d<   n8d}t          |¦  «        ‚|d         |vrd|d         › d|› �}t          |¦  «        ‚|S )zæGet default document variable name, if not provided.

        If only one variable is present in the llm_chain.prompt,
        we can infer that the formatted documents should be passed in
        with this variable name.
        r<   r   é   r   zQdocument_variable_name must be provided if there are multiple llm_chain_variableszdocument_variable_name z- was not found in llm_chain input_variables: )r   Úinput_variablesÚlenÚ
ValueError)ÚclsrE   Úllm_chain_variablesÚmsgs       r'   Ú"get_default_document_variable_namez6StuffDocumentsChain.get_default_document_variable_name¦   s²   € ð % [Ô1Ô8ÔHÐØ#¨6Ð1Ð1ÝÐ&Ñ'Ô'¨1Ò,Ð,Ø3FÀqÔ3I�Ð/Ñ0Ð0ð3ð õ ! ‘o”oÐ%ØÐ,Ô-Ð5HÐHÐHðQ¨&Ð1IÔ*Jð Qð QØ;NðQð Qð õ ˜S‘/”/Ð!Øˆr)   c                 ób   •‡ — ˆ fd„‰ j         j        D ¦   «         }t          ¦   «         j        |z   S )Nc                 ó*   •— g | ]}|‰j         k    ¯|‘ŒS r/   )r   )r$   ÚkÚselfs     €r'   ú
<listcomp>z2StuffDocumentsChain.input_keys.<locals>.<listcomp>Ä   s-   ø€ ð 
ð 
ð 
Ø°A¸Ô9TÒ4TÐ4TˆAÐ4TÐ4TÐ4Tr)   )r<   Ú
input_keysÚsuper)rR   Ú
extra_keysÚ	__class__s   ` €r'   rT   zStuffDocumentsChain.input_keysÁ   sE   øø€ ð
ð 
ð 
ð 
Ø”~Ô0ð
ñ 
ô 
ˆ
õ ‰wŒwÔ! JÑ.Ð.r)   ÚdocsÚkwargsc                 ó¨   ‡ — ˆ fd„|D ¦   «         }ˆ fd„|                      ¦   «         D ¦   «         }‰ j                             |¦  «        |‰ j        <   |S )aô  Construct inputs from kwargs and docs.

        Format and then join all the documents together into one input with name
        `self.document_variable_name`. Also pluck any additional variables
        from **kwargs.

        Args:
            docs: List of documents to format and then join into single input
            **kwargs: additional inputs to chain, will pluck any other required
                arguments from here.

        Returns:
            dictionary of inputs to LLMChain
        c                 ó:   •— g | ]}t          |‰j        ¦  «        ‘ŒS r/   )r   r   )r$   r%   rR   s     €r'   rS   z3StuffDocumentsChain._get_inputs.<locals>.<listcomp>Ù   s&   ø€ ÐRÐRÐRÀc• s¨DÔ,@ÑAÔAÐRÐRÐRr)   c                 óB   •— i | ]\  }}|‰j         j        j        v ¯||“ŒS r/   )r<   r   rH   )r$   rQ   ÚvrR   s      €r'   ú
<dictcomp>z3StuffDocumentsChain._get_inputs.<locals>.<dictcomp>Û   s<   ø€ ð 
ð 
ð 
á��1Ø�D”NÔ)Ô9Ð9Ð9ð ˆqà9Ð9Ð9r)   )Úitemsr   r*   r   )rR   rX   rY   Údoc_stringsr    s   `    r'   Ú_get_inputszStuffDocumentsChain._get_inputsÉ   st   ø€ ð  SÐRÐRÐRÈTÐRÑRÔRˆð
ð 
ð 
ð 
àŸš™œð
ñ 
ô 
ˆð
 /3Ô.E×.JÒ.JÈ;Ñ.WÔ.WˆˆtÔ*Ñ+Øˆr)   Nc                 ó€   —  | j         |fi |¤Ž} | j        j        j        di |¤Ž}| j                             |¦  «        S )a„  Return the prompt length given the documents passed in.

        This can be used by a caller to determine whether passing in a list
        of documents would exceed a certain prompt length. This useful when
        trying to ensure that the size of a prompt remains below a certain
        context limit.

        Args:
            docs: a list of documents to use to calculate the total prompt length.
            **kwargs: additional parameters to use to get inputs to LLMChain.

        Returns:
            Returns None if the method does not depend on the prompt length,
            otherwise the length of the prompt in tokens.
        r/   )ra   r<   r   ÚformatÚ_get_num_tokens)rR   rX   rY   r    r   s        r'   Úprompt_lengthz!StuffDocumentsChain.prompt_lengthã   sQ   € ð  "�Ô! $Ð1Ð1¨&Ð1Ð1ˆØ-�”Ô&Ô-Ð7Ð7°Ð7Ð7ˆØŒ~×-Ò-¨fÑ5Ô5Ð5r)   Ú	callbacksc                 óJ   —  | j         |fi |¤Ž} | j        j        dd|i|¤Ži fS )aÀ  Stuff all documents into one prompt and pass to LLM.

        Args:
            docs: List of documents to join together into one variable
            callbacks: Optional callbacks to pass along
            **kwargs: additional parameters to use to get inputs to LLMChain.

        Returns:
            The first element returned is the single string output. The second
            element returned is a dictionary of other keys to return.
        rf   r/   )ra   r<   Úpredict©rR   rX   rf   rY   r    s        r'   Úcombine_docsz StuffDocumentsChain.combine_docs÷   sC   € ð" "�Ô! $Ð1Ð1¨&Ð1Ð1ˆà%ˆtŒ~Ô%ÐDÐD°	ÐD¸VÐDÐDÀbÐHÐHr)   c              ‹   óZ   K  —  | j         |fi |¤Ž} | j        j        dd|i|¤Žƒ d{V —†i fS )aÆ  Async stuff all documents into one prompt and pass to LLM.

        Args:
            docs: List of documents to join together into one variable
            callbacks: Optional callbacks to pass along
            **kwargs: additional parameters to use to get inputs to LLMChain.

        Returns:
            The first element returned is the single string output. The second
            element returned is a dictionary of other keys to return.
        rf   Nr/   )ra   r<   Úapredictri   s        r'   Úacombine_docsz!StuffDocumentsChain.acombine_docs  sY   è è € ð" "�Ô! $Ð1Ð1¨&Ð1Ð1ˆà,�T”^Ô,ÐKÐK°yÐKÀFÐKÐKÐKÐKÐKÐKÐKÐKÈRÐOÐOr)   c                 ó   — dS )Nr.   r/   )rR   s    r'   Ú_chain_typezStuffDocumentsChain._chain_type!  s   € à&Ð&r)   r#   ) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r
   r1   r   r   Úmodel_configr   Úclassmethodr0   r   rN   Úpropertyr   ÚlistrT   r   ra   Úintre   r   Útuplerj   rm   ro   Ú__classcell__)rW   s   @r'   r;   r;   h   ss  ø€ € € € € € ð ð  ðD ÐÐÑð$à*/¨%Ø7Ð7ð+ñ +ô +€OÐ'ð ð ñ ð SØÐÐÑðIà$Ð˜Ð$Ð$Ñ$Ø?à�:Ø $Øðñ ô €Lð
 €_˜(Ð#Ñ#Ô#Øð¸ð Àð ð ð ñ „[ñ $Ô#ðð2 Øð/˜D œIð /ð /ð /ð /ð /ñ „Xñ „Xð/ð  X¤ð ¸#ð À$ð ð ð ð ð46 $ x¤.ð 6¸Cð 6ÀCÈ$ÁJð 6ð 6ð 6ð 6ð.  $ðIð Ià�8ŒnðIð ðIð ð	Ið
 
ˆs�DˆyÔ	ðIð Ið Ið Ið0  $ðPð Pà�8ŒnðPð ðPð ð	Pð
 
ˆs�DˆyÔ	ðPð Pð Pð Pð* ð'˜Sð 'ð 'ð 'ñ „Xð'ð 'ð 'ð 'ð 'r)   r;   )&rs   Útypingr   Úlangchain_core._apir   Úlangchain_core.callbacksr   Úlangchain_core.documentsr   Úlangchain_core.language_modelsr   Úlangchain_core.output_parsersr   r	   Úlangchain_core.promptsr
   r   Úlangchain_core.runnablesr   r   Úpydanticr   r   r   Útyping_extensionsr   Ú/langchain_classic.chains.combine_documents.baser   r   r   r   r   Úlangchain_classic.chains.llmr   r1   r0   r5   r;   r/   r)   r'   ú<module>rˆ      s<  ðØ =Ð =à Ð Ð Ð Ð Ð à *Ð *Ð *Ð *Ð *Ð *Ø .Ð .Ð .Ð .Ð .Ð .Ø -Ð -Ð -Ð -Ð -Ð -Ø <Ð <Ð <Ð <Ð <Ð <Ø KÐ KÐ KÐ KÐ KÐ KÐ KÐ KØ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FØ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø &Ð &Ð &Ð &Ð &Ð &ðð ð ð ð ð ð ð ð ð ð ð ð ð ð 2Ð 1Ð 1Ð 1Ð 1Ð 1ð .2Ø15Ø8Ø"/ðL4ð L4ð L4Ø	ðL4àðL4ð $ dÑ*ð	L4ð
 (¨$Ñ.ðL4ð ðL4ð  ðL4ð ˆd�3˜�8Œn˜cÐ!Ô"ðL4ð L4ð L4ð L4ð^ €Ø
ØØ/ð	>ðñ ô ðr'ð r'ð r'ð r'ð r'Ð3ñ r'ô r'ñô ðr'ð r'ð r'r)   