Skip to content

Commit 2edbdb0

Browse files
authored
main : add --in-suffix option (ggml-org#1318)
* adding --in-suffix option * print input suffix before generation
1 parent 20fbf2a commit 2edbdb0

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

examples/common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
324324
break;
325325
}
326326
params.input_prefix = argv[i];
327+
} else if (arg == "--in-suffix") {
328+
if (++i >= argc) {
329+
invalid_param = true;
330+
break;
331+
}
332+
params.input_suffix = argv[i];
327333
} else {
328334
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
329335
gpt_print_usage(argc, argv, default_params);
@@ -362,6 +368,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
362368
fprintf(stderr, " --session FNAME file to cache model state in (may be large!) (default: none)\n");
363369
fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
364370
fprintf(stderr, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
371+
fprintf(stderr, " --in-suffix STRING string to suffix after user inputs with (default: empty)\n");
365372
fprintf(stderr, " -f FNAME, --file FNAME\n");
366373
fprintf(stderr, " prompt file to start generation.\n");
367374
fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d, -1 = infinity)\n", params.n_predict);

examples/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct gpt_params {
4343
std::string prompt = "";
4444
std::string path_session = ""; // path to file for saving/loading model eval state
4545
std::string input_prefix = ""; // string to prefix user inputs with
46+
std::string input_suffix = ""; // string to suffix user inputs with
4647
std::vector<std::string> antiprompt; // string upon seeing which more user input is prompted
4748

4849
std::string lora_adapter = ""; // lora adapter path

examples/main/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ The `--in-prefix` flag is used to add a prefix to your input, primarily, this is
112112
./main -r "User:" --in-prefix " "
113113
```
114114

115+
### In-Suffix
116+
117+
The `--in-suffix` flag is used to add a suffix after your input. This is useful for adding an "Assistant:" prompt after the user's input. It's added after the new-line character (`\n`) that's automatically added to the end of the user's input. Here's an example of how to use the `--in-suffix` flag in conjunction with the `--reverse-prompt` flag:
118+
119+
```sh
120+
./main -r "User:" --in-prefix " " --in-suffix "Assistant:"
121+
```
122+
115123
### Instruction Mode
116124

117125
Instruction mode is particularly useful when working with Alpaca models, which are designed to follow user instructions for specific tasks:

examples/main/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ int main(int argc, char ** argv) {
260260
if (!params.input_prefix.empty()) {
261261
fprintf(stderr, "Input prefix: '%s'\n", params.input_prefix.c_str());
262262
}
263+
264+
if (!params.input_suffix.empty()) {
265+
fprintf(stderr, "Input suffix: '%s'\n", params.input_suffix.c_str());
266+
}
263267
}
264268
fprintf(stderr, "sampling: repeat_last_n = %d, repeat_penalty = %f, presence_penalty = %f, frequency_penalty = %f, top_k = %d, tfs_z = %f, top_p = %f, typical_p = %f, temp = %f, mirostat = %d, mirostat_lr = %f, mirostat_ent = %f\n",
265269
params.repeat_last_n, params.repeat_penalty, params.presence_penalty, params.frequency_penalty, params.top_k, params.tfs_z, params.top_p, params.typical_p, params.temp, params.mirostat, params.mirostat_eta, params.mirostat_tau);
@@ -567,6 +571,11 @@ int main(int argc, char ** argv) {
567571
// Add tokens to embd only if the input buffer is non-empty
568572
// Entering a empty line lets the user pass control back
569573
if (buffer.length() > 1) {
574+
// append input suffix if any
575+
if (!params.input_suffix.empty()) {
576+
buffer += params.input_suffix;
577+
printf("%s", params.input_suffix.c_str());
578+
}
570579

571580
// instruct mode: insert instruction prefix
572581
if (params.instruct && !is_antiprompt) {

0 commit comments

Comments
 (0)