forked from meta-llama/llama-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/support_more_data_format'
- Loading branch information
Showing
8 changed files
with
155 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
llama_stack/providers/inline/post_training/common/validator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
|
||
# Copyright (c) Meta Platforms, IAny, nc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
from llama_stack.apis.common.type_system import StringType | ||
from llama_stack.apis.datasets import Datasets | ||
from llama_stack.providers.utils.common.data_schema_validator import ( | ||
ColumnName, | ||
validate_dataset_schema, | ||
) | ||
|
||
EXPECTED_DATASET_SCHEMA = { | ||
"instruct": [ | ||
{ | ||
ColumnName.chat_completion_input.value: StringType(), | ||
ColumnName.expected_answer.value: StringType(), | ||
} | ||
], | ||
"dialog": [ | ||
{ | ||
ColumnName.dialog.value: StringType(), | ||
} | ||
], | ||
} | ||
|
||
|
||
async def validate_input_dataset_schema( | ||
datasets_api: Datasets, | ||
dataset_id: str, | ||
dataset_type: str, | ||
) -> None: | ||
dataset_def = await datasets_api.get_dataset(dataset_id=dataset_id) | ||
if not dataset_def.dataset_schema or len(dataset_def.dataset_schema) == 0: | ||
raise ValueError(f"Dataset {dataset_id} does not have a schema defined.") | ||
|
||
if dataset_type not in EXPECTED_DATASET_SCHEMA: | ||
raise ValueError(f"Dataset type {dataset_type} is not supported.") | ||
|
||
validate_dataset_schema( | ||
dataset_def.dataset_schema, EXPECTED_DATASET_SCHEMA[dataset_type] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
llama_stack/providers/inline/post_training/torchtune/datasets/format_adapter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
|
||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from typing import Any, Mapping | ||
|
||
from llama_stack.providers.utils.common.data_schema_validator import ColumnName | ||
|
||
|
||
def llama_stack_instruct_to_torchtune_instruct( | ||
sample: Mapping[str, Any] | ||
) -> Mapping[str, Any]: | ||
assert ( | ||
ColumnName.chat_completion_input.value in sample | ||
and ColumnName.expected_answer.value in sample | ||
), "Invalid input row" | ||
input_messages = eval(str(sample[ColumnName.chat_completion_input.value])) | ||
|
||
assert ( | ||
len(input_messages) == 1 | ||
), "llama stack intruct dataset format only supports 1 user message" | ||
input_message = input_messages[0] | ||
|
||
assert "content" in input_message, "content not found in input message" | ||
input = input_message["content"] | ||
output = sample[ColumnName.expected_answer.value] | ||
|
||
return { | ||
"input": input, | ||
"output": output, | ||
} | ||
|
||
|
||
def llama_stack_chat_to_torchtune_chat(sample: Mapping[str, Any]) -> Mapping[str, Any]: | ||
assert ColumnName.dialog.value in sample, "Invalid input row" | ||
role_map = {"user": "human", "assistant": "gpt"} | ||
dialog = eval(str(sample[ColumnName.dialog.value])) | ||
|
||
assert len(dialog) > 1, "dialog must have at least 2 messagse" | ||
roles = [] | ||
conversations = [] | ||
for message in dialog: | ||
assert ( | ||
"role" in message and "content" in message | ||
), "role and content must in message" | ||
roles.append(message["role"]) | ||
conversations.append( | ||
{"from": role_map[message["role"]], "value": message["content"]} | ||
) | ||
|
||
assert roles[0] == "user", "first message must be from user" | ||
assert "assistant" in roles, "at least 1 message should be from assistant" | ||
|
||
return {"conversations": conversations} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters