Skip to content

Commit 6eb73f9

Browse files
sayanshaw24Sayan Shaw
andauthored
Update <think> tag logic for DeepSeek chat template (#956)
* update <think> tag logic in deepseek chat template * resolve comment * remove hf token req for deepseek python test --------- Co-authored-by: Sayan Shaw <[email protected]>
1 parent 1fdf2e5 commit 6eb73f9

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

shared/api/chat_template.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,14 @@ OrtxStatus TokenizerImpl::DeepSeekChatTemplate(std::string& output, bool add_gen
559559

560560
// Handle assistant message without tool calls
561561
if (role == "assistant" && !content.empty()) {
562+
constexpr std::string_view think_token = "</think>";
563+
564+
// Remove content before and including "</think>" if present
565+
std::size_t think_pos = content.find(think_token);
566+
if (think_pos != std::string::npos) {
567+
content = content.substr(think_pos + think_token.size());
568+
}
569+
562570
if (is_tool) {
563571
output += "<|tool_outputs_end|>" + content;
564572
output += eos_token;

test/test_pp_api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,9 @@ def test_llama3_3_chat_template(self):
485485
ortx_inputs = tokenizer.tokenize(prompt)
486486
np.testing.assert_array_equal(ortx_inputs, hf_tok(prompt, return_tensors="np")["input_ids"][0])
487487

488-
@unittest.skipIf(hf_token_id is None, "HF_TOKEN is not available")
489488
def test_deepseek_chat_template(self):
490489
ckpt = "deepseek-ai/DeepSeek-R1-Distill-Llama-70B"
491-
hf_tok = AutoTokenizer.from_pretrained(ckpt, token=hf_token_id)
490+
hf_tok = AutoTokenizer.from_pretrained(ckpt)
492491

493492
for messages in self.messages_list:
494493
inputs = hf_tok.apply_chat_template(

0 commit comments

Comments
 (0)