3030#pragma warning(disable: 4244 4267) // possible loss of data
3131#endif
3232
33+ static const char * DEFAULT_SYSTEM_MESSAGE = " You are a helpful assistant" ;
34+
3335static llama_context ** g_ctx;
3436static llama_model ** g_model;
3537static common_sampler ** g_smpl;
@@ -204,8 +206,17 @@ int main(int argc, char ** argv) {
204206 LOG_WRN (" %s: model was trained on only %d context tokens (%d specified)\n " , __func__, n_ctx_train, n_ctx);
205207 }
206208
209+ // auto enable conversation mode if chat template is available
210+ if (
211+ params.conversation_mode == COMMON_CONVERSATION_MODE_AUTO
212+ && (!common_get_builtin_chat_template (model).empty () || !params.chat_template .empty ())
213+ ) {
214+ LOG_INF (" %s: chat template is available, enabling conversation mode (disable it with -no-cnv)\n " , __func__);
215+ params.conversation_mode = COMMON_CONVERSATION_MODE_ENABLED;
216+ }
217+
207218 // print chat template example in conversation mode
208- if (params.conversation ) {
219+ if (params.conversation_mode ) {
209220 if (params.enable_chat_template ) {
210221 LOG_INF (" %s: chat template example:\n %s\n " , __func__, common_chat_format_example (model, params.chat_template ).c_str ());
211222 } else {
@@ -252,8 +263,10 @@ int main(int argc, char ** argv) {
252263 std::vector<llama_token> embd_inp;
253264
254265 {
255- auto prompt = (params.conversation && params.enable_chat_template && !params.prompt .empty ())
256- ? chat_add_and_format (model, chat_msgs, " system" , params.prompt ) // format the system prompt in conversation mode
266+ auto prompt = (params.conversation_mode && params.enable_chat_template )
267+ // format the system prompt in conversation mode (fallback to default if empty)
268+ ? chat_add_and_format (model, chat_msgs, " system" , params.prompt .empty () ? DEFAULT_SYSTEM_MESSAGE : params.prompt )
269+ // otherwise use the prompt as is
257270 : params.prompt ;
258271 if (params.interactive_first || !params.prompt .empty () || session_tokens.empty ()) {
259272 LOG_DBG (" tokenize the prompt\n " );
@@ -327,7 +340,7 @@ int main(int argc, char ** argv) {
327340 params.n_keep += add_bos; // always keep the BOS token
328341 }
329342
330- if (params.conversation ) {
343+ if (params.conversation_mode ) {
331344 params.interactive_first = true ;
332345 }
333346
@@ -451,7 +464,11 @@ int main(int argc, char ** argv) {
451464#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
452465 LOG_INF ( " - Press Ctrl+C to interject at any time.\n " );
453466#endif
454- LOG_INF ( " %s\n " , control_message);
467+ LOG_INF ( " %s" , control_message);
468+ if (params.conversation_mode && params.enable_chat_template && params.prompt .empty ()) {
469+ LOG_INF ( " - Using default system message. To change it, set a different value via -p PROMPT or -f FILE argument.\n " );
470+ }
471+ LOG_INF (" \n " );
455472
456473 is_interacting = params.interactive_first ;
457474 }
@@ -763,15 +780,15 @@ int main(int argc, char ** argv) {
763780 }
764781
765782 // if current token is not EOG, we add it to current assistant message
766- if (params.conversation ) {
783+ if (params.conversation_mode ) {
767784 const auto id = common_sampler_last (smpl);
768785 assistant_ss << common_token_to_piece (ctx, id, false );
769786 }
770787
771788 if (n_past > 0 && is_interacting) {
772789 LOG_DBG (" waiting for user input\n " );
773790
774- if (params.conversation ) {
791+ if (params.conversation_mode ) {
775792 LOG (" \n > " );
776793 }
777794
@@ -781,7 +798,7 @@ int main(int argc, char ** argv) {
781798 }
782799
783800 std::string buffer;
784- if (!params.input_prefix .empty () && !params.conversation ) {
801+ if (!params.input_prefix .empty () && !params.conversation_mode ) {
785802 LOG_DBG (" appending input prefix: '%s'\n " , params.input_prefix .c_str ());
786803 LOG (" %s" , params.input_prefix .c_str ());
787804 }
@@ -805,7 +822,7 @@ int main(int argc, char ** argv) {
805822 // Entering a empty line lets the user pass control back
806823 if (buffer.length () > 1 ) {
807824 // append input suffix if any
808- if (!params.input_suffix .empty () && !params.conversation ) {
825+ if (!params.input_suffix .empty () && !params.conversation_mode ) {
809826 LOG_DBG (" appending input suffix: '%s'\n " , params.input_suffix .c_str ());
810827 LOG (" %s" , params.input_suffix .c_str ());
811828 }
@@ -818,7 +835,7 @@ int main(int argc, char ** argv) {
818835 string_process_escapes (buffer);
819836 }
820837
821- bool format_chat = params.conversation && params.enable_chat_template ;
838+ bool format_chat = params.conversation_mode && params.enable_chat_template ;
822839 std::string user_inp = format_chat
823840 ? chat_add_and_format (model, chat_msgs, " user" , std::move (buffer))
824841 : std::move (buffer);
0 commit comments