Skip to content

Commit 2456837

Browse files
tjohnmanJohnman
andauthored
Support for multiple reverse prompts. (abetlen#299)
Co-authored-by: Johnman <> Co-authored-by: Johnman <tjohnman@github>
1 parent 7392f1c commit 2456837

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

main.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -855,14 +855,18 @@ int main(int argc, char ** argv) {
855855
// in instruct mode, we inject a prefix and a suffix to each input by the user
856856
if (params.instruct) {
857857
params.interactive = true;
858-
params.antiprompt = "### Instruction:\n\n";
858+
params.antiprompt.push_back("### Instruction:\n\n");
859859
}
860860

861861
// tokenize the reverse prompt
862-
std::vector<gpt_vocab::id> antiprompt_inp = ::llama_tokenize(vocab, params.antiprompt, false);
862+
std::vector<std::vector<gpt_vocab::id>> antipromptv_inp;
863+
864+
for (auto antiprompt : params.antiprompt) {
865+
antipromptv_inp.push_back(::llama_tokenize(vocab, antiprompt, false));
866+
}
863867

864868
// enable interactive mode if reverse prompt is specified
865-
if (!antiprompt_inp.empty()) {
869+
if (!antipromptv_inp.size()) {
866870
params.interactive = true;
867871
}
868872

@@ -886,13 +890,16 @@ int main(int argc, char ** argv) {
886890

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

889-
if (antiprompt_inp.size()) {
890-
fprintf(stderr, "%s: reverse prompt: '%s'\n", __func__, params.antiprompt.c_str());
891-
fprintf(stderr, "%s: number of tokens in reverse prompt = %zu\n", __func__, antiprompt_inp.size());
892-
for (int i = 0; i < (int) antiprompt_inp.size(); i++) {
893-
fprintf(stderr, "%6d -> '%s'\n", antiprompt_inp[i], vocab.id_to_token.at(antiprompt_inp[i]).c_str());
893+
if(antipromptv_inp.size()) {
894+
for (size_t apindex = 0; apindex < antipromptv_inp.size(); ++apindex) {
895+
auto antiprompt_inp = antipromptv_inp.at(apindex);
896+
fprintf(stderr, "%s: reverse prompt: '%s'\n", __func__, params.antiprompt.at(apindex).c_str());
897+
fprintf(stderr, "%s: number of tokens in reverse prompt = %zu\n", __func__, antiprompt_inp.size());
898+
for (int i = 0; i < (int) antiprompt_inp.size(); i++) {
899+
fprintf(stderr, "%6d -> '%s'\n", antiprompt_inp[i], vocab.id_to_token.at(antiprompt_inp[i]).c_str());
900+
}
901+
fprintf(stderr, "\n");
894902
}
895-
fprintf(stderr, "\n");
896903
}
897904
}
898905
fprintf(stderr, "sampling parameters: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n", params.temp, params.top_k, params.top_p, params.repeat_last_n, params.repeat_penalty);
@@ -1009,9 +1016,12 @@ int main(int argc, char ** argv) {
10091016
// check if we should prompt the user for more
10101017
if (params.interactive && embd_inp.size() <= input_consumed) {
10111018
// check for reverse prompt
1012-
if (antiprompt_inp.size() && std::equal(antiprompt_inp.rbegin(), antiprompt_inp.rend(), last_n_tokens.rbegin())) {
1013-
// reverse prompt found
1014-
is_interacting = true;
1019+
for (auto antiprompt_inp : antipromptv_inp) {
1020+
if (antiprompt_inp.size() && std::equal(antiprompt_inp.rbegin(), antiprompt_inp.rend(), last_n_tokens.rbegin())) {
1021+
// reverse prompt found
1022+
is_interacting = true;
1023+
break;
1024+
}
10151025
}
10161026
if (is_interacting) {
10171027
if (params.instruct) {

utils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
7070
} else if (arg == "--color") {
7171
params.use_color = true;
7272
} else if (arg == "-r" || arg == "--reverse-prompt") {
73-
params.antiprompt = argv[++i];
73+
params.antiprompt.push_back(argv[++i]);
7474
} else if (arg == "--ignore-eos") {
7575
params.ignore_eos = true;
7676
} else if (arg == "-h" || arg == "--help") {
@@ -96,7 +96,8 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
9696
fprintf(stderr, " -i, --interactive run in interactive mode\n");
9797
fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
9898
fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
99-
fprintf(stderr, " in interactive mode, poll user input upon seeing PROMPT\n");
99+
fprintf(stderr, " in interactive mode, poll user input upon seeing PROMPT (can be\n");
100+
fprintf(stderr, " specified more than once for multiple prompts).\n");
100101
fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
101102
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
102103
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);

utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ struct gpt_params {
3030

3131
std::string model = "models/lamma-7B/ggml-model.bin"; // model path
3232
std::string prompt = "";
33-
std::string antiprompt = ""; // string upon seeing which more user input is prompted
3433

3534
bool random_prompt = false;
3635

3736
bool use_color = false; // use color to distinguish generations and inputs
3837

3938
bool interactive = false; // interactive mode
39+
bool interactive_start = false; // reverse prompt immediately
40+
std::vector<std::string> antiprompt; // string upon seeing which more user input is prompted
4041
bool instruct = false; // instruction mode (used for Alpaca models)
41-
4242
bool ignore_eos = false; // do not stop generating after eos
4343
};
4444

0 commit comments

Comments
 (0)