
    Pi&                     f   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l
mZmZ eeeeeeef         f         f         Z G d de          Z G d de          Z G d	 d
e          Z eeddi          Zde_         eeddi          Zde_         eeddi          Zde_        dedefdZdS )    )partial)DictListProtocolTupleUnion)_get_component_from_path)MessageRolec                   n    e Zd ZU dZeeeeef         f         ed<   	 d	de	e
         dede	e
         fdZdS )
PromptTemplateInterfacez
    Interface for prompt templates. Each prompt template can include structured
    text for system, user, and assistant roles that are prepended or appended to
    the message content.
    templateFmessages	inferencereturnc                     dS )a  
        Format each role's message(s) according to the prompt template

        Args:
            messages (List[Message]): a single conversation, structured as a list
                of :class:`~torchtune.data.Message` objects
            inference (bool): Whether the template is being used for inference or not.

        Returns:
            The formatted list of messages
        N )selfr   r   s      t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchtune/data/_prompt_templates.py__call__z PromptTemplateInterface.__call__   s	      	    NF)__name__
__module____qualname____doc__r   r   r   str__annotations__r   r
   boolr   r   r   r   r   r      s           4sCx())))
   w-  
g	     r   r   c                   n    e Zd ZdZdeeeeef         f         fdZ	 d
de	e
         dede	e
         fdZd	S )PromptTemplateaI  
    Quickly define a custom prompt template by passing in a dictionary mapping role to
    the prepend and append tags. For example, to achieve the following prompt
    template::

        System: {content}\n
        User: {content}\n
        Assistant: {content}\n
        Tool: {content}\n

    You need to pass in a tuple for each role, where ``PREPEND_TAG`` is the string
    added before the text content and ``APPEND_TAG`` is the string added after::

        template = {role: (PREPEND_TAG, APPEND_TAG)}

    Thus, the template would be defined as follows::

        template = {
            "system": ("System: ", "\n"),
            "user": ("User: ", "\n"),
            "assistant": ("Assistant: ", "\n"),
            "ipython": ("Tool: ", "\n"),
        }

    Once instantiated, you must call the prompt template on a list of messages. It
    will return the same list of messages updated with the template.

    Note:
        Any tags prepended/appended to the assistant message will be included
        in the loss calculation. All other prepend/append tags for other roles
        (system, user, ipython) are, in most cases, not included in loss. Consider using
        the append tags for user messages for tags that need to come before the
        assistant message but should not be included in loss. For more custom masking
        and prompt templating, you can create your own class based off the
        :class:`~torchtune.data.PromptTemplate` interface.

    Args:
        template (Dict[Role, Tuple[str, str]]): a dictionary mapping role to the
            prepend and append tags
    r   c                     || _         d S )Nr   )r   r   s     r   __init__zPromptTemplate.__init__X   s     !r   Fr   r   r   c           
         g }|D ]}|j         }|j        | j        v r| j        |j                 d         }| j        |j                 d         }|j         }t          |t                    rt          |          dk    r	d|dg|z   }t          |t                    rt          |          dk    r	|d|dgz   }|                    t          |j        ||j        |j	        |j
                             |S )a  
        Format each role's message(s) according to the prompt template by prepending
        and appending the defined tags.

        Args:
            messages (List[Message]): list of messages to apply the template to
            inference (bool): Whether the template is being used for inference or not.

        Returns:
            List[Message]: The formatted list of messages
        r      texttypecontentroler*   maskedipythoneot)r*   r,   r   
isinstancer   lenappendr
   r-   r.   r/   )r   r   r   formatted_dialoguemessager*   prepend_tag
append_tags           r   r   zPromptTemplate.__call__^   s      	 	GoG|t},,"mGL9!<!]7<8;
!/k3// SC4D4Dq4H4H(.;GGH7RGj#.. R3z??Q3F3F%&Z)P)P(QQG%% #">#O      "!r   Nr   )r   r   r   r   r   r   r   r   r$   r   r
   r   r   r   r   r   r!   r!   .   s        ' 'R!tU38_,-! ! ! ! :?$" $"W$"26$"	g$" $" $" $" $" $"r   r!   c                   P    e Zd ZdZdddddZ	 ddee         d	ed
ee         fdZdS )ChatMLTemplateuE  
    OpenAI's `Chat Markup Language
    <https://github.com/MicrosoftDocs/azure-docs/blob/772c14eeabfa0c0c561d5c2d34ef19341f528b7b/articles/ai-services/openai/how-to/chat-markup-language.md>`_
    used by their chat models.

    It is the default chat template used by Hugging Face models.

    .. code-block:: text

        <|im_start|>system
        Provide some context and/or instructions to the model.<|im_end|>
        <|im_start|>user
        The user’s message goes here<|im_end|>
        <|im_start|>assistant
        The assistant’s response goes here<|im_end|>

    )z<|im_start|>system
<|im_end|>
)z<|im_start|>user
r9   )z<|im_start|>assistant
r9   ) r:   )systemuser	assistantr.   Fr   r   r   c           
         g }t          |          D ]Q\  }}| j        |j                 d         }| j        |j                 d         }|j        dk    rl|t          |          dz
  k    rVt          |j                  dk    r>|j        }t          |t                    r!t          |          dk    rd|dg|j        z   }ni|j        }t          |t                    rt          |          dk    r	d|dg|z   }t          |t                    rt          |          dk    r	|d|dgz   }|                    t          |j        ||j
        |j        |j                             S|S )an  
        Format user, assistant, and system messages with appropriate tags.

        Args:
            messages (List[Message]): a single conversation, structured as a list
                of `Message` objects
            inference (bool): Whether the template is being used for inference or not.

        Returns:
            The formatted list of messages
        r   r&   r=   r'   r(   r+   )	enumerater   r,   r1   text_contentr*   r0   r   r2   r
   r-   r.   r/   )	r   r   r   r3   indexr4   r5   r6   r*   s	            r   r   zChatMLTemplate.__call__   s      '11 !	 !	NE7-5a8Kw|4Q7J
 ++S]]Q...,--22!/k3// (C4D4Dq4H4H!'K@@(G "/k3// SC4D4Dq4H4H(.;GGH7RGj#.. R3z??Q3F3F%&Z)P)P(QQG%% #">#O      "!r   Nr   )	r   r   r   r   r   r   r
   r   r   r   r   r   r8   r8      sz         & ;6@	 H  3" 3"w-3" 3" 
g	3" 3" 3" 3" 3" 3"r   r8   r<   )z"Correct this to standard English: z
---
Corrected: r#   z
A prompt template for grammar error correction tasks::

    Correct this to standard English: {user_message}
    ---
    Corrected: {assistant_message}

Please see :class:`~torchtune.data.PromptTemplate` for full API arguments.
)zSummarize this dialogue:
z
---
Summary:
z
A prompt template for summarization tasks::

    Summarize this dialogue:
    {user_message}
    ---
    Summary:
    {assistant_message}

Please see :class:`~torchtune.data.PromptTemplate` for full API arguments.
)z
Question: z


Answer: z
A prompt template for question answering tasks::

    Question: {user_message}

    Answer: {assistant_message}

Please see :class:`~torchtune.data.PromptTemplate` for full API arguments.
prompt_templater   c                     t          | t                    r t          |                       S t          | t                    rt	          |           S t          dt          |                      )aZ  
    Retrieve prompt template from import dotpath or create a custom one with provided
    template dictionary.

    Args:
        prompt_template (_TemplateType): optional specified prompt template.
            If a string, it is assumed to be the dotpath of a :class:`~torchtune.data.PromptTemplateInterface`
            class. If a dictionary, it is assumed to be a custom prompt template mapping role to the
            prepend/append tags.

    Returns:
        PromptTemplateInterface: the specified prompt template

    Raises:
        ValueError: If a string or dictionary is not passed in
    zQPrompt template must be a dotpath string or dictionary with custom template, got )r0   r   r	   dictr!   
ValueErrorr)   )rB   s    r   _get_prompt_templaterF     st    & /3'' 
8'88:::	OT	*	* 
o...w`det`u`uww
 
 	
r   N)	functoolsr   typingr   r   r   r   r   torchtune.config._utilsr	   torchtune.data._messagesr
   r   r   _TemplateTyper   r!   r8   GrammarErrorCorrectionTemplater   SummarizeTemplateQuestionAnswerTemplaterF   r   r   r   <module>rO      s         5 5 5 5 5 5 5 5 5 5 5 5 5 5 < < < < < < 2 2 2 2 2 2 2 2c4eCHo 5667    h   <T" T" T" T" T", T" T" T"nM" M" M" M" M", M" M" M"` ")L" " " *  & GC   
   !.   "  
"

 
 
 
 
 
r   