Browse Source

common : default content to an empty string (#18485)

* common : default content to an empty string

* common : fix tests that break when content != null
Aldehir Rojas 1 month ago
parent
commit
0f89d2ecf1
3 changed files with 9 additions and 7 deletions
  1. 1 1
      common/chat.cpp
  2. 3 3
      models/templates/llama-cpp-deepseek-r1.jinja
  3. 5 3
      tests/test-chat.cpp

+ 1 - 1
common/chat.cpp

@@ -319,7 +319,7 @@ json common_chat_msgs_to_json_oaicompat(const std::vector<common_chat_msg> & msg
                 }
                 }
             }
             }
         } else {
         } else {
-            jmsg["content"] = json(); // null
+            jmsg["content"] = "";
         }
         }
         if (!msg.reasoning_content.empty()) {
         if (!msg.reasoning_content.empty()) {
             jmsg["reasoning_content"] = msg.reasoning_content;
             jmsg["reasoning_content"] = msg.reasoning_content;

+ 3 - 3
models/templates/llama-cpp-deepseek-r1.jinja

@@ -38,7 +38,7 @@ Example function tool call syntax:
     {%- if message['role'] == 'user' -%}
     {%- if message['role'] == 'user' -%}
         {{- '<|User|>' + message['content'] + '<|end▁of▁sentence|>' -}}
         {{- '<|User|>' + message['content'] + '<|end▁of▁sentence|>' -}}
     {%- endif -%}
     {%- endif -%}
-    {%- if message['role'] == 'assistant' and message['content'] is none -%}
+    {%- if message['role'] == 'assistant' and not message['content'] -%}
         {{- '<|Assistant|><|tool▁calls▁begin|>' -}}
         {{- '<|Assistant|><|tool▁calls▁begin|>' -}}
         {%- set ns.is_first = true -%}
         {%- set ns.is_first = true -%}
         {%- for tc in message['tool_calls'] -%}
         {%- for tc in message['tool_calls'] -%}
@@ -53,7 +53,7 @@ Example function tool call syntax:
         {%- endfor -%}
         {%- endfor -%}
         {{- '<|tool▁calls▁end|><|end▁of▁sentence|>' -}}
         {{- '<|tool▁calls▁end|><|end▁of▁sentence|>' -}}
     {%- endif -%}
     {%- endif -%}
-    {%- if message['role'] == 'assistant' and message['content'] is  not none -%}
+    {%- if message['role'] == 'assistant' and message['content'] -%}
         {{- flush_tool_outputs() -}}
         {{- flush_tool_outputs() -}}
         {%- set content = message['content'] -%}
         {%- set content = message['content'] -%}
         {%- if '</think>' in content -%}
         {%- if '</think>' in content -%}
@@ -73,4 +73,4 @@ Example function tool call syntax:
 {{- flush_tool_outputs() -}}
 {{- flush_tool_outputs() -}}
 {%- if add_generation_prompt and not ns.is_tool_outputs -%}
 {%- if add_generation_prompt and not ns.is_tool_outputs -%}
     {{- '<|Assistant|><think>\n' -}}
     {{- '<|Assistant|><think>\n' -}}
-{%- endif -%}
+{%- endif -%}

+ 5 - 3
tests/test-chat.cpp

@@ -650,7 +650,7 @@ static void test_msgs_oaicompat_json_conversion() {
             "[\n"
             "[\n"
             "  {\n"
             "  {\n"
             "    \"role\": \"assistant\",\n"
             "    \"role\": \"assistant\",\n"
-            "    \"content\": null,\n"
+            "    \"content\": \"\",\n"
             "    \"tool_calls\": [\n"
             "    \"tool_calls\": [\n"
             "      {\n"
             "      {\n"
             "        \"type\": \"function\",\n"
             "        \"type\": \"function\",\n"
@@ -906,7 +906,8 @@ static void test_template_output_parsers() {
                       "      },\n"
                       "      },\n"
                       "      \"id\": \"123456789\"\n"
                       "      \"id\": \"123456789\"\n"
                       "    }\n"
                       "    }\n"
-                      "  ]\n"
+                      "  ],\n"
+                      "  \"content\": \"\"\n"
                       "}");
                       "}");
     }
     }
     {
     {
@@ -1713,7 +1714,8 @@ static void test_template_output_parsers() {
                       "      },\n"
                       "      },\n"
                       "      \"id\": \"123456789\"\n"
                       "      \"id\": \"123456789\"\n"
                       "    }\n"
                       "    }\n"
-                      "  ]\n"
+                      "  ],\n"
+                      "  \"content\": \"\"\n"
                       "}",
                       "}",
                       /* expect_grammar_triggered= */ false
                       /* expect_grammar_triggered= */ false
         );
         );