Skip to content

Commit 31a3039

Browse files
authored
add --single-turn and -p in conversation mode
1 parent 78efe60 commit 31a3039

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

examples/main/main.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,28 +276,28 @@ int main(int argc, char ** argv) {
276276
return formatted;
277277
};
278278

279+
std::string prompt;
279280
{
280-
const bool explicit_chat_template = params.enable_chat_template && (params.use_jinja || !params.chat_template.empty());
281-
std::string prompt = params.enable_chat_template ? params.system_prompt : "";
281+
if (params.conversation_mode && params.enable_chat_template) {
282+
prompt = params.system_prompt;
282283

283-
if (params.enable_chat_template && !prompt.empty()) {
284-
// format the system prompt (will use template default if not set)
285-
prompt = chat_add_and_format("system", prompt);
286-
}
284+
if (!prompt.empty()) {
285+
// format the system prompt (will use template default if empty)
286+
prompt = chat_add_and_format("system", prompt);
287+
}
287288

288-
if (!params.conversation_mode) {
289-
if (explicit_chat_template && !params.prompt.empty()) {
289+
if (!params.prompt.empty()) {
290290
// format and append the user prompt
291291
prompt += chat_add_and_format("user", params.prompt);
292292
} else {
293-
// otherwise use the prompt as is
294-
prompt = params.prompt;
293+
waiting_for_first_input = true;
295294
}
296-
} else if (prompt.empty()) {
297-
waiting_for_first_input = true;
295+
} else {
296+
// otherwise use the prompt as is
297+
prompt = params.prompt;
298298
}
299299

300-
if (params.interactive_first || !params.prompt.empty() || session_tokens.empty()) {
300+
if (params.interactive_first || !prompt.empty() || session_tokens.empty()) {
301301
LOG_DBG("tokenize the prompt\n");
302302
embd_inp = common_tokenize(ctx, prompt, true, true);
303303
} else {
@@ -378,6 +378,11 @@ int main(int argc, char ** argv) {
378378
params.interactive = true;
379379
}
380380

381+
if (params.single_turn && !params.prompt.empty()) {
382+
params.interactive = false;
383+
params.interactive_first = false;
384+
}
385+
381386
if (params.verbose_prompt) {
382387
LOG_INF("%s: prompt: '%s'\n", __func__, params.prompt.c_str());
383388
LOG_INF("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
@@ -814,6 +819,11 @@ int main(int argc, char ** argv) {
814819
if (params.conversation_mode && !waiting_for_first_input) {
815820
const auto id = common_sampler_last(smpl);
816821
assistant_ss << common_token_to_piece(ctx, id, false);
822+
823+
if (!prompt.empty()) {
824+
prompt.clear();
825+
is_interacting = false;
826+
}
817827
}
818828

819829
if ((n_past > 0 || waiting_for_first_input) && is_interacting) {
@@ -911,6 +921,11 @@ int main(int argc, char ** argv) {
911921
common_sampler_reset(smpl);
912922
}
913923
is_interacting = false;
924+
925+
if (waiting_for_first_input && params.single_turn) {
926+
params.interactive = false;
927+
params.interactive_first = false;
928+
}
914929
waiting_for_first_input = false;
915930
}
916931
}

0 commit comments

Comments
 (0)