o
    ̳i                     @   s   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 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dZd	S )    )AnyCallableDictMappingOptional)Message)PreferenceDataset)	Transform)ModelTokenizerc                   @   sT   e Zd ZdZ	ddedeeeef  fddZde	ee
f d	e	ee
f fd
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                 C   s   || _ || _d S )N)r   _column_map)selfr   r    r   ]/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/datasets/_stack_exchange_paired.py__init__.   s   
z&StackExchangePairedToMessages.__init__samplereturnc                 C   s   | j pi }|dd}|dd}|dd}td|| | j dtd|| dg}td|| | j dtd|| 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_messagesr   r   r   __call__4   s   

z&StackExchangePairedToMessages.__call__)FN)__name__
__module____qualname____doc__boolr   r   strr   r   r   r#   r   r   r   r   r      s    
&r   zlvwerra/stack-exchange-pairedNFtrain)sourcer   r   	filter_fnsplit	tokenizerr+   r   r   r,   r-   load_dataset_kwargsr   c             	   K   s8   |pdddd}t ||d}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)r+   message_transformr.   r,   r-   data_dirNr   )r   r   )r.   r+   r   r   r,   r-   r/   r3   r   r   r   stack_exchange_paired_datasetK   s$   (r5   )typingr   r   r   r   r   torchtune.datar   torchtune.datasets._preferencer   torchtune.modules.transformsr	   'torchtune.modules.transforms.tokenizersr
   r   r)   r(   r5   r   r   r   r   <module>   s8   ?
	