Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conversation_style of openai format (OpenAI API style) #890

Merged
merged 13 commits into from
Apr 28, 2024
6 changes: 5 additions & 1 deletion torchtune/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
MistralChatFormat,
)
from torchtune.data._common import CROSS_ENTROPY_IGNORE_IDX
from torchtune.data._converters import sharegpt_to_llama2_messages
from torchtune.data._converters import (
sharegpt_to_llama2_messages,
standard_chat_to_llama2_messages
)
from torchtune.data._instruct_templates import (
AlpacaInstructTemplate,
GrammarErrorCorrectionTemplate,
Expand All @@ -33,6 +36,7 @@
"MistralChatFormat",
"ChatMLFormat",
"sharegpt_to_llama2_messages",
"standard_chat_to_llama2_messages",
"truncate",
"Message",
"validate_messages",
Expand Down
56 changes: 56 additions & 0 deletions torchtune/data/_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,59 @@ def sharegpt_to_llama2_messages(
masked = (role != "assistant") and (not train_on_input)
messages.append(Message(role=role, content=content, masked=masked))
return messages


def standard_chat_to_llama2_messages(
xingyaoww marked this conversation as resolved.
Show resolved Hide resolved
sample: Mapping[str, Any],
train_on_input: bool = False,
) -> List[Message]:
"""
Convert a chat sample adhering to the OpenAI API standard chat format to the Llama2 chat format.
xingyaoww marked this conversation as resolved.
Show resolved Hide resolved

OpenAI API standard chat format follows::
xingyaoww marked this conversation as resolved.
Show resolved Hide resolved

{
# key could be "messages" OR "conversations"
"messages": [
{
"role": <system|user|assistant>,
"content": <message>,
},
...
]
}

Llama2 follows::

[
{
"role": <system|user|assistant>,
"content": <message>,
},
...
]

Args:
sample (Mapping[str, Any]): a single data sample with "conversations" field pointing
to a list of dict messages.
train_on_input (bool): whether the prompt should remain unmasked. Default: False
messages_key (str): the key in the sample that contains the messages. Default: "messages"

Returns:
xingyaoww marked this conversation as resolved.
Show resolved Hide resolved
List[Message]: A list of messages with "role" and "content" fields.
"""
if "messages" in sample:
messages_key = "messages"
elif "conversations" in sample:
messages_key = "conversations"
else:
raise ValueError(f"Sample does not contain 'messages' or 'conversations' key. Existing keys: {sample.keys()}")
conversations = sample[messages_key]

messages = []
for message in conversations:
role = message["role"]
xingyaoww marked this conversation as resolved.
Show resolved Hide resolved
content = message["content"]
masked = (role != "assistant") and (not train_on_input)
messages.append(Message(role=role, content=content, masked=masked))
return messages
3 changes: 3 additions & 0 deletions torchtune/datasets/_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CROSS_ENTROPY_IGNORE_IDX,
Message,
sharegpt_to_llama2_messages,
standard_chat_to_llama2_messages,
validate_messages,
)
from torchtune.modules.tokenizers import Tokenizer
Expand Down Expand Up @@ -159,6 +160,8 @@ def chat_dataset(
"""
if conversation_style == "sharegpt":
convert_to_messages = sharegpt_to_llama2_messages
elif conversation_style == "standard_chat":
convert_to_messages = standard_chat_to_llama2_messages
else:
raise ValueError(f"Unsupported conversation style: {conversation_style}")

Expand Down