
    gh                        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 d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ  G d de          Z G d de          ZdedefdZ eddd          dedefd            ZdS )    )Iterator)
deprecated)BaseChatModelBaseLanguageModel)HumanMessageSystemMessage)PydanticOutputFunctionsParser)ChatPromptTemplateHumanMessagePromptTemplate)Runnable)	BaseModelField)LLMChain)get_llm_kwargsc            
           e Zd ZU dZ edd          Zeed<    edd          Ze	e         ed<   dd	ed
ede
dee         fdZd
edee         fdZdS )FactWithEvidencezClass representing a single statement.

    Each fact has a body and a list of sources.
    If there are multiple facts make sure to break them apart
    such that each one only uses a set of sources that are relevant to it.
    .z+Body of the sentence, as part of a responsedescriptionfactz]Each source should be a direct quote from the context, as a substring of the original contentsubstring_quoted   quotecontexterrsreturnc              #      K   dd l }|}|}d}|                    d| d| d|          }|0||k    r*|dz  }|                    d| d| d|          }|||k    *||                                E d {V  d S d S )Nr   (z){e<=}   )regexsearchspans)	selfr   r   r   r    minormajorerrs_ss	            s/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain/chains/openai_functions/citation_fuzzy_match.py	_get_spanzFactWithEvidence._get_span    s      LL3U33%333U;;iETMMQJE777e777??A iETMM =wwyy          =    c              #   V   K   | j         D ]}|                     ||          E d {V  d S )N)r   r)   )r#   r   r   s      r(   	get_spanszFactWithEvidence.get_spans/   sK      ) 	6 	6E~~eW5555555555	6 	6r*   N)r   )__name__
__module____qualname____doc__r   r   str__annotations__r   listintr   r)   r,    r*   r(   r   r      s           c'TUUUD#UUU!&5" " "OT#Y   ! !s !S ! !hsm ! ! ! !6 6# 6 6 6 6 6 6r*   r   c                   h    e Zd ZU dZ edd          Zeed<    edd          Ze	e
         ed<   dS )	QuestionAnswerzA question and its answer as a list of facts each one should have a source.
    each sentence contains a body and a list of sources..zQuestion that was askedr   questionz]Body of the answer, each fact should be its separate object with a body and a list of sourcesanswerN)r-   r.   r/   r0   r   r8   r1   r2   r9   r3   r   r5   r*   r(   r7   r7   4   sx         < < E#+DEEEHcEEE%*UD& & &FD!"     r*   r7   llmr   c                     | j         t          j         u rt          d          t          t	          d          t          j        d          g          }||                     t                    z  S )a  Create a citation fuzzy match Runnable.

    Example usage:

        .. code-block:: python

            from langchain.chains import create_citation_fuzzy_match_runnable
            from langchain_openai import ChatOpenAI

            llm = ChatOpenAI(model="gpt-4o-mini")

            context = "Alice has blue eyes. Bob has brown eyes. Charlie has green eyes."
            question = "What color are Bob's eyes?"

            chain = create_citation_fuzzy_match_runnable(llm)
            chain.invoke({"question": question, "context": context})

    Args:
        llm: Language model to use for the chain. Must implement bind_tools.

    Returns:
        Runnable that can be used to answer questions with citations.
    z>Language model must implement bind_tools to use this function.UYou are a world class algorithm to answer questions with correct and exact citations.zAnswer question using the following context.

{context}

Question: {question}

Tips: Make sure to cite your sources, and use the exact words from the context.)	
bind_toolsr   
ValueErrorr
   r   r   from_templatewith_structured_outputr7   )r:   prompts     r(   $create_citation_fuzzy_match_runnablerB   B   s    0 ~111L
 
 	
  >  '4< 	
 F C..~>>>>r*   z0.2.13z1.0rB   )sinceremovalalternativec                    t          t                    }t                                          }|d         |d         |d}t          |          }t	          d          t          d          t          j        d          t          j        d	          t          d
          g}t          |          }t          | |||          }|S )zCreate a citation fuzzy match chain.

    Args:
        llm: Language model to use for the chain.

    Returns:
        Chain (LLMChain) that can be used to answer questions with citations.
    )pydantic_schematitler   )namer   
parametersr<   )contentz+Answer question using the following contextz	{context}zQuestion: {question}zOTips: Make sure to cite your sources, and use the exact words from the context.)messages)r:   rA   
llm_kwargsoutput_parser)
r	   r7   schemar   r   r   r   r?   r
   r   )r:   rN   rO   functionrM   rL   rA   chains           r(   !create_citation_fuzzy_match_chainrR   p   s     2.QQQM""$$Fwm, H
  ))J>	
 	
 	
 	JKKK"0=="01GHH<	
 	
 	
H"  222F#	  E Lr*   N)collections.abcr   langchain_core._apir   langchain_core.language_modelsr   r   langchain_core.messagesr   r   .langchain_core.output_parsers.openai_functionsr	   langchain_core.prompts.chatr
   r   langchain_core.runnablesr   pydanticr   r   langchain.chains.llmr   'langchain.chains.openai_functions.utilsr   r   r7   rB   rR   r5   r*   r(   <module>r]      s   $ $ $ $ $ $ * * * * * * K K K K K K K K ? ? ? ? ? ? ? ? X X X X X X V V V V V V V V - - - - - - % % % % % % % % ) ) ) ) ) ) B B B B B B"6 "6 "6 "6 "6y "6 "6 "6J    Y   +?m +? +? +? +? +?\ 
6  
*+< * * * * 
* * *r*   