§
    ÇPƒiš  ã                   óÜ   — d dl 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  G d„ de¦  «        Zdd	d
d	ddœdededeeeef                  dedee         dedeeef         de	fd„Zd	S )é    )ÚAnyÚCallableÚDictÚMappingÚOptional)ÚMessage)ÚPreferenceDataset)Ú	Transform)ÚModelTokenizerc                   ór   — e Zd ZdZ	 d
dedeeeef                  fd„Zde	ee
f         de	ee
f         fd	„ZdS )ÚStackExchangePairedToMessagesaj  
    Transform for converting datasets similar to the format in `Stack Exchange Paired dataset
    <https://huggingface.co/datasets/lvwerra/stack-exchange-paired>`_::

        |  prompt  |  chosen  |  rejected  |
        |----------|----------|------------|
        |  Q1      |  A1      |  A2        |

    into a list of chosen and rejected messages:

    .. code-block:: python

        chosen = [
            Message(role="user", content="Q1"),
            Message(role="assistant", content="A1"),
        ]
        rejected = [
            Message(role="user", content="Q1"),
            Message(role="assistant", content="A2"),
        ]

    Args:
        train_on_input (bool): Whether the model is trained on the user prompt or not.
            Default is False.
        column_map (Optional[Dict[str, str]]): a mapping to change the expected "prompt",
            "chosen", and "rejected" column names to the actual column names in the dataset.
            Keys should be "prompt", "chosen", and "rejected" and values should be the actual column names.
            Default is None, keeping the default column names.
    FNÚtrain_on_inputÚ
column_mapc                 ó"   — || _         || _        d S )N)r   Ú_column_map)Úselfr   r   s      ú}/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchtune/datasets/_stack_exchange_paired.pyÚ__init__z&StackExchangePairedToMessages.__init__.   s   € ð -ˆÔØ%ˆÔÐÐó    ÚsampleÚreturnc                 óv  — | j         pi }|                     dd¦  «        }|                     dd¦  «        }|                     dd¦  «        }t          d||         | j         ¬¦  «        t          d||         ¬¦  «        g}t          d||         | j         ¬¦  «        t          d||         ¬¦  «        g}||dœS )	NÚpromptÚchosenÚrejectedÚuser)ÚroleÚcontentÚmaskedÚ	assistant)r   r   )r   r   )r   Úgetr   r   )r   r   r   Ú
key_promptÚ
key_chosenÚkey_rejectedÚchosen_messagesÚrejected_messagess           r   Ú__call__z&StackExchangePairedToMessages.__call__4   sâ   € ØÔ%Ð+¨ˆ
Ø—^’^ H¨hÑ7Ô7ˆ
Ø—^’^ H¨hÑ7Ô7ˆ
Ø!—~’~ j°*Ñ=Ô=ˆõ Ø V¨JÔ%7ÀDÔDWÐ@Wðñ ô õ ˜¨f°ZÔ.@ÐAÑAÔAð	
ˆõ Ø V¨JÔ%7ÀDÔDWÐ@Wðñ ô õ ˜¨f°\Ô.BÐCÑCÔCð	
Ðð *Ð7HÐIÐIÐIr   )FN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úboolr   r   Ústrr   r   r   r'   © r   r   r   r      s—   € € € € € ðð ð> TXð&ð &Ø"ð&Ø8@ÀÀcÈ3ÀhÄÔ8Pð&ð &ð &ð &ðJ˜w s¨C xÔ0ð J°W¸SÀ#¸XÔ5Fð Jð Jð Jð Jð Jð Jr   r   zlvwerra/stack-exchange-pairedNFÚtrain)Úsourcer   r   Ú	filter_fnÚsplitÚ	tokenizerr0   r   r   r1   r2   Úload_dataset_kwargsr   c          
      óZ   — |pddddœ}t          ||¬¦  «        }t          d||| ||ddœ|¤ŽS )	aµ  
    Family of preference datasets similar to the `Stack Exchange Paired dataset
    <https://huggingface.co/datasets/lvwerra/stack-exchange-paired>`_.

    It is recommended to configure the tokenizer with the :class:`~torchtune.data.QuestionAnswerTemplate`
    in conjunction with this dataset.

    Args:
        tokenizer (ModelTokenizer): Tokenizer used by the model that implements the ``tokenize_messages`` method.
        source (str): path to dataset repository on Hugging Face. For local datasets,
            define source as the data file type (e.g. "json", "csv", "text") and pass
            in the filepath in ``data_files``. See `Hugging Face's
            <https://huggingface.co/docs/datasets/en/package_reference/loading_methods#datasets.load_dataset.path>`_
            ``load_dataset`` for more details. Default is ``lvwerra/stack-exchange-paired``.
        column_map (Optional[Dict[str, str]]): a mapping to change the expected "prompt",
            "chosen", and "rejected" column names to the actual column names in the dataset.
            Keys should be "prompt", "chosen", and "rejected" and values should be the actual column names.
            Default is None, keeping the default column names.
        train_on_input (bool): Whether the model is trained on the prompt or not. Default is False.
        filter_fn (Optional[Callable]): callable used to filter the dataset prior to any pre-processing. See
            the Hugging Face `docs <https://huggingface.co/docs/datasets/v2.20.0/process#select-and-filter>`_ for more
            details.
        split (str): ``split`` argument for ``datasets.load_dataset``. You can use this argument to load a subset
            of a given split, e.g. ``split="train[:10%]"``. Default is "train".
        **load_dataset_kwargs (Dict[str, Any]): additional keyword arguments to pass to ``load_dataset``.

    Returns:
        PreferenceDataset: The preference dataset built from source paired data.
    ÚquestionÚ
response_jÚ
response_k)r   r   r   )r   r   zdata/rl)r0   Úmessage_transformr3   r1   r2   Údata_dirr.   )r   r	   )r3   r0   r   r   r1   r2   r4   r9   s           r   Ústack_exchange_paired_datasetr;   K   sx   € ðP ð ØØØ ð ð  €Jõ 6Ø%°*ðñ ô Ðõ ð ØØ+ØØØØðð ð ðð ð r   )Útypingr   r   r   r   r   Útorchtune.datar   Útorchtune.datasets._preferencer	   Útorchtune.modules.transformsr
   Ú'torchtune.modules.transforms.tokenizersr   r   r-   r,   r;   r.   r   r   ú<module>rA      sM  ðð :Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9à "Ð "Ð "Ð "Ð "Ð "Ø <Ð <Ð <Ð <Ð <Ð <Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø BÐ BÐ BÐ BÐ BÐ Bð9Jð 9Jð 9Jð 9Jð 9J Iñ 9Jô 9Jð 9Jð~ 2Ø+/Ø Ø$(Øð:ð :ð :Øð:ð ð:ð ˜˜c 3˜hœÔ(ð	:ð
 ð:ð ˜Ô!ð:ð ð:ð    S œ>ð:ð ð:ð :ð :ð :ð :ð :r   