16#include <nlohmann/json.hpp>
27constexpr const char* TOOL_RESULT_SUFFIX =
28 "Continue. Batch multiple tool calls in one response when possible.";
37 static std::atomic<uint64_t> counter{0};
38 return "tc-" + std::to_string(counter.fetch_add(1, std::memory_order_relaxed));
59 auto calls = parse_xml_function_calls(content);
81std::vector<ToolCall> Qwen35Adapter::parse_xml_function_calls(
82 const std::string& content)
const
84 std::vector<ToolCall> calls;
85 std::regex func_pattern(R
"(<function=([^>]+)>([\s\S]*?)</function>)");
87 auto begin = std::sregex_iterator(content.begin(), content.end(), func_pattern);
88 auto end = std::sregex_iterator();
90 for (
auto it = begin; it != end; ++it) {
91 std::string func_name = (*it)[1].str();
92 std::string func_body = (*it)[2].str();
95 auto ns = func_name.find_first_not_of(
" \t");
96 auto ne = func_name.find_last_not_of(
" \t");
97 if (ns != std::string::npos) {
98 func_name = func_name.substr(ns, ne - ns + 1);
101 auto arguments = extract_xml_parameters(func_body);
106 tc.arguments = std::move(arguments);
107 calls.push_back(std::move(tc));
109 logger->info(
"Parsed XML tool call: {}", func_name);
124std::unordered_map<std::string, std::string> Qwen35Adapter::extract_xml_parameters(
125 const std::string& func_body)
const
127 std::string body = func_body;
130 auto nested = body.find(
"<function=");
131 if (nested != std::string::npos) {
132 logger->warn(
"Truncating function body at nested <function= tag");
133 body = body.substr(0, nested);
136 std::unordered_map<std::string, std::string> arguments;
137 std::regex param_pattern(R
"(<parameter=([^>]+)>([\s\S]*?)</parameter>)");
139 auto begin = std::sregex_iterator(body.begin(), body.end(), param_pattern);
140 auto end = std::sregex_iterator();
142 for (
auto it = begin; it != end; ++it) {
143 std::string key = (*it)[1].str();
144 std::string value = (*it)[2].str();
147 auto ks = key.find_first_not_of(
" \t\n\r");
148 auto ke = key.find_last_not_of(
" \t\n\r");
149 if (ks != std::string::npos) key = key.substr(ks, ke - ks + 1);
151 auto vs = value.find_first_not_of(
" \t\n\r");
152 auto ve = value.find_last_not_of(
" \t\n\r");
153 if (vs != std::string::npos) value = value.substr(vs, ve - vs + 1);
155 if (key.empty() || value.empty()) {
156 logger->warn(
"Skipping empty XML parameter: key='{}' value='{}'", key, value);
159 arguments[key] = value;
176 const std::string& result)
const
180 msg.
content =
"<tool_response>\n" + result +
181 "\n</tool_response>\n\n" + TOOL_RESULT_SUFFIX;
195 const std::vector<std::string>& tool_jsons)
const
197 nlohmann::json tool_defs = nlohmann::json::array();
199 for (
const auto& json_str : tool_jsons) {
201 auto j = nlohmann::json::parse(json_str);
202 tool_defs.push_back({
203 {
"type",
"function"},
205 {
"name", j.value(
"name",
"unknown")},
206 {
"description", j.value(
"description",
"")},
207 {
"parameters", j.value(
"inputSchema",
208 nlohmann::json::object())}
216 std::ostringstream out;
218 <<
"You may call one or more functions to assist with the user query.\n"
219 <<
"Put your final answer OUTSIDE of any tool calls.\n\n"
220 <<
"Here are the available tools:\n"
222 << tool_defs.dump(2) <<
"\n"
224 <<
"For each function call, return within <tool_call></tool_call> XML tags:\n"
226 <<
"<function=example_function>\n"
227 <<
"<parameter=param_name>value</parameter>\n"
242std::string Qwen35Adapter::clean_content(
const std::string& content)
const {
244 std::string cleaned = std::regex_replace(content,
245 std::regex(R
"(<tool_call>\s*[\s\S]*?\s*</tool_call>)"), "");
248 cleaned = std::regex_replace(cleaned,
249 std::regex(R
"(<function=[^>]+>[\s\S]*?</function>)"), "");
260 "\n\nYou can see and analyze images. When the user shares an "
261 "image, describe what you observe before responding to their "
273 const std::string& base_system,
274 bool has_vision)
const {
289 const std::vector<ContentPart>& parts)
const {
virtual std::string format_content_parts(const std::vector< ContentPart > &parts) const
Convert multimodal content parts to adapter-specific format.
std::vector< ToolCall > parse_tagged_tool_calls(const std::string &content) const
Parse <tool_call>JSON</tool_call> tagged blocks.
std::string strip_think_blocks(const std::string &content) const
Strip all <think>...</think> blocks from content.
std::string format_tools(const std::vector< std::string > &tool_jsons) const override
Format tools in <tools> tags with OpenAI function JSON.
Message format_tool_result(const ToolCall &tool_call, const std::string &result) const override
Wrap tool result in <tool_response> tags.
ParseResult parse_tool_calls(const std::string &content) const override
Parse XML function calls, fallback to tagged JSON.
std::string format_system_with_vision(const std::string &base_system, bool has_vision) const override
Qwen3.5 vision system prompt extension.
std::string format_content_parts(const std::vector< ContentPart > &parts) const override
Qwen3.5 content part formatting.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Activate model on GPU (WARM → ACTIVE).
static constexpr const char * VISION_INSTRUCTION
Vision instruction appended to system prompt.
std::string generate_uuid()
Generate a UUID v4 string.
A message in a conversation.
std::string content
Message text content (always populated)
std::string role
Message role.
Parsed tool call result: cleaned content + extracted calls.
std::string cleaned_content
Content with tool calls removed.
std::vector< ToolCall > tool_calls
Extracted tool calls.