Skip to content

Commit 994301d

Browse files
author
Olivier Chafik
committed
use existing string_strip
1 parent 33efcb3 commit 994301d

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

common/chat.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ std::string common_chat_format_name(common_chat_format format) {
2424
}
2525
}
2626

27-
static std::string string_trim(const std::string & s) {
28-
size_t start = 0;
29-
while (start < s.size() && std::isspace(s[start])) {
30-
start++;
31-
}
32-
size_t end = s.size();
33-
while (end > start && std::isspace(s[end - 1])) {
34-
end--;
35-
}
36-
return s.substr(start, end - start);
37-
}
38-
3927
const common_grammar_options grammar_options {
4028
/* .dotall = */ false,
4129
/* .compact_spaces = */ false,
@@ -138,7 +126,7 @@ static common_chat_msg parse_json_tool_calls(
138126
}
139127

140128
if (!result.tool_calls.empty()) {
141-
if (!string_trim(result.content).empty()) {
129+
if (!string_strip(result.content).empty()) {
142130
LOG_WRN("Content found with tool calls: %s", result.content.c_str());
143131
}
144132
result.content = "";
@@ -731,7 +719,7 @@ static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input,
731719
if (std::regex_match(input, match, reasoning_content_regex)) {
732720
std::string rest;
733721
if (think) {
734-
msg.reasoning_content = string_trim(match[2].str());
722+
msg.reasoning_content = string_strip(match[2].str());
735723
} else {
736724
msg.content = match[1].str();
737725
}
@@ -1058,11 +1046,17 @@ static common_chat_params common_chat_params_init_without_tools(const common_cha
10581046
}
10591047

10601048
common_chat_params common_chat_params_init(const common_chat_template & tmpl, const struct common_chat_inputs & inputs) {
1061-
if (inputs.tools.is_array() && inputs.tool_choice != "none" && !inputs.grammar.empty()) {
1062-
throw std::runtime_error("Cannot specify grammar with tools");
1063-
}
1064-
10651049
const auto & src = tmpl.source();
1050+
const auto & caps = tmpl.original_caps();
1051+
1052+
if (inputs.tools.is_array()) {
1053+
if (inputs.tool_choice != "none" && !inputs.grammar.empty()) {
1054+
throw std::runtime_error("Cannot specify grammar with tools");
1055+
}
1056+
if (caps.supports_tool_calls && !caps.supports_tools) {
1057+
LOG_WRN("Template supports tool calls but does not natively describe tools. The fallback behaviour used may produce bad results, inspect prompt w/ --verbose & consider overriding the template.");
1058+
}
1059+
}
10661060

10671061
// DeepSeek R1: use handler in all cases except json schema (thinking / tools).
10681062
if (src.find("<|tool▁calls▁begin|>") != std::string::npos && inputs.json_schema.is_null()) {

0 commit comments

Comments
 (0)