Skip to content

Commit

Permalink
Fix for option name and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vidyasiv committed Jul 24, 2024
1 parent e305964 commit b52231c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions examples/text-generation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,11 @@ A Transformers-like pipeline is defined and provided [here](https://github.com/h

## Conversation generation

For models that support chat like `CohereForAI/c4ai-command-r-v01` you can provide `--chat_template <JSON FILE>` that is applied to the tokenizer.
For models that support chat like `CohereForAI/c4ai-command-r-v01` you can provide `--conversation_input <JSON FILE>` that is applied to the tokenizer.

### Examples

Sample chat template `sample_command_r_template.json` for [CohereForAI/c4ai-command-r-v01](https://huggingface.co/CohereForAI/c4ai-command-r-v01) is shown below:
Sample conversation `sample_command_r_conversation.json` for [CohereForAI/c4ai-command-r-v01](https://huggingface.co/CohereForAI/c4ai-command-r-v01) is shown below:

```json
[{"role": "user", "content": "Hello, how are you?"}]
Expand All @@ -566,7 +566,7 @@ python run_generation.py \
--use_kv_cache \
--max_new_tokens 100 \
--do_sample \
--chat_template sample_command_r_template.json \
--conversation_input sample_command_r_conversation.json \
--bf16 \
--batch_size 2
```
12 changes: 6 additions & 6 deletions examples/text-generation/run_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def setup_parser(parser):
help="Whether to trust the execution of code from datasets/models defined on the Hub. This option should only be set to `True` for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine.",
)
parser.add_argument(
"--chat_template",
"--conversation_input",
default=None,
type=str,
help="Optional JSON input file containing chat template for tokenizer.",
Expand Down Expand Up @@ -374,16 +374,16 @@ def assemble_prompt(prompt_size, book_path):
"Peace is the only way",
]

# Apply tokenizer chat template if supported
if args.chat_template and hasattr(tokenizer, "chat_template"):
with open(args.chat_template, "r") as fh:
# Apply input as conversation if tokenizer has a chat template
if args.conversation_input and hasattr(tokenizer, "chat_template"):
with open(args.conversation_input, "r") as fh:
try:
messages = json.load(fh)
except json.JSONDecodeError as e:
logger.error(f"Error loading {args.chat_template}: {e}")
logger.error(f"Error loading {args.conversation_input}: {e}")
sys.exit()
try:
input_sentences = [tokenizer.apply_chat_template(messages, tokenize=False)]
input_sentences = [tokenizer.apply_chat_template(conversation=messages, tokenize=False)]
except Exception as e:
logger.error(f"Error applying chat template to tokenizer: {e}")
sys.exit()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_text_generation_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def _test_text_generation(
]

if "command_r" in model_name.lower():
path_to_template = os.path.join(path_to_example_dir, "text-generation/sample_command_r_template.json")
command += [f"--chat_template {path_to_template}"]
path_to_conv = os.path.join(path_to_example_dir, "text-generation/sample_command_r_conversation.json")
command += [f"--conversation_input {path_to_conv}"]

with TemporaryDirectory() as tmp_dir:
command.append(f"--output_dir {tmp_dir}")
Expand Down

0 comments on commit b52231c

Please sign in to comment.