forked from ikawrakow/ik_llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_calls.hpp
More file actions
284 lines (227 loc) · 10.9 KB
/
Copy pathfunction_calls.hpp
File metadata and controls
284 lines (227 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once
#include "json.hpp"
#include <string>
using json = nlohmann::ordered_json;
//
// Function calling parsers for multiple formats
//
// Parse XML-style function calls (format: <function_calls><invoke name="func"><parameter name="param">value</parameter></invoke></function_calls>)
static json parse_xml_function_calls(const std::string& text) {
json tool_calls = json::array();
// Look for function_calls section
size_t section_start = text.find("<function_calls>");
if (section_start == std::string::npos) {
return tool_calls;
}
size_t section_end = text.find("</function_calls>", section_start);
if (section_end == std::string::npos) {
return tool_calls;
}
// Extract section content
std::string section = text.substr(section_start + 16, section_end - section_start - 16);
// Parse individual invoke blocks
size_t pos = 0;
int call_index = 0;
while (pos < section.length()) {
const std::string invoke_pattern = "<invoke name=\"";
size_t invoke_start = section.find(invoke_pattern, pos);
if (invoke_start == std::string::npos) break;
size_t name_start = invoke_start + invoke_pattern.length();
size_t name_end = section.find("\"", name_start);
if (name_end == std::string::npos) break;
std::string func_name = section.substr(name_start, name_end - name_start);
size_t invoke_end = section.find("</invoke>", invoke_start);
if (invoke_end == std::string::npos) break;
// Extract parameters (skip past ">")
size_t content_start = section.find(">", name_end) + 1;
std::string invoke_content = section.substr(content_start, invoke_end - content_start);
json arguments = json::object();
const std::string param_pattern = "<parameter name=\"";
const std::string param_end_pattern = "</parameter>";
size_t param_start = invoke_content.find(param_pattern);
while (param_start != std::string::npos) {
size_t param_name_start = param_start + param_pattern.length();
size_t param_name_end = invoke_content.find("\"", param_name_start);
if (param_name_end == std::string::npos) break;
std::string param_name = invoke_content.substr(param_name_start, param_name_end - param_name_start);
size_t param_value_start = invoke_content.find(">", param_name_end) + 1;
size_t param_value_end = invoke_content.find(param_end_pattern, param_value_start);
if (param_value_end == std::string::npos) break;
std::string param_value = invoke_content.substr(param_value_start, param_value_end - param_value_start);
arguments[param_name] = param_value;
param_start = invoke_content.find(param_pattern, param_value_end);
}
// Create tool call object
json tool_call = {
{"id", "call_" + std::to_string(call_index)},
{"type", "function"},
{"function", {
{"name", func_name},
{"arguments", arguments.dump()}
}}
};
tool_calls.push_back(tool_call);
pos = invoke_end + 9;
call_index++;
}
return tool_calls;
}
// Parse anythingllm-style function calls (supports both JSON and XML variants)
static json parse_anythingllm_function_calls(const std::string& text) {
json tool_calls = json::array();
// Look for anythingllm function_calls section
size_t section_start = text.find("<anythingllm:function_calls>");
if (section_start == std::string::npos) {
return tool_calls;
}
size_t section_end = text.find("</anythingllm:function_calls>", section_start);
if (section_end == std::string::npos) {
return tool_calls;
}
// Extract content between tags
std::string content = text.substr(section_start + 28, section_end - section_start - 28);
// Trim whitespace
size_t start = content.find_first_not_of(" \t\n\r");
size_t end = content.find_last_not_of(" \t\n\r");
if (start != std::string::npos && end != std::string::npos) {
content = content.substr(start, end - start + 1);
}
// Try JSON format first (array of objects)
if (!content.empty() && content[0] == '[') {
try {
json parsed = json::parse(content);
if (parsed.is_array()) {
int call_index = 0;
for (const auto& call : parsed) {
if (call.contains("name") && (call.contains("parameters") || call.contains("arguments"))) {
// Handle both "parameters" and "arguments" fields
json args = call.contains("arguments") ? call["arguments"] : call["parameters"];
json tool_call = {
{"id", "call_" + std::to_string(call_index)},
{"type", "function"},
{"function", {
{"name", call["name"]},
{"arguments", args.dump()}
}}
};
tool_calls.push_back(tool_call);
call_index++;
}
}
}
} catch (const std::exception& e) {
// Continue to XML parsing if JSON fails
}
}
// Try XML format (anythingllm:invoke structure)
if (tool_calls.empty()) {
size_t pos = 0;
int call_index = 0;
while (pos < content.length()) {
size_t invoke_start = content.find("<anythingllm:invoke name=\"", pos);
if (invoke_start == std::string::npos) break;
size_t name_start = invoke_start + 26;
size_t name_end = content.find("\"", name_start);
if (name_end == std::string::npos) break;
std::string func_name = content.substr(name_start, name_end - name_start);
size_t invoke_end = content.find("</anythingllm:invoke>", invoke_start);
if (invoke_end == std::string::npos) break;
// Extract parameters from the invoke block
std::string invoke_content = content.substr(name_end + 2, invoke_end - name_end - 2);
json arguments = json::object();
size_t param_start = invoke_content.find("<anythingllm:parameter_name name=\"");
while (param_start != std::string::npos) {
size_t param_name_start = param_start + 34;
size_t param_name_end = invoke_content.find("\"", param_name_start);
if (param_name_end == std::string::npos) break;
std::string param_name = invoke_content.substr(param_name_start, param_name_end - param_name_start);
size_t param_value_start = invoke_content.find(">", param_name_end) + 1;
size_t param_value_end = invoke_content.find("</anythingllm:parameter_name>", param_value_start);
if (param_value_end == std::string::npos) break;
std::string param_value = invoke_content.substr(param_value_start, param_value_end - param_value_start);
arguments[param_name] = param_value;
param_start = invoke_content.find("<anythingllm:parameter_name name=\"", param_value_end);
}
// Create tool call object
json tool_call = {
{"id", "call_" + std::to_string(call_index)},
{"type", "function"},
{"function", {
{"name", func_name},
{"arguments", arguments.dump()}
}}
};
tool_calls.push_back(tool_call);
pos = invoke_end + 21;
call_index++;
}
}
return tool_calls;
}
// Parse token-style function calls (format: <|tool_calls_section_begin|>...<|tool_calls_section_end|>)
static json parse_token_function_calls(const std::string& text) {
json tool_calls = json::array();
// Look for tool calls section
size_t section_start = text.find("<|tool_calls_section_begin|>");
if (section_start == std::string::npos) {
return tool_calls;
}
size_t section_end = text.find("<|tool_calls_section_end|>", section_start);
if (section_end == std::string::npos) {
return tool_calls;
}
// Extract section content
std::string section = text.substr(section_start + 27, section_end - section_start - 27);
// Parse individual tool calls
size_t pos = 0;
int call_index = 0;
while (pos < section.length()) {
size_t call_start = section.find("<|tool_call_begin|>", pos);
if (call_start == std::string::npos) break;
size_t call_end = section.find("<|tool_call_end|>", call_start);
if (call_end == std::string::npos) break;
std::string call_content = section.substr(call_start + 19, call_end - call_start - 19);
// Parse tool call content
size_t arg_start = call_content.find("<|tool_call_argument_begin|>");
if (arg_start != std::string::npos) {
std::string tool_id = call_content.substr(0, arg_start);
std::string arguments = call_content.substr(arg_start + 28);
// Extract function name from tool_id (format: functions.{name}:{idx})
std::string func_name = "";
size_t dot_pos = tool_id.find('.');
size_t colon_pos = tool_id.find(':', dot_pos);
if (dot_pos != std::string::npos && colon_pos != std::string::npos) {
func_name = tool_id.substr(dot_pos + 1, colon_pos - dot_pos - 1);
}
// Create tool call object
json tool_call = {
{"id", tool_id},
{"type", "function"},
{"function", {
{"name", func_name},
{"arguments", arguments}
}}
};
tool_calls.push_back(tool_call);
}
pos = call_end + 18;
call_index++;
}
return tool_calls;
}
// Main function to parse function calls from text (supports multiple formats)
static json parse_kimi_k2_tool_calls(const std::string& text) {
// Try anythingllm format first
json anythingllm_result = parse_anythingllm_function_calls(text);
if (!anythingllm_result.empty()) {
return anythingllm_result;
}
// Try XML format
json xml_result = parse_xml_function_calls(text);
if (!xml_result.empty()) {
return xml_result;
}
// Fall back to token format
json token_result = parse_token_function_calls(text);
return token_result;
}