Skip to content

Commit a285e3c

Browse files
committed
main : don't print special tokens with --grammar
The CLI interface was recently changed to print special control tokens like the </s> stop message one. This token shouldn't be printed if the grammar flag was passed, unless the grammar specifies it, because that breaks shell-scriptability.
1 parent 917dc8c commit a285e3c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

examples/main/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ int main(int argc, char ** argv) {
527527
fprintf(stderr, "%s: failed to initialize sampling subsystem\n", __func__);
528528
exit(1);
529529
}
530+
bool should_show_special_tokens = sparams.grammar.empty();
530531

531532
while ((n_remain != 0 && !is_antiprompt) || params.interactive) {
532533
// predict
@@ -740,7 +741,8 @@ int main(int argc, char ** argv) {
740741
// display text
741742
if (input_echo && display) {
742743
for (auto id : embd) {
743-
const std::string token_str = llama_token_to_piece(ctx, id, !params.conversation);
744+
const std::string token_str =
745+
llama_token_to_piece(ctx, id, should_show_special_tokens);
744746
printf("%s", token_str.c_str());
745747

746748
if (embd.size() > 1) {
@@ -906,7 +908,7 @@ int main(int argc, char ** argv) {
906908
for (size_t i = original_size; i < embd_inp.size(); ++i) {
907909
const llama_token token = embd_inp[i];
908910
output_tokens.push_back(token);
909-
output_ss << llama_token_to_piece(ctx, token);
911+
output_ss << llama_token_to_piece(ctx, token, should_show_special_tokens);
910912
}
911913

912914
n_remain -= line_inp.size();

0 commit comments

Comments
 (0)