Skip to content

Commit cbe462e

Browse files
committed
main : add option to save full output to session
1 parent 173d0e6 commit cbe462e

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

examples/common.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
125125
break;
126126
}
127127
params.path_session = argv[i];
128+
} else if (arg == "--session-full") {
129+
params.session_full = true;
128130
} else if (arg == "-f" || arg == "--file") {
129131
if (++i >= argc) {
130132
invalid_param = true;
@@ -366,6 +368,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
366368
fprintf(stderr, " prompt to start generation with (default: empty)\n");
367369
fprintf(stderr, " -e process prompt escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\)\n");
368370
fprintf(stderr, " --session FNAME file to cache model state in (may be large!) (default: none)\n");
371+
fprintf(stderr, " --session-full if specified, saves output to the session file in addition to prompt\n");
369372
fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
370373
fprintf(stderr, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
371374
fprintf(stderr, " --in-suffix STRING string to suffix after user inputs with (default: empty)\n");

examples/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct gpt_params {
5353
bool random_prompt = false; // do not randomize prompt if none provided
5454
bool use_color = false; // use color to distinguish generations and inputs
5555
bool interactive = false; // interactive mode
56+
bool session_full = false; // save the output to the session file in addition to prompt
5657

5758
bool embedding = false; // get only sentence embedding
5859
bool interactive_first = false; // wait for user input immediately

examples/main/main.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,9 @@ int main(int argc, char ** argv) {
284284
is_interacting = params.interactive_first;
285285
}
286286

287-
bool is_antiprompt = false;
288-
bool input_echo = true;
289-
290-
// HACK - because session saving incurs a non-negligible delay, for now skip re-saving session
291-
// if we loaded a session with at least 75% similarity. It's currently just used to speed up the
292-
// initial prompt so it doesn't need to be an exact match.
293-
bool need_to_save_session = !path_session.empty() && n_matching_session_tokens < (embd_inp.size() * 3 / 4);
287+
bool is_antiprompt = false;
288+
bool input_echo = true;
289+
bool need_to_save_session = !path_session.empty();
294290

295291

296292
int n_past = 0;
@@ -319,6 +315,10 @@ int main(int argc, char ** argv) {
319315
embd.insert(embd.begin(), last_n_tokens.begin() + n_ctx - n_left/2 - embd.size(), last_n_tokens.end() - embd.size());
320316

321317
// stop saving session if we run out of context
318+
if (!path_session.empty() && params.session_full) {
319+
llama_save_session_file(ctx, path_session.c_str(),
320+
session_tokens.data(), session_tokens.size());
321+
}
322322
path_session = "";
323323

324324
//printf("\n---\n");
@@ -619,6 +619,11 @@ int main(int argc, char ** argv) {
619619
}
620620
}
621621

622+
if (!path_session.empty() && params.session_full) {
623+
fprintf(stderr, "\n%s: saving final output to session file '%s'\n", __func__, path_session.c_str());
624+
llama_save_session_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.size());
625+
}
626+
622627
llama_print_timings(ctx);
623628
llama_free(ctx);
624629

0 commit comments

Comments
 (0)