Skip to content

Commit a7e6f39

Browse files
ochafikochafik
andcommitted
fix tool_call.id (was in tool_call.function.id!) + add function type
Co-Authored-By: ochafik <[email protected]>
1 parent 82eb01d commit a7e6f39

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

common/chat.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,22 +387,19 @@ template <> json common_chat_msg_diff_to_json_oaicompat(const common_chat_msg_di
387387
delta["content"] = diff.content_delta;
388388
}
389389
if (diff.tool_call_index != std::string::npos) {
390+
json tool_call;
391+
tool_call["index"] = diff.tool_call_index;
392+
if (!diff.tool_call_delta.id.empty()) {
393+
tool_call["id"] = diff.tool_call_delta.id;
394+
tool_call["type"] = "function";
395+
}
390396
json function = json::object();
391397
if (!diff.tool_call_delta.name.empty()) {
392398
function["name"] = diff.tool_call_delta.name;
393399
}
394-
if (!diff.tool_call_delta.id.empty()) {
395-
function["id"] = diff.tool_call_delta.id;
396-
}
397-
if (!diff.tool_call_delta.arguments.empty()) {
398-
function["arguments"] = diff.tool_call_delta.arguments;
399-
}
400-
delta["tool_calls"] = json::array({
401-
json {
402-
{"index", diff.tool_call_index},
403-
{"function", function}
404-
}
405-
});
400+
function["arguments"] = diff.tool_call_delta.arguments;
401+
tool_call["function"] = function;
402+
delta["tool_calls"] = json::array({tool_call});
406403
}
407404
return delta;
408405
}

tools/server/tests/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ def make_any_request(
328328
if 'function' not in tc:
329329
raise ValueError(f"Expected function type, got {tc['type']}")
330330
if tc['index'] >= len(tool_calls):
331+
assert 'id' in tc
332+
assert tc.get('type') == 'function'
333+
assert 'function' in tc and 'name' in tc['function'] and len(tc['function']['name']) > 0, \
334+
f"Expected function call with name, got {tc.get('function')}"
331335
tool_calls.append(dict(
332336
id="",
333337
type="function",
@@ -340,10 +344,10 @@ def make_any_request(
340344
if tc.get('id') is not None:
341345
tool_call['id'] = tc['id']
342346
fct = tc['function']
347+
assert 'id' not in fct, f"Function call should not have id: {fct}"
343348
if fct.get('name') is not None:
344349
tool_call['function']['name'] = tool_call['function'].get('name', '') + fct['name']
345350
if fct.get('arguments') is not None:
346-
assert len(fct['arguments']) > 0, f'Expected non empty arguments delta!'
347351
tool_call['function']['arguments'] += fct['arguments']
348352

349353
print(f'Streamed response had {content_parts} content parts, {tool_call_parts} tool call parts incl. {arguments_parts} arguments parts')

0 commit comments

Comments
 (0)