From d297225e982a3ee83342d5270d9d66317f766b09 Mon Sep 17 00:00:00 2001 From: Kai Zau Date: Mon, 1 Apr 2024 16:40:07 +0900 Subject: [PATCH] Remove alpaca, match deepseek with jinja output --- llama.cpp | 15 ++++++--------- tests/test-chat-template.cpp | 7 +++---- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/llama.cpp b/llama.cpp index 5d653efd3..31e1af383 100644 --- a/llama.cpp +++ b/llama.cpp @@ -15733,22 +15733,19 @@ static int32_t llama_chat_apply_template_internal( if (add_ass) { ss << "ASSISTANT:"; } - } else if (tmpl == "alpaca" || tmpl.find("### Instruction:\\n") != std::string::npos) { - // wxjiao/alpaca-7b, deepseek-ai/deepseek-coder-33b-instruct + } else if (tmpl == "deepseek" || (tmpl.find("### Instruction:") != std::string::npos && tmpl.find("<|EOT|>") != std::string::npos)) { + // deepseek-ai/deepseek-coder-33b-instruct + // Use of both U+ff5c and U+007c pipes is deliberate, based on the Jinja template for (auto message : chat) { std::string role(message->role); if (role == "system") { - ss << message->content << "\n\n"; + ss << "<|begin▁of▁sentence|>" << message->content; } else if (role == "user") { - ss << "### Instruction:\n" << message->content << "\n\n"; + ss << "### Instruction:\n" << message->content << "\n"; } else if (role == "assistant") { - ss << "### Response:\n" << message->content << "\n\n"; + ss << "### Response:\n" << message->content << "\n<|EOT|>\n"; } } - if (add_ass) { - ss << "### Response:\n"; - } - } else { // template not supported return -1; diff --git a/tests/test-chat-template.cpp b/tests/test-chat-template.cpp index ba1bc0fd8..b8f1c24d8 100644 --- a/tests/test-chat-template.cpp +++ b/tests/test-chat-template.cpp @@ -41,8 +41,7 @@ int main(void) { // Orca-Vicuna // No template included in config_tokenizer.json; extracted from: https://github.com/oobabooga/text-generation-webui/blob/main/instruction-templates/Orca-Vicuna.yaml "{%- for message in messages %}{%- if message['role'] == 'system' -%}{{-'SYSTEM: ' + message['content'] + '\n' -}}{%- else -%}{%- if message['role'] == 'user' -%}{{-'USER: ' + message['content'] + '\n'-}}{%- else -%}{{-'ASSISTANT: ' + message['content'] + '\n' -}}{%- endif -%}{%- endif -%}{%- endfor -%}{%- if add_generation_prompt -%}{{-'ASSISTANT:'-}}{%- endif -%}", - // wxjiao/alpaca-7b, deepseek-ai/deepseek-coder-33b-instruct - // No original Alpaca template Jinja, so using DeepSeek's instead + // deepseek-ai/deepseek-coder-33b-instruct "{% if not add_generation_prompt is defined %}\n{% set add_generation_prompt = false %}\n{% endif %}\n{%- set ns = namespace(found=false) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'system' -%}\n {%- set ns.found = true -%}\n {%- endif -%}\n{%- endfor -%}\n{{bos_token}}{%- if not ns.found -%}\n{{'You are an AI programming assistant, utilizing the Deepseek Coder model, developed by Deepseek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer\\n'}}\n{%- endif %}\n{%- for message in messages %}\n {%- if message['role'] == 'system' %}\n{{ message['content'] }}\n {%- else %}\n {%- if message['role'] == 'user' %}\n{{'### Instruction:\\n' + message['content'] + '\\n'}}\n {%- else %}\n{{'### Response:\\n' + message['content'] + '\\n<|EOT|>\\n'}}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{% if add_generation_prompt %}\n{{'### Response:'}}\n{% endif %}" }; std::vector expected_output = { @@ -66,8 +65,8 @@ int main(void) { "You are a helpful assistant\n\nUSER: Hello\nASSISTANT: Hi there\nUSER: Who are you\nASSISTANT: I am an assistant \nUSER: Another question\nASSISTANT:", // Orca-Vicuna "SYSTEM: You are a helpful assistant\nUSER: Hello\nASSISTANT: Hi there\nUSER: Who are you\nASSISTANT: I am an assistant \nUSER: Another question\nASSISTANT:", - // wxjiao/alpaca-7b, deepseek-ai/deepseek-coder-33b-instruct - "You are a helpful assistant\n\n### Instruction:\nHello\n\n### Response:\nHi there\n\n### Instruction:\nWho are you\n\n### Response:\n I am an assistant \n\n### Instruction:\nAnother question\n\n### Response:\n", + // deepseek-ai/deepseek-coder-33b-instruct + "<|begin▁of▁sentence|>You are a helpful assistant### Instruction:\nHello\n### Response:\nHi there\n<|EOT|>\n### Instruction:\nWho are you\n### Response:\n I am an assistant \n<|EOT|>\n### Instruction:\nAnother question\n" }; std::vector formatted_chat(1024); int32_t res;