Repository: oumi-ai/oumi
File: src/oumi/datasets/sft/tulu3_sft_mixture.py (Python)

Code before:
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Union

import pandas as pd

from oumi.core.datasets import BaseSftDataset
from oumi.core.registry import register_dataset
from oumi.core.types.conversation import Conversation, Message, Role


@register_dataset("allenai/tulu-3-sft-mixture")
class Tulu3MixtureDataset(BaseSftDataset):
    default_dataset = "allenai/tulu-3-sft-mixture"

    def transform_conversation(self, example: Union[dict, pd.Series]) -> Conversation:
        """Convert the example into a Conversation.

        Args:
            example (dict or Pandas Series): An example containtn a `messages`
               field which is a list of dicts with `content` and `role` string
               fields
        """
        messages = [self._transform_message(msg) for msg in example["messages"]]
        return Conversation(messages=messages)

    @classmethod
    def _transform_message(cls, message: dict) -> Message:
        roles = {"system": Role.SYSTEM, "assistant": Role.ASSISTANT, "user": Role.USER}
        content = message["content"]
        try:
            role = roles[message["role"]]
            return Message(role=role, content=content)
        except KeyError as e:
            raise ValueError(f"Unknown role {e.args[0]}, was expecting one of {
                             roles.keys()}") from e


Review comment (suggestion):
```suggestion
            example (dict or Pandas Series): An example containing a `messages`
```

Code after:
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Union

import pandas as pd

from oumi.core.datasets import BaseSftDataset
from oumi.core.registry import register_dataset
from oumi.core.types.conversation import Conversation, Message, Role


@register_dataset("allenai/tulu-3-sft-mixture")
class Tulu3MixtureDataset(BaseSftDataset):
    default_dataset = "allenai/tulu-3-sft-mixture"

    def transform_conversation(self, example: Union[dict, pd.Series]) -> Conversation:
        """Convert the example into a Conversation.

        Args:
            example (dict or Pandas Series): An example containing a `messages`
               field which is a list of dicts with `content` and `role` string
               fields
        """
        messages = [self._transform_message(msg) for msg in example["messages"]]
        return Conversation(messages=messages)

    @classmethod
    def _transform_message(cls, message: dict) -> Message:
        roles = {"system": Role.SYSTEM, "assistant": Role.ASSISTANT, "user": Role.USER}
        content = message["content"]
        role_str = message["role"].lower().strip()
        try:
            role = roles[role_str]
            return Message(role=role, content=content)
        except KeyError as e:
            raise ValueError(
                f"Unknown role {message['role']}, "
                f"was expecting one of {roles.keys()}"
            ) from e