Skip to content

Commit d5f56a5

Browse files
tjohnmanJohnmanggerganov
authored
Check for reverse prompt by characters instead of tokens (ggml-org#292) (ggml-org#330)
* Check for reverse prompt by characters instead of tokens (ggml-org#292) * Update main.cpp Wording. * Cleanup. * Remove unnecessary use of std::stringstream. --------- Co-authored-by: Johnman <tjohnman@github> Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 3bfa3b4 commit d5f56a5

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

main.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -885,15 +885,8 @@ int main(int argc, char ** argv) {
885885
params.antiprompt.push_back("### Instruction:\n\n");
886886
}
887887

888-
// tokenize the reverse prompt
889-
std::vector<std::vector<llama_vocab::id>> antipromptv_inp;
890-
891-
for (auto antiprompt : params.antiprompt) {
892-
antipromptv_inp.push_back(::llama_tokenize(vocab, antiprompt, false));
893-
}
894-
895888
// enable interactive mode if reverse prompt is specified
896-
if (antipromptv_inp.size() != 0) {
889+
if (params.antiprompt.size() != 0) {
897890
params.interactive = true;
898891
}
899892

@@ -917,15 +910,9 @@ int main(int argc, char ** argv) {
917910

918911
fprintf(stderr, "%s: interactive mode on.\n", __func__);
919912

920-
if(antipromptv_inp.size()) {
921-
for (size_t apindex = 0; apindex < antipromptv_inp.size(); ++apindex) {
922-
auto antiprompt_inp = antipromptv_inp.at(apindex);
923-
fprintf(stderr, "%s: reverse prompt: '%s'\n", __func__, params.antiprompt.at(apindex).c_str());
924-
fprintf(stderr, "%s: number of tokens in reverse prompt = %zu\n", __func__, antiprompt_inp.size());
925-
for (int i = 0; i < (int) antiprompt_inp.size(); i++) {
926-
fprintf(stderr, "%6d -> '%s'\n", antiprompt_inp[i], vocab.id_to_token.at(antiprompt_inp[i]).c_str());
927-
}
928-
fprintf(stderr, "\n");
913+
if(params.antiprompt.size()) {
914+
for (auto antiprompt : params.antiprompt) {
915+
fprintf(stderr, "Reverse prompt: '%s'\n", antiprompt.c_str());
929916
}
930917
}
931918
}
@@ -1042,9 +1029,14 @@ int main(int argc, char ** argv) {
10421029
// check if we should prompt the user for more
10431030
if (params.interactive && (int) embd_inp.size() <= input_consumed) {
10441031
// check for reverse prompt
1045-
for (auto antiprompt_inp : antipromptv_inp) {
1046-
if (antiprompt_inp.size() && std::equal(antiprompt_inp.rbegin(), antiprompt_inp.rend(), last_n_tokens.rbegin())) {
1047-
// reverse prompt found
1032+
std::string last_output;
1033+
for (auto id : last_n_tokens) {
1034+
last_output += vocab.id_to_token[id];
1035+
}
1036+
1037+
// Check if each of the reverse prompts appears at the end of the output.
1038+
for (std::string antiprompt : params.antiprompt) {
1039+
if (last_output.find(antiprompt.c_str(), last_output.length() - antiprompt.length(), antiprompt.length()) != std::string::npos) {
10481040
is_interacting = true;
10491041
break;
10501042
}

0 commit comments

Comments
 (0)