Revert "perf: switch to qwen-style chat template"

This reverts commit db92212697efac74c50708139d7a5e5ab7948486.
This commit is contained in:
Evandro Fonseca Junior committed 2026-03-26 17:50:57 -03:00
1 parent 305c74e264
commit 1bd3365a31
1 file changed
+45 -80
+45 -80
View File
@@ -301,7 +301,9 @@ pub fn prepare_prompt(req: &ChatCompletionRequest, config: &ServerConfig) -> Pre
.any(|message| matches!(message.role, Role::System | Role::Developer)); .any(|message| matches!(message.role, Role::System | Role::Developer));
if !has_system { if !has_system {
append_chat_message(&mut prompt, "system", None, &config.default_system_prompt); prompt.push_str("System: ");
prompt.push_str(&config.default_system_prompt);
prompt.push('\n');
} }
if let Some(format) = &req.response_format { if let Some(format) = &req.response_format {
@@ -314,31 +316,37 @@ pub fn prepare_prompt(req: &ChatCompletionRequest, config: &ServerConfig) -> Pre
for msg in &req.messages { for msg in &req.messages {
let role = match msg.role { let role = match msg.role {
Role::System => "system", Role::System => "System",
Role::Developer => "system", Role::Developer => "Developer",
Role::User => "user", Role::User => "User",
Role::Assistant => "assistant", Role::Assistant => "Assistant",
Role::Tool => "tool", Role::Tool => "Tool",
}; };
prompt.push_str(role);
if let Some(name) = &msg.name {
prompt.push_str("(");
prompt.push_str(name);
prompt.push(')');
}
prompt.push_str(": ");
let content = if let Some(tool_calls) = &msg.tool_calls { if let Some(tool_calls) = &msg.tool_calls {
serde_json::to_string(tool_calls).unwrap_or_else(|_| "[]".to_owned()) let tool_json = serde_json::to_string(tool_calls).unwrap_or_else(|_| "[]".to_owned());
prompt.push_str(&tool_json);
} else { } else {
msg.text().to_owned() prompt.push_str(msg.text());
}; }
if let Some(tool_call_id) = &msg.tool_call_id { if let Some(tool_call_id) = &msg.tool_call_id {
let mut with_id = content; prompt.push_str(" [tool_call_id=");
with_id.push_str("\n[tool_call_id="); prompt.push_str(tool_call_id);
with_id.push_str(tool_call_id); prompt.push(']');
with_id.push(']');
append_chat_message(&mut prompt, role, msg.name.as_deref(), &with_id);
} else {
append_chat_message(&mut prompt, role, msg.name.as_deref(), &content);
}
} }
prompt.push_str("<|im_start|>assistant\n"); prompt.push('\n');
}
prompt.push_str("Assistant:");
PreparedPrompt { PreparedPrompt {
prompt, prompt,
@@ -390,20 +398,14 @@ pub fn tool_calls_to_delta(tool_calls: &[ToolCall]) -> Vec<DeltaToolCall> {
fn append_response_format_instruction(prompt: &mut String, format: &ResponseFormat) { fn append_response_format_instruction(prompt: &mut String, format: &ResponseFormat) {
match format.format_type.as_str() { match format.format_type.as_str() {
"json_object" => { "json_object" => {
append_chat_message( prompt.push_str("System: Return valid JSON only. Do not wrap it in markdown fences.\n");
prompt,
"system",
None,
"Return valid JSON only. Do not wrap it in markdown fences.",
);
} }
"json_schema" => { "json_schema" => {
let mut instruction = prompt.push_str("System: Return valid JSON only and follow this schema exactly: ");
"Return valid JSON only and follow this schema exactly: ".to_owned();
if let Some(schema) = &format.json_schema { if let Some(schema) = &format.json_schema {
instruction.push_str(&schema.to_string()); prompt.push_str(&schema.to_string());
} }
append_chat_message(prompt, "system", None, &instruction); prompt.push('\n');
} }
_ => {} _ => {}
} }
@@ -425,67 +427,32 @@ fn append_tool_instructions(
}) })
.collect(); .collect();
append_chat_message( prompt.push_str("System: Available tools: ");
prompt, prompt.push_str(&serde_json::to_string(&tool_summaries).unwrap_or_else(|_| "[]".to_owned()));
"system", prompt.push('\n');
None, prompt
&format!( .push_str("System: If you need a tool, respond with JSON only using one of these shapes: ");
"Available tools: {}", prompt.push_str(r#"{"tool_calls":[{"name":"tool_name","arguments":{}}]}"#);
serde_json::to_string(&tool_summaries).unwrap_or_else(|_| "[]".to_owned()) prompt.push_str(" or ");
), prompt.push_str(r#"{"name":"tool_name","arguments":{}}"#);
); prompt.push_str(". If no tool is needed, answer normally.\n");
append_chat_message(
prompt,
"system",
None,
&format!(
"If you need a tool, respond with JSON only using one of these shapes: {} or {}. If no tool is needed, answer normally.",
r#"{"tool_calls":[{"name":"tool_name","arguments":{}}]}"#,
r#"{"name":"tool_name","arguments":{}}"#,
),
);
match tool_choice { match tool_choice {
Some(ToolChoice::Mode(mode)) if mode == "required" => { Some(ToolChoice::Mode(mode)) if mode == "required" => {
append_chat_message( prompt.push_str("System: You must call a tool before replying.\n");
prompt,
"system",
None,
"You must call a tool before replying.",
);
} }
Some(ToolChoice::Mode(mode)) if mode == "none" => { Some(ToolChoice::Mode(mode)) if mode == "none" => {
append_chat_message( prompt.push_str("System: Do not call tools in this response.\n");
prompt,
"system",
None,
"Do not call tools in this response.",
);
} }
Some(ToolChoice::Specific(choice)) => { Some(ToolChoice::Specific(choice)) => {
append_chat_message( prompt.push_str("System: You must call the tool named ");
prompt, prompt.push_str(&choice.function.name);
"system", prompt.push_str(".\n");
None,
&format!("You must call the tool named {}.", choice.function.name),
);
} }
_ => {} _ => {}
} }
} }
fn append_chat_message(prompt: &mut String, role: &str, name: Option<&str>, content: &str) {
prompt.push_str("<|im_start|>");
prompt.push_str(role);
if let Some(name) = name {
prompt.push(' ');
prompt.push_str(name);
}
prompt.push('\n');
prompt.push_str(content);
prompt.push_str("<|im_end|>\n");
}
fn parse_tool_calls(raw_output: &str) -> Option<Vec<ToolCall>> { fn parse_tool_calls(raw_output: &str) -> Option<Vec<ToolCall>> {
let trimmed = raw_output.trim(); let trimmed = raw_output.trim();
if trimmed.is_empty() { if trimmed.is_empty() {
@@ -694,8 +661,6 @@ mod tests {
assert!(prepared.prompt.contains("Default coding system prompt")); assert!(prepared.prompt.contains("Default coding system prompt"));
assert!(prepared.prompt.contains("list_files")); assert!(prepared.prompt.contains("list_files"));
assert!(prepared.prompt.contains("must call a tool")); assert!(prepared.prompt.contains("must call a tool"));
assert!(prepared.prompt.contains("<|im_start|>system\n"));
assert!(prepared.prompt.ends_with("<|im_start|>assistant\n"));
} }
#[test] #[test]