llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Stop keywords

Open petergeneric opened this issue 1 year ago • 12 comments

It'd be useful if there was a way to define tokens that would cause the output to stop prematurely (e.g. for an assistant-style interaction where messages are prefixed with "Assistant: ", "Human: ", you'd set "Human: " as a stop word, so that you could stop the model from continuing on and having a conversation with itself

petergeneric avatar Mar 12 '23 18:03 petergeneric

I believe there already are stop keywords. At least some of my responses end with [end of text] before the character limit.

jminardi avatar Mar 12 '23 21:03 jminardi

Yeah, it would just be useful to have more control over that in cases where the model itself doesn't want to stop

petergeneric avatar Mar 12 '23 21:03 petergeneric

Yes, seconding this. It's sometimes very important to set a name prefix or even a newline character as the stop keyword.

DavidCWGA avatar Mar 12 '23 21:03 DavidCWGA

The [end of text] output corresponds to a special token (number 2) in the LLaMa embedding. As for stopping on other token strings, the "reverse prompt" parameter does that in interactive mode now, with exactly the opening post's use case in mind. Is there a use case for something like it in non-interactive mode?

blackhole89 avatar Mar 13 '23 00:03 blackhole89

It could be useful for cases where you want to pull structured data out of the model (for example, asking for a city’s population, then reading tokens up until the next whitespace to get the number out).

j-f1 avatar Mar 13 '23 00:03 j-f1

These stop keywords would have to be recorded in token space, and at each token generated a check for possible match made. Seems like the right way to do that would be state machine.

there may be other uses down the line where a callback is called every time a match is made, which could be useful for implementing "actions", although may be outside of the scope here idk

Foundation42 avatar Mar 13 '23 01:03 Foundation42

It is absolutely useful in non-interactive mode. In any "conversation"-style input it prevents the model from talking to itself. To really make this useful you would need a switch that would stop the program re-printing the prompt, and only printing the new generated output.

DavidCWGA avatar Mar 13 '23 02:03 DavidCWGA

[end of text] is 5 tokens.

   518 -> ' ['
   355 -> 'end'
   310 -> ' of'
  1426 -> ' text'
 29962 -> ']'

I looked in the vocab file to see if there are any uncommon long tokens that would be cheaper stop tokens and I found arquitect to be a single token that I don't expect to show up in the dialogue.

28827 -> ' arquitect'

KevinColemanInc avatar Mar 26 '23 05:03 KevinColemanInc

[end of text] is actually a single token (sometimes represented as </s> but llama.cpp translates it as the empty string by default) that we have special behavior for.

j-f1 avatar Mar 26 '23 12:03 j-f1

@j-f1 Why does my llama.cpp logs show 5 tokens (see above)? I am using the stop-keywords code.

KevinColemanInc avatar Mar 26 '23 16:03 KevinColemanInc

That’s because you’re trying to tokenize that literal string — if you search in the source code for "[end of text]" you’ll see where it gets printed out.

j-f1 avatar Mar 26 '23 16:03 j-f1

Ah, i see. I guess this https://github.com/ggerganov/llama.cpp/pull/365 doesn't work, because you can't encode the stop token as a string literal. So you have to use another set of tokens, which doesn't always work.

KevinColemanInc avatar Mar 26 '23 17:03 KevinColemanInc

Should this be considered resolved by https://github.com/ggerganov/llama.cpp/pull/1032 ? The chain of closed-in-favor-of lead me there, but it doesn't actually refer back to this issue.

dwillie avatar Jun 12 '23 14:06 dwillie

Seems reasonable to me.

ejones avatar Jun 12 '23 14:06 ejones

Was surprised that there is no such setting by default in the --help, is it implemented or is considered out of scope for this project?

Bec-k avatar Nov 22 '23 10:11 Bec-k

@Bec-k can you elaborate on what you think is not implemented?

Green-Sky avatar Nov 22 '23 16:11 Green-Sky

Stop token, generative model should be stopped when generation encounters stop token. I haven't found that in the cli. I suppose you have it built-in for each supported model.

Bec-k avatar Nov 27 '23 08:11 Bec-k

This is the -r option at the command line.

-r,    --reverse-prompt PROMPT  halt generation at PROMPT, return control in interactive mode`

talwrii avatar Jun 11 '24 15:06 talwrii

if I do: -r "<|im_end|>" it does not work and continues.

0wwafa avatar Jul 13 '24 14:07 0wwafa