a
    •˜ëjë  ã                   @   sÚ   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„ deƒZG dd„ deƒZeedœdd„Zedddd�eedœdd„ƒZdS )é    )ÚIterator)Ú
deprecated)ÚBaseChatModelÚBaseLanguageModel)ÚHumanMessageÚSystemMessage)ÚPydanticOutputFunctionsParser)ÚChatPromptTemplateÚHumanMessagePromptTemplate)ÚRunnable)Ú	BaseModelÚField)ÚLLMChain)Úget_llm_kwargsc                   @   sl   e Zd ZU dZeddd�Zeed< eddd�Ze	e ed< deee
ee d	œd
d„Zeee dœdd„ZdS )ÚFactWithEvidencezåClass 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_quoteéd   )ÚquoteÚcontextÚerrsÚreturnc           	      c   s|   dd l }|}|}d}| d|› d|› d�|¡}|d u rb||krb|d7 }| d|› d|› d�|¡}q.|d urx| ¡ E d H  d S )Nr   ú(z){e<=Ú}é   )ÚregexÚsearchZspans)	Úselfr   r   r   r   ÚminorÚmajorZerrs_Ús© r#   úu/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/openai_functions/citation_fuzzy_match.pyÚ	_get_span    s    zFactWithEvidence._get_span)r   r   c                 c   s"   | j D ]}|  ||¡E dH  qdS )züGet spans of the substring quote in the context.

        Args:
            context: The context in which to find the spans of the substring quote.

        Returns:
            An iterator over the spans of the substring quote in the context.
        N)r   r%   )r   r   r   r#   r#   r$   Ú	get_spans/   s    	
zFactWithEvidence.get_spansN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚstrÚ__annotations__r   ÚlistÚintr   r%   r&   r#   r#   r#   r$   r      s   
ýr   c                   @   s>   e Zd ZU dZeddd�Zeed< eddd�Ze	e
 ed< dS )	ÚQuestionAnswerz�A question and its answer as a list of facts.

    Each fact 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)   r*   r   r0   r+   r,   r1   r-   r   r#   r#   r#   r$   r/   <   s   
ýr/   )Úllmr   c                 C   s<   | j tj u rd}t|ƒ‚ttdƒt d¡gƒ}||  t¡B 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.zžAnswer question using the following context.

{context}

Question: {question}

Tips: Make sure to cite your sources, and use the exact words from the context.)	Z
bind_toolsr   Ú
ValueErrorr	   r   r
   Úfrom_templateZwith_structured_outputr/   )r2   ÚmsgÚpromptr#   r#   r$   Ú$create_citation_fuzzy_match_runnableM   s    ÿÿûÿr8   z0.2.13z1.0)ZsinceZremovalÚalternativec                 C   st   t td�}t ¡ }|d |d |dœ}t|ƒ}tdd�tdd�t d¡t d	¡td
d�g}t|d�}t	| |||d�S )zÂCreate 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.
    )Zpydantic_schemaÚtitler   )Únamer   Ú
parametersr3   )Ú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)r2   r7   Ú
llm_kwargsÚoutput_parser)
r   r/   Zmodel_json_schemar   r   r   r
   r5   r	   r   )r2   r@   ZschemaÚfunctionr?   r>   r7   r#   r#   r$   Ú!create_citation_fuzzy_match_chain{   s0    
ýþþö
ürB   N)Úcollections.abcr   Zlangchain_core._apir   Zlangchain_core.language_modelsr   r   Zlangchain_core.messagesr   r   Z.langchain_core.output_parsers.openai_functionsr   Zlangchain_core.prompts.chatr	   r
   Zlangchain_core.runnablesr   Zpydanticr   r   Zlangchain.chains.llmr   Z'langchain.chains.openai_functions.utilsr   r   r/   r8   rB   r#   r#   r#   r$   Ú<module>   s$   -.ý