diff --git a/data/alpaca_data_zh_51k.json.REMOVED.git-id b/data/alpaca_data_zh_51k.json.REMOVED.git-id index 955f6959..f28dd6e9 100644 --- a/data/alpaca_data_zh_51k.json.REMOVED.git-id +++ b/data/alpaca_data_zh_51k.json.REMOVED.git-id @@ -1 +1 @@ -fc9a6a3458caca2af8dafc6181773fe10c6d8657 \ No newline at end of file +34c723573fbc2d7601f6d9c882ccf5aa4f9bcc4b \ No newline at end of file diff --git a/src/llmtuner/data/aligner.py b/src/llmtuner/data/aligner.py index b8149589..8144141c 100644 --- a/src/llmtuner/data/aligner.py +++ b/src/llmtuner/data/aligner.py @@ -15,7 +15,7 @@ def convert_alpaca(examples: Dict[str, List[Any]], dataset_attr: "DatasetAttr") outputs = {"prompt": [], "response": [], "system": [], "tools": []} for i in range(len(examples[dataset_attr.prompt])): prompt = [] - if dataset_attr.history: + if dataset_attr.history and isinstance(examples[dataset_attr.history][i], list): for old_prompt, old_response in examples[dataset_attr.history][i]: prompt.append({"role": Role.USER, "content": old_prompt}) prompt.append({"role": Role.ASSISTANT, "content": old_response}) @@ -25,13 +25,10 @@ def convert_alpaca(examples: Dict[str, List[Any]], dataset_attr: "DatasetAttr") instruction += "\n" + examples[dataset_attr.query][i] prompt.append({"role": Role.USER, "content": instruction}) - if dataset_attr.response: - if isinstance(examples[dataset_attr.response][i], list): - response = [ - {"role": Role.ASSISTANT, "content": content} for content in examples[dataset_attr.response][i] - ] - else: - response = [{"role": Role.ASSISTANT, "content": examples[dataset_attr.response][i]}] + if dataset_attr.response and isinstance(examples[dataset_attr.response][i], list): + response = [{"role": Role.ASSISTANT, "content": content} for content in examples[dataset_attr.response][i]] + elif dataset_attr.response and isinstance(examples[dataset_attr.response][i], str): + response = [{"role": Role.ASSISTANT, "content": examples[dataset_attr.response][i]}] else: response = [] diff --git a/src/llmtuner/data/formatter.py b/src/llmtuner/data/formatter.py index e29cb833..82dff8ca 100644 --- a/src/llmtuner/data/formatter.py +++ b/src/llmtuner/data/formatter.py @@ -15,10 +15,10 @@ JSON_FORMAT_PROMPT = ( TOOL_SYSTEM_PROMPT = ( "You have access to the following tools:\n{tool_text}" - "Use the following format to answer the question:\n" + "Use the following format if using a tool:\n" "```\n" - "Action: the action to take, should be one of [{tool_names}] if using a tool.\n" - "Action Input: the input to the action{format_prompt}.\n" + "Action: tool name (one of [{tool_names}]).\n" + "Action Input: the input to the tool{format_prompt}.\n" "```\n" ) @@ -95,12 +95,15 @@ class StringFormatter(Formatter): for slot in self.slots: if isinstance(slot, str): for name, value in kwargs.items(): + if not isinstance(value, str): + raise RuntimeError("Expected a string, got {}".format(value)) + slot = slot.replace("{{" + name + "}}", value, 1) elements.append(slot) elif isinstance(slot, (dict, set)): elements.append(slot) else: - raise ValueError("Input must be string, set[str] or dict[str, str], got {}".format(type(slot))) + raise RuntimeError("Input must be string, set[str] or dict[str, str], got {}".format(type(slot))) return elements @@ -124,7 +127,7 @@ class FunctionFormatter(Formatter): elif isinstance(slot, (dict, set)): elements.append(slot) else: - raise ValueError("Input must be string, set[str] or dict[str, str], got {}".format(type(slot))) + raise RuntimeError("Input must be string, set[str] or dict[str, str], got {}".format(type(slot))) return elements diff --git a/src/llmtuner/data/preprocess.py b/src/llmtuner/data/preprocess.py index c4f182a3..e5cd9489 100644 --- a/src/llmtuner/data/preprocess.py +++ b/src/llmtuner/data/preprocess.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Tuple from ..extras.constants import IGNORE_INDEX from ..extras.logging import get_logger +from .utils import Role if TYPE_CHECKING: @@ -51,7 +52,7 @@ def preprocess_supervised_dataset( model_inputs = {"input_ids": [], "attention_mask": [], "labels": []} for i in range(len(examples["prompt"])): - if len(examples["prompt"][i]) == 0 or len(examples["response"][i]) != 1: + if len(examples["prompt"][i]) % 2 != 1 or len(examples["response"][i]) != 1: continue messages = examples["prompt"][i] + examples["response"][i] @@ -93,7 +94,7 @@ def preprocess_packed_supervised_dataset( model_inputs = {"input_ids": [], "attention_mask": [], "labels": []} input_ids, labels = [], [] for i in range(len(examples["prompt"])): - if len(examples["prompt"][i]) == 0 or len(examples["response"][i]) != 1: + if len(examples["prompt"][i]) % 2 != 1 or len(examples["response"][i]) != 1: continue messages = examples["prompt"][i] + examples["response"][i] @@ -137,10 +138,14 @@ def preprocess_unsupervised_dataset( model_inputs = {"input_ids": [], "attention_mask": [], "labels": []} for i in range(len(examples["prompt"])): - if len(examples["prompt"][i]) == 0 or len(examples["response"][i]) != 1: + if len(examples["prompt"][i]) % 2 != 1: continue - messages = examples["prompt"][i] + examples["response"][i] + if len(examples["response"][i]) == 1: + messages = examples["prompt"][i] + examples["response"][i] + else: + messages = examples["prompt"][i] + [{"role": Role.ASSISTANT, "content": ""}] + input_ids, labels = template.encode_oneturn( tokenizer, messages, examples["system"][i], examples["tools"][i], data_args.cutoff_len ) @@ -164,7 +169,7 @@ def preprocess_pairwise_dataset( # build input pairs with format ` X`, `Y1 ` and `Y2 ` model_inputs = {"prompt_ids": [], "chosen_ids": [], "rejected_ids": []} for i in range(len(examples["prompt"])): - if len(examples["prompt"][i]) == 0 or len(examples["response"][i]) < 2: + if len(examples["prompt"][i]) % 2 != 1 or len(examples["response"][i]) < 2: continue chosen_messages = examples["prompt"][i] + [examples["response"][i][0]] diff --git a/src/llmtuner/hparams/parser.py b/src/llmtuner/hparams/parser.py index 0e434a1d..8b144626 100644 --- a/src/llmtuner/hparams/parser.py +++ b/src/llmtuner/hparams/parser.py @@ -60,7 +60,7 @@ def _verify_model_args(model_args: "ModelArguments", finetuning_args: "Finetunin if finetuning_args.finetuning_type != "lora": raise ValueError("Quantization is only compatible with the LoRA method.") - if finetuning_args.create_new_adapter: + if model_args.adapter_name_or_path is not None and finetuning_args.create_new_adapter: raise ValueError("Cannot create new adapter upon a quantized model.") if model_args.adapter_name_or_path is not None and len(model_args.adapter_name_or_path) != 1: